From 38d46b2dda0243cfa60bc6945859ebd587e0a851 Mon Sep 17 00:00:00 2001
From: Steph Enders <smenders@gmail.com>
Date: Mon, 27 Jun 2022 17:45:44 -0400
Subject: Update MacOS to compile w/ Boost

MacOS seems to rely on an older/different C++ stdlib than Windows or
Linux. So replacing those with Boost equivalents.

Mostly contained to the Resources thankfully!
---
 src/resources/Resources.cpp          | 81 ++++++++++++++++++++----------------
 src/resources/Resources.h            | 41 +++++++++---------
 src/resources/macos/MacResources.cpp | 20 ++++-----
 src/resources/macos/MacResources.h   | 14 +++----
 4 files changed, 80 insertions(+), 76 deletions(-)

(limited to 'src/resources')

diff --git a/src/resources/Resources.cpp b/src/resources/Resources.cpp
index ee43002..60ea836 100644
--- a/src/resources/Resources.cpp
+++ b/src/resources/Resources.cpp
@@ -30,50 +30,50 @@
 #include <iostream>
 
 Resources::Resources() {
-  this->font = std::make_shared<fs::path>();
-  this->defaultsLua = std::make_shared<fs::path>();
+  this->font = std::make_shared<filesystem::path>();
+  this->defaultsLua = std::make_shared<filesystem::path>();
 }
 
 void Resources::loadFontFiles() {
   // We will search 1 level deep
   for (auto &base : this->fontSearchDirs()) {
-    if (fs::exists(base) && fs::is_directory(base)) {
-      for (auto &item : fs::directory_iterator(base)) {
+    if (filesystem::exists(base) && filesystem::is_directory(base)) {
+      for (auto &item : filesystem::directory_iterator(base)) {
         // only 1 deep
-        if (item.exists() && item.is_directory()) {
-          for (auto &file : fs::directory_iterator(item)) {
-            if (file.exists() && file.is_regular_file()) {
-              auto ext = file.path().extension();
+        if (filesystem::exists(item) && filesystem::is_directory(item)) {
+          for (auto &file : filesystem::directory_iterator(item)) {
+            if (filesystem::exists(file) && filesystem::is_regular_file(file)) {
+              auto ext = filesystem::path(file).extension();
               bool matched =
                   std::any_of(std::begin(FONT_TYPES), std::end(FONT_TYPES),
                               [ext](auto &supported) {
                                 return supported == ext.generic_string();
                               });
               if (matched) {
-                fonts.push_back(file.path());
+                fonts.emplace_back(file);
                 // set default if not set
-                if (!exists(*this->font) ||
-                    !is_regular_file(*this->font) &&
-                        file.path().filename() == DEFAULT_FONT) {
-                  *this->font = file.path();
+                if ((!filesystem::exists(*this->font) ||
+                     !filesystem::is_regular_file(*this->font)) &&
+                    filesystem::path(file).filename() == DEFAULT_FONT) {
+                  *this->font = filesystem::path(file);
                 }
               }
             }
           }
-        } else if (item.is_regular_file()) {
-          auto ext = item.path().extension();
+        } else if (filesystem::is_regular_file(item)) {
+          auto ext = filesystem::path(item).extension();
           bool matched =
               std::any_of(std::begin(FONT_TYPES), std::end(FONT_TYPES),
                           [ext](auto &supported) {
                             return supported == ext.generic_string();
                           });
           if (matched) {
-            fonts.push_back(item.path());
+            fonts.emplace_back(item);
             // set default if not set
-            if (!exists(*this->font) ||
-                !is_regular_file(*this->font) &&
-                    item.path().filename() == DEFAULT_FONT) {
-              *this->font = item.path();
+            if ((!filesystem::exists(*this->font) ||
+                 !filesystem::is_regular_file(*this->font)) &&
+                filesystem::path(item).filename() == DEFAULT_FONT) {
+              *this->font = filesystem::path(item);
             }
           }
         }
@@ -84,11 +84,11 @@ void Resources::loadFontFiles() {
 
 void Resources::loadLevels() {
   for (auto &base : this->levelSearchDirs()) {
-    if (fs::exists(base) && fs::is_directory(base)) {
-      for (auto &dir : fs::directory_iterator(base)) {
-        if (dir.exists() && dir.is_directory()) {
-          if (fs::exists(dir / DNG_MAP)) {
-            this->levels.push_back(dir.path());
+    if (filesystem::exists(base) && filesystem::is_directory(base)) {
+      for (auto &dir : filesystem::directory_iterator(base)) {
+        if (filesystem::exists(dir) && filesystem::is_directory(dir)) {
+          if (filesystem::exists(dir / DNG_MAP)) {
+            this->levels.emplace_back(dir);
           }
         }
       }
@@ -99,36 +99,43 @@ void Resources::loadLevels() {
 void Resources::loadDefaultLuaFile() {
   for (auto &base : this->defaultsSearchDirs()) {
     auto f = base / DEFAULT_LUA;
-    if (fs::exists(f)) {
+    if (filesystem::exists(f)) {
       *this->defaultsLua = f;
       break;
     }
   }
 }
-std::vector<fs::path> Resources::getFontFiles() { return this->fonts; }
-std::vector<fs::path> Resources::getLevels() { return this->levels; }
-shared_ptr<fs::path> Resources::getFontFile() { return this->font; }
-shared_ptr<fs::path> Resources::updateFont(int idx) {
+std::vector<filesystem::path> Resources::getFontFiles() { return this->fonts; }
+std::vector<filesystem::path> Resources::getLevels() { return this->levels; }
+std::shared_ptr<filesystem::path> Resources::getFontFile() {
+  return this->font;
+}
+std::shared_ptr<filesystem::path> Resources::updateFont(int idx) {
   auto f = this->fonts[idx];
   *this->font = f;
   return getFontFile();
 }
-shared_ptr<fs::path> Resources::getDefaultsLuaFile() {
+std::shared_ptr<filesystem::path> Resources::getDefaultsLuaFile() {
   return this->defaultsLua;
 }
-shared_ptr<fs::path> Resources::getLevelMap(int idx) {
+std::shared_ptr<filesystem::path> Resources::getLevelMap(int idx) {
   auto lvlBase = this->levels[idx];
-  fs::path dngMap = lvlBase / DNG_MAP;
+  filesystem::path dngMap = lvlBase / DNG_MAP;
   // existence of the level dng.map is asserted in the initializer
-  assert(fs::exists(dngMap));
-  return std::make_shared<fs::path>(dngMap);
+  assert(filesystem::exists(dngMap));
+  return std::make_shared<filesystem::path>(dngMap);
 }
-std::optional<shared_ptr<fs::path>> Resources::getLevelProcLua(int idx) {
+optional<std::shared_ptr<filesystem::path>>
+Resources::getLevelProcLua(int idx) {
   auto lvlBase = this->levels[idx];
   auto procLua = lvlBase / PROC_LUA;
   if (exists(procLua)) {
-    return std::make_shared<fs::path>(procLua);
+    return std::make_shared<filesystem::path>(procLua);
   } else {
+#ifdef __APPLE__
+    return boost::optional<std::shared_ptr<filesystem::path>>();
+#else
     return nullopt;
+#endif
   }
 }
\ No newline at end of file
diff --git a/src/resources/Resources.h b/src/resources/Resources.h
index 2e0e7c0..c307c1b 100644
--- a/src/resources/Resources.h
+++ b/src/resources/Resources.h
@@ -29,13 +29,15 @@
 #define DNG_RESOURCES_H
 
 #ifdef __APPLE__
-#include <experimental/filesystem>
-namespace fs = std::experimental::filesystem;
+#include "boost/filesystem.hpp"
+#include "boost/optional.hpp"
+using namespace boost;
 #else
 #include <filesystem>
-namespace fs = std::filesystem;
-#endif
 #include <optional>
+using namespace std;
+#endif
+#include <memory>
 #include <vector>
 
 // struct LevelInfo {
@@ -43,9 +45,8 @@ namespace fs = std::filesystem;
 //   filesystem::path dngMap;
 //   optional<filesystem::path> procLua;
 // };
-using namespace std;
 
-static const string FONT_TYPES[] = {".otf", ".ttf"};
+static const std::string FONT_TYPES[] = {".otf", ".ttf"};
 static const filesystem::path DEFAULT_LUA{"defaults.lua"};
 static const filesystem::path DNG_MAP{"dng.map"};
 static const filesystem::path PROC_LUA{"proc.lua"};
@@ -54,26 +55,26 @@ static const char *DEFAULT_FONT = "PressStart2P-vaV7.ttf";
 class Resources {
 
 protected:
-  shared_ptr<fs::path> font;                // chosen font
-  shared_ptr<filesystem::path> defaultsLua; // defaults lua file
-  vector<filesystem::path> fonts;           // all found maps
-  vector<filesystem::path> levels;          // all found maps
-  virtual vector<filesystem::path> levelSearchDirs() = 0;
-  virtual vector<filesystem::path> defaultsSearchDirs() = 0;
-  virtual vector<filesystem::path> fontSearchDirs() = 0;
+  std::shared_ptr<filesystem::path> font;        // chosen font
+  std::shared_ptr<filesystem::path> defaultsLua; // defaults lua file
+  std::vector<filesystem::path> fonts;           // all found maps
+  std::vector<filesystem::path> levels;          // all found maps
+  virtual std::vector<filesystem::path> levelSearchDirs() = 0;
+  virtual std::vector<filesystem::path> defaultsSearchDirs() = 0;
+  virtual std::vector<filesystem::path> fontSearchDirs() = 0;
 
 public:
   Resources();
   void loadLevels();
   void loadFontFiles();
   void loadDefaultLuaFile();
-  vector<filesystem::path> getFontFiles();
-  vector<filesystem::path> getLevels();
-  shared_ptr<fs::path> updateFont(int idx);
-  shared_ptr<fs::path> getFontFile();
-  shared_ptr<fs::path> getDefaultsLuaFile();
-  shared_ptr<fs::path> getLevelMap(int idx);
-  optional<shared_ptr<fs::path>> getLevelProcLua(int idx);
+  std::vector<filesystem::path> getFontFiles();
+  std::vector<filesystem::path> getLevels();
+  std::shared_ptr<filesystem::path> updateFont(int idx);
+  std::shared_ptr<filesystem::path> getFontFile();
+  std::shared_ptr<filesystem::path> getDefaultsLuaFile();
+  std::shared_ptr<filesystem::path> getLevelMap(int idx);
+  optional<std::shared_ptr<filesystem::path>> getLevelProcLua(int idx);
   /* Helper method to convert any path to a const char *
    * Windows uses wchar_t so this can help provide a common way to
    * interact with files
diff --git a/src/resources/macos/MacResources.cpp b/src/resources/macos/MacResources.cpp
index 2b41daa..9ae3499 100644
--- a/src/resources/macos/MacResources.cpp
+++ b/src/resources/macos/MacResources.cpp
@@ -26,28 +26,26 @@
 //========================================================================
 
 #include "MacResources.h"
-namespace fs = std::experimental::filesystem;
 
 MacResources::MacResources() : Resources() {
-  this->workingDir = std::filesystem::current_path();
-  this->exeDir = std::filesystem::canonical("/proc/self/exe").remove_filename();
+  this->workingDir = filesystem::current_path();
+  this->exeDir = filesystem::canonical("/proc/self/exe").remove_filename();
   // set an initial value - will get hardset in load if found
-  std::filesystem::path f =
-      workingDir / "res" / DEFAULT_FONT; // default as fallback
-  this->font = std::make_shared<std::filesystem::path>(f);
-  this->defaultsLua = std::make_shared<std::filesystem::path>();
+  filesystem::path f = workingDir / "res" / DEFAULT_FONT; // default as fallback
+  this->font = std::make_shared<filesystem::path>(f);
+  this->defaultsLua = std::make_shared<filesystem::path>();
 }
 
-std::vector<std::filesystem::path> MacResources::levelSearchDirs() {
+std::vector<filesystem::path> MacResources::levelSearchDirs() {
   return {workingDir / "maps", exeDir / "maps"};
 }
-std::vector<std::filesystem::path> MacResources::defaultsSearchDirs() {
+std::vector<filesystem::path> MacResources::defaultsSearchDirs() {
   return {workingDir / "dnglib", exeDir / "dnglib"};
 }
-std::vector<std::filesystem::path> MacResources::fontSearchDirs() {
+std::vector<filesystem::path> MacResources::fontSearchDirs() {
   return {workingDir / "res", exeDir / "res"};
 }
 
-const char *MacResources::convert_to_str(std::filesystem::path &path) {
+const char *MacResources::convert_to_str(filesystem::path &path) {
   return path.c_str();
 }
diff --git a/src/resources/macos/MacResources.h b/src/resources/macos/MacResources.h
index 1d19575..4cb66cd 100644
--- a/src/resources/macos/MacResources.h
+++ b/src/resources/macos/MacResources.h
@@ -29,21 +29,19 @@
 #define DNG_MACRESOURCES_H
 #include "../Resources.h"
 
-namespace fs = std::experimental::filesystem;
-
 class MacResources : public Resources {
 public:
   MacResources();
 
 protected:
 public:
-  const char *convert_to_str(fs::path &path) override;
+  const char *convert_to_str(filesystem::path &path) override;
 
 protected:
-  fs::path exeDir;
-  fs::path workingDir;
-  std::vector<fs::path> levelSearchDirs() override;
-  std::vector<fs::path> defaultsSearchDirs() override;
-  std::vector<fs::path> fontSearchDirs() override;
+  filesystem::path exeDir;
+  filesystem::path workingDir;
+  std::vector<filesystem::path> levelSearchDirs() override;
+  std::vector<filesystem::path> defaultsSearchDirs() override;
+  std::vector<filesystem::path> fontSearchDirs() override;
 };
 #endif // DNG_MACRESOURCES_H
-- 
cgit v1.2.3-54-g00ecf