ecere,ecereCOM: support Emscriptem platform. add new Emscripten interface driver.
[sdk] / ecere / src / sys / FileMonitor.ec
index bbd063c..7ea5966 100644 (file)
@@ -2,6 +2,8 @@ namespace sys;
 
 import "System"
 
+#if !defined(__EMSCRIPTEN__)
+
 public class FileChange
 {
 public:
@@ -35,7 +37,7 @@ public class FileMonitor
 public:
    property void * userData { set { data = value; } };
    property FileChange fileChange { set { watchFor = value; } };
-   property char * fileName
+   property const char * fileName
    {
       set
       {
@@ -47,7 +49,7 @@ public:
       }
       get { return fileName; }
    };
-   property char * directoryName
+   property const char * directoryName
    {
       set
       {
@@ -114,12 +116,12 @@ private:
    TimeStamp modified;
    FileSize size;
    FileAttribs attribs;
-   virtual bool any_object::OnDirNotify(FileChange action, char * fileName, char * param);
+   public virtual bool any_object::OnDirNotify(FileChange action, const char * fileName, const char * param);
 
    // For directory monitors
    bool directory;
    OldList files;
-   virtual bool any_object::OnFileNotify(FileChange action, char * param);
+   public virtual bool any_object::OnFileNotify(FileChange action, const char * param);
 
    /*
 #if defined(__WIN32__)
@@ -139,10 +141,11 @@ private:
       globalSystem.fileMonitorMutex.Release();
    }
 
-   void MonitorFile(char * filePath)
+   void MonitorFile(const char * filePath)
    {
       FileAttribs exists = FileExists(filePath);
       FileStats stats;
+      String oldFileName = fileName;
 
       //incref this;
       globalSystem.fileMonitorMutex.Wait();
@@ -153,8 +156,8 @@ private:
       if(!active && !toBeFreed)
          globalSystem.fileMonitors.Add(this);
       this.exists = exists && !exists.isDirectory;
-      delete fileName;
       fileName = CopyString(filePath);
+      delete oldFileName;
       this.watchFor = watchFor;
 
       if(FileGetStats(filePath, stats))
@@ -181,9 +184,10 @@ private:
       }
    }
 
-   void MonitorDirectory(char * filePath)
+   void MonitorDirectory(const char * filePath)
    {
       FileStats stats;
+      String oldFileName = fileName;
 
       //incref this;
       globalSystem.fileMonitorMutex.Wait();
@@ -197,8 +201,8 @@ private:
       exists = FileExists(filePath).isDirectory;
 
       this.watchFor = watchFor;
-      delete fileName;
       fileName = CopyString(filePath);
+      delete oldFileName;
 
       if(FileGetStats(fileName, stats))
       {
@@ -231,11 +235,11 @@ private:
       // delete this;
    }
 
-   bool AddFileNotify(FileChange action, char * fileName, char * param)
+   bool AddFileNotify(FileChange action, const char * fileName, const char * param)
    {
       if(watchFor & action)
       {
-         fileNotifies.Add(FileNotify { 
+         fileNotifies.Add(FileNotify {
                monitor = this, action = action, fileName = CopyString(fileName), param = CopyString(param)
             });
          return true;
@@ -267,11 +271,11 @@ static int CompareFiles(FileEntry e1, FileEntry e2, void * data)
       return 1;
    else if(e1.attribs.isDirectory && !e2.attribs.isDirectory)
       return -1;
-   else 
+   else
       return strcmp(e1.name, e2.name);
 }
 
-static void GetFileEntries(OldList list, char * path)
+static void GetFileEntries(OldList list, const char * path)
 {
    FileListing listing { path, null };
    while(listing.Find())
@@ -301,7 +305,7 @@ static class MonitorThread : Thread
             bool fileActivity = false;
             lastTime = currentTime;
 
-            // printf("[%d] Waiting in MonitorThread for fileMonitor Mutex %x...\n", GetCurrentThreadID(), globalSystem.fileMonitorMutex);
+            // printf("[%d] Waiting in MonitorThread for fileMonitor Mutex %x...\n", (int)GetCurrentThreadID(), globalSystem.fileMonitorMutex);
             globalSystem.fileMonitorMutex.Wait();
 
             for(monitor = globalSystem.fileMonitors.first; monitor; monitor = monitor.next)
@@ -363,7 +367,7 @@ static class MonitorThread : Thread
                      }
                      monitor.files.Free(FileEntry::Free);
                      monitor.files = newEntries;
-                  }            
+                  }
                }
                else if(monitor.fileName)
                {
@@ -379,7 +383,7 @@ static class MonitorThread : Thread
                   {
                      fileActivity |= monitor.AddFileNotify(FileChange { deleted = true }, monitor.fileName, null);
                      monitor.exists = false;
-                  }            
+                  }
                   else if(monitor.exists)
                   {
                      FileStats stats { };
@@ -415,17 +419,19 @@ static class MonitorThread : Thread
             }
             if(fileActivity)
             {
-               // printf("[%d] Signaling Event...\n", GetCurrentThreadID());
+               // printf("[%d] Signaling Event...\n", (int)GetCurrentThreadID());
                guiApp.SignalEvent();
             }
 
-            // printf("[%d] Releasing Mutex...\n", GetCurrentThreadID());
+            // printf("[%d] Releasing Mutex...\n", (int)GetCurrentThreadID());
             globalSystem.fileMonitorMutex.Release();
          }
-         
-         // printf("[%d] Sleeping...\n", GetCurrentThreadID());
+
+         // printf("[%d] Sleeping...\n", (int)GetCurrentThreadID());
          Sleep(1.0 / 18.2);
       }
       return 0;
    }
 }
+
+#endif // !defined(__EMSCRIPTEN__)