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
43 #define String String_
48 BOOL WINAPI GetVolumePathName(LPCTSTR lpszFileName,LPTSTR lpszVolumePathName,DWORD cchBufferLength);
50 // Missing function...
52 #ifndef WNetGetResourceInformation
53 DWORD APIENTRY WNetGetResourceInformationA(LPNETRESOURCE lpNetResource, LPVOID lpBuffer, LPDWORD lpcbBuffer, LPTSTR* lplpSystem);
55 #define WNetGetResourceInformation WNetGetResourceInformationW
57 #define WNetGetResourceInformation WNetGetResourceInformationA
69 #endif //#ifndef ECERE_BOOTSTRAP
78 #if !defined(ECERE_VANILLA) && !defined(ECERE_NONET) && !defined(ECERE_BOOTSTRAP)
84 // IMPLEMENTATION OF THESE IS IN _File.c
88 FILE *eC_stdout(void);
90 uint FILE_GetSize(FILE * input);
91 bool FILE_Lock(FILE * input, FILE * output, FileLock type, uint64 start, uint64 length, bool wait);
92 void FILE_set_buffered(FILE * input, FILE * output, bool value);
93 FileAttribs FILE_FileExists(char * fileName);
94 bool FILE_FileGetSize(char * fileName, FileSize * size);
95 bool FILE_FileGetStats(char * fileName, FileStats stats);
96 void FILE_FileFixCase(char * file);
97 void FILE_FileOpen(char * fileName, FileOpenMode mode, FILE ** input, FILE **output);
101 FileSystem httpFileSystem;
103 public class FileSize : uint
105 // defaultAlignment = Right;
107 void OnDisplay(Surface surface, int x, int y, int width, void * fieldData, int alignment, DataDisplayFlags displayFlags)
111 eUtils_PrintSize(string, *size, 2);
112 len = strlen(string);
113 surface.WriteTextDots(alignment, x, y, width, string, len);
116 int OnCompare(FileSize data2)
123 else if(this < data2)
129 char * OnGetString(char * string, void * fieldData, bool * needClass)
131 PrintSize(string, this, 2);
135 bool OnGetDataFromString(char * string)
138 double value = strtod(string, &end);
140 if(strstr(end, "GB") || strstr(end, "gb")) multiplier = (uint)1024 * 1024 * 1024;
141 else if(strstr(end, "MB") || strstr(end, "mb")) multiplier = (uint)1024 * 1024;
142 else if(strstr(end, "KB") || strstr(end, "kb")) multiplier = 1024;
144 this = (uint)(multiplier * value);
149 public class FileSize64 : uint64
151 int OnCompare(FileSize64 data2)
158 else if(this < data2)
164 char * OnGetString(char * string, void * fieldData, bool * needClass)
166 PrintBigSize(string, this, 2);
170 bool OnGetDataFromString(char * string)
173 double value = strtod(string, &end);
174 uint64 multiplier = 1;
175 if(strstr(end, "PB") || strstr(end, "pb")) multiplier = (uint64)1024 * 1024 * 1024 * 1024;
176 else if(strstr(end, "TB") || strstr(end, "tb")) multiplier = (uint64)1024 * 1024 * 1024 * 1024;
177 else if(strstr(end, "GB") || strstr(end, "gb")) multiplier = (uint64)1024 * 1024 * 1024;
178 else if(strstr(end, "MB") || strstr(end, "mb")) multiplier = (uint64)1024 * 1024;
179 else if(strstr(end, "KB") || strstr(end, "kb")) multiplier = 1024;
181 this = (uint64)(multiplier * value);
188 virtual File ::Open(char * archive, char * name, FileOpenMode mode);
191 virtual FileAttribs ::Exists(char * archive, char * fileName);
192 virtual bool ::GetSize(char * archive, char * fileName, FileSize * size);
193 virtual bool ::Stats(char * archive, char * fileName, FileStats stats);
194 virtual void ::FixCase(char * archive, char * fileName);
197 virtual bool ::Find(FileDesc file, char * archive, char * name);
198 virtual bool ::FindNext(FileDesc file);
199 virtual void ::CloseDir(FileDesc file);
201 // Archive manipulation
202 virtual Archive ::OpenArchive(char * fileName, ArchiveOpenFlags create);
203 virtual bool ::QuerySize(char * fileName, FileSize * size);
206 public enum FileOpenMode { read = 1, write, append, readWrite, writeRead, appendRead };
207 public enum FileSeekMode { start, current, end };
209 #if !defined(ECERE_BOOTSTRAP)
210 static FileDialog fileDialog { text = $"Select File" };
215 unlocked = 0, // LOCK_UN _SH_DENYNO
216 shared = 1, // LOCK_SH _SH_DENYWR
217 exclusive = 2 // LOCK_EX _SH_DENYRW
220 public class File : IOChannel
222 FILE * input, * output;
224 uint ReadData(byte * bytes, uint numBytes)
226 return Read(bytes, 1, numBytes);
229 uint WriteData(byte * bytes, uint numBytes)
231 return Write(bytes, 1, numBytes);
236 if(output && output != input)
250 bool OnGetDataFromString(char * string)
259 File f = FileOpen(string, read);
266 uint read = f.Read(buffer, 1, sizeof(buffer));
267 Write(buffer, 1, read);
276 char * OnGetString(char * tempString, void * fieldData, bool * needClass)
280 PrintSize(tempString, GetSize(), 2);
286 #ifndef ECERE_BOOTSTRAP
287 Window OnEdit(DataBox dataBox, DataBox obsolete, int x, int y, int w, int h, void * userData)
289 Window editData = class::OnEdit(dataBox, obsolete, x + 24, y, w - 48, h, userData);
292 dataBox, inactive = true, text = $"Imp", hotKey = f2,
293 position = { Max(x + 24, x + w - 24), y }, size = { 24, h };
295 bool DataBox::NotifyClicked(Button button, int x, int y, Modifiers mods)
297 fileDialog.master = rootWindow;
298 fileDialog.filePath = "";
299 fileDialog.type = open;
301 if(fileDialog.Modal() == ok)
303 char * filePath = fileDialog.filePath;
305 if(output.OnGetDataFromString(filePath))
307 SetData(output, false);
316 dataBox, inactive = true, text = $"Exp", hotKey = f2,
317 position = { Max(x + 24, x + w - 48), y }, size = { 24, h };
319 bool DataBox::NotifyClicked(Button button, int x, int y, Modifiers mods)
321 fileDialog.master = rootWindow;
322 fileDialog.type = save;
323 fileDialog.filePath = "";
324 if(fileDialog.Modal() == ok)
326 char * filePath = fileDialog.filePath;
327 File f = FileOpen(filePath, write);
330 File input = *(void **)data;
331 input.Seek(0, start);
335 uint read = input.Read(buffer, 1, sizeof(buffer));
336 f.Write(buffer, 1, read);
348 #endif //#ifndef ECERE_BOOTSTRAP
350 #if !defined(ECERE_VANILLA) && !defined(ECERE_NOARCHIVE) && !defined(ECERE_BOOTSTRAP)
351 void OnSerialize(IOChannel channel)
353 uint size = this ? GetSize() : MAXDWORD;
356 byte * uncompressed = new byte[size];
358 if(uncompressed || !size)
360 uint count = Read(uncompressed, 1, size);
363 uint cSize = size + size / 1000 + 12;
364 byte * compressed = new byte[cSize];
367 compress2(compressed, &cSize, uncompressed, size, 9);
369 size.OnSerialize(channel);
370 cSize.OnSerialize(channel);
371 channel.WriteData(compressed, cSize);
380 size.OnSerialize(channel);
385 size.OnSerialize(channel);
387 // Will add position...
391 for(c = 0; c<size; c += sizeof(data))
393 uint count = Read(data, 1, sizeof(data));
394 buffer.WriteData(data, count);
400 void OnUnserialize(IOChannel channel)
406 size.OnUnserialize(channel);
410 cSize.OnUnserialize(channel);
412 compressed = new byte[cSize];
415 if(channel.ReadData(compressed, cSize) == cSize)
417 byte * uncompressed = new byte[size];
418 if(uncompressed || !size)
421 uncompress(uncompressed, &size, compressed, cSize);
422 Write(uncompressed, 1, size);
436 size.OnUnserialize(channel);
440 for(c = 0; c<size; c += sizeof(data))
442 uint count = Min(size - c, sizeof(data));
443 channel.ReadData(data, count);
444 Write(data, 1, count);
457 virtual bool Seek(int pos, FileSeekMode mode)
459 uint fmode = SEEK_SET;
462 case start: fmode = SEEK_SET; break;
463 case end: fmode = SEEK_END; break;
464 case current: fmode = SEEK_CUR; break;
466 return fseek(input ? input : output, pos, fmode) != EOF;
469 virtual uint Tell(void)
471 return input ? ftell(input) : ftell(output);
474 virtual int Read(void * buffer, uint size, uint count)
476 return input ? (int)fread(buffer, size, count, input) : 0;
479 virtual int Write(void * buffer, uint size, uint count)
481 return output ? (int)fwrite(buffer, size, count, output) : 0;
485 virtual bool Getc(char * ch)
487 int ich = fgetc(input);
490 if(ch) *ch = (char)ich;
496 virtual bool Putc(char ch)
498 return (fputc((int)ch, output) == EOF) ? false : true;
501 virtual bool Puts(const char * string)
506 result = (fputs(string, output) == EOF) ? false : true;
507 // TODO: Check if any repercusions of commenting out fflush here
508 // This is what broke the debugger in 0.44d2 , it is required for outputting things to the DualPipe
509 // Added an explicit flush call in DualPipe::Puts
515 virtual bool Eof(void)
517 return input ? feof(input) : true;
520 virtual bool Truncate(FileSize size)
522 #ifdef ECERE_BOOTSTRAP
523 fprintf(stderr, "WARNING: File::Truncate unimplemented in ecereBootstrap.\n");
526 #if defined(__WIN32__)
527 return output ? (_chsize(fileno(output), size) == 0) : false;
529 return output ? (ftruncate(fileno(output), size) == 0) : false;
534 virtual uint GetSize(void)
536 return FILE_GetSize(input);
539 virtual void CloseInput(void)
550 virtual void CloseOutput(void)
561 virtual bool Lock(FileLock type, uint64 start, uint64 length, bool wait)
563 return FILE_Lock(input, output, type, start, length, wait);
566 virtual bool Unlock(uint64 start, uint64 length, bool wait)
568 return Lock(unlocked, start, length, wait);
572 int Printf(char * format, ...)
577 char text[MAX_F_STRING];
579 va_start(args, format);
580 vsnprintf(text, sizeof(text), format, args);
581 text[sizeof(text)-1] = 0;
583 result = strlen(text);
589 public void PrintLn(typed_object object, ...)
593 va_start(args, object);
594 PrintStdArgsToBuffer(buffer, sizeof(buffer), object, args);
600 public void Print(typed_object object, ...)
604 va_start(args, object);
605 PrintStdArgsToBuffer(buffer, sizeof(buffer), object, args);
616 bool GetLine(char *s, int max)
632 if(/*!Peek() || */ !Getc(&ch))
644 return result || c > 1;
647 // Strings and numbers separated by spaces, commas, tabs, or CR/LF, handling quotes
648 bool GetString(char * string, int max)
660 if( (ch!='\n') && (ch!='\r') && (ch!=' ') && (ch!=',') && (ch!='\t'))
666 for(c=0; c<max-1; c++)
668 if(!quoted && ((ch=='\n')||(ch=='\r')||(ch==' ')||(ch==',')||(ch=='\t')))
696 GetString(string,sizeof(string));
700 unsigned int GetHexValue(void)
703 GetString(string, sizeof(string));
704 return strtoul(string, null, 16);
710 GetString(string, sizeof(string));
711 return (float)FloatFromString(string);
714 double GetDouble(void)
717 GetString(string, sizeof(string));
718 return FloatFromString(string);
721 property void * input { set { input = value; } get { return input; } }
722 property void * output { set { output = value; } get { return output; } }
723 property bool buffered
727 FILE_set_buffered(input, output, value);
730 property bool eof { get { return Eof(); } }
732 int GetLineEx(char *s, int max, bool *hasNewLineChar)
742 if(/*!Peek() || */ !Getc(&ch))
750 *hasNewLineChar = (ch == '\n');
756 bool CopyTo(char * outputFileName)
759 File f = FileOpen(outputFileName, write);
768 uint count = Read(buffer, 1, sizeof(buffer));
769 if(count && !f.Write(buffer, 1, count))
782 virtual bool Open(char * fileName, FileOpenMode mode)
787 FILE_FileOpen(fileName, mode, &input, &output);
790 if(!input && !output);
795 // TESTING ENABLING FILE BUFFERING BY DEFAULT... DOCUMENT ANY ISSUE
798 setvbuf(file.input, null, _IONBF, 0);
800 setvbuf(file.output, null, _IONBF, 0);
806 LogErrorCode((mode == Read || mode == ReadWrite) ?
807 ERR_FILE_NOT_FOUND : ERR_FILE_WRITE_FAILED, fileName);
822 public class ConsoleFile : File
825 output = eC_stdout();
833 public class FileAttribs : bool
836 bool isFile:1, isArchive:1, isHidden:1, isReadOnly:1, isSystem:1, isTemporary:1, isDirectory:1;
837 bool isDrive:1, isCDROM:1, isRemote:1, isRemovable:1, isServer:1, isShare:1;
838 // property bool { };
841 public struct FileStats
845 SecSince1970 accessed;
846 SecSince1970 modified;
847 SecSince1970 created;
850 #if defined(__WIN32__)
852 // --- FileName functions ---
854 default TimeStamp Win32FileTimeToTimeStamp(FILETIME * fileTime)
856 // TIME_ZONE_INFORMATION tz = { 0 };
860 FileTimeToSystemTime(fileTime, <);
863 GetTimeZoneInformation(&tz);
865 _TzSpecificLocalTimeToSystemTime(&tz, <, &st);
870 t.month = (Month)(st.wMonth - 1);
873 t.minute = st.wMinute;
874 t.second = st.wSecond;
878 default void TimeStampToWin32FileTime(TimeStamp t, FILETIME * fileTime)
880 // TIME_ZONE_INFORMATION tz = { 0 };
886 st.wYear = (short)tm.year;
887 st.wMonth = (short)tm.month + 1;
888 st.wDay = (short)tm.day;
889 st.wHour = (short)tm.hour;
890 st.wMinute = (short)tm.minute;
891 st.wSecond = (short)tm.second;
892 st.wMilliseconds = 0;
896 GetTimeZoneInformation(&tz);
898 SystemTimeToTzSpecificLocalTime(&tz, &st, <);
902 SystemTimeToFileTime(<, fileTime);
905 default TimeStamp Win32FileTimeToTimeStamp(FILETIME * fileTime);
906 default void TimeStampToWin32FileTime(TimeStamp t, FILETIME * fileTime);
908 default bool WinReviveNetworkResource(uint16 * _wfileName);
912 public FileAttribs FileExists(char * fileName)
914 char archiveName[MAX_LOCATION], * archiveFile;
915 #if !defined(ECERE_BOOTSTRAP)
916 if(SplitArchivePath(fileName, archiveName, &archiveFile))
918 return EARFileSystem::Exists(archiveName, archiveFile);
920 else if(strstr(fileName, "http://") == fileName)
922 return FileAttribs { isFile = true };
926 return FILE_FileExists(fileName);
929 static int openCount;
931 public File FileOpen(char * fileName, FileOpenMode mode)
936 char archiveName[MAX_LOCATION], * archiveFile;
937 #if !defined(ECERE_BOOTSTRAP)
938 if(SplitArchivePath(fileName, archiveName, &archiveFile))
940 result = EARFileSystem::Open(archiveName, archiveFile, mode);
942 #if !defined(ECERE_VANILLA) && !defined(ECERE_NONET)
943 else if(strstr(fileName, "http://") == fileName)
945 result = FileOpenURL(fileName);
950 if(strstr(fileName, "File://") == fileName)
952 result = (File)strtoul(fileName+7, null, 16);
955 if(result._class && eClass_IsDerived(result._class, class(File)))
957 if(!result._refCount) incref result;
959 result.Seek(0, start);
970 FILE_FileOpen(fileName, mode, &file.input, &file.output);
973 if(!file.input && !file.output);
978 // TESTING ENABLING FILE BUFFERING BY DEFAULT... DOCUMENT ANY ISSUE
981 setvbuf(file.input, null, _IONBF, 0);
983 setvbuf(file.output, null, _IONBF, 0);
990 LogErrorCode((mode == Read || mode == ReadWrite) ?
991 ERR_FILE_NOT_FOUND : ERR_FILE_WRITE_FAILED, fileName);
1000 public void FileFixCase(char * file)
1002 FILE_FileFixCase(file);
1005 #if !defined(ECERE_BOOTSTRAP)
1006 public bool FileTruncate(char * fileName, FileSize size)
1008 #if defined(__WIN32__)
1009 uint16 * _wfileName = UTF8toUTF16(fileName, null);
1010 int f = _wopen(_wfileName, _O_RDWR|_O_CREAT, _S_IREAD|_S_IWRITE);
1011 bool result = false;
1014 if(!_chsize(f, size))
1021 return truncate(fileName, size) == 0;
1026 public bool FileGetSize(char * fileName, FileSize * size)
1028 bool result = false;
1034 #if !defined(ECERE_BOOTSTRAP)
1035 char archiveName[MAX_LOCATION], * archiveFile;
1036 if(SplitArchivePath(fileName, archiveName, &archiveFile))
1037 return EARFileSystem::GetSize(archiveName, archiveFile, size);
1040 result = FILE_FileGetSize(fileName, size);
1046 public bool FileGetStats(char * fileName, FileStats stats)
1048 bool result = false;
1049 if(stats && fileName)
1051 #if !defined(ECERE_BOOTSTRAP)
1052 char archiveName[MAX_LOCATION], * archiveFile;
1053 if(SplitArchivePath(fileName, archiveName, &archiveFile))
1054 result = EARFileSystem::Stats(archiveName, archiveFile, stats);
1057 return FILE_FileGetStats(fileName, stats);
1062 #ifndef ECERE_BOOTSTRAP
1064 public bool FileSetAttribs(char * fileName, FileAttribs attribs)
1067 uint winAttribs = 0;
1068 uint16 * _wfileName = UTF8toUTF16(fileName, null);
1070 if(attribs.isHidden) winAttribs |= FILE_ATTRIBUTE_HIDDEN;
1071 if(attribs.isReadOnly) winAttribs |= FILE_ATTRIBUTE_READONLY;
1073 SetFileAttributes(_wfileName, winAttribs);
1079 public bool FileSetTime(char * fileName, TimeStamp created, TimeStamp accessed, TimeStamp modified)
1081 bool result = false;
1082 TimeStamp currentTime = time(null);
1083 if(!created) created = currentTime;
1084 if(!accessed) accessed = currentTime;
1085 if(!modified) modified = currentTime;
1089 uint16 * _wfileName = UTF8toUTF16(fileName, null);
1090 HANDLE hFile = CreateFile(_wfileName, GENERIC_WRITE|GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, null,
1091 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, null);
1093 if(hFile != INVALID_HANDLE_VALUE)
1097 TimeStampToWin32FileTime(created, &c);
1098 TimeStampToWin32FileTime(accessed, &a);
1099 TimeStampToWin32FileTime(modified, &m);
1105 cc = Win32FileTimeToTimeStamp(&c);
1106 aa = Win32FileTimeToTimeStamp(&a);
1107 mm = Win32FileTimeToTimeStamp(&m);
1111 if(SetFileTime(hFile, &c, &a, &m))
1117 struct utimbuf t = { (int)accessed, (int)modified };
1118 if(!utime(fileName, &t))
1125 /****************************************************************************
1127 ****************************************************************************/
1128 // Directory Description for file listing
1129 private class Dir : struct
1131 #if defined(__WIN32__)
1135 NETRESOURCE * resources;
1139 NETRESOURCE * workGroups;
1144 char name[MAX_LOCATION];
1147 static FileDesc FileFind(char * path, char * extensions)
1149 FileDesc result = null;
1152 if((file = FileDesc {}))
1154 char archiveName[MAX_LOCATION], * archiveFile;
1155 if(SplitArchivePath(path, archiveName, &archiveFile))
1157 if(EARFileSystem::Find(file, archiveName, archiveFile))
1159 file.system = class(EARFileSystem);
1167 if((d = file.dir = Dir {}))
1169 #if defined(__WIN32__)
1170 if(!strcmp(path, "/"))
1173 d.fHandle = (void *)0xFFFFFFFF; //GetLogicalDrives();
1174 for(c = 0; c<26; c++)
1175 if(((uint)d.fHandle) & (1<<c))
1177 char volume[MAX_FILENAME] = "";
1178 uint16 _wvolume[MAX_FILENAME];
1180 uint16 _wfilePath[4];
1182 strcpy(d.name, path);
1183 file.stats.attribs = FileAttribs { isDirectory = true, isDrive = true };
1184 _wfilePath[0] = file.path[0] = (char)('A' + c);
1185 _wfilePath[1] = file.path[1] = ':';
1186 _wfilePath[2] = file.path[2] = '\\';
1187 _wfilePath[3] = file.path[3] = '\0';
1188 file.stats.size = 0;
1189 file.stats.accessed = file.stats.created = file.stats.modified = 0;
1190 driveType = GetDriveType(_wfilePath);
1193 case DRIVE_REMOVABLE: file.stats.attribs.isRemovable = true; break;
1194 case DRIVE_REMOTE: file.stats.attribs.isRemote = true; break;
1195 case DRIVE_CDROM: file.stats.attribs.isCDROM = true; break;
1197 *((uint *)&d.fHandle) ^= (1<<c);
1198 if(driveType == DRIVE_NO_ROOT_DIR) continue;
1200 if(driveType != DRIVE_REMOVABLE && driveType != DRIVE_REMOTE &&
1201 GetVolumeInformation(_wfilePath, _wvolume, MAX_FILENAME - 1, null, null, null, null, 0))
1203 file.path[2] = '\0';
1204 UTF16toUTF8Buffer(_wvolume, volume, MAX_FILENAME);
1205 sprintf(file.name, "%s [%s]", file.path, volume);
1209 file.path[2] = '\0';
1210 strcpy(file.name, file.path);
1217 else if(path[0] != '\\' || path[1] != '\\' || strstr(path+2, "\\"))
1219 WIN32_FIND_DATA winFile;
1220 uint16 dir[MAX_PATH];
1222 UTF8toUTF16Buffer(path, dir, MAX_LOCATION);
1223 if(path[0]) wcscat(dir, L"\\");
1224 wcscat(dir, L"*.*");
1226 d.fHandle = FindFirstFile(dir, &winFile);
1227 if(d.fHandle == INVALID_HANDLE_VALUE && WinReviveNetworkResource(dir))
1228 d.fHandle = FindFirstFile(dir, &winFile);
1229 if(d.fHandle != INVALID_HANDLE_VALUE)
1231 UTF16toUTF8Buffer(winFile.cFileName, file.name, MAX_FILENAME);
1232 strcpy(file.path, path);
1233 PathCat(file.path, file.name);
1235 strcat(file.path, DIR_SEPS);
1236 strcat(file.path, file.name);*/
1237 // file.sizeHigh = winFile.nFileSizeHigh;
1238 file.stats.size = winFile.nFileSizeLow;
1240 file.stats.attribs = FileAttribs { };
1241 file.stats.attribs.isArchive = (winFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) ? true : false;
1242 file.stats.attribs.isHidden = (winFile.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) ? true : false;
1243 file.stats.attribs.isReadOnly = (winFile.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? true : false;
1244 file.stats.attribs.isSystem = (winFile.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) ? true : false;
1245 file.stats.attribs.isTemporary = (winFile.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) ? true : false;
1246 file.stats.attribs.isDirectory = (winFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? true : false;
1247 file.stats.attribs.isFile = !(winFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
1248 strcpy(d.name, path);
1250 file.stats.accessed = Win32FileTimeToTimeStamp(&winFile.ftLastAccessTime);
1251 file.stats.modified = Win32FileTimeToTimeStamp(&winFile.ftLastWriteTime);
1252 file.stats.created = Win32FileTimeToTimeStamp(&winFile.ftCreationTime);
1259 int count = 0xFFFFFFFF;
1260 uint size = 512 * sizeof(NETRESOURCE);
1261 NETRESOURCE * buffer = (NETRESOURCE *)new0 byte[size];
1262 NETRESOURCE nr = {0};
1265 nr.dwScope = RESOURCE_GLOBALNET;
1266 nr.dwType = RESOURCETYPE_DISK;
1267 nr.lpProvider = L"Microsoft Windows Network";
1269 strcpy(d.name, path);
1272 nr.lpRemoteName = UTF8toUTF16(path, null);
1275 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &nr, &handle);
1278 WinReviveNetworkResource(nr.lpRemoteName);
1279 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &nr, &handle);
1286 int returnCode = WNetEnumResource(handle, &count, buffer, &size);
1287 if(returnCode != ERROR_MORE_DATA)
1290 buffer = (NETRESOURCE *)renew0 buffer byte[size];
1292 WNetCloseEnum(handle);
1295 delete nr.lpRemoteName;
1298 file.stats.attribs = FileAttribs { isDirectory = true, isShare = true };
1299 file.stats.size = 0;
1300 file.stats.accessed = file.stats.created = file.stats.modified = 0;
1302 UTF16toUTF8Buffer(buffer->lpRemoteName, file.path, MAX_LOCATION);
1303 GetLastDirectory(file.path, file.name);
1306 d.resources = buffer;
1307 d.numResources = count;
1316 nr.lpProvider = L"Microsoft Windows Network";
1319 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &nr, &handle);
1322 int returnCode = WNetEnumResource(handle, &count, buffer, &size);
1323 if(returnCode != ERROR_MORE_DATA)
1326 buffer = (NETRESOURCE *)renew0 buffer byte[size];
1328 WNetCloseEnum(handle);
1330 for(c = 0; c<count; c++)
1332 NETRESOURCE * resources;
1333 int countInGroup = 0xFFFFFFFF;
1335 size = 512 * sizeof(NETRESOURCE);
1336 resources = (NETRESOURCE *)new0 byte[size];
1339 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &buffer[c], &handle);
1342 int returnCode = WNetEnumResource(handle, &countInGroup, resources, &size);
1343 if(returnCode != ERROR_MORE_DATA)
1345 countInGroup = 0xFFFFFFFF;
1346 resources = (NETRESOURCE *)renew0 resources byte[size];
1348 WNetCloseEnum(handle);
1352 file.stats.attribs = FileAttribs { isDirectory = true, isServer = true };
1353 file.stats.size = 0;
1354 file.stats.accessed = file.stats.created = file.stats.modified = 0;
1356 UTF16toUTF8Buffer(resources->lpRemoteName, file.path, MAX_LOCATION);
1358 file.path[2] = (char)toupper(file.path[2]);
1359 GetLastDirectory(file.path, file.name);
1363 d.resources = resources;
1364 d.numResources = countInGroup;
1367 d.workGroups = buffer;
1368 d.numWorkGroups = count;
1375 if(c >= count && buffer) delete buffer;
1382 d.d = opendir((path && path[0]) ? path : ".");
1383 if(d.d && (de = readdir(d.d)))
1387 strcpy(file.path, path);
1389 strcat(file.path, DIR_SEPS);
1391 strcpy(file.name,de->d_name);
1392 strcat(file.path, file.name);
1393 if(!stat(file.path, &s))
1395 file.stats.attribs = (s.st_mode&S_IFDIR) ? FileAttribs { isDirectory = true } : FileAttribs { isFile = true };
1396 file.stats.size = s.st_size;
1397 file.stats.accessed = s.st_atime;
1398 file.stats.modified = s.st_mtime;
1399 file.stats.created = s.st_ctime;
1401 strcpy(d.name, path);
1416 while(result && !result.Validate(extensions))
1417 result = result.FindNext(extensions);
1422 private class FileDesc : struct
1425 char name[MAX_FILENAME];
1426 char path[MAX_LOCATION];
1428 subclass(FileSystem) system;
1431 bool Validate(char * extensions)
1433 if(strcmp(name, "..") && strcmp(name, ".") && strcmp(name, ""))
1435 if(extensions && !stats.attribs.isDirectory)
1437 char extension[MAX_EXTENSION], compared[MAX_EXTENSION];
1440 GetExtension(name, extension);
1441 for(c = 0; extensions[c];)
1445 for(;(ch = extensions[c]) && !IS_ALUNDER(ch); c++);
1446 for(;(ch = extensions[c]) && IS_ALUNDER(ch); c++)
1447 compared[len++] = ch;
1448 compared[len] = '\0';
1450 if(!strcmpi(extension, compared))
1460 FileDesc FindNext(char * extensions)
1462 FileDesc result = null;
1468 while(!Validate(extensions))
1474 if(system.FindNext(this))
1481 #if defined(__WIN32__)
1482 if(!strcmp(d.name, "/"))
1485 for(c = 0; c<26; c++)
1487 if(((uint)d.fHandle) & (1<<c))
1489 char volume[MAX_FILENAME] = "";
1492 uint16 _wvolume[MAX_FILENAME];
1494 stats.attribs = FileAttribs { isDirectory = true, isDrive = true };
1496 stats.accessed = stats.created = stats.modified = 0;
1497 _wpath[0] = path[0] = (char)('A' + c);
1498 _wpath[1] = path[1] = ':';
1499 _wpath[2] = path[2] = '\\';
1500 _wpath[3] = path[3] = 0;
1501 driveType = GetDriveType(_wpath);
1502 *((uint *)&d.fHandle) ^= (1<<c);
1506 case DRIVE_REMOVABLE: stats.attribs.isRemovable = true; break;
1507 case DRIVE_REMOTE: stats.attribs.isRemote = true; break;
1508 case DRIVE_CDROM: stats.attribs.isCDROM = true; break;
1510 if(driveType == DRIVE_NO_ROOT_DIR)
1512 uint16 remoteName[1024];
1517 status = WNetGetConnection(_wpath, remoteName, &size);
1518 if(status != ERROR_CONNECTION_UNAVAIL)
1525 if(driveType != DRIVE_REMOVABLE && driveType != DRIVE_REMOTE &&
1526 GetVolumeInformation(_wpath, _wvolume, MAX_FILENAME - 1, null, null, null, null, 0))
1528 UTF16toUTF8Buffer(_wvolume, volume, MAX_FILENAME);
1530 sprintf(name, "%s [%s]", path, volume);
1543 else if(d.name[0] != '\\' || d.name[1] != '\\' || strstr(d.name+2, "\\"))
1545 WIN32_FIND_DATA winFile;
1546 if(FindNextFile(d.fHandle, &winFile))
1548 UTF16toUTF8Buffer(winFile.cFileName, name, MAX_FILENAME);
1549 stats.attribs = FileAttribs { };
1550 stats.attribs.isArchive = (winFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) ? true : false;
1551 stats.attribs.isHidden = (winFile.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) ? true : false;
1552 stats.attribs.isReadOnly = (winFile.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? true : false;
1553 stats.attribs.isSystem = (winFile.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) ? true : false;
1554 stats.attribs.isTemporary = (winFile.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) ? true : false;
1555 stats.attribs.isDirectory = (winFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? true : false;
1556 stats.attribs.isFile = !(winFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
1557 stats.size = winFile.nFileSizeLow;
1559 stats.accessed = Win32FileTimeToTimeStamp(&winFile.ftLastAccessTime);
1560 stats.modified = Win32FileTimeToTimeStamp(&winFile.ftLastWriteTime);
1561 stats.created = Win32FileTimeToTimeStamp(&winFile.ftCreationTime);
1563 strcpy(path, d.name);
1564 PathCat(path, name);
1566 strcat(path, DIR_SEPS);
1567 strcat(path, name);*/
1577 if(d.resource < d.numResources)
1579 stats.attribs = FileAttribs { isDirectory = true, isShare = true };
1581 stats.accessed = stats.created = stats.modified = 0;
1583 UTF16toUTF8Buffer(d.resources[d.resource].lpRemoteName, path, MAX_LOCATION);
1584 GetLastDirectory(path, name);
1599 for(c = d.workGroup; c<d.numWorkGroups; c++)
1601 if(c != d.workGroup)
1603 int countInGroup = 0xFFFFFFFF;
1605 NETRESOURCE * resources;
1606 uint size = 512 * sizeof(NETRESOURCE);
1608 resources = (NETRESOURCE *)new0 byte[size];
1610 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &d.workGroups[c], &handle);
1613 int returnCode = WNetEnumResource(handle, &countInGroup, resources, &size);
1614 if(returnCode != ERROR_MORE_DATA)
1616 countInGroup = 0xFFFFFFFF;
1617 resources = (NETRESOURCE *)renew0 resources byte[size];
1620 WNetCloseEnum(handle);
1621 d.numResources = countInGroup;
1622 d.resources = resources;
1626 if(d.resource < d.numResources)
1628 stats.attribs = FileAttribs { isDirectory = true, isServer = true };
1630 stats.accessed = stats.created = stats.modified = 0;
1632 UTF16toUTF8Buffer(d.resources[d.resource].lpRemoteName, path, MAX_LOCATION);
1634 path[2] = (char)toupper(path[2]);
1635 GetLastDirectory(path, name);
1649 if(d.workGroup == d.numWorkGroups && d.resource == d.numResources)
1651 delete d.workGroups;
1663 strcpy(name,de->d_name);
1664 strcpy(path, d.name);
1665 if(d.name[0] && d.name[1])
1666 strcat(path, DIR_SEPS);
1670 stats.attribs = FileAttribs { };
1671 stats.attribs = (s.st_mode&S_IFDIR) ? FileAttribs { isDirectory = true } : FileAttribs { isFile = true };
1672 stats.size = s.st_size;
1673 stats.accessed = s.st_atime;
1674 stats.modified = s.st_mtime;
1675 stats.created = s.st_ctime;
1692 system.CloseDir(this);
1698 #if defined(__WIN32__)
1699 if(d.fHandle && strcmp(d.name, "/"))
1700 FindClose(d.fHandle);
1711 public struct FileListing
1719 bool result = false;
1721 desc = desc.FindNext(extensions);
1723 desc = FileFind(directory, extensions);
1736 property char * name { get { return (char *)(desc ? desc.name : null); } };
1737 property char * path { get { return (char *)(desc ? desc.path : null); } };
1738 property FileStats stats { get { value = desc ? desc.stats : FileStats { }; } };
1745 public File CreateTemporaryFile(char * tempFileName, char * template)
1747 #ifndef ECERE_BOOTSTRAP // quick fix for now
1749 #if defined(__unix__) || defined(__APPLE__)
1750 char buffer[MAX_FILENAME];
1752 strcpy(buffer, "/tmp/");
1753 strcat(buffer, template);
1754 //strcpy(buffer, template);
1755 strcat(buffer, "XXXXXX");
1757 fd = mkstemp(buffer);
1758 strcpy(tempFileName, buffer);
1760 f.output = f.input = fdopen(fd, "r+");
1762 char tempPath[MAX_LOCATION];
1763 GetTempPathA(MAX_LOCATION, tempPath); // TODO: Patch this whole thing to support Unicode temp path
1764 GetTempFileNameA(tempPath, template, 0, tempFileName);
1765 f = FileOpen(tempFileName, readWrite);
1773 public void CreateTemporaryDir(char * tempFileName, char * template)
1775 #ifndef ECERE_BOOTSTRAP // quick fix for now
1776 #if defined(__unix__) || defined(__APPLE__)
1777 char buffer[MAX_FILENAME];
1778 strcpy(buffer, "/tmp/");
1779 strcat(buffer, template);
1780 //strcpy(buffer, template);
1781 strcat(buffer, "XXXXXX");
1784 strcpy(tempFileName, buffer);
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 DeleteFile(tempFileName);
1790 MakeDir(tempFileName);
1795 public void MakeSlashPath(char * p)
1799 ChangeCh(p, '\\', '/');
1803 public void MakeSystemPath(char * p)
1808 public char * CopySystemPath(char * p)
1810 char * d = CopyString(p);
1816 public char * CopyUnixPath(char * p)
1818 char * d = CopyString(p);
1824 public char * GetSystemPathBuffer(char * d, char * p)
1827 strcpy(d, p ? p : "");
1832 public char * GetSlashPathBuffer(char * d, char * p)
1835 strcpy(d, p ? p : "");