13 #define IS_ALUNDER(ch) ((ch) == '_' || isalnum((ch)))
15 #if defined(ECERE_BOOTSTRAP)
22 #ifndef ECERE_BOOTSTRAP
23 #if defined(__GNUC__) || defined(__WATCOMC__) || defined(__WIN32__)
25 #include <sys/types.h>
30 #if defined(__unix__) || defined(__APPLE__)
34 #if defined(__WIN32__) || defined(__WATCOMC__)
40 #if defined(__WIN32__)
41 #define WIN32_LEAN_AND_MEAN
45 BOOL WINAPI GetVolumePathName(LPCTSTR lpszFileName,LPTSTR lpszVolumePathName,DWORD cchBufferLength);
47 // Missing function...
49 #ifndef WNetGetResourceInformation
50 DWORD APIENTRY WNetGetResourceInformationA(LPNETRESOURCE lpNetResource, LPVOID lpBuffer, LPDWORD lpcbBuffer, LPTSTR* lplpSystem);
52 #define WNetGetResourceInformation WNetGetResourceInformationW
54 #define WNetGetResourceInformation WNetGetResourceInformationA
66 #endif //#ifndef ECERE_BOOTSTRAP
74 #if !defined(ECERE_VANILLA) && !defined(ECERE_NONET) && !defined(ECERE_BOOTSTRAP)
80 // IMPLEMENTATION OF THESE IS IN _File.c
83 uint FILE_GetSize(FILE * input);
84 bool FILE_Lock(FILE * input, FILE * output, FileLock type, uint64 start, uint64 length, bool wait);
85 void FILE_set_buffered(FILE * input, FILE * output, bool value);
86 FileAttribs FILE_FileExists(char * fileName);
87 bool FILE_FileGetSize(char * fileName, FileSize * size);
88 bool FILE_FileGetStats(char * fileName, FileStats stats);
89 void FILE_FileFixCase(char * file);
90 void FILE_FileOpen(char * fileName, FileOpenMode mode, FILE ** input, FILE **output);
94 FileSystem httpFileSystem;
96 public class FileSize : uint
98 // defaultAlignment = Right;
100 void OnDisplay(Surface surface, int x, int y, int width, void * fieldData, int alignment, DataDisplayFlags displayFlags)
104 eUtils_PrintSize(string, *size, 2);
105 len = strlen(string);
106 surface.WriteTextDots(alignment, x, y, width, string, len);
109 int OnCompare(FileSize data2)
116 else if(this < data2)
122 char * OnGetString(char * string, void * fieldData, bool * needClass)
124 PrintSize(string, this, 2);
128 bool OnGetDataFromString(char * string)
131 double value = strtod(string, &end);
133 if(strstr(end, "GB") || strstr(end, "gb")) multiplier = (uint)1024 * 1024 * 1024;
134 else if(strstr(end, "MB") || strstr(end, "mb")) multiplier = (uint)1024 * 1024;
135 else if(strstr(end, "KB") || strstr(end, "kb")) multiplier = 1024;
137 this = (uint)(multiplier * value);
144 virtual File ::Open(char * archive, char * name, FileOpenMode mode);
147 virtual FileAttribs ::Exists(char * archive, char * fileName);
148 virtual bool ::GetSize(char * archive, char * fileName, FileSize * size);
149 virtual bool ::Stats(char * archive, char * fileName, FileStats stats);
150 virtual void ::FixCase(char * archive, char * fileName);
153 virtual bool ::Find(FileDesc file, char * archive, char * name);
154 virtual bool ::FindNext(FileDesc file);
155 virtual void ::CloseDir(FileDesc file);
157 // Archive manipulation
158 virtual Archive ::OpenArchive(char * fileName, ArchiveOpenFlags create);
159 virtual bool ::QuerySize(char * fileName, FileSize * size);
162 public enum FileOpenMode { read = 1, write, append, readWrite, writeRead, appendRead };
163 public enum FileSeekMode { start, current, end };
165 #if !defined(ECERE_BOOTSTRAP)
166 static FileDialog fileDialog { text = "Select File" };
171 unlocked = 0, // LOCK_UN _SH_DENYNO
172 shared = 1, // LOCK_SH _SH_DENYWR
173 exclusive = 2 // LOCK_EX _SH_DENYRW
176 public class File : IOChannel
178 FILE * input, * output;
180 uint ReadData(byte * bytes, uint numBytes)
182 return Read(bytes, 1, numBytes);
185 uint WriteData(byte * bytes, uint numBytes)
187 return Write(bytes, 1, numBytes);
192 if(output && output != input)
206 bool OnGetDataFromString(char * string)
215 File f = FileOpen(string, read);
222 uint read = f.Read(buffer, 1, sizeof(buffer));
223 Write(buffer, 1, read);
232 char * OnGetString(char * tempString, void * fieldData, bool * needClass)
236 PrintSize(tempString, GetSize(), 2);
242 #ifndef ECERE_BOOTSTRAP
243 Window OnEdit(DataBox dataBox, DataBox obsolete, int x, int y, int w, int h, void * userData)
245 Window editData = class::OnEdit(dataBox, obsolete, x + 24, y, w - 48, h, userData);
248 dataBox, inactive = true, text = "Imp", hotKey = f2,
249 position = { Max(x + 24, x + w - 24), y }, size = { 24, h };
251 bool DataBox::NotifyClicked(Button button, int x, int y, Modifiers mods)
253 fileDialog.master = rootWindow;
254 fileDialog.filePath = "";
255 fileDialog.type = open;
257 if(fileDialog.Modal() == ok)
259 char * filePath = fileDialog.filePath;
261 if(output.OnGetDataFromString(filePath))
263 SetData(output, false);
272 dataBox, inactive = true, text = "Exp", hotKey = f2,
273 position = { Max(x + 24, x + w - 48), y }, size = { 24, h };
275 bool DataBox::NotifyClicked(Button button, int x, int y, Modifiers mods)
277 fileDialog.master = rootWindow;
278 fileDialog.type = save;
279 fileDialog.filePath = "";
280 if(fileDialog.Modal() == ok)
282 char * filePath = fileDialog.filePath;
283 File f = FileOpen(filePath, write);
286 File input = *(void **)data;
287 input.Seek(0, start);
291 uint read = input.Read(buffer, 1, sizeof(buffer));
292 f.Write(buffer, 1, read);
304 #endif //#ifndef ECERE_BOOTSTRAP
306 #if !defined(ECERE_VANILLA) && !defined(ECERE_NOARCHIVE) && !defined(ECERE_BOOTSTRAP)
307 void OnSerialize(IOChannel channel)
309 uint size = this ? GetSize() : MAXDWORD;
312 byte * uncompressed = new byte[size];
314 if(uncompressed || !size)
316 uint count = Read(uncompressed, 1, size);
319 uint cSize = size + size / 1000 + 12;
320 byte * compressed = new byte[cSize];
323 compress2(compressed, &cSize, uncompressed, size, 9);
325 size.OnSerialize(channel);
326 cSize.OnSerialize(channel);
327 channel.WriteData(compressed, cSize);
336 size.OnSerialize(channel);
341 size.OnSerialize(channel);
343 // Will add position...
347 for(c = 0; c<size; c += sizeof(data))
349 uint count = Read(data, 1, sizeof(data));
350 buffer.WriteData(data, count);
356 void OnUnserialize(IOChannel channel)
362 size.OnUnserialize(channel);
366 cSize.OnUnserialize(channel);
368 compressed = new byte[cSize];
371 if(channel.ReadData(compressed, cSize) == cSize)
373 byte * uncompressed = new byte[size];
374 if(uncompressed || !size)
377 uncompress(uncompressed, &size, compressed, cSize);
378 Write(uncompressed, 1, size);
392 size.OnUnserialize(channel);
396 for(c = 0; c<size; c += sizeof(data))
398 uint count = Min(size - c, sizeof(data));
399 channel.ReadData(data, count);
400 Write(data, 1, count);
413 virtual bool Seek(int pos, FileSeekMode mode)
415 uint fmode = SEEK_SET;
418 case start: fmode = SEEK_SET; break;
419 case end: fmode = SEEK_END; break;
420 case current: fmode = SEEK_CUR; break;
422 return fseek(input ? input : output, pos, fmode) != EOF;
425 virtual uint Tell(void)
427 return input ? ftell(input) : ftell(output);
430 virtual int Read(void * buffer, uint size, uint count)
432 return input ? (int)fread(buffer, size, count, input) : 0;
435 virtual int Write(void * buffer, uint size, uint count)
437 return output ? (int)fwrite(buffer, size, count, output) : 0;
441 virtual bool Getc(char * ch)
443 int ich = fgetc(input);
446 if(ch) *ch = (char)ich;
452 virtual bool Putc(char ch)
454 return (fputc((int)ch, output) == EOF) ? false : true;
457 virtual bool Puts(const char * string)
462 result = (fputs(string, output) == EOF) ? false : true;
463 // TODO: Check if any repercusions of commenting out fflush here
464 // This is what broke the debugger in 0.44d2 , it is required for outputting things to the DualPipe
465 // Added an explicit flush call in DualPipe::Puts
471 virtual bool Eof(void)
473 return input ? feof(input) : true;
476 virtual bool Truncate(FileSize size)
478 #ifdef ECERE_BOOTSTRAP
479 fprintf(stderr, "WARNING: File::Truncate unimplemented in ecereBootstrap.\n");
482 #if defined(__WIN32__)
483 return output ? (_chsize(fileno(output), size) == 0) : false;
485 return output ? (ftruncate(fileno(output), size) == 0) : false;
490 virtual uint GetSize(void)
492 return FILE_GetSize(input);
495 virtual void CloseInput(void)
506 virtual void CloseOutput(void)
517 virtual bool Lock(FileLock type, uint64 start, uint64 length, bool wait)
519 return FILE_Lock(input, output, type, start, length, wait);
522 virtual bool Unlock(uint64 start, uint64 length, bool wait)
524 return Lock(unlocked, start, length, wait);
528 int Printf(char * format, ...)
531 char text[MAX_F_STRING];
533 va_start(args, format);
534 vsprintf(text, format, args);
536 result = strlen(text);
541 public void PrintLn(typed_object object, ...)
545 va_start(args, object);
546 PrintStdArgsToBuffer(buffer, sizeof(buffer), object, args);
552 public void Print(typed_object object, ...)
556 va_start(args, object);
557 PrintStdArgsToBuffer(buffer, sizeof(buffer), object, args);
568 bool GetLine(char *s, int max)
584 if(/*!Peek() || */ !Getc(&ch))
596 return result || c > 1;
599 // Strings and numbers separated by spaces, commas, tabs, or CR/LF, handling quotes
600 bool GetString(char * string, int max)
612 if( (ch!='\n') && (ch!='\r') && (ch!=' ') && (ch!=',') && (ch!='\t'))
618 for(c=0; c<max-1; c++)
620 if(!quoted && ((ch=='\n')||(ch=='\r')||(ch==' ')||(ch==',')||(ch=='\t')))
648 GetString(string,sizeof(string));
652 unsigned int GetHexValue(void)
655 GetString(string, sizeof(string));
656 return strtoul(string, null, 16);
662 GetString(string, sizeof(string));
663 return (float)FloatFromString(string);
666 double GetDouble(void)
669 GetString(string, sizeof(string));
670 return FloatFromString(string);
673 property void * input { set { input = value; } get { return input; } }
674 property void * output { set { output = value; } get { return output; } }
675 property bool buffered
679 FILE_set_buffered(input, output, value);
682 property bool eof { get { return Eof(); } }
684 int GetLineEx(char *s, int max, bool *hasNewLineChar)
694 if(/*!Peek() || */ !Getc(&ch))
702 *hasNewLineChar = (ch == '\n');
708 bool CopyTo(char * outputFileName)
711 File f = FileOpen(outputFileName, write);
720 uint count = Read(buffer, 1, sizeof(buffer));
721 if(count && !f.Write(buffer, 1, count))
734 virtual bool Open(char * fileName, FileOpenMode mode)
739 FILE_FileOpen(fileName, mode, &input, &output);
742 if(!input && !output);
747 // TESTING ENABLING FILE BUFFERING BY DEFAULT... DOCUMENT ANY ISSUE
750 setvbuf(file.input, null, _IONBF, 0);
752 setvbuf(file.output, null, _IONBF, 0);
758 LogErrorCode((mode == Read || mode == ReadWrite) ?
759 ERR_FILE_NOT_FOUND : ERR_FILE_WRITE_FAILED, fileName);
774 public class ConsoleFile : File
785 public class FileAttribs : bool
788 bool isFile:1, isArchive:1, isHidden:1, isReadOnly:1, isSystem:1, isTemporary:1, isDirectory:1;
789 bool isDrive:1, isCDROM:1, isRemote:1, isRemovable:1, isServer:1, isShare:1;
790 // property bool { };
793 public struct FileStats
797 SecSince1970 accessed;
798 SecSince1970 modified;
799 SecSince1970 created;
802 #if defined(__WIN32__)
804 // --- FileName functions ---
806 default TimeStamp Win32FileTimeToTimeStamp(FILETIME * fileTime)
808 // TIME_ZONE_INFORMATION tz = { 0 };
812 FileTimeToSystemTime(fileTime, <);
815 GetTimeZoneInformation(&tz);
817 _TzSpecificLocalTimeToSystemTime(&tz, <, &st);
822 t.month = (Month)(st.wMonth - 1);
825 t.minute = st.wMinute;
826 t.second = st.wSecond;
830 default void TimeStampToWin32FileTime(TimeStamp t, FILETIME * fileTime)
832 // TIME_ZONE_INFORMATION tz = { 0 };
838 st.wYear = (short)tm.year;
839 st.wMonth = (short)tm.month + 1;
840 st.wDay = (short)tm.day;
841 st.wHour = (short)tm.hour;
842 st.wMinute = (short)tm.minute;
843 st.wSecond = (short)tm.second;
844 st.wMilliseconds = 0;
848 GetTimeZoneInformation(&tz);
850 SystemTimeToTzSpecificLocalTime(&tz, &st, <);
854 SystemTimeToFileTime(<, fileTime);
857 default TimeStamp Win32FileTimeToTimeStamp(FILETIME * fileTime);
858 default void TimeStampToWin32FileTime(TimeStamp t, FILETIME * fileTime);
860 default bool WinReviveNetworkResource(uint16 * _wfileName);
864 public FileAttribs FileExists(char * fileName)
866 char archiveName[MAX_LOCATION], * archiveFile;
867 #if !defined(ECERE_BOOTSTRAP)
868 if(SplitArchivePath(fileName, archiveName, &archiveFile))
870 return EARFileSystem::Exists(archiveName, archiveFile);
872 else if(strstr(fileName, "http://") == fileName)
874 return FileAttribs { isFile = true };
878 return FILE_FileExists(fileName);
881 static int openCount;
883 public File FileOpen(char * fileName, FileOpenMode mode)
888 char archiveName[MAX_LOCATION], * archiveFile;
889 #if !defined(ECERE_BOOTSTRAP)
890 if(SplitArchivePath(fileName, archiveName, &archiveFile))
892 result = EARFileSystem::Open(archiveName, archiveFile, mode);
894 #if !defined(ECERE_VANILLA) && !defined(ECERE_NONET)
895 else if(strstr(fileName, "http://") == fileName)
897 result = FileOpenURL(fileName);
902 if(strstr(fileName, "File://") == fileName)
904 result = (File)strtoul(fileName+7, null, 16);
907 if(result._class && eClass_IsDerived(result._class, class(File)))
909 if(!result._refCount) incref result;
911 result.Seek(0, start);
922 FILE_FileOpen(fileName, mode, &file.input, &file.output);
925 if(!file.input && !file.output);
930 // TESTING ENABLING FILE BUFFERING BY DEFAULT... DOCUMENT ANY ISSUE
933 setvbuf(file.input, null, _IONBF, 0);
935 setvbuf(file.output, null, _IONBF, 0);
942 LogErrorCode((mode == Read || mode == ReadWrite) ?
943 ERR_FILE_NOT_FOUND : ERR_FILE_WRITE_FAILED, fileName);
952 public void FileFixCase(char * file)
954 FILE_FileFixCase(file);
957 #if !defined(ECERE_BOOTSTRAP)
958 public bool FileTruncate(char * fileName, FileSize size)
960 #if defined(__WIN32__)
961 uint16 * _wfileName = UTF8toUTF16(fileName, null);
962 int f = _wopen(_wfileName, _O_RDWR|_O_CREAT, _S_IREAD|_S_IWRITE);
966 if(!_chsize(f, size))
973 return truncate(fileName, size) == 0;
978 public bool FileGetSize(char * fileName, FileSize * size)
986 #if !defined(ECERE_BOOTSTRAP)
987 char archiveName[MAX_LOCATION], * archiveFile;
988 if(SplitArchivePath(fileName, archiveName, &archiveFile))
989 return EARFileSystem::GetSize(archiveName, archiveFile, size);
992 result = FILE_FileGetSize(fileName, size);
998 public bool FileGetStats(char * fileName, FileStats stats)
1000 bool result = false;
1001 if(stats && fileName)
1003 #if !defined(ECERE_BOOTSTRAP)
1004 char archiveName[MAX_LOCATION], * archiveFile;
1005 if(SplitArchivePath(fileName, archiveName, &archiveFile))
1006 result = EARFileSystem::Stats(archiveName, archiveFile, stats);
1009 return FILE_FileGetStats(fileName, stats);
1014 #ifndef ECERE_BOOTSTRAP
1016 public bool FileSetAttribs(char * fileName, FileAttribs attribs)
1019 uint winAttribs = 0;
1020 uint16 * _wfileName = UTF8toUTF16(fileName, null);
1022 if(attribs.isHidden) winAttribs |= FILE_ATTRIBUTE_HIDDEN;
1023 if(attribs.isReadOnly) winAttribs |= FILE_ATTRIBUTE_READONLY;
1025 SetFileAttributes(_wfileName, winAttribs);
1031 public bool FileSetTime(char * fileName, TimeStamp created, TimeStamp accessed, TimeStamp modified)
1033 bool result = false;
1034 TimeStamp currentTime = time(null);
1035 if(!created) created = currentTime;
1036 if(!accessed) accessed = currentTime;
1037 if(!modified) modified = currentTime;
1041 uint16 * _wfileName = UTF8toUTF16(fileName, null);
1042 HANDLE hFile = CreateFile(_wfileName, GENERIC_WRITE|GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, null,
1043 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, null);
1045 if(hFile != INVALID_HANDLE_VALUE)
1049 TimeStampToWin32FileTime(created, &c);
1050 TimeStampToWin32FileTime(accessed, &a);
1051 TimeStampToWin32FileTime(modified, &m);
1057 cc = Win32FileTimeToTimeStamp(&c);
1058 aa = Win32FileTimeToTimeStamp(&a);
1059 mm = Win32FileTimeToTimeStamp(&m);
1063 if(SetFileTime(hFile, &c, &a, &m))
1069 struct utimbuf t = { (int)accessed, (int)modified };
1070 if(!utime(fileName, &t))
1077 /****************************************************************************
1079 ****************************************************************************/
1080 // Directory Description for file listing
1081 private class Dir : struct
1083 #if defined(__WIN32__)
1087 NETRESOURCE * resources;
1091 NETRESOURCE * workGroups;
1096 char name[MAX_LOCATION];
1099 static FileDesc FileFind(char * path, char * extensions)
1101 FileDesc result = null;
1104 if((file = FileDesc {}))
1106 char archiveName[MAX_LOCATION], * archiveFile;
1107 if(SplitArchivePath(path, archiveName, &archiveFile))
1109 if(EARFileSystem::Find(file, archiveName, archiveFile))
1111 file.system = class(EARFileSystem);
1119 if((d = file.dir = Dir {}))
1121 #if defined(__WIN32__)
1122 if(!strcmp(path, "/"))
1125 d.fHandle = (void *)0xFFFFFFFF; //GetLogicalDrives();
1126 for(c = 0; c<26; c++)
1127 if(((uint)d.fHandle) & (1<<c))
1129 char volume[MAX_FILENAME] = "";
1130 uint16 _wvolume[MAX_FILENAME];
1132 uint16 _wfilePath[4];
1134 strcpy(d.name, path);
1135 file.stats.attribs = FileAttribs { isDirectory = true, isDrive = true };
1136 _wfilePath[0] = file.path[0] = (char)('A' + c);
1137 _wfilePath[1] = file.path[1] = ':';
1138 _wfilePath[2] = file.path[2] = '\\';
1139 _wfilePath[3] = file.path[3] = '\0';
1140 file.stats.size = 0;
1141 file.stats.accessed = file.stats.created = file.stats.modified = 0;
1142 driveType = GetDriveType(_wfilePath);
1145 case DRIVE_REMOVABLE: file.stats.attribs.isRemovable = true; break;
1146 case DRIVE_REMOTE: file.stats.attribs.isRemote = true; break;
1147 case DRIVE_CDROM: file.stats.attribs.isCDROM = true; break;
1149 *((uint *)&d.fHandle) ^= (1<<c);
1150 if(driveType == DRIVE_NO_ROOT_DIR) continue;
1152 if(driveType != DRIVE_REMOVABLE && driveType != DRIVE_REMOTE &&
1153 GetVolumeInformation(_wfilePath, _wvolume, MAX_FILENAME - 1, null, null, null, null, 0))
1155 file.path[2] = '\0';
1156 UTF16toUTF8Buffer(_wvolume, volume, MAX_FILENAME);
1157 sprintf(file.name, "%s [%s]", file.path, volume);
1161 file.path[2] = '\0';
1162 strcpy(file.name, file.path);
1169 else if(path[0] != '\\' || path[1] != '\\' || strstr(path+2, "\\"))
1171 WIN32_FIND_DATA winFile;
1172 uint16 dir[MAX_PATH];
1174 UTF8toUTF16Buffer(path, dir, MAX_LOCATION);
1175 if(path[0]) wcscat(dir, L"\\");
1176 wcscat(dir, L"*.*");
1178 d.fHandle = FindFirstFile(dir, &winFile);
1179 if(d.fHandle == INVALID_HANDLE_VALUE && WinReviveNetworkResource(dir))
1180 d.fHandle = FindFirstFile(dir, &winFile);
1181 if(d.fHandle != INVALID_HANDLE_VALUE)
1183 UTF16toUTF8Buffer(winFile.cFileName, file.name, MAX_FILENAME);
1184 strcpy(file.path, path);
1185 PathCat(file.path, file.name);
1187 strcat(file.path, DIR_SEPS);
1188 strcat(file.path, file.name);*/
1189 // file.sizeHigh = winFile.nFileSizeHigh;
1190 file.stats.size = winFile.nFileSizeLow;
1192 file.stats.attribs = FileAttribs { };
1193 file.stats.attribs.isArchive = (winFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) ? true : false;
1194 file.stats.attribs.isHidden = (winFile.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) ? true : false;
1195 file.stats.attribs.isReadOnly = (winFile.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? true : false;
1196 file.stats.attribs.isSystem = (winFile.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) ? true : false;
1197 file.stats.attribs.isTemporary = (winFile.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) ? true : false;
1198 file.stats.attribs.isDirectory = (winFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? true : false;
1199 file.stats.attribs.isFile = !(winFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
1200 strcpy(d.name, path);
1202 file.stats.accessed = Win32FileTimeToTimeStamp(&winFile.ftLastAccessTime);
1203 file.stats.modified = Win32FileTimeToTimeStamp(&winFile.ftLastWriteTime);
1204 file.stats.created = Win32FileTimeToTimeStamp(&winFile.ftCreationTime);
1211 int count = 0xFFFFFFFF;
1212 uint size = 512 * sizeof(NETRESOURCE);
1213 NETRESOURCE * buffer = (NETRESOURCE *)new0 byte[size];
1214 NETRESOURCE nr = {0};
1217 nr.dwScope = RESOURCE_GLOBALNET;
1218 nr.dwType = RESOURCETYPE_DISK;
1219 nr.lpProvider = L"Microsoft Windows Network";
1221 strcpy(d.name, path);
1224 nr.lpRemoteName = UTF8toUTF16(path, null);
1227 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &nr, &handle);
1230 WinReviveNetworkResource(nr.lpRemoteName);
1231 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &nr, &handle);
1238 int returnCode = WNetEnumResource(handle, &count, buffer, &size);
1239 if(returnCode != ERROR_MORE_DATA)
1242 buffer = (NETRESOURCE *)renew0 buffer byte[size];
1244 WNetCloseEnum(handle);
1247 delete nr.lpRemoteName;
1250 file.stats.attribs = FileAttribs { isDirectory = true, isShare = true };
1251 file.stats.size = 0;
1252 file.stats.accessed = file.stats.created = file.stats.modified = 0;
1254 UTF16toUTF8Buffer(buffer->lpRemoteName, file.path, MAX_LOCATION);
1255 GetLastDirectory(file.path, file.name);
1258 d.resources = buffer;
1259 d.numResources = count;
1268 nr.lpProvider = L"Microsoft Windows Network";
1271 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &nr, &handle);
1274 int returnCode = WNetEnumResource(handle, &count, buffer, &size);
1275 if(returnCode != ERROR_MORE_DATA)
1278 buffer = (NETRESOURCE *)renew0 buffer byte[size];
1280 WNetCloseEnum(handle);
1282 for(c = 0; c<count; c++)
1284 NETRESOURCE * resources;
1285 int countInGroup = 0xFFFFFFFF;
1287 size = 512 * sizeof(NETRESOURCE);
1288 resources = (NETRESOURCE *)new0 byte[size];
1291 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &buffer[c], &handle);
1294 int returnCode = WNetEnumResource(handle, &countInGroup, resources, &size);
1295 if(returnCode != ERROR_MORE_DATA)
1297 countInGroup = 0xFFFFFFFF;
1298 resources = (NETRESOURCE *)renew0 resources byte[size];
1300 WNetCloseEnum(handle);
1304 file.stats.attribs = FileAttribs { isDirectory = true, isServer = true };
1305 file.stats.size = 0;
1306 file.stats.accessed = file.stats.created = file.stats.modified = 0;
1308 UTF16toUTF8Buffer(resources->lpRemoteName, file.path, MAX_LOCATION);
1310 file.path[2] = (char)toupper(file.path[2]);
1311 GetLastDirectory(file.path, file.name);
1315 d.resources = resources;
1316 d.numResources = countInGroup;
1319 d.workGroups = buffer;
1320 d.numWorkGroups = count;
1327 if(c >= count && buffer) delete buffer;
1334 d.d = opendir((path && path[0]) ? path : ".");
1335 if(d.d && (de = readdir(d.d)))
1339 strcpy(file.path, path);
1341 strcat(file.path, DIR_SEPS);
1343 strcpy(file.name,de->d_name);
1344 strcat(file.path, file.name);
1345 stat(file.path, &s);
1346 file.stats.attribs = (s.st_mode&S_IFDIR) ? FileAttribs { isDirectory = true } : FileAttribs { isFile = true };
1347 file.stats.size = s.st_size;
1348 file.stats.accessed = s.st_atime;
1349 file.stats.modified = s.st_mtime;
1350 file.stats.created = s.st_ctime;
1352 strcpy(d.name, path);
1367 while(result && !result.Validate(extensions))
1368 result = result.FindNext(extensions);
1373 private class FileDesc : struct
1376 char name[MAX_FILENAME];
1377 char path[MAX_LOCATION];
1379 subclass(FileSystem) system;
1382 bool Validate(char * extensions)
1384 if(strcmp(name, "..") && strcmp(name, ".") && strcmp(name, ""))
1386 if(extensions && !stats.attribs.isDirectory)
1388 char extension[MAX_EXTENSION], compared[MAX_EXTENSION];
1391 GetExtension(name, extension);
1392 for(c = 0; extensions[c];)
1396 for(;(ch = extensions[c]) && !IS_ALUNDER(ch); c++);
1397 for(;(ch = extensions[c]) && IS_ALUNDER(ch); c++)
1398 compared[len++] = ch;
1399 compared[len] = '\0';
1401 if(!strcmpi(extension, compared))
1411 FileDesc FindNext(char * extensions)
1413 FileDesc result = null;
1419 while(!Validate(extensions))
1425 if(system.FindNext(this))
1432 #if defined(__WIN32__)
1433 if(!strcmp(d.name, "/"))
1436 for(c = 0; c<26; c++)
1438 if(((uint)d.fHandle) & (1<<c))
1440 char volume[MAX_FILENAME] = "";
1443 uint16 _wvolume[MAX_FILENAME];
1445 stats.attribs = FileAttribs { isDirectory = true, isDrive = true };
1447 stats.accessed = stats.created = stats.modified = 0;
1448 _wpath[0] = path[0] = (char)('A' + c);
1449 _wpath[1] = path[1] = ':';
1450 _wpath[2] = path[2] = '\\';
1451 _wpath[3] = path[3] = 0;
1452 driveType = GetDriveType(_wpath);
1453 *((uint *)&d.fHandle) ^= (1<<c);
1457 case DRIVE_REMOVABLE: stats.attribs.isRemovable = true; break;
1458 case DRIVE_REMOTE: stats.attribs.isRemote = true; break;
1459 case DRIVE_CDROM: stats.attribs.isCDROM = true; break;
1461 if(driveType == DRIVE_NO_ROOT_DIR)
1463 uint16 remoteName[1024];
1468 status = WNetGetConnection(_wpath, remoteName, &size);
1469 if(status != ERROR_CONNECTION_UNAVAIL)
1476 if(driveType != DRIVE_REMOVABLE && driveType != DRIVE_REMOTE &&
1477 GetVolumeInformation(_wpath, _wvolume, MAX_FILENAME - 1, null, null, null, null, 0))
1479 UTF16toUTF8Buffer(_wvolume, volume, MAX_FILENAME);
1481 sprintf(name, "%s [%s]", path, volume);
1494 else if(d.name[0] != '\\' || d.name[1] != '\\' || strstr(d.name+2, "\\"))
1496 WIN32_FIND_DATA winFile;
1497 if(FindNextFile(d.fHandle, &winFile))
1499 UTF16toUTF8Buffer(winFile.cFileName, name, MAX_FILENAME);
1500 stats.attribs = FileAttribs { };
1501 stats.attribs.isArchive = (winFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) ? true : false;
1502 stats.attribs.isHidden = (winFile.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) ? true : false;
1503 stats.attribs.isReadOnly = (winFile.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? true : false;
1504 stats.attribs.isSystem = (winFile.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) ? true : false;
1505 stats.attribs.isTemporary = (winFile.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) ? true : false;
1506 stats.attribs.isDirectory = (winFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? true : false;
1507 stats.attribs.isFile = !(winFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
1508 stats.size = winFile.nFileSizeLow;
1510 stats.accessed = Win32FileTimeToTimeStamp(&winFile.ftLastAccessTime);
1511 stats.modified = Win32FileTimeToTimeStamp(&winFile.ftLastWriteTime);
1512 stats.created = Win32FileTimeToTimeStamp(&winFile.ftCreationTime);
1514 strcpy(path, d.name);
1515 PathCat(path, name);
1517 strcat(path, DIR_SEPS);
1518 strcat(path, name);*/
1528 if(d.resource < d.numResources)
1530 stats.attribs = FileAttribs { isDirectory = true, isShare = true };
1532 stats.accessed = stats.created = stats.modified = 0;
1534 UTF16toUTF8Buffer(d.resources[d.resource].lpRemoteName, path, MAX_LOCATION);
1535 GetLastDirectory(path, name);
1550 for(c = d.workGroup; c<d.numWorkGroups; c++)
1552 if(c != d.workGroup)
1554 int countInGroup = 0xFFFFFFFF;
1556 NETRESOURCE * resources;
1557 uint size = 512 * sizeof(NETRESOURCE);
1559 resources = (NETRESOURCE *)new0 byte[size];
1561 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &d.workGroups[c], &handle);
1564 int returnCode = WNetEnumResource(handle, &countInGroup, resources, &size);
1565 if(returnCode != ERROR_MORE_DATA)
1567 countInGroup = 0xFFFFFFFF;
1568 resources = (NETRESOURCE *)renew0 resources byte[size];
1571 WNetCloseEnum(handle);
1572 d.numResources = countInGroup;
1573 d.resources = resources;
1577 if(d.resource < d.numResources)
1579 stats.attribs = FileAttribs { isDirectory = true, isServer = true };
1581 stats.accessed = stats.created = stats.modified = 0;
1583 UTF16toUTF8Buffer(d.resources[d.resource].lpRemoteName, path, MAX_LOCATION);
1585 path[2] = (char)toupper(path[2]);
1586 GetLastDirectory(path, name);
1600 if(d.workGroup == d.numWorkGroups && d.resource == d.numResources)
1602 delete d.workGroups;
1614 strcpy(name,de->d_name);
1615 strcpy(path, d.name);
1616 if(d.name[0] && d.name[1])
1617 strcat(path, DIR_SEPS);
1620 stats.attribs = FileAttribs { };
1621 stats.attribs = (s.st_mode&S_IFDIR) ? FileAttribs { isDirectory = true } : FileAttribs { isFile = true };
1622 stats.size = s.st_size;
1623 stats.accessed = s.st_atime;
1624 stats.modified = s.st_mtime;
1625 stats.created = s.st_ctime;
1641 system.CloseDir(this);
1647 #if defined(__WIN32__)
1648 if(d.fHandle && strcmp(d.name, "/"))
1649 FindClose(d.fHandle);
1660 public struct FileListing
1668 bool result = false;
1670 desc = desc.FindNext(extensions);
1672 desc = FileFind(directory, extensions);
1685 property char * name { get { return (char *)(desc ? desc.name : null); } };
1686 property char * path { get { return (char *)(desc ? desc.path : null); } };
1687 property FileStats stats { get { value = desc ? desc.stats : FileStats { }; } };
1694 public File CreateTemporaryFile(char * tempFileName, char * template)
1696 #ifndef ECERE_BOOTSTRAP // quick fix for now
1698 #if defined(__unix__) || defined(__APPLE__)
1699 char buffer[MAX_FILENAME];
1701 strcpy(buffer, "/tmp/");
1702 strcat(buffer, template);
1703 //strcpy(buffer, template);
1704 strcat(buffer, "XXXXXX");
1706 fd = mkstemp(buffer);
1707 strcpy(tempFileName, buffer);
1709 f.output = f.input = fdopen(fd, "r+");
1711 char tempPath[MAX_LOCATION];
1712 GetTempPathA(MAX_LOCATION, tempPath); // TODO: Patch this whole thing to support Unicode temp path
1713 GetTempFileNameA(tempPath, template, 0, tempFileName);
1714 f = FileOpen(tempFileName, readWrite);
1722 public void CreateTemporaryDir(char * tempFileName, char * template)
1724 #ifndef ECERE_BOOTSTRAP // quick fix for now
1725 #if defined(__unix__) || defined(__APPLE__)
1726 char buffer[MAX_FILENAME];
1727 strcpy(buffer, "/tmp/");
1728 strcat(buffer, template);
1729 //strcpy(buffer, template);
1730 strcat(buffer, "XXXXXX");
1733 strcpy(tempFileName, buffer);
1735 char tempPath[MAX_LOCATION];
1736 GetTempPathA(MAX_LOCATION, tempPath); // TODO: Patch this whole thing to support Unicode temp path
1737 GetTempFileNameA(tempPath, template, 0, tempFileName);
1738 DeleteFile(tempFileName);
1739 MakeDir(tempFileName);
1744 public void MakeSlashPath(char * p)
1748 ChangeCh(p, '\\', '/');
1752 public void MakeSystemPath(char * p)
1757 public char * CopySystemPath(char * p)
1759 char * d = CopyString(p);
1765 public char * CopyUnixPath(char * p)
1767 char * d = CopyString(p);
1773 public char * GetSystemPathBuffer(char * d, char * p)
1776 strcpy(d, p ? p : "");
1781 public char * GetSlashPathBuffer(char * d, char * p)
1784 strcpy(d, p ? p : "");