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