12 #define IS_ALUNDER(ch) ((ch) == '_' || isalnum((ch)))
14 #if defined(ECERE_BOOTSTRAP)
21 #ifndef ECERE_BOOTSTRAP
22 #if defined(__GNUC__) || defined(__WATCOMC__) || defined(__WIN32__)
24 #include <sys/types.h>
29 #if defined(__unix__) || defined(__APPLE__)
33 #if defined(__WIN32__) || defined(__WATCOMC__)
39 #if defined(__WIN32__)
40 #define WIN32_LEAN_AND_MEAN
44 BOOL WINAPI GetVolumePathName(LPCTSTR lpszFileName,LPTSTR lpszVolumePathName,DWORD cchBufferLength);
46 // Missing function...
48 #ifndef WNetGetResourceInformation
49 DWORD APIENTRY WNetGetResourceInformationA(LPNETRESOURCE lpNetResource, LPVOID lpBuffer, LPDWORD lpcbBuffer, LPTSTR* lplpSystem);
51 #define WNetGetResourceInformation WNetGetResourceInformationW
53 #define WNetGetResourceInformation WNetGetResourceInformationA
65 #endif //#ifndef ECERE_BOOTSTRAP
73 #if !defined(ECERE_VANILLA) && !defined(ECERE_NONET) && !defined(ECERE_BOOTSTRAP)
79 // IMPLEMENTATION OF THESE IS IN _File.c
82 uint FILE_GetSize(FILE * input);
83 bool FILE_Lock(FILE * input, FILE * output, FileLock type, uint64 start, uint64 length, bool wait);
84 void FILE_set_buffered(FILE * input, FILE * output, bool value);
85 FileAttribs FILE_FileExists(char * fileName);
86 bool FILE_FileGetSize(char * fileName, FileSize * size);
87 bool FILE_FileGetStats(char * fileName, FileStats stats);
88 void FILE_FileFixCase(char * file);
89 void FILE_FileOpen(char * fileName, FileOpenMode mode, FILE ** input, FILE **output);
93 FileSystem httpFileSystem;
95 public class FileSize : uint
97 // defaultAlignment = Right;
99 void OnDisplay(Surface surface, int x, int y, int width, void * fieldData, int alignment, DataDisplayFlags displayFlags)
103 eUtils_PrintSize(string, *size, 2);
104 len = strlen(string);
105 surface.WriteTextDots(alignment, x, y, width, string, len);
108 int OnCompare(FileSize data2)
115 else if(this < data2)
121 char * OnGetString(char * string, void * fieldData, bool * needClass)
123 PrintSize(string, this, 2);
127 bool OnGetDataFromString(char * string)
130 double value = strtod(string, &end);
132 if(strstr(end, "GB") || strstr(end, "gb")) multiplier = (uint)1024 * 1024 * 1024;
133 else if(strstr(end, "MB") || strstr(end, "mb")) multiplier = (uint)1024 * 1024;
134 else if(strstr(end, "KB") || strstr(end, "kb")) multiplier = 1024;
136 this = (uint)(multiplier * value);
143 virtual File ::Open(char * archive, char * name, FileOpenMode mode);
146 virtual FileAttribs ::Exists(char * archive, char * fileName);
147 virtual bool ::GetSize(char * archive, char * fileName, FileSize * size);
148 virtual bool ::Stats(char * archive, char * fileName, FileStats stats);
149 virtual void ::FixCase(char * archive, char * fileName);
152 virtual bool ::Find(FileDesc file, char * archive, char * name);
153 virtual bool ::FindNext(FileDesc file);
154 virtual void ::CloseDir(FileDesc file);
156 // Archive manipulation
157 virtual Archive ::OpenArchive(char * fileName, ArchiveOpenFlags create);
158 virtual bool ::QuerySize(char * fileName, FileSize * size);
161 public enum FileOpenMode { read = 1, write, append, readWrite, writeRead, appendRead };
162 public enum FileSeekMode { start, current, end };
164 #if !defined(ECERE_BOOTSTRAP)
165 static FileDialog fileDialog { text = "Select File" };
170 unlocked = 0, // LOCK_UN _SH_DENYNO
171 shared = 1, // LOCK_SH _SH_DENYWR
172 exclusive = 2 // LOCK_EX _SH_DENYRW
175 public class File : IOChannel
177 FILE * input, * output;
179 uint ReadData(byte * bytes, uint numBytes)
181 return Read(bytes, 1, numBytes);
184 uint WriteData(byte * bytes, uint numBytes)
186 return Write(bytes, 1, numBytes);
191 if(output && output != input)
205 bool OnGetDataFromString(char * string)
214 File f = FileOpen(string, read);
221 uint read = f.Read(buffer, 1, sizeof(buffer));
222 Write(buffer, 1, read);
231 char * OnGetString(char * tempString, void * fieldData, bool * needClass)
235 PrintSize(tempString, GetSize(), 2);
241 #ifndef ECERE_BOOTSTRAP
242 Window OnEdit(DataBox dataBox, DataBox obsolete, int x, int y, int w, int h, void * userData)
244 Window editData = class::OnEdit(dataBox, obsolete, x + 24, y, w - 48, h, userData);
247 dataBox, inactive = true, text = "Imp", hotKey = f2,
248 position = { Max(x + 24, x + w - 24), y }, size = { 24, h };
250 bool DataBox::NotifyClicked(Button button, int x, int y, Modifiers mods)
252 fileDialog.master = rootWindow;
253 fileDialog.filePath = "";
254 fileDialog.type = open;
256 if(fileDialog.Modal() == ok)
258 char * filePath = fileDialog.filePath;
260 if(output.OnGetDataFromString(filePath))
262 SetData(output, false);
271 dataBox, inactive = true, text = "Exp", hotKey = f2,
272 position = { Max(x + 24, x + w - 48), y }, size = { 24, h };
274 bool DataBox::NotifyClicked(Button button, int x, int y, Modifiers mods)
276 fileDialog.master = rootWindow;
277 fileDialog.type = save;
278 fileDialog.filePath = "";
279 if(fileDialog.Modal() == ok)
281 char * filePath = fileDialog.filePath;
282 File f = FileOpen(filePath, write);
285 File input = *(void **)data;
286 input.Seek(0, start);
290 uint read = input.Read(buffer, 1, sizeof(buffer));
291 f.Write(buffer, 1, read);
303 #endif //#ifndef ECERE_BOOTSTRAP
305 #if !defined(ECERE_VANILLA) && !defined(ECERE_NOARCHIVE) && !defined(ECERE_BOOTSTRAP)
306 void OnSerialize(IOChannel channel)
308 uint size = this ? GetSize() : MAXDWORD;
311 byte * uncompressed = new byte[size];
313 if(uncompressed || !size)
315 uint count = Read(uncompressed, 1, size);
318 uint cSize = size + size / 1000 + 12;
319 byte * compressed = new byte[cSize];
322 compress2(compressed, &cSize, uncompressed, size, 9);
324 size.OnSerialize(channel);
325 cSize.OnSerialize(channel);
326 channel.WriteData(compressed, cSize);
335 size.OnSerialize(channel);
340 size.OnSerialize(channel);
342 // Will add position...
346 for(c = 0; c<size; c += sizeof(data))
348 uint count = Read(data, 1, sizeof(data));
349 buffer.WriteData(data, count);
355 void OnUnserialize(IOChannel channel)
361 size.OnUnserialize(channel);
365 cSize.OnUnserialize(channel);
367 compressed = new byte[cSize];
370 if(channel.ReadData(compressed, cSize) == cSize)
372 byte * uncompressed = new byte[size];
373 if(uncompressed || !size)
376 uncompress(uncompressed, &size, compressed, cSize);
377 Write(uncompressed, 1, size);
391 size.OnUnserialize(channel);
395 for(c = 0; c<size; c += sizeof(data))
397 uint count = Min(size - c, sizeof(data));
398 channel.ReadData(data, count);
399 Write(data, 1, count);
412 virtual bool Seek(int pos, FileSeekMode mode)
414 uint fmode = SEEK_SET;
417 case start: fmode = SEEK_SET; break;
418 case end: fmode = SEEK_END; break;
419 case current: fmode = SEEK_CUR; break;
421 return fseek(input ? input : output, pos, fmode) != EOF;
424 virtual uint Tell(void)
426 return input ? ftell(input) : ftell(output);
429 virtual int Read(void * buffer, uint size, uint count)
431 return input ? (int)fread(buffer, size, count, input) : 0;
434 virtual int Write(void * buffer, uint size, uint count)
436 return output ? (int)fwrite(buffer, size, count, output) : 0;
440 virtual bool Getc(char * ch)
442 int ich = fgetc(input);
445 if(ch) *ch = (char)ich;
451 virtual bool Putc(char ch)
453 return (fputc((int)ch, output) == EOF) ? false : true;
456 virtual bool Puts(const char * string)
461 result = (fputs(string, output) == EOF) ? false : true;
462 // TODO: Check if any repercusions of commenting out fflush here
463 // This is what broke the debugger in 0.44d2 , it is required for outputting things to the DualPipe
464 // Added an explicit flush call in DualPipe::Puts
470 virtual bool Eof(void)
472 return input ? feof(input) : true;
475 virtual bool Truncate(FileSize size)
477 #ifdef ECERE_BOOTSTRAP
478 fprintf(stderr, "WARNING: File::Truncate unimplemented in ecereBootstrap.\n");
481 #if defined(__WIN32__)
482 return output ? (_chsize(fileno(output), size) == 0) : false;
484 return output ? (ftruncate(fileno(output), size) == 0) : false;
489 virtual uint GetSize(void)
491 return FILE_GetSize(input);
494 virtual void CloseInput(void)
505 virtual void CloseOutput(void)
516 virtual bool Lock(FileLock type, uint64 start, uint64 length, bool wait)
518 return FILE_Lock(input, output, type, start, length, wait);
521 virtual bool Unlock(uint64 start, uint64 length, bool wait)
523 return Lock(unlocked, start, length, wait);
527 int Printf(char * format, ...)
530 char text[MAX_F_STRING];
532 va_start(args, format);
533 vsprintf(text, format, args);
535 result = strlen(text);
540 public void PrintLn(typed_object object, ...)
544 va_start(args, object);
545 PrintStdArgsToBuffer(buffer, sizeof(buffer), object, args);
551 public void Print(typed_object object, ...)
555 va_start(args, object);
556 PrintStdArgsToBuffer(buffer, sizeof(buffer), object, args);
567 bool GetLine(char *s, int max)
583 if(/*!Peek() || */ !Getc(&ch))
595 return result || c > 1;
598 // Strings and numbers separated by spaces, commas, tabs, or CR/LF, handling quotes
599 bool GetString(char * string, int max)
611 if( (ch!='\n') && (ch!='\r') && (ch!=' ') && (ch!=',') && (ch!='\t'))
617 for(c=0; c<max-1; c++)
619 if(!quoted && ((ch=='\n')||(ch=='\r')||(ch==' ')||(ch==',')||(ch=='\t')))
647 GetString(string,sizeof(string));
651 unsigned int GetHexValue(void)
654 GetString(string, sizeof(string));
655 return strtoul(string, null, 16);
661 GetString(string, sizeof(string));
662 return (float)FloatFromString(string);
665 double GetDouble(void)
668 GetString(string, sizeof(string));
669 return FloatFromString(string);
672 property void * input { set { input = value; } get { return input; } }
673 property void * output { set { output = value; } get { return output; } }
674 property bool buffered
678 FILE_set_buffered(input, output, value);
681 property bool eof { get { return Eof(); } }
683 int GetLineEx(char *s, int max, bool *hasNewLineChar)
693 if(/*!Peek() || */ !Getc(&ch))
701 *hasNewLineChar = (ch == '\n');
707 bool CopyTo(char * outputFileName)
710 File f = FileOpen(outputFileName, write);
719 uint count = Read(buffer, 1, sizeof(buffer));
720 if(count && !f.Write(buffer, 1, count))
732 virtual bool Open(char * fileName, FileOpenMode mode)
737 FILE_FileOpen(fileName, mode, &input, &output);
740 if(!input && !output);
745 // TESTING ENABLING FILE BUFFERING BY DEFAULT... DOCUMENT ANY ISSUE
748 setvbuf(file.input, null, _IONBF, 0);
750 setvbuf(file.output, null, _IONBF, 0);
756 LogErrorCode((mode == Read || mode == ReadWrite) ?
757 ERR_FILE_NOT_FOUND : ERR_FILE_WRITE_FAILED, fileName);
772 public class ConsoleFile : File
783 public class FileAttribs : bool
786 bool isFile:1, isArchive:1, isHidden:1, isReadOnly:1, isSystem:1, isTemporary:1, isDirectory:1;
787 bool isDrive:1, isCDROM:1, isRemote:1, isRemovable:1, isServer:1, isShare:1;
788 // property bool { };
791 public struct FileStats
795 SecSince1970 accessed;
796 SecSince1970 modified;
797 SecSince1970 created;
800 #if defined(__WIN32__)
802 // --- FileName functions ---
804 default TimeStamp Win32FileTimeToTimeStamp(FILETIME * fileTime)
806 // TIME_ZONE_INFORMATION tz = { 0 };
810 FileTimeToSystemTime(fileTime, <);
813 GetTimeZoneInformation(&tz);
815 _TzSpecificLocalTimeToSystemTime(&tz, <, &st);
820 t.month = (Month)(st.wMonth - 1);
823 t.minute = st.wMinute;
824 t.second = st.wSecond;
828 default void TimeStampToWin32FileTime(TimeStamp t, FILETIME * fileTime)
830 // TIME_ZONE_INFORMATION tz = { 0 };
836 st.wYear = (short)tm.year;
837 st.wMonth = (short)tm.month + 1;
838 st.wDay = (short)tm.day;
839 st.wHour = (short)tm.hour;
840 st.wMinute = (short)tm.minute;
841 st.wSecond = (short)tm.second;
842 st.wMilliseconds = 0;
846 GetTimeZoneInformation(&tz);
848 SystemTimeToTzSpecificLocalTime(&tz, &st, <);
852 SystemTimeToFileTime(<, fileTime);
855 default TimeStamp Win32FileTimeToTimeStamp(FILETIME * fileTime);
856 default void TimeStampToWin32FileTime(TimeStamp t, FILETIME * fileTime);
858 default bool WinReviveNetworkResource(uint16 * _wfileName);
862 public FileAttribs FileExists(char * fileName)
864 char archiveName[MAX_LOCATION], * archiveFile;
865 #if !defined(ECERE_BOOTSTRAP)
866 if(SplitArchivePath(fileName, archiveName, &archiveFile))
868 return EARFileSystem::Exists(archiveName, archiveFile);
870 else if(strstr(fileName, "http://") == fileName)
872 return FileAttribs { isFile = true };
876 return FILE_FileExists(fileName);
879 static int openCount;
881 public File FileOpen(char * fileName, FileOpenMode mode)
886 char archiveName[MAX_LOCATION], * archiveFile;
887 #if !defined(ECERE_BOOTSTRAP)
888 if(SplitArchivePath(fileName, archiveName, &archiveFile))
890 result = EARFileSystem::Open(archiveName, archiveFile, mode);
892 #if !defined(ECERE_VANILLA) && !defined(ECERE_NONET)
893 else if(strstr(fileName, "http://") == fileName)
895 result = FileOpenURL(fileName);
900 if(strstr(fileName, "File://") == fileName)
902 result = (File)strtoul(fileName+7, null, 16);
905 if(result._class && eClass_IsDerived(result._class, class(File)))
907 if(!result._refCount) incref result;
909 result.Seek(0, start);
920 FILE_FileOpen(fileName, mode, &file.input, &file.output);
923 if(!file.input && !file.output);
928 // TESTING ENABLING FILE BUFFERING BY DEFAULT... DOCUMENT ANY ISSUE
931 setvbuf(file.input, null, _IONBF, 0);
933 setvbuf(file.output, null, _IONBF, 0);
940 LogErrorCode((mode == Read || mode == ReadWrite) ?
941 ERR_FILE_NOT_FOUND : ERR_FILE_WRITE_FAILED, fileName);
950 public void FileFixCase(char * file)
952 FILE_FileFixCase(file);
955 #if !defined(ECERE_BOOTSTRAP)
956 public bool FileTruncate(char * fileName, FileSize size)
958 #if defined(__WIN32__)
959 uint16 * _wfileName = UTF8toUTF16(fileName, null);
960 int f = _wopen(_wfileName, _O_RDWR|_O_CREAT, _S_IREAD|_S_IWRITE);
964 if(!_chsize(f, size))
971 return truncate(fileName, size) == 0;
976 public bool FileGetSize(char * fileName, FileSize * size)
984 #if !defined(ECERE_BOOTSTRAP)
985 char archiveName[MAX_LOCATION], * archiveFile;
986 if(SplitArchivePath(fileName, archiveName, &archiveFile))
987 return EARFileSystem::GetSize(archiveName, archiveFile, size);
990 result = FILE_FileGetSize(fileName, size);
996 public bool FileGetStats(char * fileName, FileStats stats)
999 if(stats && fileName)
1001 #if !defined(ECERE_BOOTSTRAP)
1002 char archiveName[MAX_LOCATION], * archiveFile;
1003 if(SplitArchivePath(fileName, archiveName, &archiveFile))
1004 result = EARFileSystem::Stats(archiveName, archiveFile, stats);
1007 return FILE_FileGetStats(fileName, stats);
1012 #ifndef ECERE_BOOTSTRAP
1014 public bool FileSetAttribs(char * fileName, FileAttribs attribs)
1017 uint winAttribs = 0;
1018 uint16 * _wfileName = UTF8toUTF16(fileName, null);
1020 if(attribs.isHidden) winAttribs |= FILE_ATTRIBUTE_HIDDEN;
1021 if(attribs.isReadOnly) winAttribs |= FILE_ATTRIBUTE_READONLY;
1023 SetFileAttributes(_wfileName, winAttribs);
1029 public bool FileSetTime(char * fileName, TimeStamp created, TimeStamp accessed, TimeStamp modified)
1031 bool result = false;
1032 TimeStamp currentTime = time(null);
1033 if(!created) created = currentTime;
1034 if(!accessed) accessed = currentTime;
1035 if(!modified) modified = currentTime;
1039 uint16 * _wfileName = UTF8toUTF16(fileName, null);
1040 HANDLE hFile = CreateFile(_wfileName, GENERIC_WRITE|GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, null,
1041 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, null);
1043 if(hFile != INVALID_HANDLE_VALUE)
1047 TimeStampToWin32FileTime(created, &c);
1048 TimeStampToWin32FileTime(accessed, &a);
1049 TimeStampToWin32FileTime(modified, &m);
1055 cc = Win32FileTimeToTimeStamp(&c);
1056 aa = Win32FileTimeToTimeStamp(&a);
1057 mm = Win32FileTimeToTimeStamp(&m);
1061 if(SetFileTime(hFile, &c, &a, &m))
1067 struct utimbuf t = { (int)accessed, (int)modified };
1068 if(!utime(fileName, &t))
1075 /****************************************************************************
1077 ****************************************************************************/
1078 // Directory Description for file listing
1079 private class Dir : struct
1081 #if defined(__WIN32__)
1085 NETRESOURCE * resources;
1089 NETRESOURCE * workGroups;
1094 char name[MAX_LOCATION];
1097 static FileDesc FileFind(char * path, char * extensions)
1099 FileDesc result = null;
1102 if((file = FileDesc {}))
1104 char archiveName[MAX_LOCATION], * archiveFile;
1105 if(SplitArchivePath(path, archiveName, &archiveFile))
1107 if(EARFileSystem::Find(file, archiveName, archiveFile))
1109 file.system = class(EARFileSystem);
1117 if((d = file.dir = Dir {}))
1119 #if defined(__WIN32__)
1120 if(!strcmp(path, "/"))
1123 d.fHandle = (void *)0xFFFFFFFF; //GetLogicalDrives();
1124 for(c = 0; c<26; c++)
1125 if(((uint)d.fHandle) & (1<<c))
1127 char volume[MAX_FILENAME] = "";
1128 uint16 _wvolume[MAX_FILENAME];
1130 uint16 _wfilePath[4];
1132 strcpy(d.name, path);
1133 file.stats.attribs = FileAttribs { isDirectory = true, isDrive = true };
1134 _wfilePath[0] = file.path[0] = (char)('A' + c);
1135 _wfilePath[1] = file.path[1] = ':';
1136 _wfilePath[2] = file.path[2] = '\\';
1137 _wfilePath[3] = file.path[3] = '\0';
1138 file.stats.size = 0;
1139 file.stats.accessed = file.stats.created = file.stats.modified = 0;
1140 driveType = GetDriveType(_wfilePath);
1143 case DRIVE_REMOVABLE: file.stats.attribs.isRemovable = true; break;
1144 case DRIVE_REMOTE: file.stats.attribs.isRemote = true; break;
1145 case DRIVE_CDROM: file.stats.attribs.isCDROM = true; break;
1147 *((uint *)&d.fHandle) ^= (1<<c);
1148 if(driveType == DRIVE_NO_ROOT_DIR) continue;
1150 if(driveType != DRIVE_REMOVABLE && driveType != DRIVE_REMOTE &&
1151 GetVolumeInformation(_wfilePath, _wvolume, MAX_FILENAME - 1, null, null, null, null, 0))
1153 file.path[2] = '\0';
1154 UTF16toUTF8Buffer(_wvolume, volume, MAX_FILENAME);
1155 sprintf(file.name, "%s [%s]", file.path, volume);
1159 file.path[2] = '\0';
1160 strcpy(file.name, file.path);
1167 else if(path[0] != '\\' || path[1] != '\\' || strstr(path+2, "\\"))
1169 WIN32_FIND_DATA winFile;
1170 uint16 dir[MAX_PATH];
1172 UTF8toUTF16Buffer(path, dir, MAX_LOCATION);
1173 if(path[0]) wcscat(dir, L"\\");
1174 wcscat(dir, L"*.*");
1176 d.fHandle = FindFirstFile(dir, &winFile);
1177 if(d.fHandle == INVALID_HANDLE_VALUE && WinReviveNetworkResource(dir))
1178 d.fHandle = FindFirstFile(dir, &winFile);
1179 if(d.fHandle != INVALID_HANDLE_VALUE)
1181 UTF16toUTF8Buffer(winFile.cFileName, file.name, MAX_FILENAME);
1182 strcpy(file.path, path);
1183 PathCat(file.path, file.name);
1185 strcat(file.path, DIR_SEPS);
1186 strcat(file.path, file.name);*/
1187 // file.sizeHigh = winFile.nFileSizeHigh;
1188 file.stats.size = winFile.nFileSizeLow;
1190 file.stats.attribs = FileAttribs { };
1191 file.stats.attribs.isArchive = (winFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) ? true : false;
1192 file.stats.attribs.isHidden = (winFile.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) ? true : false;
1193 file.stats.attribs.isReadOnly = (winFile.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? true : false;
1194 file.stats.attribs.isSystem = (winFile.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) ? true : false;
1195 file.stats.attribs.isTemporary = (winFile.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) ? true : false;
1196 file.stats.attribs.isDirectory = (winFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? true : false;
1197 file.stats.attribs.isFile = !(winFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
1198 strcpy(d.name, path);
1200 file.stats.accessed = Win32FileTimeToTimeStamp(&winFile.ftLastAccessTime);
1201 file.stats.modified = Win32FileTimeToTimeStamp(&winFile.ftLastWriteTime);
1202 file.stats.created = Win32FileTimeToTimeStamp(&winFile.ftCreationTime);
1209 int count = 0xFFFFFFFF;
1210 uint size = 512 * sizeof(NETRESOURCE);
1211 NETRESOURCE * buffer = (NETRESOURCE *)new0 byte[size];
1212 NETRESOURCE nr = {0};
1215 nr.dwScope = RESOURCE_GLOBALNET;
1216 nr.dwType = RESOURCETYPE_DISK;
1217 nr.lpProvider = L"Microsoft Windows Network";
1219 strcpy(d.name, path);
1222 nr.lpRemoteName = UTF8toUTF16(path, null);
1225 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &nr, &handle);
1228 WinReviveNetworkResource(nr.lpRemoteName);
1229 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &nr, &handle);
1236 int returnCode = WNetEnumResource(handle, &count, buffer, &size);
1237 if(returnCode != ERROR_MORE_DATA)
1240 buffer = (NETRESOURCE *)renew0 buffer byte[size];
1242 WNetCloseEnum(handle);
1245 delete nr.lpRemoteName;
1248 file.stats.attribs = FileAttribs { isDirectory = true, isShare = true };
1249 file.stats.size = 0;
1250 file.stats.accessed = file.stats.created = file.stats.modified = 0;
1252 UTF16toUTF8Buffer(buffer->lpRemoteName, file.path, MAX_LOCATION);
1253 GetLastDirectory(file.path, file.name);
1256 d.resources = buffer;
1257 d.numResources = count;
1266 nr.lpProvider = L"Microsoft Windows Network";
1269 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &nr, &handle);
1272 int returnCode = WNetEnumResource(handle, &count, buffer, &size);
1273 if(returnCode != ERROR_MORE_DATA)
1276 buffer = (NETRESOURCE *)renew0 buffer byte[size];
1278 WNetCloseEnum(handle);
1280 for(c = 0; c<count; c++)
1282 NETRESOURCE * resources;
1283 int countInGroup = 0xFFFFFFFF;
1285 size = 512 * sizeof(NETRESOURCE);
1286 resources = (NETRESOURCE *)new0 byte[size];
1289 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &buffer[c], &handle);
1292 int returnCode = WNetEnumResource(handle, &countInGroup, resources, &size);
1293 if(returnCode != ERROR_MORE_DATA)
1295 countInGroup = 0xFFFFFFFF;
1296 resources = (NETRESOURCE *)renew0 resources byte[size];
1298 WNetCloseEnum(handle);
1302 file.stats.attribs = FileAttribs { isDirectory = true, isServer = true };
1303 file.stats.size = 0;
1304 file.stats.accessed = file.stats.created = file.stats.modified = 0;
1306 UTF16toUTF8Buffer(resources->lpRemoteName, file.path, MAX_LOCATION);
1308 file.path[2] = (char)toupper(file.path[2]);
1309 GetLastDirectory(file.path, file.name);
1313 d.resources = resources;
1314 d.numResources = countInGroup;
1317 d.workGroups = buffer;
1318 d.numWorkGroups = count;
1325 if(c >= count && buffer) delete buffer;
1332 d.d = opendir((path && path[0]) ? path : ".");
1333 if(d.d && (de = readdir(d.d)))
1337 strcpy(file.path, path);
1339 strcat(file.path, DIR_SEPS);
1341 strcpy(file.name,de->d_name);
1342 strcat(file.path, file.name);
1343 stat(file.path, &s);
1344 file.stats.attribs = (s.st_mode&S_IFDIR) ? FileAttribs { isDirectory = true } : FileAttribs { isFile = true };
1345 file.stats.size = s.st_size;
1346 file.stats.accessed = s.st_atime;
1347 file.stats.modified = s.st_mtime;
1348 file.stats.created = s.st_ctime;
1350 strcpy(d.name, path);
1365 while(result && !result.Validate(extensions))
1366 result = result.FindNext(extensions);
1371 private class FileDesc : struct
1374 char name[MAX_FILENAME];
1375 char path[MAX_LOCATION];
1377 subclass(FileSystem) system;
1380 bool Validate(char * extensions)
1382 if(strcmp(name, "..") && strcmp(name, ".") && strcmp(name, ""))
1384 if(extensions && !stats.attribs.isDirectory)
1386 char extension[MAX_EXTENSION], compared[MAX_EXTENSION];
1389 GetExtension(name, extension);
1390 for(c = 0; extensions[c];)
1394 for(;(ch = extensions[c]) && !IS_ALUNDER(ch); c++);
1395 for(;(ch = extensions[c]) && IS_ALUNDER(ch); c++)
1396 compared[len++] = ch;
1397 compared[len] = '\0';
1399 if(!strcmpi(extension, compared))
1409 FileDesc FindNext(char * extensions)
1411 FileDesc result = null;
1417 while(!Validate(extensions))
1423 if(system.FindNext(this))
1430 #if defined(__WIN32__)
1431 if(!strcmp(d.name, "/"))
1434 for(c = 0; c<26; c++)
1436 if(((uint)d.fHandle) & (1<<c))
1438 char volume[MAX_FILENAME] = "";
1441 uint16 _wvolume[MAX_FILENAME];
1443 stats.attribs = FileAttribs { isDirectory = true, isDrive = true };
1445 stats.accessed = stats.created = stats.modified = 0;
1446 _wpath[0] = path[0] = (char)('A' + c);
1447 _wpath[1] = path[1] = ':';
1448 _wpath[2] = path[2] = '\\';
1449 _wpath[3] = path[3] = 0;
1450 driveType = GetDriveType(_wpath);
1451 *((uint *)&d.fHandle) ^= (1<<c);
1455 case DRIVE_REMOVABLE: stats.attribs.isRemovable = true; break;
1456 case DRIVE_REMOTE: stats.attribs.isRemote = true; break;
1457 case DRIVE_CDROM: stats.attribs.isCDROM = true; break;
1459 if(driveType == DRIVE_NO_ROOT_DIR)
1461 uint16 remoteName[1024];
1466 status = WNetGetConnection(_wpath, remoteName, &size);
1467 if(status != ERROR_CONNECTION_UNAVAIL)
1474 if(driveType != DRIVE_REMOVABLE && driveType != DRIVE_REMOTE &&
1475 GetVolumeInformation(_wpath, _wvolume, MAX_FILENAME - 1, null, null, null, null, 0))
1477 UTF16toUTF8Buffer(_wvolume, volume, MAX_FILENAME);
1479 sprintf(name, "%s [%s]", path, volume);
1492 else if(d.name[0] != '\\' || d.name[1] != '\\' || strstr(d.name+2, "\\"))
1494 WIN32_FIND_DATA winFile;
1495 if(FindNextFile(d.fHandle, &winFile))
1497 UTF16toUTF8Buffer(winFile.cFileName, name, MAX_FILENAME);
1498 stats.attribs = FileAttribs { };
1499 stats.attribs.isArchive = (winFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) ? true : false;
1500 stats.attribs.isHidden = (winFile.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) ? true : false;
1501 stats.attribs.isReadOnly = (winFile.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? true : false;
1502 stats.attribs.isSystem = (winFile.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) ? true : false;
1503 stats.attribs.isTemporary = (winFile.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) ? true : false;
1504 stats.attribs.isDirectory = (winFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? true : false;
1505 stats.attribs.isFile = !(winFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
1506 stats.size = winFile.nFileSizeLow;
1508 stats.accessed = Win32FileTimeToTimeStamp(&winFile.ftLastAccessTime);
1509 stats.modified = Win32FileTimeToTimeStamp(&winFile.ftLastWriteTime);
1510 stats.created = Win32FileTimeToTimeStamp(&winFile.ftCreationTime);
1512 strcpy(path, d.name);
1513 PathCat(path, name);
1515 strcat(path, DIR_SEPS);
1516 strcat(path, name);*/
1526 if(d.resource < d.numResources)
1528 stats.attribs = FileAttribs { isDirectory = true, isShare = true };
1530 stats.accessed = stats.created = stats.modified = 0;
1532 UTF16toUTF8Buffer(d.resources[d.resource].lpRemoteName, path, MAX_LOCATION);
1533 GetLastDirectory(path, name);
1548 for(c = d.workGroup; c<d.numWorkGroups; c++)
1550 if(c != d.workGroup)
1552 int countInGroup = 0xFFFFFFFF;
1554 NETRESOURCE * resources;
1555 uint size = 512 * sizeof(NETRESOURCE);
1557 resources = (NETRESOURCE *)new0 byte[size];
1559 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &d.workGroups[c], &handle);
1562 int returnCode = WNetEnumResource(handle, &countInGroup, resources, &size);
1563 if(returnCode != ERROR_MORE_DATA)
1565 countInGroup = 0xFFFFFFFF;
1566 resources = (NETRESOURCE *)renew0 resources byte[size];
1569 WNetCloseEnum(handle);
1570 d.numResources = countInGroup;
1571 d.resources = resources;
1575 if(d.resource < d.numResources)
1577 stats.attribs = FileAttribs { isDirectory = true, isServer = true };
1579 stats.accessed = stats.created = stats.modified = 0;
1581 UTF16toUTF8Buffer(d.resources[d.resource].lpRemoteName, path, MAX_LOCATION);
1583 path[2] = (char)toupper(path[2]);
1584 GetLastDirectory(path, name);
1598 if(d.workGroup == d.numWorkGroups && d.resource == d.numResources)
1600 delete d.workGroups;
1612 strcpy(name,de->d_name);
1613 strcpy(path, d.name);
1614 if(d.name[0] && d.name[1])
1615 strcat(path, DIR_SEPS);
1618 stats.attribs = FileAttribs { };
1619 stats.attribs = (s.st_mode&S_IFDIR) ? FileAttribs { isDirectory = true } : FileAttribs { isFile = true };
1620 stats.size = s.st_size;
1621 stats.accessed = s.st_atime;
1622 stats.modified = s.st_mtime;
1623 stats.created = s.st_ctime;
1639 system.CloseDir(this);
1645 #if defined(__WIN32__)
1646 if(d.fHandle && strcmp(d.name, "/"))
1647 FindClose(d.fHandle);
1658 public struct FileListing
1666 bool result = false;
1668 desc = desc.FindNext(extensions);
1670 desc = FileFind(directory, extensions);
1683 property char * name { get { return (char *)(desc ? desc.name : null); } };
1684 property char * path { get { return (char *)(desc ? desc.path : null); } };
1685 property FileStats stats { get { value = desc ? desc.stats : FileStats { }; } };
1692 public File CreateTemporaryFile(char * tempFileName, char * template)
1694 #ifndef ECERE_BOOTSTRAP // quick fix for now
1696 #if defined(__unix__) || defined(__APPLE__)
1697 char buffer[MAX_FILENAME];
1699 strcpy(buffer, "/tmp/");
1700 strcat(buffer, template);
1701 //strcpy(buffer, template);
1702 strcat(buffer, "XXXXXX");
1704 fd = mkstemp(buffer);
1705 strcpy(tempFileName, buffer);
1707 f.output = f.input = fdopen(fd, "r+");
1709 char tempPath[MAX_LOCATION];
1710 GetTempPathA(MAX_LOCATION, tempPath); // TODO: Patch this whole thing to support Unicode temp path
1711 GetTempFileNameA(tempPath, template, 0, tempFileName);
1712 f = FileOpen(tempFileName, readWrite);
1720 public void CreateTemporaryDir(char * tempFileName, char * template)
1722 #ifndef ECERE_BOOTSTRAP // quick fix for now
1723 #if defined(__unix__) || defined(__APPLE__)
1724 char buffer[MAX_FILENAME];
1725 strcpy(buffer, "/tmp/");
1726 strcat(buffer, template);
1727 //strcpy(buffer, template);
1728 strcat(buffer, "XXXXXX");
1731 strcpy(tempFileName, buffer);
1733 char tempPath[MAX_LOCATION];
1734 GetTempPathA(MAX_LOCATION, tempPath); // TODO: Patch this whole thing to support Unicode temp path
1735 GetTempFileNameA(tempPath, template, 0, tempFileName);
1736 DeleteFile(tempFileName);
1737 MakeDir(tempFileName);
1742 public void MakeSlashPath(char * p)
1746 ChangeCh(p, '\\', '/');
1750 public void MakeSystemPath(char * p)
1755 public char * CopySystemPath(char * p)
1757 char * d = CopyString(p);
1763 public char * CopyUnixPath(char * p)
1765 char * d = CopyString(p);
1771 public char * GetSystemPathBuffer(char * d, char * p)
1774 strcpy(d, p ? p : "");
1779 public char * GetSlashPathBuffer(char * d, char * p)
1782 strcpy(d, p ? p : "");