glue.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. Copyright (c) 2020-2023 Bruce A Henderson
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely, subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not
  10. claim that you wrote the original software. If you use this software
  11. in a product, an acknowledgment in the product documentation would be
  12. appreciated but is not required.
  13. 2. Altered source versions must be plainly marked as such, and must not be
  14. misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source distribution.
  16. */
  17. #include "physfs.h"
  18. #include "brl.mod/blitz.mod/blitz.h"
  19. static BBString * bmx_char_to_bbstring(char* txt) {
  20. if (txt == NULL) {
  21. return &bbEmptyString;
  22. } else {
  23. return bbStringFromUTF8String(txt);
  24. }
  25. }
  26. struct MaxFilesEnumeration {
  27. char ** files;
  28. int index;
  29. };
  30. int bmx_PHYSFS_init() {
  31. return PHYSFS_init(bbArgv0);
  32. }
  33. int bmx_PHYSFS_getLastErrorCode() {
  34. return PHYSFS_getLastErrorCode();
  35. }
  36. BBString * bmx_PHYSFS_getErrorForCode(int code) {
  37. return bbStringFromUTF8String(PHYSFS_getErrorByCode(code));
  38. }
  39. BBString * bmx_PHYSFS_getLastError() {
  40. int code = PHYSFS_getLastErrorCode();
  41. if (code == PHYSFS_ERR_OK) {
  42. return &bbEmptyString;
  43. }
  44. return bmx_PHYSFS_getErrorForCode(code);
  45. }
  46. int bmx_PHYSFS_mount(BBString * newDir, BBString * mountPoint, int appendToPath) {
  47. char dbuf[1024];
  48. size_t dlen = 1024;
  49. bbStringToUTF8StringBuffer(newDir, dbuf, &dlen);
  50. char mbuf[256];
  51. size_t mlen = 256;
  52. if (mountPoint != &bbEmptyString) {
  53. bbStringToUTF8StringBuffer(mountPoint, mbuf, &mlen);
  54. return PHYSFS_mount(dbuf, mbuf, appendToPath);
  55. }
  56. return PHYSFS_mount(dbuf, NULL, appendToPath);
  57. }
  58. BBString * bmx_PHYSFS_getBaseDir() {
  59. return bbStringFromUTF8String(PHYSFS_getBaseDir());
  60. }
  61. BBString * bmx_PHYSFS_getPrefDir(BBString * org, BBString * app) {
  62. char obuf[128];
  63. size_t olen = 128;
  64. bbStringToUTF8StringBuffer(org, obuf, &olen);
  65. char abuf[128];
  66. size_t alen = 128;
  67. bbStringToUTF8StringBuffer(app, abuf, &alen);
  68. return bbStringFromUTF8String(PHYSFS_getPrefDir(obuf, abuf));
  69. }
  70. int bmx_PHYSFS_mountMemory(void * dirPtr, int dirLen, BBString * newDir, BBString * mountPoint, int appendToPath) {
  71. char dbuf[1024];
  72. size_t dlen = 1024;
  73. bbStringToUTF8StringBuffer(newDir, dbuf, &dlen);
  74. char mbuf[256];
  75. size_t mlen = 256;
  76. if (mountPoint != &bbEmptyString) {
  77. bbStringToUTF8StringBuffer(mountPoint, mbuf, &mlen);
  78. return PHYSFS_mountMemory(dirPtr, dirLen, NULL, dbuf, mbuf, appendToPath);
  79. }
  80. return PHYSFS_mountMemory(dirPtr, dirLen, NULL, dbuf, NULL, appendToPath);
  81. }
  82. PHYSFS_File * bmx_PHYSFS_openAppend(BBString * path) {
  83. char buf[1024];
  84. size_t len = 1024;
  85. bbStringToUTF8StringBuffer(path, buf, &len);
  86. return PHYSFS_openAppend(buf);
  87. }
  88. PHYSFS_File * bmx_PHYSFS_openWrite(BBString * path) {
  89. char buf[1024];
  90. size_t len = 1024;
  91. bbStringToUTF8StringBuffer(path, buf, &len);
  92. return PHYSFS_openWrite(buf);
  93. }
  94. PHYSFS_File * bmx_PHYSFS_openRead(BBString * path) {
  95. char buf[1024];
  96. size_t len = 1024;
  97. bbStringToUTF8StringBuffer(path, buf, &len);
  98. return PHYSFS_openRead(buf);
  99. }
  100. int bmx_PHYSFS_stat(BBString * filename, PHYSFS_Stat * stat) {
  101. char buf[1024];
  102. size_t len = 1024;
  103. bbStringToUTF8StringBuffer(filename, buf, &len);
  104. return PHYSFS_stat(buf, stat);
  105. }
  106. int bmx_PHYSFS_delete(BBString * filename) {
  107. char buf[1024];
  108. size_t len = 1024;
  109. bbStringToUTF8StringBuffer(filename, buf, &len);
  110. return PHYSFS_delete(buf);
  111. }
  112. int bmx_PHYSFS_mkdir(BBString * dirName) {
  113. char buf[1024];
  114. size_t len = 1024;
  115. bbStringToUTF8StringBuffer(dirName, buf, &len);
  116. return PHYSFS_mkdir(buf);
  117. }
  118. struct MaxFilesEnumeration * bmx_blitzio_readdir(BBString * dir) {
  119. char buf[1024];
  120. size_t len = 1024;
  121. bbStringToUTF8StringBuffer(dir, buf, &len);
  122. char ** files = PHYSFS_enumerateFiles(buf);
  123. if (files == NULL)
  124. return NULL;
  125. struct MaxFilesEnumeration * mfe = malloc(sizeof(struct MaxFilesEnumeration));
  126. mfe->files = files;
  127. mfe->index = 0;
  128. return mfe;
  129. }
  130. BBString * bmx_blitzio_nextFile(struct MaxFilesEnumeration * mfe) {
  131. char * f = mfe->files[mfe->index];
  132. if (f) {
  133. mfe->index++;
  134. return bbStringFromUTF8String(f);
  135. }
  136. return &bbEmptyString;
  137. }
  138. void bmx_blitzio_closeDir(struct MaxFilesEnumeration * mfe) {
  139. PHYSFS_freeList(mfe->files);
  140. free(mfe);
  141. }
  142. int bmx_PHYSFS_setWriteDir(BBString * newDir) {
  143. if (newDir == &bbEmptyString) {
  144. return PHYSFS_setWriteDir(NULL);
  145. } else {
  146. char buf[1024];
  147. size_t len = 1024;
  148. bbStringToUTF8StringBuffer(newDir, buf, &len);
  149. return PHYSFS_setWriteDir(buf);
  150. }
  151. }
  152. BBString * bmx_PHYSFS_getWriteDir() {
  153. char * dir = PHYSFS_getWriteDir();
  154. return bmx_char_to_bbstring(dir);
  155. }
  156. BBString * bmx_PHYSFS_getRealDir(BBString * filename) {
  157. char buf[1024];
  158. size_t len = 1024;
  159. bbStringToUTF8StringBuffer(filename, buf, &len);
  160. char * dir = PHYSFS_getRealDir(buf);
  161. return bmx_char_to_bbstring(dir);
  162. }
  163. BBString * bmx_PHYSFS_getMountPoint(BBString * dir) {
  164. char buf[1024];
  165. size_t len = 1024;
  166. bbStringToUTF8StringBuffer(dir, buf, &len);
  167. char * mount = PHYSFS_getMountPoint(buf);
  168. return bmx_char_to_bbstring(mount);
  169. }
  170. int bmx_PHYSFS_setRoot(BBString * archive, BBString * subdir) {
  171. char abuf[1024];
  172. size_t len = 1024;
  173. bbStringToUTF8StringBuffer(archive, abuf, &len);
  174. char sbuf[1024];
  175. size_t slen = 1024;
  176. if (subdir != &bbEmptyString) {
  177. bbStringToUTF8StringBuffer(subdir, sbuf, &slen);
  178. }
  179. return PHYSFS_setRoot(abuf, sbuf);
  180. }
  181. int bmx_PHYSFS_unmount(BBString * oldDir) {
  182. char buf[1024];
  183. size_t len = 1024;
  184. bbStringToUTF8StringBuffer(oldDir, buf, &len);
  185. return PHYSFS_unmount(buf);
  186. }
  187. BBArray * bmx_PHYSFS_getSearchPath() {
  188. char **list = PHYSFS_getSearchPath();
  189. int count = 0;
  190. char **i;
  191. for (i = list; *i != NULL; i++) {
  192. count++;
  193. }
  194. if (count == 0) {
  195. return &bbEmptyArray;
  196. }
  197. int n = 0;
  198. BBArray * p = bbArrayNew1D("$", count);
  199. BBString **s=(BBString**)BBARRAYDATA( p,p->dims );
  200. for (i = list; *i != NULL; i++) {
  201. s[n++]=bbStringFromUTF8String( *i );
  202. }
  203. PHYSFS_freeList(list);
  204. return p;
  205. }