OSDN Git Service

Merge branch 'develop' into macos-develop
[hengbandforosx/hengbandosx.git] / src / util / angband-files.cpp
index c44e149..0aacc46 100644 (file)
@@ -5,6 +5,8 @@
 #include <sstream>
 #include <string>
 
+void (*file_open_hook)(const std::filesystem::path &path, const FileOpenType ftype) = 0;
+
 #ifdef SET_UID
 
 #ifndef HAVE_USLEEP
@@ -207,11 +209,17 @@ static std::string make_file_mode(const FileOpenMode mode, const bool is_binary)
  * @param is_binary バイナリモードか否か (無指定の場合false:テキストモード)
  * @return ファイルポインタ
  */
-FILE *angband_fopen(const std::filesystem::path &path, const FileOpenMode mode, const bool is_binary)
+FILE *angband_fopen(const std::filesystem::path &path, const FileOpenMode mode, const bool is_binary, const FileOpenType ftype)
 {
+    FILE *result;
+
     const auto &parsed_path = path_parse(path);
     const auto &open_mode = make_file_mode(mode, is_binary);
-    return fopen(parsed_path.string().data(), open_mode.data());
+    result = fopen(parsed_path.string().data(), open_mode.data());
+    if (result && mode != FileOpenMode::READ && file_open_hook) {
+        file_open_hook(path, ftype);
+    }
+    return result;
 }
 
 /*
@@ -273,6 +281,14 @@ errr angband_fgets(FILE *fff, char *buf, ulong n)
         guess_convert_to_system_encoding(file_read__tmp.data(), FILE_READ_BUFF_SIZE);
 #endif
         for (s = file_read__tmp.data(); *s; s++) {
+#ifdef MACH_O_COCOA
+            /*
+             * Be nice to the Macintosh, where a file can have Mac or Unix
+             * end of line, especially since the introduction of OS X.
+             * MPW tools were also very tolerant to the Unix EOL.
+             */
+            if (*s == '\r') *s = '\n';
+#endif /* MACH_O_COCOA */
             if (*s == '\n') {
                 buf[i] = '\0';
                 return 0;