14 #define IS_ALUNDER(ch) ((ch) == '_' || isalnum((ch)))
16 #if defined(ECERE_BOOTSTRAP)
23 #ifndef ECERE_BOOTSTRAP
24 #if defined(__GNUC__) || defined(__WATCOMC__) || defined(__WIN32__)
26 #include <sys/types.h>
31 #if defined(__unix__) || defined(__APPLE__)
35 #if defined(__WIN32__) || defined(__WATCOMC__)
41 #if defined(__WIN32__)
42 #define WIN32_LEAN_AND_MEAN
46 BOOL WINAPI GetVolumePathName(LPCTSTR lpszFileName,LPTSTR lpszVolumePathName,DWORD cchBufferLength);
48 // Missing function...
50 #ifndef WNetGetResourceInformation
51 DWORD APIENTRY WNetGetResourceInformationA(LPNETRESOURCE lpNetResource, LPVOID lpBuffer, LPDWORD lpcbBuffer, LPTSTR* lplpSystem);
53 #define WNetGetResourceInformation WNetGetResourceInformationW
55 #define WNetGetResourceInformation WNetGetResourceInformationA
67 #endif //#ifndef ECERE_BOOTSTRAP
76 #if !defined(ECERE_VANILLA) && !defined(ECERE_NONET) && !defined(ECERE_BOOTSTRAP)
82 // IMPLEMENTATION OF THESE IS IN _File.c
85 uint FILE_GetSize(FILE * input);
86 bool FILE_Lock(FILE * input, FILE * output, FileLock type, uint64 start, uint64 length, bool wait);
87 void FILE_set_buffered(FILE * input, FILE * output, bool value);
88 FileAttribs FILE_FileExists(char * fileName);
89 bool FILE_FileGetSize(char * fileName, FileSize * size);
90 bool FILE_FileGetStats(char * fileName, FileStats stats);
91 void FILE_FileFixCase(char * file);
92 void FILE_FileOpen(char * fileName, FileOpenMode mode, FILE ** input, FILE **output);
96 FileSystem httpFileSystem;
98 public class FileSize : uint
100 // defaultAlignment = Right;
102 void OnDisplay(Surface surface, int x, int y, int width, void * fieldData, int alignment, DataDisplayFlags displayFlags)
106 eUtils_PrintSize(string, *size, 2);
107 len = strlen(string);
108 surface.WriteTextDots(alignment, x, y, width, string, len);
111 int OnCompare(FileSize data2)
118 else if(this < data2)
124 char * OnGetString(char * string, void * fieldData, bool * needClass)
126 PrintSize(string, this, 2);
130 bool OnGetDataFromString(char * string)
133 double value = strtod(string, &end);
135 if(strstr(end, "GB") || strstr(end, "gb")) multiplier = (uint)1024 * 1024 * 1024;
136 else if(strstr(end, "MB") || strstr(end, "mb")) multiplier = (uint)1024 * 1024;
137 else if(strstr(end, "KB") || strstr(end, "kb")) multiplier = 1024;
139 this = (uint)(multiplier * value);
144 public class FileSize64 : uint64
146 int OnCompare(FileSize64 data2)
153 else if(this < data2)
159 char * OnGetString(char * string, void * fieldData, bool * needClass)
161 PrintBigSize(string, this, 2);
165 bool OnGetDataFromString(char * string)
168 double value = strtod(string, &end);
169 uint64 multiplier = 1;
170 if(strstr(end, "PB") || strstr(end, "pb")) multiplier = (uint64)1024 * 1024 * 1024 * 1024;
171 else if(strstr(end, "TB") || strstr(end, "tb")) multiplier = (uint64)1024 * 1024 * 1024 * 1024;
172 else if(strstr(end, "GB") || strstr(end, "gb")) multiplier = (uint64)1024 * 1024 * 1024;
173 else if(strstr(end, "MB") || strstr(end, "mb")) multiplier = (uint64)1024 * 1024;
174 else if(strstr(end, "KB") || strstr(end, "kb")) multiplier = 1024;
176 this = (uint64)(multiplier * value);
183 virtual File ::Open(char * archive, char * name, FileOpenMode mode);
186 virtual FileAttribs ::Exists(char * archive, char * fileName);
187 virtual bool ::GetSize(char * archive, char * fileName, FileSize * size);
188 virtual bool ::Stats(char * archive, char * fileName, FileStats stats);
189 virtual void ::FixCase(char * archive, char * fileName);
192 virtual bool ::Find(FileDesc file, char * archive, char * name);
193 virtual bool ::FindNext(FileDesc file);
194 virtual void ::CloseDir(FileDesc file);
196 // Archive manipulation
197 virtual Archive ::OpenArchive(char * fileName, ArchiveOpenFlags create);
198 virtual bool ::QuerySize(char * fileName, FileSize * size);
201 public enum FileOpenMode { read = 1, write, append, readWrite, writeRead, appendRead };
202 public enum FileSeekMode { start, current, end };
204 #if !defined(ECERE_BOOTSTRAP)
205 static FileDialog fileDialog { text = $"Select File" };
210 unlocked = 0, // LOCK_UN _SH_DENYNO
211 shared = 1, // LOCK_SH _SH_DENYWR
212 exclusive = 2 // LOCK_EX _SH_DENYRW
215 public class File : IOChannel
217 FILE * input, * output;
219 uint ReadData(byte * bytes, uint numBytes)
221 return Read(bytes, 1, numBytes);
224 uint WriteData(byte * bytes, uint numBytes)
226 return Write(bytes, 1, numBytes);
231 if(output && output != input)
245 bool OnGetDataFromString(char * string)
254 File f = FileOpen(string, read);
261 uint read = f.Read(buffer, 1, sizeof(buffer));
262 Write(buffer, 1, read);
271 char * OnGetString(char * tempString, void * fieldData, bool * needClass)
275 PrintSize(tempString, GetSize(), 2);
281 #ifndef ECERE_BOOTSTRAP
282 Window OnEdit(DataBox dataBox, DataBox obsolete, int x, int y, int w, int h, void * userData)
284 Window editData = class::OnEdit(dataBox, obsolete, x + 24, y, w - 48, h, userData);
287 dataBox, inactive = true, text = $"Imp", hotKey = f2,
288 position = { Max(x + 24, x + w - 24), y }, size = { 24, h };
290 bool DataBox::NotifyClicked(Button button, int x, int y, Modifiers mods)
292 fileDialog.master = rootWindow;
293 fileDialog.filePath = "";
294 fileDialog.type = open;
296 if(fileDialog.Modal() == ok)
298 char * filePath = fileDialog.filePath;
300 if(output.OnGetDataFromString(filePath))
302 SetData(output, false);
311 dataBox, inactive = true, text = $"Exp", hotKey = f2,
312 position = { Max(x + 24, x + w - 48), y }, size = { 24, h };
314 bool DataBox::NotifyClicked(Button button, int x, int y, Modifiers mods)
316 fileDialog.master = rootWindow;
317 fileDialog.type = save;
318 fileDialog.filePath = "";
319 if(fileDialog.Modal() == ok)
321 char * filePath = fileDialog.filePath;
322 File f = FileOpen(filePath, write);
325 File input = *(void **)data;
326 input.Seek(0, start);
330 uint read = input.Read(buffer, 1, sizeof(buffer));
331 f.Write(buffer, 1, read);
343 #endif //#ifndef ECERE_BOOTSTRAP
345 #if !defined(ECERE_VANILLA) && !defined(ECERE_NOARCHIVE) && !defined(ECERE_BOOTSTRAP)
346 void OnSerialize(IOChannel channel)
348 uint size = this ? GetSize() : MAXDWORD;
351 byte * uncompressed = new byte[size];
353 if(uncompressed || !size)
355 uint count = Read(uncompressed, 1, size);
358 uint cSize = size + size / 1000 + 12;
359 byte * compressed = new byte[cSize];
362 compress2(compressed, &cSize, uncompressed, size, 9);
364 size.OnSerialize(channel);
365 cSize.OnSerialize(channel);
366 channel.WriteData(compressed, cSize);
375 size.OnSerialize(channel);
380 size.OnSerialize(channel);
382 // Will add position...
386 for(c = 0; c<size; c += sizeof(data))
388 uint count = Read(data, 1, sizeof(data));
389 buffer.WriteData(data, count);
395 void OnUnserialize(IOChannel channel)
401 size.OnUnserialize(channel);
405 cSize.OnUnserialize(channel);
407 compressed = new byte[cSize];
410 if(channel.ReadData(compressed, cSize) == cSize)
412 byte * uncompressed = new byte[size];
413 if(uncompressed || !size)
416 uncompress(uncompressed, &size, compressed, cSize);
417 Write(uncompressed, 1, size);
431 size.OnUnserialize(channel);
435 for(c = 0; c<size; c += sizeof(data))
437 uint count = Min(size - c, sizeof(data));
438 channel.ReadData(data, count);
439 Write(data, 1, count);
452 virtual bool Seek(int pos, FileSeekMode mode)
454 uint fmode = SEEK_SET;
457 case start: fmode = SEEK_SET; break;
458 case end: fmode = SEEK_END; break;
459 case current: fmode = SEEK_CUR; break;
461 return fseek(input ? input : output, pos, fmode) != EOF;
464 virtual uint Tell(void)
466 return input ? ftell(input) : ftell(output);
469 virtual int Read(void * buffer, uint size, uint count)
471 return input ? (int)fread(buffer, size, count, input) : 0;
474 virtual int Write(void * buffer, uint size, uint count)
476 return output ? (int)fwrite(buffer, size, count, output) : 0;
480 virtual bool Getc(char * ch)
482 int ich = fgetc(input);
485 if(ch) *ch = (char)ich;
491 virtual bool Putc(char ch)
493 return (fputc((int)ch, output) == EOF) ? false : true;
496 virtual bool Puts(const char * string)
501 result = (fputs(string, output) == EOF) ? false : true;
502 // TODO: Check if any repercusions of commenting out fflush here
503 // This is what broke the debugger in 0.44d2 , it is required for outputting things to the DualPipe
504 // Added an explicit flush call in DualPipe::Puts
510 virtual bool Eof(void)
512 return input ? feof(input) : true;
515 virtual bool Truncate(FileSize size)
517 #ifdef ECERE_BOOTSTRAP
518 fprintf(stderr, "WARNING: File::Truncate unimplemented in ecereBootstrap.\n");
521 #if defined(__WIN32__)
522 return output ? (_chsize(fileno(output), size) == 0) : false;
524 return output ? (ftruncate(fileno(output), size) == 0) : false;
529 virtual uint GetSize(void)
531 return FILE_GetSize(input);
534 virtual void CloseInput(void)
545 virtual void CloseOutput(void)
556 virtual bool Lock(FileLock type, uint64 start, uint64 length, bool wait)
558 return FILE_Lock(input, output, type, start, length, wait);
561 virtual bool Unlock(uint64 start, uint64 length, bool wait)
563 return Lock(unlocked, start, length, wait);
567 int Printf(char * format, ...)
572 char text[MAX_F_STRING];
574 text[sizeof(text)-1] = 0;
575 va_start(args, format);
576 vsnprintf(text, sizeof(text), format, args);
578 result = strlen(text);
584 public void PrintLn(typed_object object, ...)
588 va_start(args, object);
589 PrintStdArgsToBuffer(buffer, sizeof(buffer), object, args);
595 public void Print(typed_object object, ...)
599 va_start(args, object);
600 PrintStdArgsToBuffer(buffer, sizeof(buffer), object, args);
611 bool GetLine(char *s, int max)
627 if(/*!Peek() || */ !Getc(&ch))
639 return result || c > 1;
642 // Strings and numbers separated by spaces, commas, tabs, or CR/LF, handling quotes
643 bool GetString(char * string, int max)
655 if( (ch!='\n') && (ch!='\r') && (ch!=' ') && (ch!=',') && (ch!='\t'))
661 for(c=0; c<max-1; c++)
663 if(!quoted && ((ch=='\n')||(ch=='\r')||(ch==' ')||(ch==',')||(ch=='\t')))
691 GetString(string,sizeof(string));
695 unsigned int GetHexValue(void)
698 GetString(string, sizeof(string));
699 return strtoul(string, null, 16);
705 GetString(string, sizeof(string));
706 return (float)FloatFromString(string);
709 double GetDouble(void)
712 GetString(string, sizeof(string));
713 return FloatFromString(string);
716 property void * input { set { input = value; } get { return input; } }
717 property void * output { set { output = value; } get { return output; } }
718 property bool buffered
722 FILE_set_buffered(input, output, value);
725 property bool eof { get { return Eof(); } }
727 int GetLineEx(char *s, int max, bool *hasNewLineChar)
737 if(/*!Peek() || */ !Getc(&ch))
745 *hasNewLineChar = (ch == '\n');
751 bool CopyTo(char * outputFileName)
754 File f = FileOpen(outputFileName, write);
763 uint count = Read(buffer, 1, sizeof(buffer));
764 if(count && !f.Write(buffer, 1, count))
777 virtual bool Open(char * fileName, FileOpenMode mode)
782 FILE_FileOpen(fileName, mode, &input, &output);
785 if(!input && !output);
790 // TESTING ENABLING FILE BUFFERING BY DEFAULT... DOCUMENT ANY ISSUE
793 setvbuf(file.input, null, _IONBF, 0);
795 setvbuf(file.output, null, _IONBF, 0);
801 LogErrorCode((mode == Read || mode == ReadWrite) ?
802 ERR_FILE_NOT_FOUND : ERR_FILE_WRITE_FAILED, fileName);
817 public class ConsoleFile : File
828 public class FileAttribs : bool
831 bool isFile:1, isArchive:1, isHidden:1, isReadOnly:1, isSystem:1, isTemporary:1, isDirectory:1;
832 bool isDrive:1, isCDROM:1, isRemote:1, isRemovable:1, isServer:1, isShare:1;
833 // property bool { };
836 public struct FileStats
840 SecSince1970 accessed;
841 SecSince1970 modified;
842 SecSince1970 created;
845 #if defined(__WIN32__)
847 // --- FileName functions ---
849 default TimeStamp Win32FileTimeToTimeStamp(FILETIME * fileTime)
851 // TIME_ZONE_INFORMATION tz = { 0 };
855 FileTimeToSystemTime(fileTime, <);
858 GetTimeZoneInformation(&tz);
860 _TzSpecificLocalTimeToSystemTime(&tz, <, &st);
865 t.month = (Month)(st.wMonth - 1);
868 t.minute = st.wMinute;
869 t.second = st.wSecond;
873 default void TimeStampToWin32FileTime(TimeStamp t, FILETIME * fileTime)
875 // TIME_ZONE_INFORMATION tz = { 0 };
881 st.wYear = (short)tm.year;
882 st.wMonth = (short)tm.month + 1;
883 st.wDay = (short)tm.day;
884 st.wHour = (short)tm.hour;
885 st.wMinute = (short)tm.minute;
886 st.wSecond = (short)tm.second;
887 st.wMilliseconds = 0;
891 GetTimeZoneInformation(&tz);
893 SystemTimeToTzSpecificLocalTime(&tz, &st, <);
897 SystemTimeToFileTime(<, fileTime);
900 default TimeStamp Win32FileTimeToTimeStamp(FILETIME * fileTime);
901 default void TimeStampToWin32FileTime(TimeStamp t, FILETIME * fileTime);
903 default bool WinReviveNetworkResource(uint16 * _wfileName);
907 public FileAttribs FileExists(char * fileName)
909 char archiveName[MAX_LOCATION], * archiveFile;
910 #if !defined(ECERE_BOOTSTRAP)
911 if(SplitArchivePath(fileName, archiveName, &archiveFile))
913 return EARFileSystem::Exists(archiveName, archiveFile);
915 else if(strstr(fileName, "http://") == fileName)
917 return FileAttribs { isFile = true };
921 return FILE_FileExists(fileName);
924 static int openCount;
926 public File FileOpen(char * fileName, FileOpenMode mode)
931 char archiveName[MAX_LOCATION], * archiveFile;
932 #if !defined(ECERE_BOOTSTRAP)
933 if(SplitArchivePath(fileName, archiveName, &archiveFile))
935 result = EARFileSystem::Open(archiveName, archiveFile, mode);
937 #if !defined(ECERE_VANILLA) && !defined(ECERE_NONET)
938 else if(strstr(fileName, "http://") == fileName)
940 result = FileOpenURL(fileName);
945 if(strstr(fileName, "File://") == fileName)
947 result = (File)strtoul(fileName+7, null, 16);
950 if(result._class && eClass_IsDerived(result._class, class(File)))
952 if(!result._refCount) incref result;
954 result.Seek(0, start);
965 FILE_FileOpen(fileName, mode, &file.input, &file.output);
968 if(!file.input && !file.output);
973 // TESTING ENABLING FILE BUFFERING BY DEFAULT... DOCUMENT ANY ISSUE
976 setvbuf(file.input, null, _IONBF, 0);
978 setvbuf(file.output, null, _IONBF, 0);
985 LogErrorCode((mode == Read || mode == ReadWrite) ?
986 ERR_FILE_NOT_FOUND : ERR_FILE_WRITE_FAILED, fileName);
995 public void FileFixCase(char * file)
997 FILE_FileFixCase(file);
1000 #if !defined(ECERE_BOOTSTRAP)
1001 public bool FileTruncate(char * fileName, FileSize size)
1003 #if defined(__WIN32__)
1004 uint16 * _wfileName = UTF8toUTF16(fileName, null);
1005 int f = _wopen(_wfileName, _O_RDWR|_O_CREAT, _S_IREAD|_S_IWRITE);
1006 bool result = false;
1009 if(!_chsize(f, size))
1016 return truncate(fileName, size) == 0;
1021 public bool FileGetSize(char * fileName, FileSize * size)
1023 bool result = false;
1029 #if !defined(ECERE_BOOTSTRAP)
1030 char archiveName[MAX_LOCATION], * archiveFile;
1031 if(SplitArchivePath(fileName, archiveName, &archiveFile))
1032 return EARFileSystem::GetSize(archiveName, archiveFile, size);
1035 result = FILE_FileGetSize(fileName, size);
1041 public bool FileGetStats(char * fileName, FileStats stats)
1043 bool result = false;
1044 if(stats && fileName)
1046 #if !defined(ECERE_BOOTSTRAP)
1047 char archiveName[MAX_LOCATION], * archiveFile;
1048 if(SplitArchivePath(fileName, archiveName, &archiveFile))
1049 result = EARFileSystem::Stats(archiveName, archiveFile, stats);
1052 return FILE_FileGetStats(fileName, stats);
1057 #ifndef ECERE_BOOTSTRAP
1059 public bool FileSetAttribs(char * fileName, FileAttribs attribs)
1062 uint winAttribs = 0;
1063 uint16 * _wfileName = UTF8toUTF16(fileName, null);
1065 if(attribs.isHidden) winAttribs |= FILE_ATTRIBUTE_HIDDEN;
1066 if(attribs.isReadOnly) winAttribs |= FILE_ATTRIBUTE_READONLY;
1068 SetFileAttributes(_wfileName, winAttribs);
1074 public bool FileSetTime(char * fileName, TimeStamp created, TimeStamp accessed, TimeStamp modified)
1076 bool result = false;
1077 TimeStamp currentTime = time(null);
1078 if(!created) created = currentTime;
1079 if(!accessed) accessed = currentTime;
1080 if(!modified) modified = currentTime;
1084 uint16 * _wfileName = UTF8toUTF16(fileName, null);
1085 HANDLE hFile = CreateFile(_wfileName, GENERIC_WRITE|GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, null,
1086 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, null);
1088 if(hFile != INVALID_HANDLE_VALUE)
1092 TimeStampToWin32FileTime(created, &c);
1093 TimeStampToWin32FileTime(accessed, &a);
1094 TimeStampToWin32FileTime(modified, &m);
1100 cc = Win32FileTimeToTimeStamp(&c);
1101 aa = Win32FileTimeToTimeStamp(&a);
1102 mm = Win32FileTimeToTimeStamp(&m);
1106 if(SetFileTime(hFile, &c, &a, &m))
1112 struct utimbuf t = { (int)accessed, (int)modified };
1113 if(!utime(fileName, &t))
1120 /****************************************************************************
1122 ****************************************************************************/
1123 // Directory Description for file listing
1124 private class Dir : struct
1126 #if defined(__WIN32__)
1130 NETRESOURCE * resources;
1134 NETRESOURCE * workGroups;
1139 char name[MAX_LOCATION];
1142 static FileDesc FileFind(char * path, char * extensions)
1144 FileDesc result = null;
1147 if((file = FileDesc {}))
1149 char archiveName[MAX_LOCATION], * archiveFile;
1150 if(SplitArchivePath(path, archiveName, &archiveFile))
1152 if(EARFileSystem::Find(file, archiveName, archiveFile))
1154 file.system = class(EARFileSystem);
1162 if((d = file.dir = Dir {}))
1164 #if defined(__WIN32__)
1165 if(!strcmp(path, "/"))
1168 d.fHandle = (void *)0xFFFFFFFF; //GetLogicalDrives();
1169 for(c = 0; c<26; c++)
1170 if(((uint)d.fHandle) & (1<<c))
1172 char volume[MAX_FILENAME] = "";
1173 uint16 _wvolume[MAX_FILENAME];
1175 uint16 _wfilePath[4];
1177 strcpy(d.name, path);
1178 file.stats.attribs = FileAttribs { isDirectory = true, isDrive = true };
1179 _wfilePath[0] = file.path[0] = (char)('A' + c);
1180 _wfilePath[1] = file.path[1] = ':';
1181 _wfilePath[2] = file.path[2] = '\\';
1182 _wfilePath[3] = file.path[3] = '\0';
1183 file.stats.size = 0;
1184 file.stats.accessed = file.stats.created = file.stats.modified = 0;
1185 driveType = GetDriveType(_wfilePath);
1188 case DRIVE_REMOVABLE: file.stats.attribs.isRemovable = true; break;
1189 case DRIVE_REMOTE: file.stats.attribs.isRemote = true; break;
1190 case DRIVE_CDROM: file.stats.attribs.isCDROM = true; break;
1192 *((uint *)&d.fHandle) ^= (1<<c);
1193 if(driveType == DRIVE_NO_ROOT_DIR) continue;
1195 if(driveType != DRIVE_REMOVABLE && driveType != DRIVE_REMOTE &&
1196 GetVolumeInformation(_wfilePath, _wvolume, MAX_FILENAME - 1, null, null, null, null, 0))
1198 file.path[2] = '\0';
1199 UTF16toUTF8Buffer(_wvolume, volume, MAX_FILENAME);
1200 sprintf(file.name, "%s [%s]", file.path, volume);
1204 file.path[2] = '\0';
1205 strcpy(file.name, file.path);
1212 else if(path[0] != '\\' || path[1] != '\\' || strstr(path+2, "\\"))
1214 WIN32_FIND_DATA winFile;
1215 uint16 dir[MAX_PATH];
1217 UTF8toUTF16Buffer(path, dir, MAX_LOCATION);
1218 if(path[0]) wcscat(dir, L"\\");
1219 wcscat(dir, L"*.*");
1221 d.fHandle = FindFirstFile(dir, &winFile);
1222 if(d.fHandle == INVALID_HANDLE_VALUE && WinReviveNetworkResource(dir))
1223 d.fHandle = FindFirstFile(dir, &winFile);
1224 if(d.fHandle != INVALID_HANDLE_VALUE)
1226 UTF16toUTF8Buffer(winFile.cFileName, file.name, MAX_FILENAME);
1227 strcpy(file.path, path);
1228 PathCat(file.path, file.name);
1230 strcat(file.path, DIR_SEPS);
1231 strcat(file.path, file.name);*/
1232 // file.sizeHigh = winFile.nFileSizeHigh;
1233 file.stats.size = winFile.nFileSizeLow;
1235 file.stats.attribs = FileAttribs { };
1236 file.stats.attribs.isArchive = (winFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) ? true : false;
1237 file.stats.attribs.isHidden = (winFile.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) ? true : false;
1238 file.stats.attribs.isReadOnly = (winFile.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? true : false;
1239 file.stats.attribs.isSystem = (winFile.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) ? true : false;
1240 file.stats.attribs.isTemporary = (winFile.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) ? true : false;
1241 file.stats.attribs.isDirectory = (winFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? true : false;
1242 file.stats.attribs.isFile = !(winFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
1243 strcpy(d.name, path);
1245 file.stats.accessed = Win32FileTimeToTimeStamp(&winFile.ftLastAccessTime);
1246 file.stats.modified = Win32FileTimeToTimeStamp(&winFile.ftLastWriteTime);
1247 file.stats.created = Win32FileTimeToTimeStamp(&winFile.ftCreationTime);
1254 int count = 0xFFFFFFFF;
1255 uint size = 512 * sizeof(NETRESOURCE);
1256 NETRESOURCE * buffer = (NETRESOURCE *)new0 byte[size];
1257 NETRESOURCE nr = {0};
1260 nr.dwScope = RESOURCE_GLOBALNET;
1261 nr.dwType = RESOURCETYPE_DISK;
1262 nr.lpProvider = L"Microsoft Windows Network";
1264 strcpy(d.name, path);
1267 nr.lpRemoteName = UTF8toUTF16(path, null);
1270 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &nr, &handle);
1273 WinReviveNetworkResource(nr.lpRemoteName);
1274 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &nr, &handle);
1281 int returnCode = WNetEnumResource(handle, &count, buffer, &size);
1282 if(returnCode != ERROR_MORE_DATA)
1285 buffer = (NETRESOURCE *)renew0 buffer byte[size];
1287 WNetCloseEnum(handle);
1290 delete nr.lpRemoteName;
1293 file.stats.attribs = FileAttribs { isDirectory = true, isShare = true };
1294 file.stats.size = 0;
1295 file.stats.accessed = file.stats.created = file.stats.modified = 0;
1297 UTF16toUTF8Buffer(buffer->lpRemoteName, file.path, MAX_LOCATION);
1298 GetLastDirectory(file.path, file.name);
1301 d.resources = buffer;
1302 d.numResources = count;
1311 nr.lpProvider = L"Microsoft Windows Network";
1314 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &nr, &handle);
1317 int returnCode = WNetEnumResource(handle, &count, buffer, &size);
1318 if(returnCode != ERROR_MORE_DATA)
1321 buffer = (NETRESOURCE *)renew0 buffer byte[size];
1323 WNetCloseEnum(handle);
1325 for(c = 0; c<count; c++)
1327 NETRESOURCE * resources;
1328 int countInGroup = 0xFFFFFFFF;
1330 size = 512 * sizeof(NETRESOURCE);
1331 resources = (NETRESOURCE *)new0 byte[size];
1334 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &buffer[c], &handle);
1337 int returnCode = WNetEnumResource(handle, &countInGroup, resources, &size);
1338 if(returnCode != ERROR_MORE_DATA)
1340 countInGroup = 0xFFFFFFFF;
1341 resources = (NETRESOURCE *)renew0 resources byte[size];
1343 WNetCloseEnum(handle);
1347 file.stats.attribs = FileAttribs { isDirectory = true, isServer = true };
1348 file.stats.size = 0;
1349 file.stats.accessed = file.stats.created = file.stats.modified = 0;
1351 UTF16toUTF8Buffer(resources->lpRemoteName, file.path, MAX_LOCATION);
1353 file.path[2] = (char)toupper(file.path[2]);
1354 GetLastDirectory(file.path, file.name);
1358 d.resources = resources;
1359 d.numResources = countInGroup;
1362 d.workGroups = buffer;
1363 d.numWorkGroups = count;
1370 if(c >= count && buffer) delete buffer;
1377 d.d = opendir((path && path[0]) ? path : ".");
1378 if(d.d && (de = readdir(d.d)))
1382 strcpy(file.path, path);
1384 strcat(file.path, DIR_SEPS);
1386 strcpy(file.name,de->d_name);
1387 strcat(file.path, file.name);
1388 if(!stat(file.path, &s))
1390 file.stats.attribs = (s.st_mode&S_IFDIR) ? FileAttribs { isDirectory = true } : FileAttribs { isFile = true };
1391 file.stats.size = s.st_size;
1392 file.stats.accessed = s.st_atime;
1393 file.stats.modified = s.st_mtime;
1394 file.stats.created = s.st_ctime;
1396 strcpy(d.name, path);
1411 while(result && !result.Validate(extensions))
1412 result = result.FindNext(extensions);
1417 private class FileDesc : struct
1420 char name[MAX_FILENAME];
1421 char path[MAX_LOCATION];
1423 subclass(FileSystem) system;
1426 bool Validate(char * extensions)
1428 if(strcmp(name, "..") && strcmp(name, ".") && strcmp(name, ""))
1430 if(extensions && !stats.attribs.isDirectory)
1432 char extension[MAX_EXTENSION], compared[MAX_EXTENSION];
1435 GetExtension(name, extension);
1436 for(c = 0; extensions[c];)
1440 for(;(ch = extensions[c]) && !IS_ALUNDER(ch); c++);
1441 for(;(ch = extensions[c]) && IS_ALUNDER(ch); c++)
1442 compared[len++] = ch;
1443 compared[len] = '\0';
1445 if(!strcmpi(extension, compared))
1455 FileDesc FindNext(char * extensions)
1457 FileDesc result = null;
1463 while(!Validate(extensions))
1469 if(system.FindNext(this))
1476 #if defined(__WIN32__)
1477 if(!strcmp(d.name, "/"))
1480 for(c = 0; c<26; c++)
1482 if(((uint)d.fHandle) & (1<<c))
1484 char volume[MAX_FILENAME] = "";
1487 uint16 _wvolume[MAX_FILENAME];
1489 stats.attribs = FileAttribs { isDirectory = true, isDrive = true };
1491 stats.accessed = stats.created = stats.modified = 0;
1492 _wpath[0] = path[0] = (char)('A' + c);
1493 _wpath[1] = path[1] = ':';
1494 _wpath[2] = path[2] = '\\';
1495 _wpath[3] = path[3] = 0;
1496 driveType = GetDriveType(_wpath);
1497 *((uint *)&d.fHandle) ^= (1<<c);
1501 case DRIVE_REMOVABLE: stats.attribs.isRemovable = true; break;
1502 case DRIVE_REMOTE: stats.attribs.isRemote = true; break;
1503 case DRIVE_CDROM: stats.attribs.isCDROM = true; break;
1505 if(driveType == DRIVE_NO_ROOT_DIR)
1507 uint16 remoteName[1024];
1512 status = WNetGetConnection(_wpath, remoteName, &size);
1513 if(status != ERROR_CONNECTION_UNAVAIL)
1520 if(driveType != DRIVE_REMOVABLE && driveType != DRIVE_REMOTE &&
1521 GetVolumeInformation(_wpath, _wvolume, MAX_FILENAME - 1, null, null, null, null, 0))
1523 UTF16toUTF8Buffer(_wvolume, volume, MAX_FILENAME);
1525 sprintf(name, "%s [%s]", path, volume);
1538 else if(d.name[0] != '\\' || d.name[1] != '\\' || strstr(d.name+2, "\\"))
1540 WIN32_FIND_DATA winFile;
1541 if(FindNextFile(d.fHandle, &winFile))
1543 UTF16toUTF8Buffer(winFile.cFileName, name, MAX_FILENAME);
1544 stats.attribs = FileAttribs { };
1545 stats.attribs.isArchive = (winFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) ? true : false;
1546 stats.attribs.isHidden = (winFile.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) ? true : false;
1547 stats.attribs.isReadOnly = (winFile.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? true : false;
1548 stats.attribs.isSystem = (winFile.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) ? true : false;
1549 stats.attribs.isTemporary = (winFile.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) ? true : false;
1550 stats.attribs.isDirectory = (winFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? true : false;
1551 stats.attribs.isFile = !(winFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
1552 stats.size = winFile.nFileSizeLow;
1554 stats.accessed = Win32FileTimeToTimeStamp(&winFile.ftLastAccessTime);
1555 stats.modified = Win32FileTimeToTimeStamp(&winFile.ftLastWriteTime);
1556 stats.created = Win32FileTimeToTimeStamp(&winFile.ftCreationTime);
1558 strcpy(path, d.name);
1559 PathCat(path, name);
1561 strcat(path, DIR_SEPS);
1562 strcat(path, name);*/
1572 if(d.resource < d.numResources)
1574 stats.attribs = FileAttribs { isDirectory = true, isShare = true };
1576 stats.accessed = stats.created = stats.modified = 0;
1578 UTF16toUTF8Buffer(d.resources[d.resource].lpRemoteName, path, MAX_LOCATION);
1579 GetLastDirectory(path, name);
1594 for(c = d.workGroup; c<d.numWorkGroups; c++)
1596 if(c != d.workGroup)
1598 int countInGroup = 0xFFFFFFFF;
1600 NETRESOURCE * resources;
1601 uint size = 512 * sizeof(NETRESOURCE);
1603 resources = (NETRESOURCE *)new0 byte[size];
1605 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &d.workGroups[c], &handle);
1608 int returnCode = WNetEnumResource(handle, &countInGroup, resources, &size);
1609 if(returnCode != ERROR_MORE_DATA)
1611 countInGroup = 0xFFFFFFFF;
1612 resources = (NETRESOURCE *)renew0 resources byte[size];
1615 WNetCloseEnum(handle);
1616 d.numResources = countInGroup;
1617 d.resources = resources;
1621 if(d.resource < d.numResources)
1623 stats.attribs = FileAttribs { isDirectory = true, isServer = true };
1625 stats.accessed = stats.created = stats.modified = 0;
1627 UTF16toUTF8Buffer(d.resources[d.resource].lpRemoteName, path, MAX_LOCATION);
1629 path[2] = (char)toupper(path[2]);
1630 GetLastDirectory(path, name);
1644 if(d.workGroup == d.numWorkGroups && d.resource == d.numResources)
1646 delete d.workGroups;
1658 strcpy(name,de->d_name);
1659 strcpy(path, d.name);
1660 if(d.name[0] && d.name[1])
1661 strcat(path, DIR_SEPS);
1665 stats.attribs = FileAttribs { };
1666 stats.attribs = (s.st_mode&S_IFDIR) ? FileAttribs { isDirectory = true } : FileAttribs { isFile = true };
1667 stats.size = s.st_size;
1668 stats.accessed = s.st_atime;
1669 stats.modified = s.st_mtime;
1670 stats.created = s.st_ctime;
1687 system.CloseDir(this);
1693 #if defined(__WIN32__)
1694 if(d.fHandle && strcmp(d.name, "/"))
1695 FindClose(d.fHandle);
1706 public struct FileListing
1714 bool result = false;
1716 desc = desc.FindNext(extensions);
1718 desc = FileFind(directory, extensions);
1731 property char * name { get { return (char *)(desc ? desc.name : null); } };
1732 property char * path { get { return (char *)(desc ? desc.path : null); } };
1733 property FileStats stats { get { value = desc ? desc.stats : FileStats { }; } };
1740 public File CreateTemporaryFile(char * tempFileName, char * template)
1742 #ifndef ECERE_BOOTSTRAP // quick fix for now
1744 #if defined(__unix__) || defined(__APPLE__)
1745 char buffer[MAX_FILENAME];
1747 strcpy(buffer, "/tmp/");
1748 strcat(buffer, template);
1749 //strcpy(buffer, template);
1750 strcat(buffer, "XXXXXX");
1752 fd = mkstemp(buffer);
1753 strcpy(tempFileName, buffer);
1755 f.output = f.input = fdopen(fd, "r+");
1757 char tempPath[MAX_LOCATION];
1758 GetTempPathA(MAX_LOCATION, tempPath); // TODO: Patch this whole thing to support Unicode temp path
1759 GetTempFileNameA(tempPath, template, 0, tempFileName);
1760 f = FileOpen(tempFileName, readWrite);
1768 public void CreateTemporaryDir(char * tempFileName, char * template)
1770 #ifndef ECERE_BOOTSTRAP // quick fix for now
1771 #if defined(__unix__) || defined(__APPLE__)
1772 char buffer[MAX_FILENAME];
1773 strcpy(buffer, "/tmp/");
1774 strcat(buffer, template);
1775 //strcpy(buffer, template);
1776 strcat(buffer, "XXXXXX");
1779 strcpy(tempFileName, buffer);
1781 char tempPath[MAX_LOCATION];
1782 GetTempPathA(MAX_LOCATION, tempPath); // TODO: Patch this whole thing to support Unicode temp path
1783 GetTempFileNameA(tempPath, template, 0, tempFileName);
1784 DeleteFile(tempFileName);
1785 MakeDir(tempFileName);
1790 public void MakeSlashPath(char * p)
1794 ChangeCh(p, '\\', '/');
1798 public void MakeSystemPath(char * p)
1803 public char * CopySystemPath(char * p)
1805 char * d = CopyString(p);
1811 public char * CopyUnixPath(char * p)
1813 char * d = CopyString(p);
1819 public char * GetSystemPathBuffer(char * d, char * p)
1822 strcpy(d, p ? p : "");
1827 public char * GetSlashPathBuffer(char * d, char * p)
1830 strcpy(d, p ? p : "");