main: add multiple search paths for config.json

For some platforms, /usr/share is read-only folder which is not allowed
to add new config.json.

Add multiple default paths for searching config.json

1. pwd
2. /var/lib/swampd
3. /usr/share/swampd

Tested on Bletchley.

Signed-off-by: Potin Lai <potin.lai@quantatw.com>
Change-Id: I21f3963f5f33b65557b42c8e3700d73b139140a5
diff --git a/main.cpp b/main.cpp
index 09d9b70..bace238 100644
--- a/main.cpp
+++ b/main.cpp
@@ -61,9 +61,7 @@
 
 } // namespace pid_control
 
-/** the swampd daemon will check for the existence of this file. */
-constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json";
-std::string configPath = "";
+std::filesystem::path configPath = "";
 
 /* async io context for operation */
 boost::asio::io_context io;
@@ -80,6 +78,24 @@
 namespace pid_control
 {
 
+std::filesystem::path searchConfigurationPath()
+{
+    static constexpr auto name = "config.json";
+
+    for (auto pathSeg : {std::filesystem::current_path(),
+                         std::filesystem::path{"/var/lib/swampd"},
+                         std::filesystem::path{"/usr/share/swampd"}})
+    {
+        auto file = pathSeg / name;
+        if (std::filesystem::exists(file))
+        {
+            return file;
+        }
+    }
+
+    return name;
+}
+
 void restartControlLoops()
 {
     static SensorManager mgmr;
@@ -101,8 +117,8 @@
     zones.clear();
     isCanceling = false;
 
-    const std::string& path =
-        (configPath.length() > 0) ? configPath : jsonConfigurationPath;
+    const std::filesystem::path path =
+        (!configPath.empty()) ? configPath : searchConfigurationPath();
 
     if (std::filesystem::exists(path))
     {