xref: /openbsd-src/gnu/llvm/lldb/tools/lldb-vscode/IOStream.h (revision be691f3bb6417f04a68938fadbcaee2d5795e764)
1061da546Spatrick //===-- IOStream.h ----------------------------------------------*- C++ -*-===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9dda28197Spatrick #ifndef LLDB_TOOLS_LLDB_VSCODE_IOSTREAM_H
10dda28197Spatrick #define LLDB_TOOLS_LLDB_VSCODE_IOSTREAM_H
11061da546Spatrick 
12*be691f3bSpatrick #include "llvm/Config/llvm-config.h" // for LLVM_ON_UNIX
13*be691f3bSpatrick 
14061da546Spatrick #if defined(_WIN32)
15061da546Spatrick // We need to #define NOMINMAX in order to skip `min()` and `max()` macro
16061da546Spatrick // definitions that conflict with other system headers.
17061da546Spatrick // We also need to #undef GetObject (which is defined to GetObjectW) because
18061da546Spatrick // the JSON code we use also has methods named `GetObject()` and we conflict
19061da546Spatrick // against these.
20061da546Spatrick #define NOMINMAX
21061da546Spatrick #include <windows.h>
22061da546Spatrick #else
23061da546Spatrick typedef int SOCKET;
24061da546Spatrick #endif
25061da546Spatrick 
26061da546Spatrick #include "llvm/ADT/StringRef.h"
27061da546Spatrick 
28061da546Spatrick #include <fstream>
29061da546Spatrick #include <string>
30061da546Spatrick 
31061da546Spatrick // Windows requires different system calls for dealing with sockets and other
32061da546Spatrick // types of files, so we can't simply have one code path that just uses read
33061da546Spatrick // and write everywhere.  So we need an abstraction in order to allow us to
34061da546Spatrick // treat them identically.
35061da546Spatrick namespace lldb_vscode {
36061da546Spatrick struct StreamDescriptor {
37061da546Spatrick   StreamDescriptor();
38061da546Spatrick   ~StreamDescriptor();
39061da546Spatrick   StreamDescriptor(StreamDescriptor &&other);
40061da546Spatrick 
41061da546Spatrick   StreamDescriptor &operator=(StreamDescriptor &&other);
42061da546Spatrick 
43061da546Spatrick   static StreamDescriptor from_socket(SOCKET s, bool close);
44061da546Spatrick   static StreamDescriptor from_file(int fd, bool close);
45061da546Spatrick 
46061da546Spatrick   bool m_is_socket = false;
47061da546Spatrick   bool m_close = false;
48061da546Spatrick   union {
49061da546Spatrick     int m_fd;
50061da546Spatrick     SOCKET m_socket;
51061da546Spatrick   };
52061da546Spatrick };
53061da546Spatrick 
54061da546Spatrick struct InputStream {
55061da546Spatrick   StreamDescriptor descriptor;
56061da546Spatrick 
57061da546Spatrick   bool read_full(std::ofstream *log, size_t length, std::string &text);
58061da546Spatrick 
59061da546Spatrick   bool read_line(std::ofstream *log, std::string &line);
60061da546Spatrick 
61061da546Spatrick   bool read_expected(std::ofstream *log, llvm::StringRef expected);
62061da546Spatrick };
63061da546Spatrick 
64061da546Spatrick struct OutputStream {
65061da546Spatrick   StreamDescriptor descriptor;
66061da546Spatrick 
67061da546Spatrick   bool write_full(llvm::StringRef str);
68061da546Spatrick };
69061da546Spatrick } // namespace lldb_vscode
70061da546Spatrick 
71061da546Spatrick #endif
72