ecere: Fixes to support MinGW-w64
[sdk] / ecere / src / sys / File.ec
index d71b900..58fef0f 100644 (file)
@@ -1,8 +1,10 @@
 namespace sys;
 
 default:
+#define set _set
 #define uint _uint
 #define File _File
+#undef __BLOCKS__
 #include <stdio.h>
 #include <stdarg.h>
 #include <stdlib.h>
@@ -38,7 +40,9 @@ default:
 
 #if defined(__WIN32__)
 #define WIN32_LEAN_AND_MEAN
+#define String String_
 #include <windows.h>
+#undef String
 #include <io.h>
 
 BOOL WINAPI GetVolumePathName(LPCTSTR lpszFileName,LPTSTR lpszVolumePathName,DWORD cchBufferLength);
@@ -65,6 +69,7 @@ DWORD APIENTRY WNetGetResourceInformationA(LPNETRESOURCE lpNetResource, LPVOID l
 #endif //#ifndef ECERE_BOOTSTRAP
 private:
 
+#undef set
 #undef uint
 #undef File
 
@@ -138,6 +143,43 @@ public class FileSize : uint
    }
 };
 
+public class FileSize64 : uint64
+{
+   int OnCompare(FileSize64 data2)
+   {
+      int result = 0;
+      if(&this && &data2)
+      {
+         if(this > data2)
+            result = 1;
+         else if(this < data2)
+            result = -1;
+      }
+      return result;
+   }
+
+   char * OnGetString(char * string, void * fieldData, bool * needClass)
+   {
+      PrintBigSize(string, this, 2);
+      return string;
+   }
+
+   bool OnGetDataFromString(char * string)
+   {
+      char * end;
+      double value = strtod(string, &end);
+      uint64 multiplier = 1;
+           if(strstr(end, "PB") || strstr(end, "pb")) multiplier = (uint64)1024 * 1024 * 1024 * 1024;
+      else if(strstr(end, "TB") || strstr(end, "tb")) multiplier = (uint64)1024 * 1024 * 1024 * 1024;
+      else if(strstr(end, "GB") || strstr(end, "gb")) multiplier = (uint64)1024 * 1024 * 1024;
+      else if(strstr(end, "MB") || strstr(end, "mb")) multiplier = (uint64)1024 * 1024;
+      else if(strstr(end, "KB") || strstr(end, "kb")) multiplier = 1024;
+
+      this = (uint64)(multiplier * value);
+      return true;
+   }
+};
+
 class FileSystem
 {
    virtual File ::Open(char * archive, char * name, FileOpenMode mode);
@@ -162,7 +204,7 @@ public enum FileOpenMode { read = 1, write, append, readWrite, writeRead, append
 public enum FileSeekMode { start, current, end };
 
 #if !defined(ECERE_BOOTSTRAP)
-static FileDialog fileDialog { text = "Select File" };
+static FileDialog fileDialog { text = $"Select File" };
 #endif
 
 public enum FileLock
@@ -244,7 +286,7 @@ public class File : IOChannel
       Window editData = class::OnEdit(dataBox, obsolete, x + 24, y, w - 48, h, userData);
       Button load
       { 
-         dataBox, inactive = true, text = "Imp", hotKey = f2,
+         dataBox, inactive = true, text = $"Imp", hotKey = f2,
          position = { Max(x + 24, x + w - 24), y }, size = { 24, h };
 
          bool DataBox::NotifyClicked(Button button, int x, int y, Modifiers mods)
@@ -268,7 +310,7 @@ public class File : IOChannel
       };
       Button save
       { 
-         dataBox, inactive = true, text = "Exp", hotKey = f2,
+         dataBox, inactive = true, text = $"Exp", hotKey = f2,
          position = { Max(x + 24, x + w - 48), y }, size = { 24, h };
 
          bool DataBox::NotifyClicked(Button button, int x, int y, Modifiers mods)
@@ -527,13 +569,17 @@ public:
    int Printf(char * format, ...)
    {
       int result = 0;
-      char text[MAX_F_STRING];
-      va_list args;
-      va_start(args, format);
-      vsprintf(text, format, args);
-      if(Puts(text))
-         result = strlen(text);
-      va_end(args);
+      if(format)
+      {
+         char text[MAX_F_STRING];
+         va_list args;
+         va_start(args, format);
+         vsnprintf(text, sizeof(text), format, args);
+         text[sizeof(text)-1] = 0;
+         if(Puts(text))
+            result = strlen(text);
+         va_end(args);
+      }
       return result;
    }
 
@@ -723,6 +769,7 @@ public:
                break;
             }
          }
+         delete f;
       }
       Seek(0, start);
       return result;
@@ -1335,17 +1382,19 @@ static FileDesc FileFind(char * path, char * extensions)
                if(path[0])
                {
                   strcpy(file.path, path);
-                  strcat(file.path, DIR_SEPS);
+                  if(path[1])
+                     strcat(file.path, DIR_SEPS);
                }
                strcpy(file.name,de->d_name);
                strcat(file.path, file.name);
-               stat(file.path, &s);
-               file.stats.attribs = (s.st_mode&S_IFDIR) ? FileAttribs { isDirectory = true } : FileAttribs { isFile = true };
-               file.stats.size = s.st_size;
-               file.stats.accessed = s.st_atime;
-               file.stats.modified = s.st_mtime;
-               file.stats.created = s.st_ctime;
-          
+               if(!stat(file.path, &s))
+               {
+                  file.stats.attribs = (s.st_mode&S_IFDIR) ? FileAttribs { isDirectory = true } : FileAttribs { isFile = true };
+                  file.stats.size = s.st_size;
+                  file.stats.accessed = s.st_atime;
+                  file.stats.modified = s.st_mtime;
+                  file.stats.created = s.st_ctime;
+               }
                strcpy(d.name, path);
 
                result = file;
@@ -1610,16 +1659,18 @@ private class FileDesc : struct
             {
                strcpy(name,de->d_name);
                strcpy(path, d.name);
-               if(d.name[0] && d.name[0])
+               if(d.name[0] && d.name[1])
                   strcat(path, DIR_SEPS);
                strcat(path, name);
-               stat(path, &s);
-               stats.attribs = FileAttribs { };
-               stats.attribs = (s.st_mode&S_IFDIR) ? FileAttribs { isDirectory = true } : FileAttribs { isFile = true };
-               stats.size = s.st_size;
-               stats.accessed = s.st_atime;
-               stats.modified = s.st_mtime;
-               stats.created = s.st_ctime;
+               if(!stat(path, &s))
+               {
+                  stats.attribs = FileAttribs { };
+                  stats.attribs = (s.st_mode&S_IFDIR) ? FileAttribs { isDirectory = true } : FileAttribs { isFile = true };
+                  stats.size = s.st_size;
+                  stats.accessed = s.st_atime;
+                  stats.modified = s.st_mtime;
+                  stats.created = s.st_ctime;
+               }
                result = this;
             }
             else