101263c6cSJonas Devlieghere //===-- IOStream.h ----------------------------------------------*- C++ -*-===// 201263c6cSJonas Devlieghere // 301263c6cSJonas Devlieghere // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 401263c6cSJonas Devlieghere // See https://llvm.org/LICENSE.txt for license information. 501263c6cSJonas Devlieghere // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 601263c6cSJonas Devlieghere // 701263c6cSJonas Devlieghere //===----------------------------------------------------------------------===// 801263c6cSJonas Devlieghere 901263c6cSJonas Devlieghere #ifndef LLDB_TOOLS_LLDB_DAP_IOSTREAM_H 1001263c6cSJonas Devlieghere #define LLDB_TOOLS_LLDB_DAP_IOSTREAM_H 1101263c6cSJonas Devlieghere 1201263c6cSJonas Devlieghere #if defined(_WIN32) 1301263c6cSJonas Devlieghere // We need to #define NOMINMAX in order to skip `min()` and `max()` macro 1401263c6cSJonas Devlieghere // definitions that conflict with other system headers. 1501263c6cSJonas Devlieghere // We also need to #undef GetObject (which is defined to GetObjectW) because 1601263c6cSJonas Devlieghere // the JSON code we use also has methods named `GetObject()` and we conflict 1701263c6cSJonas Devlieghere // against these. 1801263c6cSJonas Devlieghere #define NOMINMAX 1901263c6cSJonas Devlieghere #include <windows.h> 2001263c6cSJonas Devlieghere #else 2101263c6cSJonas Devlieghere typedef int SOCKET; 2201263c6cSJonas Devlieghere #endif 2301263c6cSJonas Devlieghere 2401263c6cSJonas Devlieghere #include "llvm/ADT/StringRef.h" 2501263c6cSJonas Devlieghere 2601263c6cSJonas Devlieghere #include <fstream> 2701263c6cSJonas Devlieghere #include <string> 2801263c6cSJonas Devlieghere 2901263c6cSJonas Devlieghere // Windows requires different system calls for dealing with sockets and other 3001263c6cSJonas Devlieghere // types of files, so we can't simply have one code path that just uses read 3101263c6cSJonas Devlieghere // and write everywhere. So we need an abstraction in order to allow us to 3201263c6cSJonas Devlieghere // treat them identically. 3301263c6cSJonas Devlieghere namespace lldb_dap { 3401263c6cSJonas Devlieghere struct StreamDescriptor { 3501263c6cSJonas Devlieghere StreamDescriptor(); 3601263c6cSJonas Devlieghere ~StreamDescriptor(); 3701263c6cSJonas Devlieghere StreamDescriptor(StreamDescriptor &&other); 3801263c6cSJonas Devlieghere 3901263c6cSJonas Devlieghere StreamDescriptor &operator=(StreamDescriptor &&other); 4001263c6cSJonas Devlieghere 4101263c6cSJonas Devlieghere static StreamDescriptor from_socket(SOCKET s, bool close); 4201263c6cSJonas Devlieghere static StreamDescriptor from_file(int fd, bool close); 4301263c6cSJonas Devlieghere 4401263c6cSJonas Devlieghere bool m_is_socket = false; 4501263c6cSJonas Devlieghere bool m_close = false; 4601263c6cSJonas Devlieghere union { 4701263c6cSJonas Devlieghere int m_fd; 4801263c6cSJonas Devlieghere SOCKET m_socket; 4901263c6cSJonas Devlieghere }; 5001263c6cSJonas Devlieghere }; 5101263c6cSJonas Devlieghere 5201263c6cSJonas Devlieghere struct InputStream { 5301263c6cSJonas Devlieghere StreamDescriptor descriptor; 5401263c6cSJonas Devlieghere 55*873426beSJohn Harrison explicit InputStream(StreamDescriptor descriptor) 56*873426beSJohn Harrison : descriptor(std::move(descriptor)) {} 57*873426beSJohn Harrison 5801263c6cSJonas Devlieghere bool read_full(std::ofstream *log, size_t length, std::string &text); 5901263c6cSJonas Devlieghere 6001263c6cSJonas Devlieghere bool read_line(std::ofstream *log, std::string &line); 6101263c6cSJonas Devlieghere 6201263c6cSJonas Devlieghere bool read_expected(std::ofstream *log, llvm::StringRef expected); 6301263c6cSJonas Devlieghere }; 6401263c6cSJonas Devlieghere 6501263c6cSJonas Devlieghere struct OutputStream { 6601263c6cSJonas Devlieghere StreamDescriptor descriptor; 6701263c6cSJonas Devlieghere 68*873426beSJohn Harrison explicit OutputStream(StreamDescriptor descriptor) 69*873426beSJohn Harrison : descriptor(std::move(descriptor)) {} 70*873426beSJohn Harrison 7101263c6cSJonas Devlieghere bool write_full(llvm::StringRef str); 7201263c6cSJonas Devlieghere }; 7301263c6cSJonas Devlieghere } // namespace lldb_dap 7401263c6cSJonas Devlieghere 7501263c6cSJonas Devlieghere #endif 76