OSBasics.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "OSBasics.h"
  20. #ifdef _WINDOWS
  21. void wtoc(char* Dest, const WCHAR* Source)
  22. {
  23. int i = 0;
  24. while(Source[i] != '\0') {
  25. Dest[i] = (char)Source[i];
  26. ++i;
  27. }
  28. }
  29. void ctow(WCHAR* Dest, const char* Source)
  30. {
  31. int i = 0;
  32. while(Source[i] != '\0') {
  33. Dest[i] = (WCHAR)Source[i];
  34. ++i;
  35. }
  36. }
  37. #endif
  38. OSFileEntry::OSFileEntry(String path, String name, int type) {
  39. this->basePath = path;
  40. this->fullPath = path + "/" + name;
  41. this->name = name;
  42. this->type = type;
  43. size_t found;
  44. found=this->name.rfind(".");
  45. if (found!=string::npos) {
  46. extension = this->name.substr(found+1);
  47. nameWithoutExtension = this->name.substr(0, found);
  48. } else {
  49. extension = "";
  50. nameWithoutExtension = name;
  51. }
  52. }
  53. void OSFILE::debugDump() {
  54. long tellval = OSBasics::tell(this);
  55. OSBasics::seek(this, 0, SEEK_SET);
  56. char buffer;
  57. while(OSBasics::read(&buffer, 1, 1, this)) {
  58. printf("%c", buffer);
  59. }
  60. OSBasics::seek(this, tellval, SEEK_SET);
  61. }
  62. OSFILE *OSBasics::open(String filename, String opts) {
  63. OSFILE *retFile = NULL;
  64. if(PHYSFS_exists(filename.c_str())) {
  65. if(!PHYSFS_isDirectory(filename.c_str())) {
  66. retFile = new OSFILE;
  67. retFile->fileType = OSFILE::TYPE_ARCHIVE_FILE;
  68. if(opts.find("a") !=string::npos) {
  69. retFile->physFSFile = PHYSFS_openAppend(filename.c_str());
  70. if(!retFile->physFSFile){
  71. printf("Error opening file from archive (%s)\n", filename.c_str());
  72. return NULL;
  73. }
  74. } else if(opts.find("w") !=string::npos) {
  75. retFile->physFSFile = PHYSFS_openWrite(filename.c_str());
  76. if(!retFile->physFSFile){
  77. printf("Error opening file from archive (%s)\n", filename.c_str());
  78. return NULL;
  79. }
  80. } else {
  81. retFile->physFSFile = PHYSFS_openRead(filename.c_str());
  82. if(!retFile->physFSFile){
  83. printf("Error opening file from archive (%s)\n", filename.c_str());
  84. return NULL;
  85. }
  86. }
  87. return retFile;
  88. }
  89. } else {
  90. // Logger::log("File doesn't exist in archive (%s)\n", filename.c_str());
  91. }
  92. FILE *file = fopen(filename.c_str(), opts.c_str());
  93. if(file) {
  94. retFile = new OSFILE;
  95. retFile->fileType = OSFILE::TYPE_FILE;
  96. retFile->file = file;
  97. return retFile;
  98. }
  99. return NULL;
  100. }
  101. int OSBasics::close(OSFILE *file) {
  102. switch(file->fileType) {
  103. case OSFILE::TYPE_FILE:
  104. return fclose(file->file);
  105. break;
  106. case OSFILE::TYPE_ARCHIVE_FILE:
  107. return PHYSFS_close(file->physFSFile);
  108. break;
  109. }
  110. return 0;
  111. }
  112. long OSBasics::tell(OSFILE * stream) {
  113. switch(stream->fileType) {
  114. case OSFILE::TYPE_FILE:
  115. return ftell(stream->file);
  116. break;
  117. case OSFILE::TYPE_ARCHIVE_FILE:
  118. return PHYSFS_tell(stream->physFSFile);
  119. break;
  120. }
  121. return 0;
  122. }
  123. size_t OSBasics::read( void * ptr, size_t size, size_t count, OSFILE * stream ) {
  124. switch(stream->fileType) {
  125. case OSFILE::TYPE_FILE:
  126. return fread(ptr, size, count, stream->file);
  127. break;
  128. case OSFILE::TYPE_ARCHIVE_FILE:
  129. return PHYSFS_read(stream->physFSFile, ptr, size, count);
  130. break;
  131. }
  132. return 0;
  133. }
  134. size_t OSBasics::write( const void * ptr, size_t size, size_t count, OSFILE * stream ) {
  135. switch(stream->fileType) {
  136. case OSFILE::TYPE_FILE:
  137. fwrite(ptr, size, count, stream->file);
  138. break;
  139. case OSFILE::TYPE_ARCHIVE_FILE:
  140. PHYSFS_write(stream->physFSFile, ptr, size, count);
  141. break;
  142. }
  143. return 0;
  144. }
  145. int OSBasics::seek(OSFILE * stream, long int offset, int origin ) {
  146. switch(stream->fileType) {
  147. case OSFILE::TYPE_FILE:
  148. return fseek(stream->file, offset, origin);
  149. break;
  150. case OSFILE::TYPE_ARCHIVE_FILE:
  151. switch(origin) {
  152. case SEEK_SET:
  153. return PHYSFS_seek(stream->physFSFile, offset);
  154. break;
  155. case SEEK_CUR: {
  156. PHYSFS_sint64 curoffset = PHYSFS_tell(stream->physFSFile);
  157. return PHYSFS_seek(stream->physFSFile, curoffset+offset);
  158. }
  159. break;
  160. case SEEK_END: {
  161. PHYSFS_sint64 fileLength = PHYSFS_fileLength(stream->physFSFile);
  162. return PHYSFS_seek(stream->physFSFile, fileLength-offset);
  163. }
  164. break;
  165. }
  166. break;
  167. }
  168. return 0;
  169. }
  170. vector<OSFileEntry> OSBasics::parsePhysFSFolder(String pathString, bool showHidden) {
  171. vector<OSFileEntry> returnVector;
  172. char **rc = PHYSFS_enumerateFiles(pathString.c_str());
  173. char **i;
  174. String fullPath;
  175. String fname;
  176. for (i = rc; *i != NULL; i++) {
  177. fname = string(*i);
  178. fullPath = pathString + "/" + fname;
  179. printf("parsing in %s\n", *i);
  180. if((fname.c_str()[0] != '.' || (fname.c_str()[0] == '.' && showHidden)) && fname != "..") {
  181. if(PHYSFS_isDirectory(fullPath.c_str())) {
  182. returnVector.push_back(OSFileEntry(pathString, fname, OSFileEntry::TYPE_FOLDER));
  183. } else {
  184. returnVector.push_back(OSFileEntry(pathString, fname, OSFileEntry::TYPE_FILE));
  185. }
  186. }
  187. }
  188. PHYSFS_freeList(rc);
  189. return returnVector;
  190. }
  191. vector<OSFileEntry> OSBasics::parseFolder(String pathString, bool showHidden) {
  192. vector<OSFileEntry> returnVector;
  193. if(pathString.size() < 128) {
  194. if(PHYSFS_exists(pathString.c_str())) {
  195. if(PHYSFS_isDirectory(pathString.c_str())) {
  196. return parsePhysFSFolder(pathString, showHidden);
  197. }
  198. }
  199. }
  200. #ifdef _WINDOWS
  201. WIN32_FIND_DATA findFileData;
  202. WCHAR curDir[4096];
  203. GetCurrentDirectory(4096, curDir);
  204. WCHAR tmp[4096];
  205. memset(tmp, 0, sizeof(WCHAR)*4096);
  206. ctow(tmp, pathString.c_str());
  207. SetCurrentDirectory(tmp);
  208. HANDLE hFind = FindFirstFile((LPCWSTR)"*", &findFileData);
  209. if(hFind == INVALID_HANDLE_VALUE) {
  210. SetCurrentDirectory(curDir);
  211. return returnVector;
  212. }
  213. char fileName[260];
  214. do {
  215. memset(fileName, 0, 260);
  216. wtoc(fileName, findFileData.cFileName);
  217. String fname = string(fileName);
  218. if((fname.c_str()[0] != '.' || (fname.c_str()[0] == '.' && showHidden)) && fname != "..") {
  219. if( findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) {
  220. returnVector.push_back(OSFileEntry(pathString, fname, OSFileEntry::TYPE_FOLDER));
  221. } else {
  222. returnVector.push_back(OSFileEntry(pathString, fname, OSFileEntry::TYPE_FILE));
  223. }
  224. }
  225. } while(FindNextFile(hFind, &findFileData));
  226. FindClose(hFind);
  227. SetCurrentDirectory(curDir);
  228. #else
  229. DIR *d;
  230. struct dirent *dir;
  231. d = opendir(pathString.c_str());
  232. if(d) {
  233. while ((dir = readdir(d)) != NULL) {
  234. if(dir->d_name[0] != '.' || (dir->d_name[0] == '.' && showHidden)) {
  235. if(dir->d_type == DT_DIR) {
  236. returnVector.push_back(OSFileEntry(pathString, dir->d_name, OSFileEntry::TYPE_FOLDER));
  237. } else {
  238. returnVector.push_back(OSFileEntry(pathString, dir->d_name, OSFileEntry::TYPE_FILE));
  239. }
  240. }
  241. }
  242. closedir(d);
  243. }
  244. #endif
  245. return returnVector;
  246. }
  247. void OSBasics::removeItem(String pathString) {
  248. #ifdef _WINDOWS
  249. #else
  250. remove(pathString.c_str());
  251. #endif
  252. }
  253. void OSBasics::createFolder(String pathString) {
  254. #ifdef _WINDOWS
  255. #else
  256. mkdir(pathString.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  257. #endif
  258. }
  259. bool OSBasics::isFolder(String pathString) {
  260. bool retVal = false;
  261. #ifdef _WINDOWS
  262. #else
  263. DIR *d;
  264. d = opendir(pathString.c_str());
  265. if(d) {
  266. retVal = true;
  267. closedir(d);
  268. }
  269. #endif
  270. return retVal;
  271. }