aboutsummaryrefslogtreecommitdiff
path: root/store/works/life/computer-network-experiment/Common.h
diff options
context:
space:
mode:
Diffstat (limited to 'store/works/life/computer-network-experiment/Common.h')
-rw-r--r--store/works/life/computer-network-experiment/Common.h54
1 files changed, 54 insertions, 0 deletions
diff --git a/store/works/life/computer-network-experiment/Common.h b/store/works/life/computer-network-experiment/Common.h
new file mode 100644
index 0000000..1e6c277
--- /dev/null
+++ b/store/works/life/computer-network-experiment/Common.h
@@ -0,0 +1,54 @@
+#pragma once
+#include "StringUtil.hpp"
+
+#include <iostream>
+#include <sstream>
+#include <string>
+#include <string_view>
+
+#ifdef WIN32
+using Char = wchar_t;
+using String = std::wstring;
+using StringView = std::wstring_view;
+using StringStream = std::wstringstream;
+inline auto &input_stream = std::wcin;
+inline auto &output_stream = std::wcout;
+inline auto &error_stream = std::wcerr;
+#define CRUT(string_literal) L##string_literal
+
+inline String ConvertCharString(std::string_view s) {
+ return cru::ToUtf16WString(s);
+}
+
+inline std::string ConvertCharStringBack(StringView s) {
+ return cru::ToUtf8(s);
+}
+#else
+using Char = char;
+using String = std::string;
+using StringView = std::string_view;
+using StringStream = std::stringstream;
+inline auto &input_stream = std::cin;
+inline auto &output_stream = std::cout;
+inline auto &error_stream = std::cerr;
+#define CRUT(string_literal) string_literal
+
+inline String ConvertCharString(std::string_view s) { return String(s); }
+inline std::string ConvertCharStringBack(StringView s) {
+ return std::string(s);
+}
+#endif
+
+int Main();
+
+[[noreturn]] void PrintErrorMessageAndExit(StringView message,
+ bool print_last_error = true);
+
+int CloseSocket(int socket);
+
+void BeforeExit();
+
+// Return false for error.
+bool SafeSend(int socket, std::string_view buffer);
+// Return false for error.
+bool SafeReadUntil(int socket, char c, std::string &data, std::string &rest);