1 | //===-- IOStream.h ----------------------------------------------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #ifndef LLDB_TOOLS_LLDB_DAP_IOSTREAM_H |
10 | #define LLDB_TOOLS_LLDB_DAP_IOSTREAM_H |
11 | |
12 | #include "llvm/Config/llvm-config.h" // for LLVM_ON_UNIX |
13 | |
14 | #if defined(_WIN32) |
15 | // We need to #define NOMINMAX in order to skip `min()` and `max()` macro |
16 | // definitions that conflict with other system headers. |
17 | // We also need to #undef GetObject (which is defined to GetObjectW) because |
18 | // the JSON code we use also has methods named `GetObject()` and we conflict |
19 | // against these. |
20 | #define NOMINMAX |
21 | #include <windows.h> |
22 | #else |
23 | typedef int SOCKET; |
24 | #endif |
25 | |
26 | #include "llvm/ADT/StringRef.h" |
27 | |
28 | #include <fstream> |
29 | #include <string> |
30 | |
31 | // Windows requires different system calls for dealing with sockets and other |
32 | // types of files, so we can't simply have one code path that just uses read |
33 | // and write everywhere. So we need an abstraction in order to allow us to |
34 | // treat them identically. |
35 | namespace lldb_dap { |
36 | struct StreamDescriptor { |
37 | StreamDescriptor(); |
38 | ~StreamDescriptor(); |
39 | StreamDescriptor(StreamDescriptor &&other); |
40 | |
41 | StreamDescriptor &operator=(StreamDescriptor &&other); |
42 | |
43 | static StreamDescriptor from_socket(SOCKET s, bool close); |
44 | static StreamDescriptor from_file(int fd, bool close); |
45 | |
46 | bool m_is_socket = false; |
47 | bool m_close = false; |
48 | union { |
49 | int m_fd; |
50 | SOCKET m_socket; |
51 | }; |
52 | }; |
53 | |
54 | struct InputStream { |
55 | StreamDescriptor descriptor; |
56 | |
57 | bool read_full(std::ofstream *log, size_t length, std::string &text); |
58 | |
59 | bool read_line(std::ofstream *log, std::string &line); |
60 | |
61 | bool read_expected(std::ofstream *log, llvm::StringRef expected); |
62 | }; |
63 | |
64 | struct OutputStream { |
65 | StreamDescriptor descriptor; |
66 | |
67 | bool write_full(llvm::StringRef str); |
68 | }; |
69 | } // namespace lldb_dap |
70 | |
71 | #endif |
72 | |