OSBasics.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * OSBasics.cpp
  3. * PolyStudio
  4. *
  5. * Created by Ivan Safrin on 8/4/08.
  6. * Copyright 2008 __MyCompanyName__. All rights reserved.
  7. *
  8. */
  9. #include "OSBasics.h"
  10. #ifdef _WINDOWS
  11. void wtoc(char* Dest, const WCHAR* Source)
  12. {
  13. int i = 0;
  14. while(Source[i] != '\0') {
  15. Dest[i] = (char)Source[i];
  16. ++i;
  17. }
  18. }
  19. void ctow(WCHAR* Dest, const char* Source)
  20. {
  21. int i = 0;
  22. while(Source[i] != '\0') {
  23. Dest[i] = (WCHAR)Source[i];
  24. ++i;
  25. }
  26. }
  27. #endif
  28. OSFileEntry::OSFileEntry(String path, String name, int type) {
  29. this->basePath = path;
  30. this->fullPath = path + "/" + name;
  31. this->name = name;
  32. this->type = type;
  33. size_t found;
  34. found=this->name.rfind(".");
  35. if (found!=string::npos) {
  36. extension = this->name.substr(found+1);
  37. nameWithoutExtension = this->name.substr(0, found);
  38. } else {
  39. extension = "";
  40. nameWithoutExtension = name;
  41. }
  42. }
  43. void OSFILE::debugDump() {
  44. long tellval = OSBasics::tell(this);
  45. OSBasics::seek(this, 0, SEEK_SET);
  46. char buffer;
  47. while(OSBasics::read(&buffer, 1, 1, this)) {
  48. printf("%c", buffer);
  49. }
  50. OSBasics::seek(this, tellval, SEEK_SET);
  51. }
  52. OSFILE *OSBasics::open(String filename, String opts) {
  53. OSFILE *retFile = NULL;
  54. if(PHYSFS_exists(filename.c_str())) {
  55. if(!PHYSFS_isDirectory(filename.c_str())) {
  56. retFile = new OSFILE;
  57. retFile->fileType = OSFILE::TYPE_ARCHIVE_FILE;
  58. if(opts.find("a") !=string::npos) {
  59. retFile->physFSFile = PHYSFS_openAppend(filename.c_str());
  60. if(!retFile->physFSFile){
  61. printf("Error opening file from archive (%s)\n", filename.c_str());
  62. return NULL;
  63. }
  64. } else if(opts.find("w") !=string::npos) {
  65. retFile->physFSFile = PHYSFS_openWrite(filename.c_str());
  66. if(!retFile->physFSFile){
  67. printf("Error opening file from archive (%s)\n", filename.c_str());
  68. return NULL;
  69. }
  70. } else {
  71. retFile->physFSFile = PHYSFS_openRead(filename.c_str());
  72. if(!retFile->physFSFile){
  73. printf("Error opening file from archive (%s)\n", filename.c_str());
  74. return NULL;
  75. }
  76. }
  77. return retFile;
  78. }
  79. }
  80. FILE *file = fopen(filename.c_str(), opts.c_str());
  81. if(file) {
  82. retFile = new OSFILE;
  83. retFile->fileType = OSFILE::TYPE_FILE;
  84. retFile->file = file;
  85. return retFile;
  86. }
  87. return NULL;
  88. }
  89. int OSBasics::close(OSFILE *file) {
  90. switch(file->fileType) {
  91. case OSFILE::TYPE_FILE:
  92. return fclose(file->file);
  93. break;
  94. case OSFILE::TYPE_ARCHIVE_FILE:
  95. return PHYSFS_close(file->physFSFile);
  96. break;
  97. }
  98. return 0;
  99. }
  100. long OSBasics::tell(OSFILE * stream) {
  101. switch(stream->fileType) {
  102. case OSFILE::TYPE_FILE:
  103. return ftell(stream->file);
  104. break;
  105. case OSFILE::TYPE_ARCHIVE_FILE:
  106. return PHYSFS_tell(stream->physFSFile);
  107. break;
  108. }
  109. return 0;
  110. }
  111. size_t OSBasics::read( void * ptr, size_t size, size_t count, OSFILE * stream ) {
  112. switch(stream->fileType) {
  113. case OSFILE::TYPE_FILE:
  114. return fread(ptr, size, count, stream->file);
  115. break;
  116. case OSFILE::TYPE_ARCHIVE_FILE:
  117. return PHYSFS_read(stream->physFSFile, ptr, size, count);
  118. break;
  119. }
  120. return 0;
  121. }
  122. size_t OSBasics::write( const void * ptr, size_t size, size_t count, OSFILE * stream ) {
  123. switch(stream->fileType) {
  124. case OSFILE::TYPE_FILE:
  125. fwrite(ptr, size, count, stream->file);
  126. break;
  127. case OSFILE::TYPE_ARCHIVE_FILE:
  128. PHYSFS_write(stream->physFSFile, ptr, size, count);
  129. break;
  130. }
  131. return 0;
  132. }
  133. int OSBasics::seek(OSFILE * stream, long int offset, int origin ) {
  134. switch(stream->fileType) {
  135. case OSFILE::TYPE_FILE:
  136. return fseek(stream->file, offset, origin);
  137. break;
  138. case OSFILE::TYPE_ARCHIVE_FILE:
  139. switch(origin) {
  140. case SEEK_SET:
  141. return PHYSFS_seek(stream->physFSFile, offset);
  142. break;
  143. case SEEK_CUR: {
  144. PHYSFS_sint64 curoffset = PHYSFS_tell(stream->physFSFile);
  145. return PHYSFS_seek(stream->physFSFile, curoffset+offset);
  146. }
  147. break;
  148. case SEEK_END: {
  149. PHYSFS_sint64 fileLength = PHYSFS_fileLength(stream->physFSFile);
  150. return PHYSFS_seek(stream->physFSFile, fileLength-offset);
  151. }
  152. break;
  153. }
  154. break;
  155. }
  156. return 0;
  157. }
  158. vector<OSFileEntry> OSBasics::parsePhysFSFolder(String pathString, bool showHidden) {
  159. vector<OSFileEntry> returnVector;
  160. char **rc = PHYSFS_enumerateFiles(pathString.c_str());
  161. char **i;
  162. String fullPath;
  163. String fname;
  164. for (i = rc; *i != NULL; i++) {
  165. fname = string(*i);
  166. fullPath = pathString + "/" + fname;
  167. printf("parsing in %s\n", *i);
  168. if((fname.c_str()[0] != '.' || (fname.c_str()[0] == '.' && showHidden)) && fname != "..") {
  169. if(PHYSFS_isDirectory(fullPath.c_str())) {
  170. returnVector.push_back(OSFileEntry(pathString, fname, OSFileEntry::TYPE_FOLDER));
  171. } else {
  172. returnVector.push_back(OSFileEntry(pathString, fname, OSFileEntry::TYPE_FILE));
  173. }
  174. }
  175. }
  176. PHYSFS_freeList(rc);
  177. return returnVector;
  178. }
  179. vector<OSFileEntry> OSBasics::parseFolder(String pathString, bool showHidden) {
  180. vector<OSFileEntry> returnVector;
  181. if(pathString.size() < 128) {
  182. if(PHYSFS_exists(pathString.c_str())) {
  183. if(PHYSFS_isDirectory(pathString.c_str())) {
  184. return parsePhysFSFolder(pathString, showHidden);
  185. }
  186. }
  187. }
  188. #ifdef _WINDOWS
  189. WIN32_FIND_DATA findFileData;
  190. WCHAR curDir[4096];
  191. GetCurrentDirectory(4096, curDir);
  192. WCHAR tmp[4096];
  193. memset(tmp, 0, sizeof(WCHAR)*4096);
  194. ctow(tmp, pathString.c_str());
  195. SetCurrentDirectory(tmp);
  196. HANDLE hFind = FindFirstFile((LPCWSTR)"*", &findFileData);
  197. if(hFind == INVALID_HANDLE_VALUE) {
  198. SetCurrentDirectory(curDir);
  199. return returnVector;
  200. }
  201. char fileName[260];
  202. do {
  203. memset(fileName, 0, 260);
  204. wtoc(fileName, findFileData.cFileName);
  205. String fname = string(fileName);
  206. if((fname.c_str()[0] != '.' || (fname.c_str()[0] == '.' && showHidden)) && fname != "..") {
  207. if( findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) {
  208. returnVector.push_back(OSFileEntry(pathString, fname, OSFileEntry::TYPE_FOLDER));
  209. } else {
  210. returnVector.push_back(OSFileEntry(pathString, fname, OSFileEntry::TYPE_FILE));
  211. }
  212. }
  213. } while(FindNextFile(hFind, &findFileData));
  214. FindClose(hFind);
  215. SetCurrentDirectory(curDir);
  216. #else
  217. DIR *d;
  218. struct dirent *dir;
  219. d = opendir(pathString.c_str());
  220. if(d) {
  221. while ((dir = readdir(d)) != NULL) {
  222. if(dir->d_name[0] != '.' || (dir->d_name[0] == '.' && showHidden)) {
  223. if(dir->d_type == DT_DIR) {
  224. returnVector.push_back(OSFileEntry(pathString, dir->d_name, OSFileEntry::TYPE_FOLDER));
  225. } else {
  226. returnVector.push_back(OSFileEntry(pathString, dir->d_name, OSFileEntry::TYPE_FILE));
  227. }
  228. }
  229. }
  230. closedir(d);
  231. }
  232. #endif
  233. return returnVector;
  234. }
  235. void OSBasics::createFolder(String pathString) {
  236. #ifdef _WINDOWS
  237. #else
  238. mkdir(pathString.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  239. #endif
  240. }
  241. bool OSBasics::isFolder(String pathString) {
  242. bool retVal = false;
  243. #ifdef _WINDOWS
  244. #else
  245. DIR *d;
  246. d = opendir(pathString.c_str());
  247. if(d) {
  248. retVal = true;
  249. closedir(d);
  250. }
  251. #endif
  252. return retVal;
  253. }