OSBasics.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. #include <windows.h>
  22. #include <Shellapi.h>
  23. #else
  24. #include <dirent.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <unistd.h>
  28. #endif
  29. #include <vector>
  30. #include <string>
  31. #include "physfs.h"
  32. using namespace std;
  33. using namespace Polycode;
  34. #ifdef _WINDOWS
  35. void wtoc(char* Dest, const WCHAR* Source)
  36. {
  37. int i = 0;
  38. while(Source[i] != '\0') {
  39. Dest[i] = (char)Source[i];
  40. ++i;
  41. }
  42. }
  43. void ctow(WCHAR* Dest, const char* Source)
  44. {
  45. int i = 0;
  46. while(Source[i] != '\0') {
  47. Dest[i] = (WCHAR)Source[i];
  48. ++i;
  49. }
  50. }
  51. #endif
  52. OSFileEntry::OSFileEntry(const Polycode::String& fullPath, int type) {
  53. std::vector<String> parts = fullPath.split("/");
  54. if(parts.size() > 0) {
  55. String path = parts[0];
  56. if(parts.size() > 1) {
  57. for(int i=1; i < parts.size()-1; i++) {
  58. path += "/" + parts[i];
  59. }
  60. init(path, parts[parts.size()-1], type);
  61. }
  62. } else {
  63. init("", fullPath, type);
  64. }
  65. }
  66. OSFileEntry::OSFileEntry(const String& path, const String& name, int type) {
  67. init(path, name, type);
  68. }
  69. void OSFileEntry::init(const Polycode::String& path, const Polycode::String& name, int type) {
  70. this->basePath = path;
  71. if(path == "/") {
  72. this->fullPath = "/" + name;
  73. } else {
  74. this->fullPath = path + "/" + name;
  75. }
  76. this->name = name;
  77. this->type = type;
  78. size_t found;
  79. found=this->name.rfind(".");
  80. if (found!=string::npos) {
  81. extension = this->name.substr(found+1);
  82. nameWithoutExtension = this->name.substr(0, found);
  83. } else {
  84. extension = "";
  85. nameWithoutExtension = name;
  86. }
  87. }
  88. void OSFILE::debugDump() {
  89. long tellval = OSBasics::tell(this);
  90. OSBasics::seek(this, 0, SEEK_SET);
  91. char buffer;
  92. while(OSBasics::read(&buffer, 1, 1, this)) {
  93. printf("%c", buffer);
  94. }
  95. OSBasics::seek(this, tellval, SEEK_SET);
  96. }
  97. OSFILE *OSBasics::open(const String& filename, const String& opts) {
  98. OSFILE *retFile = NULL;
  99. if(PHYSFS_exists(filename.c_str())) {
  100. if(!PHYSFS_isDirectory(filename.c_str())) {
  101. retFile = new OSFILE;
  102. retFile->fileType = OSFILE::TYPE_ARCHIVE_FILE;
  103. if(opts.find("a") !=string::npos) {
  104. retFile->physFSFile = PHYSFS_openAppend(filename.c_str());
  105. if(!retFile->physFSFile){
  106. printf("Error opening file from archive (%s)\n", filename.c_str());
  107. return NULL;
  108. }
  109. } else if(opts.find("w") !=string::npos) {
  110. retFile->physFSFile = PHYSFS_openWrite(filename.c_str());
  111. if(!retFile->physFSFile){
  112. printf("Error opening file from archive (%s)\n", filename.c_str());
  113. return NULL;
  114. }
  115. } else {
  116. retFile->physFSFile = PHYSFS_openRead(filename.c_str());
  117. if(!retFile->physFSFile){
  118. printf("Error opening file from archive (%s)\n", filename.c_str());
  119. return NULL;
  120. }
  121. }
  122. return retFile;
  123. }
  124. } else {
  125. // Logger::log("File doesn't exist in archive (%s)\n", filename.c_str());
  126. }
  127. FILE *file = fopen(filename.c_str(), opts.c_str());
  128. if(file) {
  129. retFile = new OSFILE;
  130. retFile->fileType = OSFILE::TYPE_FILE;
  131. retFile->file = file;
  132. return retFile;
  133. }
  134. return NULL;
  135. }
  136. int OSBasics::close(OSFILE *file) {
  137. int result = 0;
  138. switch(file->fileType) {
  139. case OSFILE::TYPE_FILE:
  140. result = fclose(file->file);
  141. break;
  142. case OSFILE::TYPE_ARCHIVE_FILE:
  143. result = PHYSFS_close(file->physFSFile);
  144. break;
  145. }
  146. delete file;
  147. return result;
  148. }
  149. long OSBasics::tell(OSFILE * stream) {
  150. switch(stream->fileType) {
  151. case OSFILE::TYPE_FILE:
  152. return ftell(stream->file);
  153. break;
  154. case OSFILE::TYPE_ARCHIVE_FILE:
  155. return PHYSFS_tell(stream->physFSFile);
  156. break;
  157. }
  158. return 0;
  159. }
  160. size_t OSBasics::read( void * ptr, size_t size, size_t count, OSFILE * stream ) {
  161. switch(stream->fileType) {
  162. case OSFILE::TYPE_FILE:
  163. return fread(ptr, size, count, stream->file);
  164. break;
  165. case OSFILE::TYPE_ARCHIVE_FILE:
  166. return PHYSFS_read(stream->physFSFile, ptr, size, count);
  167. break;
  168. }
  169. return 0;
  170. }
  171. size_t OSBasics::write( const void * ptr, size_t size, size_t count, OSFILE * stream ) {
  172. switch(stream->fileType) {
  173. case OSFILE::TYPE_FILE:
  174. fwrite(ptr, size, count, stream->file);
  175. break;
  176. case OSFILE::TYPE_ARCHIVE_FILE:
  177. PHYSFS_write(stream->physFSFile, ptr, size, count);
  178. break;
  179. }
  180. return 0;
  181. }
  182. int OSBasics::seek(OSFILE * stream, long int offset, int origin ) {
  183. switch(stream->fileType) {
  184. case OSFILE::TYPE_FILE:
  185. return fseek(stream->file, offset, origin);
  186. break;
  187. case OSFILE::TYPE_ARCHIVE_FILE:
  188. switch(origin) {
  189. case SEEK_SET:
  190. return PHYSFS_seek(stream->physFSFile, offset);
  191. break;
  192. case SEEK_CUR: {
  193. PHYSFS_sint64 curoffset = PHYSFS_tell(stream->physFSFile);
  194. return PHYSFS_seek(stream->physFSFile, curoffset+offset);
  195. }
  196. break;
  197. case SEEK_END: {
  198. PHYSFS_sint64 fileLength = PHYSFS_fileLength(stream->physFSFile);
  199. return PHYSFS_seek(stream->physFSFile, fileLength-offset);
  200. }
  201. break;
  202. }
  203. break;
  204. }
  205. return 0;
  206. }
  207. vector<OSFileEntry> OSBasics::parsePhysFSFolder(const String& pathString, bool showHidden) {
  208. vector<OSFileEntry> returnVector;
  209. char **rc = PHYSFS_enumerateFiles(pathString.c_str());
  210. char **i;
  211. String fullPath;
  212. String fname;
  213. for (i = rc; *i != NULL; i++) {
  214. fname = string(*i);
  215. fullPath = pathString + "/" + fname;
  216. if((fname.c_str()[0] != '.' || (fname.c_str()[0] == '.' && showHidden)) && fname != "..") {
  217. if(PHYSFS_isDirectory(fullPath.c_str())) {
  218. returnVector.push_back(OSFileEntry(pathString, fname, OSFileEntry::TYPE_FOLDER));
  219. } else {
  220. returnVector.push_back(OSFileEntry(pathString, fname, OSFileEntry::TYPE_FILE));
  221. }
  222. }
  223. }
  224. PHYSFS_freeList(rc);
  225. return returnVector;
  226. }
  227. bool OSBasics::fileExists(const Polycode::String& pathString) {
  228. if(PHYSFS_exists(pathString.c_str())) {
  229. return true;
  230. }
  231. #ifdef _WINDOWS
  232. WCHAR tmp[4096];
  233. memset(tmp, 0, sizeof(WCHAR)*4096);
  234. ctow(tmp, pathString.c_str());
  235. DWORD dwAttrib = GetFileAttributes(tmp);
  236. return (dwAttrib != 0xFFFFFFFF);
  237. #else
  238. return (access(pathString.c_str(), F_OK) != -1);
  239. #endif
  240. }
  241. vector<OSFileEntry> OSBasics::parseFolder(const String& pathString, bool showHidden) {
  242. vector<OSFileEntry> returnVector;
  243. if(pathString != "/") {
  244. if(pathString.size() < 128) {
  245. if(PHYSFS_exists(pathString.c_str())) {
  246. if(PHYSFS_isDirectory(pathString.c_str())) {
  247. return parsePhysFSFolder(pathString, showHidden);
  248. }
  249. }
  250. }
  251. }
  252. #ifdef _WINDOWS
  253. WIN32_FIND_DATA findFileData;
  254. WCHAR curDir[4096];
  255. GetCurrentDirectory(4096, curDir);
  256. WCHAR tmp[4096];
  257. memset(tmp, 0, sizeof(WCHAR)*4096);
  258. ctow(tmp, pathString.c_str());
  259. DWORD dwAttrib = GetFileAttributes(tmp);
  260. if(! (dwAttrib != INVALID_FILE_ATTRIBUTES &&
  261. (dwAttrib & FILE_ATTRIBUTE_DIRECTORY))) {
  262. return returnVector;
  263. }
  264. SetCurrentDirectory(tmp);
  265. HANDLE hFind = FindFirstFile((LPCWSTR)"*", &findFileData);
  266. if(hFind == INVALID_HANDLE_VALUE) {
  267. SetCurrentDirectory(curDir);
  268. return returnVector;
  269. }
  270. char fileName[260];
  271. do {
  272. memset(fileName, 0, 260);
  273. wtoc(fileName, findFileData.cFileName);
  274. String fname = string(fileName);
  275. if((fname.c_str()[0] != '.' || (fname.c_str()[0] == '.' && showHidden)) && fname != "..") {
  276. if( findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) {
  277. returnVector.push_back(OSFileEntry(pathString, fname, OSFileEntry::TYPE_FOLDER));
  278. } else {
  279. returnVector.push_back(OSFileEntry(pathString, fname, OSFileEntry::TYPE_FILE));
  280. }
  281. }
  282. } while(FindNextFile(hFind, &findFileData));
  283. FindClose(hFind);
  284. SetCurrentDirectory(curDir);
  285. #else
  286. DIR *d;
  287. struct dirent *dir;
  288. d = opendir(pathString.c_str());
  289. if(d) {
  290. while ((dir = readdir(d)) != NULL) {
  291. if(dir->d_name[0] != '.' || (dir->d_name[0] == '.' && showHidden)) {
  292. if(dir->d_type == DT_DIR) {
  293. returnVector.push_back(OSFileEntry(pathString, dir->d_name, OSFileEntry::TYPE_FOLDER));
  294. } else {
  295. returnVector.push_back(OSFileEntry(pathString, dir->d_name, OSFileEntry::TYPE_FILE));
  296. }
  297. }
  298. }
  299. closedir(d);
  300. }
  301. #endif
  302. return returnVector;
  303. }
  304. void OSBasics::removeItem(const String& pathString) {
  305. #ifdef _WINDOWS
  306. String _tmp = pathString.replace("/", "\\");
  307. DeleteFile(_tmp.getWDataWithEncoding(String::ENCODING_UTF8));
  308. #else
  309. remove(pathString.c_str());
  310. #endif
  311. }
  312. void OSBasics::createFolder(const String& pathString) {
  313. #ifdef _WINDOWS
  314. String path = pathString;
  315. CreateDirectory(path.getWDataWithEncoding(String::ENCODING_UTF8), NULL);
  316. #else
  317. mkdir(pathString.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  318. #endif
  319. }
  320. bool OSBasics::isFolder(const String& pathString) {
  321. bool retVal = false;
  322. #ifdef _WINDOWS
  323. String path = pathString;
  324. DWORD dwAttrib = GetFileAttributes(path.getWDataWithEncoding(String::ENCODING_UTF8));
  325. return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
  326. (dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
  327. #else
  328. DIR *d;
  329. d = opendir(pathString.c_str());
  330. if(d) {
  331. retVal = true;
  332. closedir(d);
  333. }
  334. #endif
  335. return retVal;
  336. }