windows,singlejar: compile "token_stream"

We can now compile
//src/tools/singlejar:token_stream on Windows.

See https://github.com/bazelbuild/bazel/issues/2241

Change-Id: I98f86e608e5ebaf685e4de26b2dabe75fcca78d2
PiperOrigin-RevId: 185655986
diff --git a/src/tools/singlejar/diag.h b/src/tools/singlejar/diag.h
index 5dd4a7f..775ab05 100644
--- a/src/tools/singlejar/diag.h
+++ b/src/tools/singlejar/diag.h
@@ -20,12 +20,32 @@
  * for portability.
  */
 #if defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__)
+
 #include <err.h>
 #define diag_err(...) err(__VA_ARGS__)
 #define diag_errx(...) errx(__VA_ARGS__)
 #define diag_warn(...) warn(__VA_ARGS__)
 #define diag_warnx(...) warnx(__VA_ARGS__)
+
+#elif defined(COMPILER_MSVC)
+
+#include <stdio.h>
+#include <string.h>
+#include <windows.h>
+#define _diag_msg(prefix, msg, ...) \
+  { fprintf(stderr, prefix " [" __FILE__ ":%d]" msg, __LINE__, __VA_ARGS__); }
+#define _diag_msgx(eval, prefix, msg, ...) \
+  {                                        \
+    _diag_msg(prefix, msg, __VA_ARGS__);   \
+    ::ExitProcess(eval);                   \
+  }
+#define diag_err(eval, fmt, ...) _diag_msgx(eval, "ERROR", fmt, __VA_ARGS__)
+#define diag_errx(eval, ...) _diag_msgx(eval, "ERROR", "", __VA_ARGS__)
+#define diag_warn(...) _diag_msg("WARNING", __VA_ARGS__)
+#define diag_warnx(...) _diag_msg("WARNING")
+
 #else
 #error Unknown platform
 #endif
+
 #endif  // BAZEL_SRC_TOOLS_SINGLEJAR_DIAG_H_
diff --git a/src/tools/singlejar/token_stream.h b/src/tools/singlejar/token_stream.h
index 23ee84d..3bad0a8 100644
--- a/src/tools/singlejar/token_stream.h
+++ b/src/tools/singlejar/token_stream.h
@@ -57,6 +57,13 @@
   class FileTokenStream {
    public:
     FileTokenStream(const char *filename) {
+      // TODO(laszlocsomor): use the fopen and related file handling API
+      // implementations from ProtoBuf, in order to support long paths:
+      // https://github.com/google/protobuf/blob/
+      //   47b7d2c7cadf74ceec90fc5042232819cd0dd557/
+      //   src/google/protobuf/stubs/io_win32.cc
+      // Best would be to extract that library to a common location and use
+      // here, in ProtoBuf, and in Bazel itself.
       if (!(fp_ = fopen(filename, "r"))) {
         diag_err(1, "%s", filename);
       }