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 strcpy(d.name, path);
1199 file.stats.accessed = Win32FileTimeToTimeStamp(&winFile.ftLastAccessTime);
1200 file.stats.modified = Win32FileTimeToTimeStamp(&winFile.ftLastWriteTime);
1201 file.stats.created = Win32FileTimeToTimeStamp(&winFile.ftCreationTime);
1208 int count = 0xFFFFFFFF;
1209 uint size = 512 * sizeof(NETRESOURCE);
1210 NETRESOURCE * buffer = (NETRESOURCE *)new0 byte[size];
1211 NETRESOURCE nr = {0};
1214 nr.dwScope = RESOURCE_GLOBALNET;
1215 nr.dwType = RESOURCETYPE_DISK;
1216 nr.lpProvider = L"Microsoft Windows Network";
1218 strcpy(d.name, path);
1221 nr.lpRemoteName = UTF8toUTF16(path, null);
1224 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &nr, &handle);
1227 WinReviveNetworkResource(nr.lpRemoteName);
1228 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &nr, &handle);
1235 int returnCode = WNetEnumResource(handle, &count, buffer, &size);
1236 if(returnCode != ERROR_MORE_DATA)
1239 buffer = (NETRESOURCE *)renew0 buffer byte[size];
1241 WNetCloseEnum(handle);
1244 delete nr.lpRemoteName;
1247 file.stats.attribs = FileAttribs { isDirectory = true, isShare = true };
1248 file.stats.size = 0;
1249 file.stats.accessed = file.stats.created = file.stats.modified = 0;
1251 UTF16toUTF8Buffer(buffer->lpRemoteName, file.path, MAX_LOCATION);
1252 GetLastDirectory(file.path, file.name);
1255 d.resources = buffer;
1256 d.numResources = count;
1265 nr.lpProvider = L"Microsoft Windows Network";
1268 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &nr, &handle);
1271 int returnCode = WNetEnumResource(handle, &count, buffer, &size);
1272 if(returnCode != ERROR_MORE_DATA)
1275 buffer = (NETRESOURCE *)renew0 buffer byte[size];
1277 WNetCloseEnum(handle);
1279 for(c = 0; c<count; c++)
1281 NETRESOURCE * resources;
1282 int countInGroup = 0xFFFFFFFF;
1284 size = 512 * sizeof(NETRESOURCE);
1285 resources = (NETRESOURCE *)new0 byte[size];
1288 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &buffer[c], &handle);
1291 int returnCode = WNetEnumResource(handle, &countInGroup, resources, &size);
1292 if(returnCode != ERROR_MORE_DATA)
1294 countInGroup = 0xFFFFFFFF;
1295 resources = (NETRESOURCE *)renew0 resources byte[size];
1297 WNetCloseEnum(handle);
1301 file.stats.attribs = FileAttribs { isDirectory = true, isServer = true };
1302 file.stats.size = 0;
1303 file.stats.accessed = file.stats.created = file.stats.modified = 0;
1305 UTF16toUTF8Buffer(resources->lpRemoteName, file.path, MAX_LOCATION);
1307 file.path[2] = (char)toupper(file.path[2]);
1308 GetLastDirectory(file.path, file.name);
1312 d.resources = resources;
1313 d.numResources = countInGroup;
1316 d.workGroups = buffer;
1317 d.numWorkGroups = count;
1324 if(c >= count && buffer) delete buffer;
1331 d.d = opendir((path && path[0]) ? path : ".");
1332 if(d.d && (de = readdir(d.d)))
1336 strcpy(file.path, path);
1337 strcat(file.path, DIR_SEPS);
1339 strcpy(file.name,de->d_name);
1340 strcat(file.path, file.name);
1341 stat(file.path, &s);
1342 file.stats.attribs = (s.st_mode&S_IFDIR) ? FileAttribs { isDirectory = true } : FileAttribs { isFile = true };
1343 file.stats.size = s.st_size;
1344 file.stats.accessed = s.st_atime;
1345 file.stats.modified = s.st_mtime;
1346 file.stats.created = s.st_ctime;
1348 strcpy(d.name, path);
1363 while(result && !result.Validate(extensions))
1364 result = result.FindNext(extensions);
1369 private class FileDesc : struct
1372 char name[MAX_FILENAME];
1373 char path[MAX_LOCATION];
1375 subclass(FileSystem) system;
1378 bool Validate(char * extensions)
1380 if(strcmp(name, "..") && strcmp(name, ".") && strcmp(name, ""))
1382 if(extensions && !stats.attribs.isDirectory)
1384 char extension[MAX_EXTENSION], compared[MAX_EXTENSION];
1387 GetExtension(name, extension);
1388 for(c = 0; extensions[c];)
1392 for(;(ch = extensions[c]) && !IS_ALUNDER(ch); c++);
1393 for(;(ch = extensions[c]) && IS_ALUNDER(ch); c++)
1394 compared[len++] = ch;
1395 compared[len] = '\0';
1397 if(!strcmpi(extension, compared))
1407 FileDesc FindNext(char * extensions)
1409 FileDesc result = null;
1415 while(!Validate(extensions))
1421 if(system.FindNext(this))
1428 #if defined(__WIN32__)
1429 if(!strcmp(d.name, "/"))
1432 for(c = 0; c<26; c++)
1434 if(((uint)d.fHandle) & (1<<c))
1436 char volume[MAX_FILENAME] = "";
1439 uint16 _wvolume[MAX_FILENAME];
1441 stats.attribs = FileAttribs { isDirectory = true, isDrive = true };
1443 stats.accessed = stats.created = stats.modified = 0;
1444 _wpath[0] = path[0] = (char)('A' + c);
1445 _wpath[1] = path[1] = ':';
1446 _wpath[2] = path[2] = '\\';
1447 _wpath[3] = path[3] = 0;
1448 driveType = GetDriveType(_wpath);
1449 *((uint *)&d.fHandle) ^= (1<<c);
1453 case DRIVE_REMOVABLE: stats.attribs.isRemovable = true; break;
1454 case DRIVE_REMOTE: stats.attribs.isRemote = true; break;
1455 case DRIVE_CDROM: stats.attribs.isCDROM = true; break;
1457 if(driveType == DRIVE_NO_ROOT_DIR)
1459 uint16 remoteName[1024];
1464 status = WNetGetConnection(_wpath, remoteName, &size);
1465 if(status != ERROR_CONNECTION_UNAVAIL)
1472 if(driveType != DRIVE_REMOVABLE && driveType != DRIVE_REMOTE &&
1473 GetVolumeInformation(_wpath, _wvolume, MAX_FILENAME - 1, null, null, null, null, 0))
1475 UTF16toUTF8Buffer(_wvolume, volume, MAX_FILENAME);
1477 sprintf(name, "%s [%s]", path, volume);
1490 else if(d.name[0] != '\\' || d.name[1] != '\\' || strstr(d.name+2, "\\"))
1492 WIN32_FIND_DATA winFile;
1493 if(FindNextFile(d.fHandle, &winFile))
1495 UTF16toUTF8Buffer(winFile.cFileName, name, MAX_FILENAME);
1496 stats.attribs = FileAttribs { };
1497 stats.attribs.isArchive = (winFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) ? true : false;
1498 stats.attribs.isHidden = (winFile.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) ? true : false;
1499 stats.attribs.isReadOnly = (winFile.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? true : false;
1500 stats.attribs.isSystem = (winFile.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) ? true : false;
1501 stats.attribs.isTemporary = (winFile.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) ? true : false;
1502 stats.attribs.isDirectory = (winFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? true : false;
1503 stats.size = winFile.nFileSizeLow;
1505 stats.accessed = Win32FileTimeToTimeStamp(&winFile.ftLastAccessTime);
1506 stats.modified = Win32FileTimeToTimeStamp(&winFile.ftLastWriteTime);
1507 stats.created = Win32FileTimeToTimeStamp(&winFile.ftCreationTime);
1509 strcpy(path, d.name);
1510 PathCat(path, name);
1512 strcat(path, DIR_SEPS);
1513 strcat(path, name);*/
1523 if(d.resource < d.numResources)
1525 stats.attribs = FileAttribs { isDirectory = true, isShare = true };
1527 stats.accessed = stats.created = stats.modified = 0;
1529 UTF16toUTF8Buffer(d.resources[d.resource].lpRemoteName, path, MAX_LOCATION);
1530 GetLastDirectory(path, name);
1545 for(c = d.workGroup; c<d.numWorkGroups; c++)
1547 if(c != d.workGroup)
1549 int countInGroup = 0xFFFFFFFF;
1551 NETRESOURCE * resources;
1552 uint size = 512 * sizeof(NETRESOURCE);
1554 resources = (NETRESOURCE *)new0 byte[size];
1556 WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, &d.workGroups[c], &handle);
1559 int returnCode = WNetEnumResource(handle, &countInGroup, resources, &size);
1560 if(returnCode != ERROR_MORE_DATA)
1562 countInGroup = 0xFFFFFFFF;
1563 resources = (NETRESOURCE *)renew0 resources byte[size];
1566 WNetCloseEnum(handle);
1567 d.numResources = countInGroup;
1568 d.resources = resources;
1572 if(d.resource < d.numResources)
1574 stats.attribs = FileAttribs { isDirectory = true, isServer = true };
1576 stats.accessed = stats.created = stats.modified = 0;
1578 UTF16toUTF8Buffer(d.resources[d.resource].lpRemoteName, path, MAX_LOCATION);
1580 path[2] = (char)toupper(path[2]);
1581 GetLastDirectory(path, name);
1595 if(d.workGroup == d.numWorkGroups && d.resource == d.numResources)
1597 delete d.workGroups;
1609 strcpy(name,de->d_name);
1610 strcpy(path, d.name);
1611 if(d.name[0] && d.name[0])
1612 strcat(path, DIR_SEPS);
1615 stats.attribs = FileAttribs { };
1616 if(s.st_mode & S_IFDIR) stats.attribs.isDirectory = true;
1617 stats.size = s.st_size;
1618 stats.accessed = s.st_atime;
1619 stats.modified = s.st_mtime;
1620 stats.created = s.st_ctime;
1636 system.CloseDir(this);
1642 #if defined(__WIN32__)
1643 if(d.fHandle && strcmp(d.name, "/"))
1644 FindClose(d.fHandle);
1655 public struct FileListing
1663 bool result = false;
1665 desc = desc.FindNext(extensions);
1667 desc = FileFind(directory, extensions);
1680 property char * name { get { return (char *)(desc ? desc.name : null); } };
1681 property char * path { get { return (char *)(desc ? desc.path : null); } };
1682 property FileStats stats { get { value = desc ? desc.stats : FileStats { }; } };
1689 public File CreateTemporaryFile(char * tempFileName, char * template)
1691 #ifndef ECERE_BOOTSTRAP // quick fix for now
1693 #if defined(__unix__) || defined(__APPLE__)
1694 char buffer[MAX_FILENAME];
1696 strcpy(buffer, "/tmp/");
1697 strcat(buffer, template);
1698 //strcpy(buffer, template);
1699 strcat(buffer, "XXXXXX");
1701 fd = mkstemp(buffer);
1702 strcpy(tempFileName, buffer);
1704 f.output = f.input = fdopen(fd, "r+");
1706 char tempPath[MAX_LOCATION];
1707 GetTempPathA(MAX_LOCATION, tempPath); // TODO: Patch this whole thing to support Unicode temp path
1708 GetTempFileNameA(tempPath, template, 0, tempFileName);
1709 f = FileOpen(tempFileName, readWrite);
1717 public void CreateTemporaryDir(char * tempFileName, char * template)
1719 #ifndef ECERE_BOOTSTRAP // quick fix for now
1720 #if defined(__unix__) || defined(__APPLE__)
1721 char buffer[MAX_FILENAME];
1722 strcpy(buffer, "/tmp/");
1723 strcat(buffer, template);
1724 //strcpy(buffer, template);
1725 strcat(buffer, "XXXXXX");
1728 strcpy(tempFileName, buffer);
1730 char tempPath[MAX_LOCATION];
1731 GetTempPathA(MAX_LOCATION, tempPath); // TODO: Patch this whole thing to support Unicode temp path
1732 GetTempFileNameA(tempPath, template, 0, tempFileName);
1733 DeleteFile(tempFileName);
1734 MakeDir(tempFileName);
1739 public void MakeSlashPath(char * p)
1743 ChangeCh(p, '\\', '/');
1747 public void MakeSystemPath(char * p)
1752 public char * CopySystemPath(char * p)
1754 char * d = CopyString(p);
1760 public char * CopyUnixPath(char * p)
1762 char * d = CopyString(p);
1768 public char * GetSystemPathBuffer(char * d, char * p)
1771 strcpy(d, p ? p : "");
1776 public char * GetSlashPathBuffer(char * d, char * p)
1779 strcpy(d, p ? p : "");