15 #define IS_ALUNDER(ch) ((ch) == '_' || isalnum((ch)))
17 #if defined(ECERE_BOOTSTRAP)
24 #ifndef ECERE_BOOTSTRAP
25 #if defined(__GNUC__) || defined(__WATCOMC__) || defined(__WIN32__)
27 #include <sys/types.h>
32 #if defined(__unix__) || defined(__APPLE__)
36 #if defined(__WIN32__) || defined(__WATCOMC__)
42 #if defined(__WIN32__)
43 #define WIN32_LEAN_AND_MEAN
44 #define String String_
49 BOOL WINAPI GetVolumePathName(LPCTSTR lpszFileName,LPTSTR lpszVolumePathName,DWORD cchBufferLength);
51 // Missing function...
53 #ifndef WNetGetResourceInformation
54 DWORD APIENTRY WNetGetResourceInformationA(LPNETRESOURCE lpNetResource, LPVOID lpBuffer, LPDWORD lpcbBuffer, LPTSTR* lplpSystem);
56 #define WNetGetResourceInformation WNetGetResourceInformationW
58 #define WNetGetResourceInformation WNetGetResourceInformationA
70 #endif //#ifndef ECERE_BOOTSTRAP
80 #if !defined(ECERE_VANILLA) && !defined(ECERE_NONET) && !defined(ECERE_BOOTSTRAP)
86 // IMPLEMENTATION OF THESE IS IN _File.c
90 FILE *eC_stdout(void);
92 uint FILE_GetSize(FILE * input);
93 bool FILE_Lock(FILE * input, FILE * output, FileLock type, uint64 start, uint64 length, bool wait);
94 void FILE_set_buffered(FILE * input, FILE * output, bool value);
95 FileAttribs FILE_FileExists(char * fileName);
96 bool FILE_FileGetSize(char * fileName, FileSize * size);
97 bool FILE_FileGetStats(char * fileName, FileStats stats);
98 void FILE_FileFixCase(char * file);
99 void FILE_FileOpen(char * fileName, FileOpenMode mode, FILE ** input, FILE **output);
103 FileSystem httpFileSystem;
105 public class FileSize : uint
107 // defaultAlignment = Right;
109 void OnDisplay(Surface surface, int x, int y, int width, void * fieldData, int alignment, DataDisplayFlags displayFlags)
113 eUtils_PrintSize(string, *size, 2);
114 len = strlen(string);
115 surface.WriteTextDots(alignment, x, y, width, string, len);
118 int OnCompare(FileSize data2)
125 else if(this < data2)
131 char * OnGetString(char * string, void * fieldData, bool * needClass)
133 PrintSize(string, this, 2);
137 bool OnGetDataFromString(char * string)
140 double value = strtod(string, &end);
142 if(strstr(end, "GB") || strstr(end, "gb")) multiplier = (uint)1024 * 1024 * 1024;
143 else if(strstr(end, "MB") || strstr(end, "mb")) multiplier = (uint)1024 * 1024;
144 else if(strstr(end, "KB") || strstr(end, "kb")) multiplier = 1024;
146 this = (uint)(multiplier * value);
151 public class FileSize64 : uint64
153 int OnCompare(FileSize64 data2)
160 else if(this < data2)
166 char * OnGetString(char * string, void * fieldData, bool * needClass)
168 PrintBigSize(string, this, 2);
172 bool OnGetDataFromString(char * string)
175 double value = strtod(string, &end);
176 uint64 multiplier = 1;
177 if(strstr(end, "PB") || strstr(end, "pb")) multiplier = (uint64)1024 * 1024 * 1024 * 1024;
178 else if(strstr(end, "TB") || strstr(end, "tb")) multiplier = (uint64)1024 * 1024 * 1024 * 1024;
179 else if(strstr(end, "GB") || strstr(end, "gb")) multiplier = (uint64)1024 * 1024 * 1024;
180 else if(strstr(end, "MB") || strstr(end, "mb")) multiplier = (uint64)1024 * 1024;
181 else if(strstr(end, "KB") || strstr(end, "kb")) multiplier = 1024;
183 this = (uint64)(multiplier * value);
190 virtual File ::Open(char * archive, char * name, FileOpenMode mode);
193 virtual FileAttribs ::Exists(char * archive, char * fileName);
194 virtual bool ::GetSize(char * archive, char * fileName, FileSize * size);
195 virtual bool ::Stats(char * archive, char * fileName, FileStats stats);
196 virtual void ::FixCase(char * archive, char * fileName);
199 virtual bool ::Find(FileDesc file, char * archive, char * name);
200 virtual bool ::FindNext(FileDesc file);
201 virtual void ::CloseDir(FileDesc file);
203 // Archive manipulation
204 virtual Archive ::OpenArchive(char * fileName, ArchiveOpenFlags create);
205 virtual bool ::QuerySize(char * fileName, FileSize * size);
208 public enum FileOpenMode { read = 1, write, append, readWrite, writeRead, appendRead };
209 public enum FileSeekMode { start, current, end };
211 #if !defined(ECERE_BOOTSTRAP)
212 static FileDialog fileDialog { text = $"Select File" };
217 unlocked = 0, // LOCK_UN _SH_DENYNO
218 shared = 1, // LOCK_SH _SH_DENYWR
219 exclusive = 2 // LOCK_EX _SH_DENYRW
222 public class File : IOChannel
224 FILE * input, * output;
226 uint ReadData(byte * bytes, uint numBytes)
228 return Read(bytes, 1, numBytes);
231 uint WriteData(byte * bytes, uint numBytes)
233 return Write(bytes, 1, numBytes);
238 if(output && output != input)
252 bool OnGetDataFromString(char * string)
261 File f = FileOpen(string, read);
268 uint read = f.Read(buffer, 1, sizeof(buffer));
269 Write(buffer, 1, read);
278 char * OnGetString(char * tempString, void * fieldData, bool * needClass)
282 PrintSize(tempString, GetSize(), 2);
288 #ifndef ECERE_BOOTSTRAP
289 Window OnEdit(DataBox dataBox, DataBox obsolete, int x, int y, int w, int h, void * userData)
291 Window editData = class::OnEdit(dataBox, obsolete, x + 24, y, w - 48, h, userData);
294 dataBox, inactive = true, text = $"Import"."Imp", hotKey = f2,
295 position = { Max(x + 24, x + w - 24), y }, size = { 24, h };
297 bool DataBox::NotifyClicked(Button button, int x, int y, Modifiers mods)
299 fileDialog.master = rootWindow;
300 fileDialog.filePath = "";
301 fileDialog.type = open;
303 if(fileDialog.Modal() == ok)
305 char * filePath = fileDialog.filePath;
307 if(output.OnGetDataFromString(filePath))
309 SetData(output, false);
318 dataBox, inactive = true, text = $"Export"."Exp", hotKey = f2,
319 position = { Max(x + 24, x + w - 48), y }, size = { 24, h };
321 bool DataBox::NotifyClicked(Button button, int x, int y, Modifiers mods)
323 fileDialog.master = rootWindow;
324 fileDialog.type = save;
325 fileDialog.filePath = "";
326 if(fileDialog.Modal() == ok)
328 char * filePath = fileDialog.filePath;
329 File f = FileOpen(filePath, write);
332 File input = *(void **)data;
333 input.Seek(0, start);
337 uint read = input.Read(buffer, 1, sizeof(buffer));
338 f.Write(buffer, 1, read);
350 #endif //#ifndef ECERE_BOOTSTRAP
352 #if !defined(ECERE_VANILLA) && !defined(ECERE_NOARCHIVE) && !defined(ECERE_BOOTSTRAP)
353 void OnSerialize(IOChannel channel)
355 uint size = this ? GetSize() : MAXDWORD;
358 byte * uncompressed = new byte[size];
360 if(uncompressed || !size)
362 uint count = Read(uncompressed, 1, size);
365 uint cSize = size + size / 1000 + 12;
366 byte * compressed = new byte[cSize];
369 compress2(compressed, &cSize, uncompressed, size, 9);
371 size.OnSerialize(channel);
372 cSize.OnSerialize(channel);
373 channel.WriteData(compressed, cSize);
382 size.OnSerialize(channel);
387 size.OnSerialize(channel);
389 // Will add position...
393 for(c = 0; c<size; c += sizeof(data))
395 uint count = Read(data, 1, sizeof(data));
396 buffer.WriteData(data, count);
402 void OnUnserialize(IOChannel channel)
408 size.OnUnserialize(channel);
412 cSize.OnUnserialize(channel);
414 compressed = new byte[cSize];
417 if(channel.ReadData(compressed, cSize) == cSize)
419 byte * uncompressed = new byte[size];
420 if(uncompressed || !size)
423 uncompress(uncompressed, &size, compressed, cSize);
424 Write(uncompressed, 1, size);
438 size.OnUnserialize(channel);
442 for(c = 0; c<size; c += sizeof(data))
444 uint count = Min(size - c, sizeof(data));
445 channel.ReadData(data, count);
446 Write(data, 1, count);
459 virtual bool Seek(int pos, FileSeekMode mode)
461 uint fmode = SEEK_SET;
464 case start: fmode = SEEK_SET; break;
465 case end: fmode = SEEK_END; break;
466 case current: fmode = SEEK_CUR; break;
468 return fseek(input ? input : output, pos, fmode) != EOF;
471 virtual uint Tell(void)
473 return (uint)(input ? ftell(input) : ftell(output));
476 virtual int Read(void * buffer, uint size, uint count)
478 return input ? (int)fread(buffer, size, count, input) : 0;
481 virtual int Write(void * buffer, uint size, uint count)
483 return output ? (int)fwrite(buffer, size, count, output) : 0;
487 virtual bool Getc(char * ch)
489 int ich = fgetc(input);
492 if(ch) *ch = (char)ich;
498 virtual bool Putc(char ch)
500 return (fputc((int)ch, output) == EOF) ? false : true;
503 virtual bool Puts(const char * string)
508 result = (fputs(string, output) == EOF) ? false : true;
509 // TODO: Check if any repercusions of commenting out fflush here
510 // This is what broke the debugger in 0.44d2 , it is required for outputting things to the DualPipe
511 // Added an explicit flush call in DualPipe::Puts
517 virtual bool Eof(void)
519 return input ? feof(input) : true;
522 virtual bool Truncate(FileSize size)
524 #ifdef ECERE_BOOTSTRAP
525 fprintf(stderr, "WARNING: File::Truncate unimplemented in ecereBootstrap.\n");
528 #if defined(__WIN32__)
529 return output ? (_chsize(fileno(output), size) == 0) : false;
531 return output ? (ftruncate(fileno(output), size) == 0) : false;
536 virtual uint GetSize(void)
538 return FILE_GetSize(input);
541 virtual void CloseInput(void)
552 virtual void CloseOutput(void)
563 virtual bool Lock(FileLock type, uint64 start, uint64 length, bool wait)
565 return FILE_Lock(input, output, type, start, length, wait);
568 virtual bool Unlock(uint64 start, uint64 length, bool wait)
570 return Lock(unlocked, start, length, wait);
574 int Printf(char * format, ...)
579 char text[MAX_F_STRING];
581 va_start(args, format);
582 vsnprintf(text, sizeof(text), format, args);
583 text[sizeof(text)-1] = 0;
585 result = strlen(text);
591 public void PrintLn(typed_object object, ...)
595 va_start(args, object);
596 PrintStdArgsToBuffer(buffer, sizeof(buffer), object, args);
602 public void Print(typed_object object, ...)
606 va_start(args, object);
607 PrintStdArgsToBuffer(buffer, sizeof(buffer), object, args);
618 bool GetLine(char *s, int max)
634 if(/*!Peek() || */ !Getc(&ch))
646 return result || c > 1;
649 // Strings and numbers separated by spaces, commas, tabs, or CR/LF, handling quotes
650 bool GetString(char * string, int max)
662 if( (ch!='\n') && (ch!='\r') && (ch!=' ') && (ch!=',') && (ch!='\t'))
668 for(c=0; c<max-1; c++)
670 if(!quoted && ((ch=='\n')||(ch=='\r')||(ch==' ')||(ch==',')||(ch=='\t')))
698 GetString(string,sizeof(string));
702 unsigned int GetHexValue(void)
705 GetString(string, sizeof(string));
706 return (uint)strtoul(string, null, 16);
712 GetString(string, sizeof(string));
713 return (float)FloatFromString(string);
716 double GetDouble(void)
719 GetString(string, sizeof(string));
720 return FloatFromString(string);
723 property void * input { set { input = value; } get { return input; } }
724 property void * output { set { output = value; } get { return output; } }
725 property bool buffered
729 FILE_set_buffered(input, output, value);
732 property bool eof { get { return Eof(); } }
734 int GetLineEx(char *s, int max, bool *hasNewLineChar)
744 if(/*!Peek() || */ !Getc(&ch))
752 *hasNewLineChar = (ch == '\n');
758 bool CopyTo(char * outputFileName)
761 File f = FileOpen(outputFileName, write);
770 uint count = Read(buffer, 1, sizeof(buffer));
771 if(count && !f.Write(buffer, 1, count))
784 virtual bool Open(char * fileName, FileOpenMode mode)
789 FILE_FileOpen(fileName, mode, &input, &output);
792 if(!input && !output);
797 // TESTING ENABLING FILE BUFFERING BY DEFAULT... DOCUMENT ANY ISSUE
800 setvbuf(file.input, null, _IONBF, 0);
802 setvbuf(file.output, null, _IONBF, 0);
808 LogErrorCode((mode == Read || mode == ReadWrite) ?
809 ERR_FILE_NOT_FOUND : ERR_FILE_WRITE_FAILED, fileName);
824 #if defined(__WIN32__)
825 default extern intptr_t stdinHandle;
826 default extern intptr_t stdoutHandle;
829 public class ConsoleFile : File
832 output = eC_stdout();
834 #if defined(__WIN32__)
837 CloseHandle((HANDLE)stdinHandle);
842 CloseHandle((HANDLE)stdoutHandle);
853 public class FileAttribs : bool
856 bool isFile:1, isArchive:1, isHidden:1, isReadOnly:1, isSystem:1, isTemporary:1, isDirectory:1;
857 bool isDrive:1, isCDROM:1, isRemote:1, isRemovable:1, isServer:1, isShare:1;
858 // property bool { };
861 public struct FileStats
865 SecSince1970 accessed;
866 SecSince1970 modified;
867 SecSince1970 created;
870 #if defined(__WIN32__)
872 // --- FileName functions ---
874 default TimeStamp Win32FileTimeToTimeStamp(FILETIME * fileTime)
876 // TIME_ZONE_INFORMATION tz = { 0 };
880 FileTimeToSystemTime(fileTime, <);
883 GetTimeZoneInformation(&tz);
885 _TzSpecificLocalTimeToSystemTime(&tz, <, &st);
890 t.month = (Month)(st.wMonth - 1);
893 t.minute = st.wMinute;
894 t.second = st.wSecond;
898 default void TimeStampToWin32FileTime(TimeStamp t, FILETIME * fileTime)
900 // TIME_ZONE_INFORMATION tz = { 0 };
906 st.wYear = (short)tm.year;
907 st.wMonth = (short)tm.month + 1;
908 st.wDay = (short)tm.day;
909 st.wHour = (short)tm.hour;
910 st.wMinute = (short)tm.minute;
911 st.wSecond = (short)tm.second;
912 st.wMilliseconds = 0;
916 GetTimeZoneInformation(&tz);
918 SystemTimeToTzSpecificLocalTime(&tz, &st, <);
922 SystemTimeToFileTime(<, fileTime);
925 default TimeStamp Win32FileTimeToTimeStamp(FILETIME * fileTime);
926 default void TimeStampToWin32FileTime(TimeStamp t, FILETIME * fileTime);
928 default bool WinReviveNetworkResource(uint16 * _wfileName);
932 public FileAttribs FileExists(char * fileName)
934 char archiveName[MAX_LOCATION], * archiveFile;
935 #if !defined(ECERE_BOOTSTRAP)
936 if(SplitArchivePath(fileName, archiveName, &archiveFile))
938 return EARFileSystem::Exists(archiveName, archiveFile);
940 else if(strstr(fileName, "http://") == fileName)
942 return FileAttribs { isFile = true };
946 return FILE_FileExists(fileName);
949 static int openCount;
951 public File FileOpen(char * fileName, FileOpenMode mode)
956 char archiveName[MAX_LOCATION], * archiveFile;
957 #if !defined(ECERE_BOOTSTRAP)
958 if(SplitArchivePath(fileName, archiveName, &archiveFile))
960 result = EARFileSystem::Open(archiveName, archiveFile, mode);
962 #if !defined(ECERE_VANILLA) && !defined(ECERE_NONET)
963 else if(strstr(fileName, "http://") == fileName)
965 result = FileOpenURL(fileName);
970 if(strstr(fileName, "File://") == fileName)
972 result = (File)(uintptr)strtoull(fileName+7, null, 16);
975 if(result._class && eClass_IsDerived(result._class, class(File)))
977 if(!result._refCount) incref result;
979 result.Seek(0, start);
990 FILE_FileOpen(fileName, mode, &file.input, &file.output);
993 if(!file.input && !file.output);
998 // TESTING ENABLING FILE BUFFERING BY DEFAULT... DOCUMENT ANY ISSUE
1001 setvbuf(file.input, null, _IONBF, 0);
1003 setvbuf(file.output, null, _IONBF, 0);
1010 LogErrorCode((mode == Read || mode == ReadWrite) ?
1011 ERR_FILE_NOT_FOUND : ERR_FILE_WRITE_FAILED, fileName);
1020 public void FileFixCase(char * file)
1022 FILE_FileFixCase(file);
1025 #if !defined(ECERE_BOOTSTRAP)
1026 public bool FileTruncate(char * fileName, FileSize size)
1028 #if defined(__WIN32__)
1029 uint16 * _wfileName = UTF8toUTF16(fileName, null);
1030 int f = _wopen(_wfileName, _O_RDWR|_O_CREAT, _S_IREAD|_S_IWRITE);
1031 bool result = false;
1034 if(!_chsize(f, size))
1041 return truncate(fileName, size) == 0;
1046 public bool FileGetSize(char * fileName, FileSize * size)
1048 bool result = false;
1054 #if !defined(ECERE_BOOTSTRAP)
1055 char archiveName[MAX_LOCATION], * archiveFile;
1056 if(SplitArchivePath(fileName, archiveName, &archiveFile))
1057 return EARFileSystem::GetSize(archiveName, archiveFile, size);
1060 result = FILE_FileGetSize(fileName, size);
1066 public bool FileGetStats(char * fileName, FileStats stats)
1068 bool result = false;
1069 if(stats && fileName)
1071 #if !defined(ECERE_BOOTSTRAP)
1072 char archiveName[MAX_LOCATION], * archiveFile;
1073 if(SplitArchivePath(fileName, archiveName, &archiveFile))
1074 result = EARFileSystem::Stats(archiveName, archiveFile, stats);
1077 return FILE_FileGetStats(fileName, stats);
1082 #ifndef ECERE_BOOTSTRAP
1084 public bool FileSetAttribs(char * fileName, FileAttribs attribs)
1087 uint winAttribs = 0;
1088 uint16 * _wfileName = UTF8toUTF16(fileName, null);
1090 if(attribs.isHidden) winAttribs |= FILE_ATTRIBUTE_HIDDEN;
1091 if(attribs.isReadOnly) winAttribs |= FILE_ATTRIBUTE_READONLY;
1093 SetFileAttributes(_wfileName, winAttribs);
1099 public bool FileSetTime(char * fileName, TimeStamp created, TimeStamp accessed, TimeStamp modified)
1101 bool result = false;
1102 TimeStamp currentTime = time(null);
1103 if(!created) created = currentTime;
1104 if(!accessed) accessed = currentTime;
1105 if(!modified) modified = currentTime;
1109 uint16 * _wfileName = UTF8toUTF16(fileName, null);
1110 HANDLE hFile = CreateFile(_wfileName, GENERIC_WRITE|GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, null,
1111 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, null);
1113 if(hFile != INVALID_HANDLE_VALUE)
1117 TimeStampToWin32FileTime(created, &c);
1118 TimeStampToWin32FileTime(accessed, &a);
1119 TimeStampToWin32FileTime(modified, &m);
1125 cc = Win32FileTimeToTimeStamp(&c);
1126 aa = Win32FileTimeToTimeStamp(&a);
1127 mm = Win32FileTimeToTimeStamp(&m);
1131 if(SetFileTime(hFile, &c, &a, &m))
1137 struct utimbuf t = { (int)accessed, (int)modified };
1138 if(!utime(fileName, &t))
1145 /****************************************************************************
1147 ****************************************************************************/
1148 // Directory Description for file listing
1149 private class Dir : struct
1151 #if defined(__WIN32__)
1155 NETRESOURCE * resources;
1159 NETRESOURCE * workGroups;
1164 char name[MAX_LOCATION];
1167 static FileDesc FileFind(char * path, char * extensions)
1169 FileDesc result = null;
1172 if((file = FileDesc {}))
1174 char archiveName[MAX_LOCATION], * archiveFile;
1175 if(SplitArchivePath(path, archiveName, &archiveFile))
1177 if(EARFileSystem::Find(file, archiveName, archiveFile))
1179 file.system = class(EARFileSystem);
1187 if((d = file.dir = Dir {}))
1189 #if defined(__WIN32__)
1190 if(!strcmp(path, "/"))
1193 uint drives = 0xFFFFFFFF;
1194 d.fHandle = (HANDLE)drives; //GetLogicalDrives();
1195 for(c = 0; c<26; c++)
1196 if(((uint)d.fHandle) & (1<<c))
1198 char volume[MAX_FILENAME] = "";
1199 uint16 _wvolume[MAX_FILENAME];
1201 uint16 _wfilePath[4];
1203 strcpy(d.name, path);
1204 file.stats.attribs = FileAttribs { isDirectory = true, isDrive = true };
1205 _wfilePath[0] = file.path[0] = (char)('A' + c);
1206 _wfilePath[1] = file.path[1] = ':';
1207 _wfilePath[2] = file.path[2] = '\\';
1208 _wfilePath[3] = file.path[3] = '\0';
1209 file.stats.size = 0;
1210 file.stats.accessed = file.stats.created = file.stats.modified = 0;
1211 driveType = GetDriveType(_wfilePath);
1214 case DRIVE_REMOVABLE: file.stats.attribs.isRemovable = true; break;
1215 case DRIVE_REMOTE: file.stats.attribs.isRemote = true; break;
1216 case DRIVE_CDROM: file.stats.attribs.isCDROM = true; break;
1219 if(driveType == DRIVE_NO_ROOT_DIR) continue;
1221 if(driveType != DRIVE_REMOVABLE && driveType != DRIVE_REMOTE &&
1222 GetVolumeInformation(_wfilePath, _wvolume, MAX_FILENAME - 1, null, null, null, null, 0))
1224 file.path[2] = '\0';
1225 UTF16toUTF8Buffer(_wvolume, volume, MAX_FILENAME);
1226 sprintf(file.name, "%s [%s]", file.path, volume);
1230 file.path[2] = '\0';
1231 strcpy(file.name, file.path);
1236 d.fHandle = (HANDLE) drives;
1239 else if(path[0] != '\\' || path[1] != '\\' || strstr(path+2, "\\"))
1241 WIN32_FIND_DATA winFile;
1242 uint16 dir[MAX_PATH];
1244 UTF8toUTF16Buffer(path, dir, MAX_LOCATION);
1245 if(path[0]) wcscat(dir, L"\\");
1246 wcscat(dir, L"*.*");
1248 d.fHandle = FindFirstFile(dir, &winFile);
1249 if(d.fHandle == INVALID_HANDLE_VALUE && WinReviveNetworkResource(dir))
1250 d.fHandle = FindFirstFile(dir, &winFile);
1251 if(d.fHandle != INVALID_HANDLE_VALUE)
1253 UTF16toUTF8Buffer(winFile.cFileName, file.name, MAX_FILENAME);
1254 strcpy(file.path, path);
1255 PathCat(file.path, file.name);
1257 strcat(file.path, DIR_SEPS);
1258 strcat(file.path, file.name);*/
1259 // file.sizeHigh = winFile.nFileSizeHigh;
1260 file.stats.size = winFile.nFileSizeLow;
1262 file.stats.attribs = FileAttribs { };
1263 file.stats.attribs.isArchive = (winFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) ? true : false;
1264 file.stats.attribs.isHidden = (winFile.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) ? true : false;
1265 file.stats.attribs.isReadOnly = (winFile.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? true : false;
1266 file.stats.attribs.isSystem = (winFile.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) ? true : false;
1267 file.stats.attribs.isTemporary = (winFile.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) ? true : false;
1268 file.stats.attribs.isDirectory = (winFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? true : false;
1269 file.stats.attribs.isFile = !(winFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
1270 strcpy(d.name, path);
1272 file.stats.accessed = Win32FileTimeToTimeStamp(&winFile.ftLastAccessTime);
1273 file.stats.modified = Win32FileTimeToTimeStamp(&winFile.ftLastWriteTime);
1274 file.stats.created = Win32FileTimeToTimeStamp(&winFile.ftCreationTime);
1281 int count = 0xFFFFFFFF;
1282 uint size = 512 * sizeof(NETRESOURCE);
1283 NETRESOURCE * buffer = (NETRESOURCE *)new0 byte[size];
1284 NETRESOURCE nr = {0};
1287 nr.dwScope = RESOURCE_GLOBALNET;
1288 nr.dwType = RESOURCETYPE_DISK;
1289 nr.lpProvider = L"Microsoft Windows Network";
1291 strcpy(d.name, path);
1294 nr.lpRemoteName = UTF8toUTF16(path, null);
1297 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &nr, &handle);
1300 WinReviveNetworkResource(nr.lpRemoteName);
1301 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &nr, &handle);
1308 int returnCode = WNetEnumResource(handle, &count, buffer, &size);
1309 if(returnCode != ERROR_MORE_DATA)
1312 buffer = (NETRESOURCE *)renew0 buffer byte[size];
1314 WNetCloseEnum(handle);
1317 delete nr.lpRemoteName;
1320 file.stats.attribs = FileAttribs { isDirectory = true, isShare = true };
1321 file.stats.size = 0;
1322 file.stats.accessed = file.stats.created = file.stats.modified = 0;
1324 UTF16toUTF8Buffer(buffer->lpRemoteName, file.path, MAX_LOCATION);
1325 GetLastDirectory(file.path, file.name);
1328 d.resources = buffer;
1329 d.numResources = count;
1338 nr.lpProvider = L"Microsoft Windows Network";
1341 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &nr, &handle);
1344 int returnCode = WNetEnumResource(handle, &count, buffer, &size);
1345 if(returnCode != ERROR_MORE_DATA)
1348 buffer = (NETRESOURCE *)renew0 buffer byte[size];
1350 WNetCloseEnum(handle);
1352 for(c = 0; c<count; c++)
1354 NETRESOURCE * resources;
1355 int countInGroup = 0xFFFFFFFF;
1357 size = 512 * sizeof(NETRESOURCE);
1358 resources = (NETRESOURCE *)new0 byte[size];
1361 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &buffer[c], &handle);
1364 int returnCode = WNetEnumResource(handle, &countInGroup, resources, &size);
1365 if(returnCode != ERROR_MORE_DATA)
1367 countInGroup = 0xFFFFFFFF;
1368 resources = (NETRESOURCE *)renew0 resources byte[size];
1370 WNetCloseEnum(handle);
1374 file.stats.attribs = FileAttribs { isDirectory = true, isServer = true };
1375 file.stats.size = 0;
1376 file.stats.accessed = file.stats.created = file.stats.modified = 0;
1378 UTF16toUTF8Buffer(resources->lpRemoteName, file.path, MAX_LOCATION);
1380 file.path[2] = (char)toupper(file.path[2]);
1381 GetLastDirectory(file.path, file.name);
1385 d.resources = resources;
1386 d.numResources = countInGroup;
1389 d.workGroups = buffer;
1390 d.numWorkGroups = count;
1397 if(c >= count && buffer) delete buffer;
1404 d.d = opendir((path && path[0]) ? path : ".");
1405 if(d.d && (de = readdir(d.d)))
1409 strcpy(file.path, path);
1411 strcat(file.path, DIR_SEPS);
1413 strcpy(file.name,de->d_name);
1414 strcat(file.path, file.name);
1415 if(!stat(file.path, &s))
1417 file.stats.attribs = (s.st_mode&S_IFDIR) ? FileAttribs { isDirectory = true } : FileAttribs { isFile = true };
1418 file.stats.size = (FileSize)s.st_size;
1419 file.stats.accessed = s.st_atime;
1420 file.stats.modified = s.st_mtime;
1421 file.stats.created = s.st_ctime;
1423 strcpy(d.name, path);
1438 while(result && !result.Validate(extensions))
1439 result = result.FindNext(extensions);
1444 private class FileDesc : struct
1447 char name[MAX_FILENAME];
1448 char path[MAX_LOCATION];
1450 subclass(FileSystem) system;
1453 bool Validate(char * extensions)
1455 if(strcmp(name, "..") && strcmp(name, ".") && strcmp(name, ""))
1457 if(extensions && !stats.attribs.isDirectory)
1459 char extension[MAX_EXTENSION], compared[MAX_EXTENSION];
1462 GetExtension(name, extension);
1463 for(c = 0; extensions[c];)
1467 for(;(ch = extensions[c]) && !IS_ALUNDER(ch); c++);
1468 for(;(ch = extensions[c]) && IS_ALUNDER(ch); c++)
1469 compared[len++] = ch;
1470 compared[len] = '\0';
1472 if(!strcmpi(extension, compared))
1482 FileDesc FindNext(char * extensions)
1484 FileDesc result = null;
1490 while(!Validate(extensions))
1496 if(system.FindNext(this))
1503 #if defined(__WIN32__)
1504 if(!strcmp(d.name, "/"))
1507 uint drives = (uint)d.fHandle;
1508 for(c = 0; c<26; c++)
1512 char volume[MAX_FILENAME] = "";
1515 uint16 _wvolume[MAX_FILENAME];
1517 stats.attribs = FileAttribs { isDirectory = true, isDrive = true };
1519 stats.accessed = stats.created = stats.modified = 0;
1520 _wpath[0] = path[0] = (char)('A' + c);
1521 _wpath[1] = path[1] = ':';
1522 _wpath[2] = path[2] = '\\';
1523 _wpath[3] = path[3] = 0;
1524 driveType = GetDriveType(_wpath);
1529 case DRIVE_REMOVABLE: stats.attribs.isRemovable = true; break;
1530 case DRIVE_REMOTE: stats.attribs.isRemote = true; break;
1531 case DRIVE_CDROM: stats.attribs.isCDROM = true; break;
1533 if(driveType == DRIVE_NO_ROOT_DIR)
1535 uint16 remoteName[1024];
1540 status = WNetGetConnection(_wpath, remoteName, &size);
1541 if(status != ERROR_CONNECTION_UNAVAIL)
1548 if(driveType != DRIVE_REMOVABLE && driveType != DRIVE_REMOTE &&
1549 GetVolumeInformation(_wpath, _wvolume, MAX_FILENAME - 1, null, null, null, null, 0))
1551 UTF16toUTF8Buffer(_wvolume, volume, MAX_FILENAME);
1553 sprintf(name, "%s [%s]", path, volume);
1564 d.fHandle = (HANDLE) drives;
1567 else if(d.name[0] != '\\' || d.name[1] != '\\' || strstr(d.name+2, "\\"))
1569 WIN32_FIND_DATA winFile;
1570 if(FindNextFile(d.fHandle, &winFile))
1572 UTF16toUTF8Buffer(winFile.cFileName, name, MAX_FILENAME);
1573 stats.attribs = FileAttribs { };
1574 stats.attribs.isArchive = (winFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) ? true : false;
1575 stats.attribs.isHidden = (winFile.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) ? true : false;
1576 stats.attribs.isReadOnly = (winFile.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? true : false;
1577 stats.attribs.isSystem = (winFile.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) ? true : false;
1578 stats.attribs.isTemporary = (winFile.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) ? true : false;
1579 stats.attribs.isDirectory = (winFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? true : false;
1580 stats.attribs.isFile = !(winFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
1581 stats.size = winFile.nFileSizeLow;
1583 stats.accessed = Win32FileTimeToTimeStamp(&winFile.ftLastAccessTime);
1584 stats.modified = Win32FileTimeToTimeStamp(&winFile.ftLastWriteTime);
1585 stats.created = Win32FileTimeToTimeStamp(&winFile.ftCreationTime);
1587 strcpy(path, d.name);
1588 PathCat(path, name);
1590 strcat(path, DIR_SEPS);
1591 strcat(path, name);*/
1601 if(d.resource < d.numResources)
1603 stats.attribs = FileAttribs { isDirectory = true, isShare = true };
1605 stats.accessed = stats.created = stats.modified = 0;
1607 UTF16toUTF8Buffer(d.resources[d.resource].lpRemoteName, path, MAX_LOCATION);
1608 GetLastDirectory(path, name);
1623 for(c = d.workGroup; c<d.numWorkGroups; c++)
1625 if(c != d.workGroup)
1627 int countInGroup = 0xFFFFFFFF;
1629 NETRESOURCE * resources;
1630 uint size = 512 * sizeof(NETRESOURCE);
1632 resources = (NETRESOURCE *)new0 byte[size];
1634 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &d.workGroups[c], &handle);
1637 int returnCode = WNetEnumResource(handle, &countInGroup, resources, &size);
1638 if(returnCode != ERROR_MORE_DATA)
1640 countInGroup = 0xFFFFFFFF;
1641 resources = (NETRESOURCE *)renew0 resources byte[size];
1644 WNetCloseEnum(handle);
1645 d.numResources = countInGroup;
1646 d.resources = resources;
1650 if(d.resource < d.numResources)
1652 stats.attribs = FileAttribs { isDirectory = true, isServer = true };
1654 stats.accessed = stats.created = stats.modified = 0;
1656 UTF16toUTF8Buffer(d.resources[d.resource].lpRemoteName, path, MAX_LOCATION);
1658 path[2] = (char)toupper(path[2]);
1659 GetLastDirectory(path, name);
1673 if(d.workGroup == d.numWorkGroups && d.resource == d.numResources)
1675 delete d.workGroups;
1687 strcpy(name,de->d_name);
1688 strcpy(path, d.name);
1689 if(d.name[0] && d.name[1])
1690 strcat(path, DIR_SEPS);
1694 stats.attribs = FileAttribs { };
1695 stats.attribs = (s.st_mode&S_IFDIR) ? FileAttribs { isDirectory = true } : FileAttribs { isFile = true };
1696 stats.size = (FileSize)s.st_size;
1697 stats.accessed = s.st_atime;
1698 stats.modified = s.st_mtime;
1699 stats.created = s.st_ctime;
1716 system.CloseDir(this);
1722 #if defined(__WIN32__)
1723 if(d.fHandle && strcmp(d.name, "/"))
1724 FindClose(d.fHandle);
1735 public struct FileListing
1743 bool result = false;
1745 desc = desc.FindNext(extensions);
1747 desc = FileFind(directory, extensions);
1760 property char * name { get { return (char *)(desc ? desc.name : null); } };
1761 property char * path { get { return (char *)(desc ? desc.path : null); } };
1762 property FileStats stats { get { value = desc ? desc.stats : FileStats { }; } };
1769 public File CreateTemporaryFile(char * tempFileName, char * template)
1771 #ifndef ECERE_BOOTSTRAP // quick fix for now
1773 #if defined(__unix__) || defined(__APPLE__)
1774 char buffer[MAX_FILENAME];
1776 strcpy(buffer, "/tmp/");
1777 strcat(buffer, template);
1778 //strcpy(buffer, template);
1779 strcat(buffer, "XXXXXX");
1781 fd = mkstemp(buffer);
1782 strcpy(tempFileName, buffer);
1784 f.output = f.input = fdopen(fd, "r+");
1786 char tempPath[MAX_LOCATION];
1787 GetTempPathA(MAX_LOCATION, tempPath); // TODO: Patch this whole thing to support Unicode temp path
1788 GetTempFileNameA(tempPath, template, 0, tempFileName);
1789 f = FileOpen(tempFileName, readWrite);
1797 public void CreateTemporaryDir(char * tempFileName, char * template)
1799 #ifndef ECERE_BOOTSTRAP // quick fix for now
1800 #if defined(__unix__) || defined(__APPLE__)
1801 char buffer[MAX_FILENAME];
1802 strcpy(buffer, "/tmp/");
1803 strcat(buffer, template);
1804 //strcpy(buffer, template);
1805 strcat(buffer, "XXXXXX");
1808 strcpy(tempFileName, buffer);
1810 char tempPath[MAX_LOCATION];
1811 GetTempPathA(MAX_LOCATION, tempPath); // TODO: Patch this whole thing to support Unicode temp path
1812 GetTempFileNameA(tempPath, template, 0, tempFileName);
1813 DeleteFile(tempFileName);
1814 MakeDir(tempFileName);
1819 public void MakeSlashPath(char * p)
1823 ChangeCh(p, '\\', '/');
1827 public void MakeSystemPath(char * p)
1832 public char * CopySystemPath(char * p)
1834 char * d = CopyString(p);
1840 public char * CopyUnixPath(char * p)
1842 char * d = CopyString(p);
1848 public char * GetSystemPathBuffer(char * d, char * p)
1851 strcpy(d, p ? p : "");
1856 public char * GetSlashPathBuffer(char * d, char * p)
1859 strcpy(d, p ? p : "");