17 #define IS_ALUNDER(ch) ((ch) == '_' || isalnum((ch)))
19 #if defined(ECERE_BOOTSTRAP)
26 #ifndef ECERE_BOOTSTRAP
27 #if defined(__GNUC__) || defined(__WATCOMC__) || defined(__WIN32__)
29 #include <sys/types.h>
34 #if defined(__unix__) || defined(__APPLE__)
38 #if defined(__WIN32__) || defined(__WATCOMC__)
44 #if defined(__WIN32__)
45 #define WIN32_LEAN_AND_MEAN
46 #define String String_
51 BOOL __declspec(dllimport) WINAPI GetVolumePathName(LPCTSTR lpszFileName,LPTSTR lpszVolumePathName,DWORD cchBufferLength);
53 // Missing function...
55 #ifndef WNetGetResourceInformation
56 DWORD APIENTRY WNetGetResourceInformationA(LPNETRESOURCE lpNetResource, LPVOID lpBuffer, LPDWORD lpcbBuffer, LPTSTR* lplpSystem);
58 #define WNetGetResourceInformation WNetGetResourceInformationW
60 #define WNetGetResourceInformation WNetGetResourceInformationA
72 #endif //#ifndef ECERE_BOOTSTRAP
82 #if !defined(ECERE_VANILLA) && !defined(ECERE_NONET) && !defined(ECERE_BOOTSTRAP)
88 // IMPLEMENTATION OF THESE IS IN _File.c
92 FILE *eC_stdout(void);
94 uint FILE_GetSize(FILE * input);
95 bool FILE_Lock(FILE * input, FILE * output, FileLock type, uint64 start, uint64 length, bool wait);
96 void FILE_set_buffered(FILE * input, FILE * output, bool value);
97 FileAttribs FILE_FileExists(const char * fileName);
98 bool FILE_FileGetSize(const char * fileName, FileSize * size);
99 bool FILE_FileGetStats(const char * fileName, FileStats stats);
100 void FILE_FileFixCase(char * file);
101 void FILE_FileOpen(const char * fileName, FileOpenMode mode, FILE ** input, FILE **output);
105 FileSystem httpFileSystem;
107 public class FileSize : uint
109 // defaultAlignment = Right;
111 void OnDisplay(Surface surface, int x, int y, int width, void * fieldData, int alignment, DataDisplayFlags displayFlags)
115 eUtils_PrintSize(string, *size, 2);
116 len = strlen(string);
117 surface.WriteTextDots(alignment, x, y, width, string, len);
120 int OnCompare(FileSize data2)
127 else if(this < data2)
133 const char * OnGetString(char * string, void * fieldData, bool * needClass)
135 PrintSize(string, this, 2);
139 bool OnGetDataFromString(const char * string)
142 double value = strtod(string, &end);
144 if(strstr(end, "GB") || strstr(end, "gb")) multiplier = (uint)1024 * 1024 * 1024;
145 else if(strstr(end, "MB") || strstr(end, "mb")) multiplier = (uint)1024 * 1024;
146 else if(strstr(end, "KB") || strstr(end, "kb")) multiplier = 1024;
148 this = (uint)(multiplier * value);
153 public class FileSize64 : uint64
155 int OnCompare(FileSize64 data2)
162 else if(this < data2)
168 const char * OnGetString(char * string, void * fieldData, bool * needClass)
170 PrintBigSize(string, this, 2);
174 bool OnGetDataFromString(const char * string)
177 double value = strtod(string, &end);
178 uint64 multiplier = 1;
179 if(strstr(end, "PB") || strstr(end, "pb")) multiplier = (uint64)1024 * 1024 * 1024 * 1024;
180 else if(strstr(end, "TB") || strstr(end, "tb")) multiplier = (uint64)1024 * 1024 * 1024 * 1024;
181 else if(strstr(end, "GB") || strstr(end, "gb")) multiplier = (uint64)1024 * 1024 * 1024;
182 else if(strstr(end, "MB") || strstr(end, "mb")) multiplier = (uint64)1024 * 1024;
183 else if(strstr(end, "KB") || strstr(end, "kb")) multiplier = 1024;
185 this = (uint64)(multiplier * value);
192 virtual File ::Open(const char * archive, const char * name, FileOpenMode mode);
195 virtual FileAttribs ::Exists(const char * archive, const char * fileName);
196 virtual bool ::GetSize(const char * archive, const char * fileName, FileSize * size);
197 virtual bool ::Stats(const char * archive, const char * fileName, FileStats stats);
198 virtual void ::FixCase(const char * archive, char * fileName);
201 virtual bool ::Find(FileDesc file, const char * archive, const char * name);
202 virtual bool ::FindNext(FileDesc file);
203 virtual void ::CloseDir(FileDesc file);
205 // Archive manipulation
206 virtual Archive ::OpenArchive(const char * fileName, ArchiveOpenFlags create);
207 virtual bool ::QuerySize(const char * fileName, FileSize * size);
210 public enum FileOpenMode { read = 1, write, append, readWrite, writeRead, appendRead };
211 public enum FileSeekMode { start, current, end };
213 #if !defined(ECERE_BOOTSTRAP)
214 static FileDialog fileDialog { text = $"Select File" };
219 unlocked = 0, // LOCK_UN _SH_DENYNO
220 shared = 1, // LOCK_SH _SH_DENYWR
221 exclusive = 2 // LOCK_EX _SH_DENYRW
224 public class File : IOChannel
226 FILE * input, * output;
228 uint ReadData(byte * bytes, uint numBytes)
230 return Read(bytes, 1, numBytes);
233 uint WriteData(const byte * bytes, uint numBytes)
235 return Write(bytes, 1, numBytes);
240 if(output && output != input)
254 bool OnGetDataFromString(const char * string)
263 File f = FileOpen(string, read);
270 uint read = f.Read(buffer, 1, sizeof(buffer));
271 Write(buffer, 1, read);
280 const char * OnGetString(char * tempString, void * fieldData, bool * needClass)
284 PrintSize(tempString, GetSize(), 2);
290 #ifndef ECERE_BOOTSTRAP
291 Window OnEdit(DataBox dataBox, DataBox obsolete, int x, int y, int w, int h, void * userData)
293 Window editData = class::OnEdit(dataBox, obsolete, x + 24, y, w - 48, h, userData);
296 dataBox, inactive = true, text = $"Import"."Imp", hotKey = f2,
297 position = { Max(x + 24, x + w - 24), y }, size = { 24, h };
299 bool DataBox::NotifyClicked(Button button, int x, int y, Modifiers mods)
301 fileDialog.master = rootWindow;
302 fileDialog.filePath = "";
303 fileDialog.type = open;
305 if(fileDialog.Modal() == ok)
307 const char * filePath = fileDialog.filePath;
309 if(output.OnGetDataFromString(filePath))
311 SetData(output, false);
320 dataBox, inactive = true, text = $"Export"."Exp", hotKey = f2,
321 position = { Max(x + 24, x + w - 48), y }, size = { 24, h };
323 bool DataBox::NotifyClicked(Button button, int x, int y, Modifiers mods)
325 fileDialog.master = rootWindow;
326 fileDialog.type = save;
327 fileDialog.filePath = "";
328 if(fileDialog.Modal() == ok)
330 const char * filePath = fileDialog.filePath;
331 File f = FileOpen(filePath, write);
334 File input = *(void **)data;
335 input.Seek(0, start);
339 uint read = input.Read(buffer, 1, sizeof(buffer));
340 f.Write(buffer, 1, read);
352 #endif //#ifndef ECERE_BOOTSTRAP
354 #if !defined(ECERE_VANILLA) && !defined(ECERE_NOARCHIVE) && !defined(ECERE_BOOTSTRAP)
355 void OnSerialize(IOChannel channel)
357 uint size = this ? GetSize() : MAXDWORD;
360 byte * uncompressed = new byte[size];
362 if(uncompressed || !size)
364 uint count = Read(uncompressed, 1, size);
367 uLongf cSize = size + size / 1000 + 12;
368 byte * compressed = new byte[cSize];
371 compress2(compressed, &cSize, uncompressed, size, 9);
373 size.OnSerialize(channel);
374 cSize.OnSerialize(channel);
375 channel.WriteData(compressed, (uint)cSize);
384 size.OnSerialize(channel);
389 size.OnSerialize(channel);
391 // Will add position...
395 for(c = 0; c<size; c += sizeof(data))
397 uint count = Read(data, 1, sizeof(data));
398 buffer.WriteData(data, count);
404 void OnUnserialize(IOChannel channel)
411 size.OnUnserialize(channel);
415 cSize.OnUnserialize(channel);
417 compressed = new byte[cSize];
420 if(channel.ReadData(compressed, cSize) == cSize)
422 byte * uncompressed = new byte[size];
423 if(uncompressed || !size)
426 uncompress(uncompressed, &size, compressed, cSize);
427 Write(uncompressed, 1, (uint)size);
441 size.OnUnserialize(channel);
445 for(c = 0; c<size; c += sizeof(data))
447 uint count = Min(size - c, sizeof(data));
448 channel.ReadData(data, count);
449 Write(data, 1, count);
462 virtual bool Seek(int pos, FileSeekMode mode)
464 uint fmode = SEEK_SET;
467 case start: fmode = SEEK_SET; break;
468 case end: fmode = SEEK_END; break;
469 case current: fmode = SEEK_CUR; break;
471 return fseek(input ? input : output, pos, fmode) != EOF;
474 virtual uint Tell(void)
476 return (uint)(input ? ftell(input) : ftell(output));
479 virtual int Read(void * buffer, uint size, uint count)
481 return input ? (int)fread(buffer, size, count, input) : 0;
484 virtual int Write(const void * buffer, uint size, uint count)
486 return output ? (int)fwrite(buffer, size, count, output) : 0;
490 virtual bool Getc(char * ch)
492 int ich = fgetc(input);
495 if(ch) *ch = (char)ich;
501 virtual bool Putc(char ch)
503 return (fputc((int)ch, output) == EOF) ? false : true;
506 virtual bool Puts(const char * string)
511 result = (fputs(string, output) == EOF) ? false : true;
512 // TODO: Check if any repercusions of commenting out fflush here
513 // This is what broke the debugger in 0.44d2 , it is required for outputting things to the DualPipe
514 // Added an explicit flush call in DualPipe::Puts
520 virtual bool Eof(void)
522 return input ? feof(input) != 0 : true;
525 virtual bool Truncate(FileSize size)
527 #ifdef ECERE_BOOTSTRAP
528 fprintf(stderr, "WARNING: File::Truncate unimplemented in ecereBootstrap.\n");
531 #if defined(__WIN32__)
532 return output ? (_chsize(fileno(output), size) == 0) : false;
534 return output ? (ftruncate(fileno(output), size) == 0) : false;
539 virtual uint GetSize(void)
541 return FILE_GetSize(input);
544 virtual void CloseInput(void)
555 virtual void CloseOutput(void)
566 virtual bool Lock(FileLock type, uint64 start, uint64 length, bool wait)
568 return FILE_Lock(input, output, type, start, length, wait);
571 virtual bool Unlock(uint64 start, uint64 length, bool wait)
573 return Lock(unlocked, start, length, wait);
577 int Printf(const char * format, ...)
582 char text[MAX_F_STRING];
584 va_start(args, format);
585 vsnprintf(text, sizeof(text), format, args);
586 text[sizeof(text)-1] = 0;
588 result = strlen(text);
594 public void PrintLn(typed_object object, ...)
598 va_start(args, object);
599 PrintStdArgsToBuffer(buffer, sizeof(buffer), object, args);
605 public void Print(typed_object object, ...)
609 va_start(args, object);
610 PrintStdArgsToBuffer(buffer, sizeof(buffer), object, args);
621 bool GetLine(char *s, int max)
637 if(/*!Peek() || */ !Getc(&ch))
649 return result || c > 1;
652 // Strings and numbers separated by spaces, commas, tabs, or CR/LF, handling quotes
653 bool GetString(char * string, int max)
668 if( (ch!='\n') && (ch!='\r') && (ch!=' ') && (ch!=',') && (ch!='\t'))
674 for(c=0; c<max-1; c++)
676 if(!quoted && ((ch=='\n')||(ch=='\r')||(ch==' ')||(ch==',')||(ch=='\t')))
704 GetString(string,sizeof(string));
708 unsigned int GetHexValue(void)
711 GetString(string, sizeof(string));
712 return (uint)strtoul(string, null, 16);
718 GetString(string, sizeof(string));
719 return (float)FloatFromString(string);
722 double GetDouble(void)
725 GetString(string, sizeof(string));
726 return FloatFromString(string);
729 property void * input { set { input = value; } get { return input; } }
730 property void * output { set { output = value; } get { return output; } }
731 property bool buffered
735 FILE_set_buffered(input, output, value);
738 property bool eof { get { return Eof(); } }
740 int GetLineEx(char *s, int max, bool *hasNewLineChar)
750 if(/*!Peek() || */ !Getc(&ch))
758 *hasNewLineChar = (ch == '\n');
764 bool CopyTo(const char * outputFileName)
767 File f = FileOpen(outputFileName, write);
776 uint count = Read(buffer, 1, sizeof(buffer));
777 if(count && !f.Write(buffer, 1, count))
790 virtual bool Open(const char * fileName, FileOpenMode mode)
795 FILE_FileOpen(fileName, mode, &input, &output);
798 if(!input && !output);
803 // TESTING ENABLING FILE BUFFERING BY DEFAULT... DOCUMENT ANY ISSUE
806 setvbuf(file.input, null, _IONBF, 0);
808 setvbuf(file.output, null, _IONBF, 0);
814 LogErrorCode((mode == Read || mode == ReadWrite) ?
815 ERR_FILE_NOT_FOUND : ERR_FILE_WRITE_FAILED, fileName);
830 #if defined(__WIN32__)
831 default extern intptr_t stdinHandle;
832 default extern intptr_t stdoutHandle;
835 public class ConsoleFile : File
838 output = eC_stdout();
840 #if defined(__WIN32__)
843 CloseHandle((HANDLE)stdinHandle);
848 CloseHandle((HANDLE)stdoutHandle);
859 public class FileAttribs : bool
862 bool isFile:1, isArchive:1, isHidden:1, isReadOnly:1, isSystem:1, isTemporary:1, isDirectory:1;
863 bool isDrive:1, isCDROM:1, isRemote:1, isRemovable:1, isServer:1, isShare:1;
864 // property bool { };
867 #ifdef ECERE_BOOTSTRAP
868 public class SecSince1970 : int64;
871 public struct FileStats
875 SecSince1970 accessed;
876 SecSince1970 modified;
877 SecSince1970 created;
880 #if defined(__WIN32__)
882 // --- FileName functions ---
884 default TimeStamp Win32FileTimeToTimeStamp(FILETIME * fileTime)
886 // TIME_ZONE_INFORMATION tz = { 0 };
890 FileTimeToSystemTime(fileTime, <);
893 GetTimeZoneInformation(&tz);
895 _TzSpecificLocalTimeToSystemTime(&tz, <, &st);
900 t.month = (Month)(st.wMonth - 1);
903 t.minute = st.wMinute;
904 t.second = st.wSecond;
908 default void TimeStampToWin32FileTime(TimeStamp t, FILETIME * fileTime)
910 // TIME_ZONE_INFORMATION tz = { 0 };
916 st.wYear = (short)tm.year;
917 st.wMonth = (short)(tm.month + 1);
918 st.wDay = (short)tm.day;
919 st.wHour = (short)tm.hour;
920 st.wMinute = (short)tm.minute;
921 st.wSecond = (short)tm.second;
922 st.wMilliseconds = 0;
926 GetTimeZoneInformation(&tz);
928 SystemTimeToTzSpecificLocalTime(&tz, &st, <);
932 SystemTimeToFileTime(<, fileTime);
935 default TimeStamp Win32FileTimeToTimeStamp(FILETIME * fileTime);
936 default void TimeStampToWin32FileTime(TimeStamp t, FILETIME * fileTime);
938 default bool WinReviveNetworkResource(uint16 * _wfileName);
942 public FileAttribs FileExists(const char * fileName)
944 #if !defined(ECERE_BOOTSTRAP)
945 char archiveName[MAX_LOCATION];
946 const char * archiveFile;
947 if(SplitArchivePath(fileName, archiveName, &archiveFile))
949 return EARFileSystem::Exists(archiveName, archiveFile);
951 else if(strstr(fileName, "http://") == fileName)
953 return FileAttribs { isFile = true };
957 return FILE_FileExists(fileName);
960 static int openCount;
962 public File FileOpen(const char * fileName, FileOpenMode mode)
967 #if !defined(ECERE_BOOTSTRAP)
968 char archiveName[MAX_LOCATION];
969 const char * archiveFile;
970 if(SplitArchivePath(fileName, archiveName, &archiveFile))
972 result = EARFileSystem::Open(archiveName, archiveFile, mode);
974 #if !defined(ECERE_VANILLA) && !defined(ECERE_NONET)
975 else if(strstr(fileName, "http://") == fileName || strstr(fileName, "https://"))
977 result = FileOpenURL(fileName);
982 if(strstr(fileName, "File://") == fileName)
984 result = (File)(uintptr)strtoull(fileName+7, null, 16);
987 if(result._class && eClass_IsDerived(result._class, class(File)))
989 if(!result._refCount) incref result;
991 result.Seek(0, start);
1002 FILE_FileOpen(fileName, mode, &file.input, &file.output);
1005 if(!file.input && !file.output);
1010 // TESTING ENABLING FILE BUFFERING BY DEFAULT... DOCUMENT ANY ISSUE
1013 setvbuf(file.input, null, _IONBF, 0);
1015 setvbuf(file.output, null, _IONBF, 0);
1022 LogErrorCode((mode == Read || mode == ReadWrite) ?
1023 ERR_FILE_NOT_FOUND : ERR_FILE_WRITE_FAILED, fileName);
1032 public void FileFixCase(char * file)
1034 FILE_FileFixCase(file);
1037 #if !defined(ECERE_BOOTSTRAP)
1038 public bool FileTruncate(const char * fileName, FileSize size)
1040 #if defined(__WIN32__)
1041 uint16 * _wfileName = UTF8toUTF16(fileName, null);
1042 int f = _wopen(_wfileName, _O_RDWR|_O_CREAT, _S_IREAD|_S_IWRITE);
1043 bool result = false;
1046 if(!_chsize(f, size))
1053 return truncate(fileName, size) == 0;
1058 public bool FileGetSize(const char * fileName, FileSize * size)
1060 bool result = false;
1066 #if !defined(ECERE_BOOTSTRAP)
1067 char archiveName[MAX_LOCATION];
1068 const char * archiveFile;
1069 if(SplitArchivePath(fileName, archiveName, &archiveFile))
1070 return EARFileSystem::GetSize(archiveName, archiveFile, size);
1073 result = FILE_FileGetSize(fileName, size);
1079 public bool FileGetStats(const char * fileName, FileStats stats)
1081 bool result = false;
1082 if(stats && fileName)
1084 #if !defined(ECERE_BOOTSTRAP)
1085 char archiveName[MAX_LOCATION];
1086 const char * archiveFile;
1087 if(SplitArchivePath(fileName, archiveName, &archiveFile))
1088 result = EARFileSystem::Stats(archiveName, archiveFile, stats);
1091 return FILE_FileGetStats(fileName, stats);
1096 #ifndef ECERE_BOOTSTRAP
1098 public bool FileSetAttribs(const char * fileName, FileAttribs attribs)
1101 uint winAttribs = 0;
1102 uint16 * _wfileName = UTF8toUTF16(fileName, null);
1104 if(attribs.isHidden) winAttribs |= FILE_ATTRIBUTE_HIDDEN;
1105 if(attribs.isReadOnly) winAttribs |= FILE_ATTRIBUTE_READONLY;
1107 SetFileAttributes(_wfileName, winAttribs);
1113 public bool FileSetTime(const char * fileName, TimeStamp created, TimeStamp accessed, TimeStamp modified)
1115 bool result = false;
1116 TimeStamp currentTime = time(null);
1117 if(!created) created = currentTime;
1118 if(!accessed) accessed = currentTime;
1119 if(!modified) modified = currentTime;
1123 uint16 * _wfileName = UTF8toUTF16(fileName, null);
1124 HANDLE hFile = CreateFile(_wfileName, GENERIC_WRITE|GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, null,
1125 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, null);
1127 if(hFile != INVALID_HANDLE_VALUE)
1131 TimeStampToWin32FileTime(created, &c);
1132 TimeStampToWin32FileTime(accessed, &a);
1133 TimeStampToWin32FileTime(modified, &m);
1139 cc = Win32FileTimeToTimeStamp(&c);
1140 aa = Win32FileTimeToTimeStamp(&a);
1141 mm = Win32FileTimeToTimeStamp(&m);
1145 if(SetFileTime(hFile, &c, &a, &m))
1151 struct utimbuf t = { (int)accessed, (int)modified };
1152 if(!utime(fileName, &t))
1159 /****************************************************************************
1161 ****************************************************************************/
1162 // Directory Description for file listing
1163 private class Dir : struct
1165 #if defined(__WIN32__)
1169 NETRESOURCE * resources;
1173 NETRESOURCE * workGroups;
1178 char name[MAX_LOCATION];
1181 static FileDesc FileFind(const char * path, const char * extensions)
1183 FileDesc result = null;
1186 if((file = FileDesc {}))
1188 char archiveName[MAX_LOCATION];
1189 const char * archiveFile;
1190 if(SplitArchivePath(path, archiveName, &archiveFile))
1192 if(EARFileSystem::Find(file, archiveName, archiveFile))
1194 file.system = class(EARFileSystem);
1202 if((d = file.dir = Dir {}))
1204 #if defined(__WIN32__)
1205 if(!strcmp(path, "/"))
1208 uint drives = 0xFFFFFFFF;
1209 d.fHandle = (HANDLE)(uintptr)drives; //GetLogicalDrives();
1210 for(c = 0; c<26; c++)
1211 if(((uint)(uintptr)d.fHandle) & (1<<c))
1213 char volume[MAX_FILENAME] = "";
1214 uint16 _wvolume[MAX_FILENAME];
1216 uint16 _wfilePath[4];
1218 strcpy(d.name, path);
1219 file.stats.attribs = FileAttribs { isDirectory = true, isDrive = true };
1220 _wfilePath[0] = file.path[0] = (char)('A' + c);
1221 _wfilePath[1] = file.path[1] = ':';
1222 _wfilePath[2] = file.path[2] = '\\';
1223 _wfilePath[3] = file.path[3] = '\0';
1224 file.stats.size = 0;
1225 file.stats.accessed = file.stats.created = file.stats.modified = 0;
1226 driveType = GetDriveType(_wfilePath);
1229 case DRIVE_REMOVABLE: file.stats.attribs.isRemovable = true; break;
1230 case DRIVE_REMOTE: file.stats.attribs.isRemote = true; break;
1231 case DRIVE_CDROM: file.stats.attribs.isCDROM = true; break;
1234 if(driveType == DRIVE_NO_ROOT_DIR) continue;
1236 if(driveType != DRIVE_REMOVABLE && driveType != DRIVE_REMOTE &&
1237 GetVolumeInformation(_wfilePath, _wvolume, MAX_FILENAME - 1, null, null, null, null, 0))
1239 file.path[2] = '\0';
1240 UTF16toUTF8Buffer(_wvolume, volume, MAX_FILENAME);
1241 sprintf(file.name, "%s [%s]", file.path, volume);
1245 file.path[2] = '\0';
1246 strcpy(file.name, file.path);
1251 d.fHandle = (HANDLE)(uintptr) drives;
1254 else if(path[0] != '\\' || path[1] != '\\' || strstr(path+2, "\\"))
1256 WIN32_FIND_DATA winFile;
1257 uint16 dir[MAX_PATH];
1259 UTF8toUTF16Buffer(path, dir, MAX_LOCATION);
1260 if(path[0]) wcscat(dir, L"\\");
1261 wcscat(dir, L"*.*");
1263 d.fHandle = FindFirstFile(dir, &winFile);
1264 if(d.fHandle == INVALID_HANDLE_VALUE && WinReviveNetworkResource(dir))
1265 d.fHandle = FindFirstFile(dir, &winFile);
1266 if(d.fHandle != INVALID_HANDLE_VALUE)
1268 UTF16toUTF8Buffer(winFile.cFileName, file.name, MAX_FILENAME);
1269 strcpy(file.path, path);
1270 PathCat(file.path, file.name);
1272 strcat(file.path, DIR_SEPS);
1273 strcat(file.path, file.name);*/
1274 // file.sizeHigh = winFile.nFileSizeHigh;
1275 file.stats.size = winFile.nFileSizeLow;
1277 file.stats.attribs = FileAttribs { };
1278 file.stats.attribs.isArchive = (winFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) ? true : false;
1279 file.stats.attribs.isHidden = (winFile.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) ? true : false;
1280 file.stats.attribs.isReadOnly = (winFile.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? true : false;
1281 file.stats.attribs.isSystem = (winFile.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) ? true : false;
1282 file.stats.attribs.isTemporary = (winFile.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) ? true : false;
1283 file.stats.attribs.isDirectory = (winFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? true : false;
1284 file.stats.attribs.isFile = !(winFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
1285 strcpy(d.name, path);
1287 file.stats.accessed = Win32FileTimeToTimeStamp(&winFile.ftLastAccessTime);
1288 file.stats.modified = Win32FileTimeToTimeStamp(&winFile.ftLastWriteTime);
1289 file.stats.created = Win32FileTimeToTimeStamp(&winFile.ftCreationTime);
1296 DWORD count = 0xFFFFFFFF;
1297 DWORD size = 512 * sizeof(NETRESOURCE);
1298 NETRESOURCE * buffer = (NETRESOURCE *)new0 byte[size];
1299 NETRESOURCE nr = {0};
1302 nr.dwScope = RESOURCE_GLOBALNET;
1303 nr.dwType = RESOURCETYPE_DISK;
1304 nr.lpProvider = (uint16 *)L"Microsoft Windows Network";
1306 strcpy(d.name, path);
1309 nr.lpRemoteName = UTF8toUTF16(path, null);
1312 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &nr, &handle);
1315 WinReviveNetworkResource(nr.lpRemoteName);
1316 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &nr, &handle);
1323 int returnCode = WNetEnumResource(handle, &count, buffer, &size);
1324 if(returnCode != ERROR_MORE_DATA)
1327 buffer = (NETRESOURCE *)renew0 buffer byte[size];
1329 WNetCloseEnum(handle);
1332 delete nr.lpRemoteName;
1335 file.stats.attribs = FileAttribs { isDirectory = true, isShare = true };
1336 file.stats.size = 0;
1337 file.stats.accessed = file.stats.created = file.stats.modified = 0;
1339 UTF16toUTF8Buffer(buffer->lpRemoteName, file.path, MAX_LOCATION);
1340 GetLastDirectory(file.path, file.name);
1343 d.resources = buffer;
1344 d.numResources = count;
1353 nr.lpProvider = (uint16 *)L"Microsoft Windows Network";
1356 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &nr, &handle);
1359 int returnCode = WNetEnumResource(handle, &count, buffer, &size);
1360 if(returnCode != ERROR_MORE_DATA)
1363 buffer = (NETRESOURCE *)renew0 buffer byte[size];
1365 WNetCloseEnum(handle);
1367 for(c = 0; c<count; c++)
1369 NETRESOURCE * resources;
1370 DWORD countInGroup = 0xFFFFFFFF;
1372 size = 512 * sizeof(NETRESOURCE);
1373 resources = (NETRESOURCE *)new0 byte[size];
1376 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &buffer[c], &handle);
1379 int returnCode = WNetEnumResource(handle, &countInGroup, resources, &size);
1380 if(returnCode != ERROR_MORE_DATA)
1382 countInGroup = 0xFFFFFFFF;
1383 resources = (NETRESOURCE *)renew0 resources byte[size];
1385 WNetCloseEnum(handle);
1389 file.stats.attribs = FileAttribs { isDirectory = true, isServer = true };
1390 file.stats.size = 0;
1391 file.stats.accessed = file.stats.created = file.stats.modified = 0;
1393 UTF16toUTF8Buffer(resources->lpRemoteName, file.path, MAX_LOCATION);
1395 file.path[2] = (char)toupper(file.path[2]);
1396 GetLastDirectory(file.path, file.name);
1400 d.resources = resources;
1401 d.numResources = countInGroup;
1404 d.workGroups = buffer;
1405 d.numWorkGroups = count;
1412 if(c >= count && buffer) delete buffer;
1419 d.d = opendir((path && path[0]) ? path : ".");
1420 if(d.d && (de = readdir(d.d)))
1424 strcpy(file.path, path);
1426 strcat(file.path, DIR_SEPS);
1428 strcpy(file.name,de->d_name);
1429 strcat(file.path, file.name);
1430 if(!stat(file.path, &s))
1432 file.stats.attribs = (s.st_mode&S_IFDIR) ? FileAttribs { isDirectory = true } : FileAttribs { isFile = true };
1433 file.stats.size = (FileSize)s.st_size;
1434 file.stats.accessed = s.st_atime;
1435 file.stats.modified = s.st_mtime;
1436 file.stats.created = s.st_ctime;
1438 strcpy(d.name, path);
1453 while(result && !result.Validate(extensions))
1454 result = result.FindNext(extensions);
1459 private class FileDesc : struct
1462 char name[MAX_FILENAME];
1463 char path[MAX_LOCATION];
1465 subclass(FileSystem) system;
1468 bool Validate(const char * extensions)
1470 if(strcmp(name, "..") && strcmp(name, ".") && strcmp(name, ""))
1472 if(extensions && !stats.attribs.isDirectory)
1474 char extension[MAX_EXTENSION], compared[MAX_EXTENSION];
1477 GetExtension(name, extension);
1478 for(c = 0; extensions[c];)
1482 for(;(ch = extensions[c]) && !IS_ALUNDER(ch); c++);
1483 for(;(ch = extensions[c]) && IS_ALUNDER(ch); c++)
1484 compared[len++] = ch;
1485 compared[len] = '\0';
1487 if(!strcmpi(extension, compared))
1497 FileDesc FindNext(const char * extensions)
1499 FileDesc result = null;
1505 while(!Validate(extensions))
1511 if(system.FindNext(this))
1518 #if defined(__WIN32__)
1519 if(!strcmp(d.name, "/"))
1522 uint drives = (uint)(uintptr)d.fHandle;
1523 for(c = 0; c<26; c++)
1527 char volume[MAX_FILENAME] = "";
1530 uint16 _wvolume[MAX_FILENAME];
1532 stats.attribs = FileAttribs { isDirectory = true, isDrive = true };
1534 stats.accessed = stats.created = stats.modified = 0;
1535 _wpath[0] = path[0] = (char)('A' + c);
1536 _wpath[1] = path[1] = ':';
1537 _wpath[2] = path[2] = '\\';
1538 _wpath[3] = path[3] = 0;
1539 driveType = GetDriveType(_wpath);
1544 case DRIVE_REMOVABLE: stats.attribs.isRemovable = true; break;
1545 case DRIVE_REMOTE: stats.attribs.isRemote = true; break;
1546 case DRIVE_CDROM: stats.attribs.isCDROM = true; break;
1548 if(driveType == DRIVE_NO_ROOT_DIR)
1550 uint16 remoteName[1024];
1555 status = WNetGetConnection(_wpath, remoteName, &size);
1556 if(status != ERROR_CONNECTION_UNAVAIL)
1563 if(driveType != DRIVE_REMOVABLE && driveType != DRIVE_REMOTE &&
1564 GetVolumeInformation(_wpath, _wvolume, MAX_FILENAME - 1, null, null, null, null, 0))
1566 UTF16toUTF8Buffer(_wvolume, volume, MAX_FILENAME);
1568 sprintf(name, "%s [%s]", path, volume);
1579 d.fHandle = (HANDLE)(uintptr) drives;
1582 else if(d.name[0] != '\\' || d.name[1] != '\\' || strstr(d.name+2, "\\"))
1584 WIN32_FIND_DATA winFile;
1585 if(FindNextFile(d.fHandle, &winFile))
1587 UTF16toUTF8Buffer(winFile.cFileName, name, MAX_FILENAME);
1588 stats.attribs = FileAttribs { };
1589 stats.attribs.isArchive = (winFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) ? true : false;
1590 stats.attribs.isHidden = (winFile.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) ? true : false;
1591 stats.attribs.isReadOnly = (winFile.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? true : false;
1592 stats.attribs.isSystem = (winFile.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) ? true : false;
1593 stats.attribs.isTemporary = (winFile.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) ? true : false;
1594 stats.attribs.isDirectory = (winFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? true : false;
1595 stats.attribs.isFile = !(winFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
1596 stats.size = winFile.nFileSizeLow;
1598 stats.accessed = Win32FileTimeToTimeStamp(&winFile.ftLastAccessTime);
1599 stats.modified = Win32FileTimeToTimeStamp(&winFile.ftLastWriteTime);
1600 stats.created = Win32FileTimeToTimeStamp(&winFile.ftCreationTime);
1602 strcpy(path, d.name);
1603 PathCat(path, name);
1605 strcat(path, DIR_SEPS);
1606 strcat(path, name);*/
1616 if(d.resource < d.numResources)
1618 stats.attribs = FileAttribs { isDirectory = true, isShare = true };
1620 stats.accessed = stats.created = stats.modified = 0;
1622 UTF16toUTF8Buffer(d.resources[d.resource].lpRemoteName, path, MAX_LOCATION);
1623 GetLastDirectory(path, name);
1638 for(c = d.workGroup; c<d.numWorkGroups; c++)
1640 if(c != d.workGroup)
1642 DWORD countInGroup = 0xFFFFFFFF;
1644 NETRESOURCE * resources;
1645 DWORD size = 512 * sizeof(NETRESOURCE);
1647 resources = (NETRESOURCE *)new0 byte[size];
1649 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &d.workGroups[c], &handle);
1652 int returnCode = WNetEnumResource(handle, &countInGroup, resources, &size);
1653 if(returnCode != ERROR_MORE_DATA)
1655 countInGroup = 0xFFFFFFFF;
1656 resources = (NETRESOURCE *)renew0 resources byte[size];
1659 WNetCloseEnum(handle);
1660 d.numResources = countInGroup;
1661 d.resources = resources;
1665 if(d.resource < d.numResources)
1667 stats.attribs = FileAttribs { isDirectory = true, isServer = true };
1669 stats.accessed = stats.created = stats.modified = 0;
1671 UTF16toUTF8Buffer(d.resources[d.resource].lpRemoteName, path, MAX_LOCATION);
1673 path[2] = (char)toupper(path[2]);
1674 GetLastDirectory(path, name);
1688 if(d.workGroup == d.numWorkGroups && d.resource == d.numResources)
1690 delete d.workGroups;
1702 strcpy(name,de->d_name);
1703 strcpy(path, d.name);
1704 if(d.name[0] && d.name[1])
1705 strcat(path, DIR_SEPS);
1709 stats.attribs = FileAttribs { };
1710 stats.attribs = (s.st_mode&S_IFDIR) ? FileAttribs { isDirectory = true } : FileAttribs { isFile = true };
1711 stats.size = (FileSize)s.st_size;
1712 stats.accessed = s.st_atime;
1713 stats.modified = s.st_mtime;
1714 stats.created = s.st_ctime;
1731 system.CloseDir(this);
1737 #if defined(__WIN32__)
1738 if(d.fHandle && strcmp(d.name, "/"))
1739 FindClose(d.fHandle);
1750 public struct FileListing
1753 const char * directory;
1754 const char * extensions;
1759 desc = desc.FindNext(extensions);
1761 desc = FileFind(directory, extensions);
1774 property const char * name { get { return desc ? desc.name : null; } };
1775 property const char * path { get { return desc ? desc.path : null; } };
1776 property FileStats stats { get { value = desc ? desc.stats : FileStats { }; } };
1783 public File CreateTemporaryFile(char * tempFileName, const char * template)
1785 #ifndef ECERE_BOOTSTRAP // quick fix for now
1787 #if defined(__unix__) || defined(__APPLE__)
1788 char buffer[MAX_FILENAME];
1790 strcpy(buffer, "/tmp/");
1791 strcat(buffer, template);
1792 //strcpy(buffer, template);
1793 strcat(buffer, "XXXXXX");
1795 fd = mkstemp(buffer);
1796 strcpy(tempFileName, buffer);
1798 f.output = f.input = fdopen(fd, "r+");
1800 char tempPath[MAX_LOCATION];
1801 GetTempPathA(MAX_LOCATION, tempPath); // TODO: Patch this whole thing to support Unicode temp path
1802 GetTempFileNameA(tempPath, template, 0, tempFileName);
1803 f = FileOpen(tempFileName, readWrite);
1813 public void CreateTemporaryDir(char * tempFileName, const char * template)
1815 #ifndef ECERE_BOOTSTRAP // quick fix for now
1816 #if defined(__unix__) || defined(__APPLE__)
1817 char buffer[MAX_FILENAME];
1818 strcpy(buffer, "/tmp/");
1819 strcat(buffer, template);
1820 //strcpy(buffer, template);
1821 strcat(buffer, "XXXXXX");
1824 strcpy(tempFileName, buffer);
1826 char tempPath[MAX_LOCATION];
1827 GetTempPathA(MAX_LOCATION, tempPath); // TODO: Patch this whole thing to support Unicode temp path
1828 GetTempFileNameA(tempPath, template, 0, tempFileName);
1829 DeleteFile(tempFileName);
1830 MakeDir(tempFileName);
1835 public void MakeSlashPath(char * p)
1838 if(__runtimePlatform == win32)
1839 ChangeCh(p, '\\', '/');
1842 public void MakeSystemPath(char * p)
1847 public char * CopySystemPath(const char * p)
1849 char * d = CopyString(p);
1855 public char * CopyUnixPath(const char * p)
1857 char * d = CopyString(p);
1863 public char * GetSystemPathBuffer(char * d, const char * p)
1866 strcpy(d, p ? p : "");
1871 public char * GetSlashPathBuffer(char * d, const char * p)
1874 strcpy(d, p ? p : "");