123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- /*
- Copyright (c) 2020-2023 Bruce A Henderson
-
- This software is provided 'as-is', without any express or implied
- warranty. In no event will the authors be held liable for any damages
- arising from the use of this software.
-
- Permission is granted to anyone to use this software for any purpose,
- including commercial applications, and to alter it and redistribute it
- freely, subject to the following restrictions:
-
- 1. The origin of this software must not be misrepresented; you must not
- claim that you wrote the original software. If you use this software
- in a product, an acknowledgment in the product documentation would be
- appreciated but is not required.
- 2. Altered source versions must be plainly marked as such, and must not be
- misrepresented as being the original software.
- 3. This notice may not be removed or altered from any source distribution.
- */
- #include "physfs.h"
- #include "brl.mod/blitz.mod/blitz.h"
- static BBString * bmx_char_to_bbstring(char* txt) {
- if (txt == NULL) {
- return &bbEmptyString;
- } else {
- return bbStringFromUTF8String(txt);
- }
- }
- struct MaxFilesEnumeration {
- char ** files;
- int index;
- };
- int bmx_PHYSFS_init() {
- return PHYSFS_init(bbArgv0);
- }
- int bmx_PHYSFS_getLastErrorCode() {
- return PHYSFS_getLastErrorCode();
- }
- BBString * bmx_PHYSFS_getErrorForCode(int code) {
- return bbStringFromUTF8String(PHYSFS_getErrorByCode(code));
- }
- BBString * bmx_PHYSFS_getLastError() {
- int code = PHYSFS_getLastErrorCode();
- if (code == PHYSFS_ERR_OK) {
- return &bbEmptyString;
- }
- return bmx_PHYSFS_getErrorForCode(code);
- }
- int bmx_PHYSFS_mount(BBString * newDir, BBString * mountPoint, int appendToPath) {
- char dbuf[1024];
- size_t dlen = 1024;
- bbStringToUTF8StringBuffer(newDir, dbuf, &dlen);
- char mbuf[256];
- size_t mlen = 256;
- if (mountPoint != &bbEmptyString) {
- bbStringToUTF8StringBuffer(mountPoint, mbuf, &mlen);
- return PHYSFS_mount(dbuf, mbuf, appendToPath);
- }
- return PHYSFS_mount(dbuf, NULL, appendToPath);
- }
- BBString * bmx_PHYSFS_getBaseDir() {
- return bbStringFromUTF8String(PHYSFS_getBaseDir());
- }
- BBString * bmx_PHYSFS_getPrefDir(BBString * org, BBString * app) {
- char obuf[128];
- size_t olen = 128;
- bbStringToUTF8StringBuffer(org, obuf, &olen);
- char abuf[128];
- size_t alen = 128;
- bbStringToUTF8StringBuffer(app, abuf, &alen);
- return bbStringFromUTF8String(PHYSFS_getPrefDir(obuf, abuf));
- }
- int bmx_PHYSFS_mountMemory(void * dirPtr, int dirLen, BBString * newDir, BBString * mountPoint, int appendToPath) {
- char dbuf[1024];
- size_t dlen = 1024;
- bbStringToUTF8StringBuffer(newDir, dbuf, &dlen);
- char mbuf[256];
- size_t mlen = 256;
- if (mountPoint != &bbEmptyString) {
- bbStringToUTF8StringBuffer(mountPoint, mbuf, &mlen);
- return PHYSFS_mountMemory(dirPtr, dirLen, NULL, dbuf, mbuf, appendToPath);
- }
- return PHYSFS_mountMemory(dirPtr, dirLen, NULL, dbuf, NULL, appendToPath);
- }
- PHYSFS_File * bmx_PHYSFS_openAppend(BBString * path) {
- char buf[1024];
- size_t len = 1024;
- bbStringToUTF8StringBuffer(path, buf, &len);
- return PHYSFS_openAppend(buf);
- }
- PHYSFS_File * bmx_PHYSFS_openWrite(BBString * path) {
- char buf[1024];
- size_t len = 1024;
- bbStringToUTF8StringBuffer(path, buf, &len);
- return PHYSFS_openWrite(buf);
- }
- PHYSFS_File * bmx_PHYSFS_openRead(BBString * path) {
- char buf[1024];
- size_t len = 1024;
- bbStringToUTF8StringBuffer(path, buf, &len);
- return PHYSFS_openRead(buf);
- }
- int bmx_PHYSFS_stat(BBString * filename, PHYSFS_Stat * stat) {
- char buf[1024];
- size_t len = 1024;
- bbStringToUTF8StringBuffer(filename, buf, &len);
- return PHYSFS_stat(buf, stat);
- }
- int bmx_PHYSFS_delete(BBString * filename) {
- char buf[1024];
- size_t len = 1024;
- bbStringToUTF8StringBuffer(filename, buf, &len);
- return PHYSFS_delete(buf);
- }
- int bmx_PHYSFS_mkdir(BBString * dirName) {
- char buf[1024];
- size_t len = 1024;
- bbStringToUTF8StringBuffer(dirName, buf, &len);
- return PHYSFS_mkdir(buf);
- }
- struct MaxFilesEnumeration * bmx_blitzio_readdir(BBString * dir) {
- char buf[1024];
- size_t len = 1024;
- bbStringToUTF8StringBuffer(dir, buf, &len);
-
- char ** files = PHYSFS_enumerateFiles(buf);
-
- if (files == NULL)
- return NULL;
- struct MaxFilesEnumeration * mfe = malloc(sizeof(struct MaxFilesEnumeration));
- mfe->files = files;
- mfe->index = 0;
-
- return mfe;
- }
- BBString * bmx_blitzio_nextFile(struct MaxFilesEnumeration * mfe) {
- char * f = mfe->files[mfe->index];
- if (f) {
- mfe->index++;
- return bbStringFromUTF8String(f);
- }
- return &bbEmptyString;
- }
- void bmx_blitzio_closeDir(struct MaxFilesEnumeration * mfe) {
- PHYSFS_freeList(mfe->files);
- free(mfe);
- }
- int bmx_PHYSFS_setWriteDir(BBString * newDir) {
- if (newDir == &bbEmptyString) {
- return PHYSFS_setWriteDir(NULL);
- } else {
- char buf[1024];
- size_t len = 1024;
- bbStringToUTF8StringBuffer(newDir, buf, &len);
- return PHYSFS_setWriteDir(buf);
- }
- }
- BBString * bmx_PHYSFS_getWriteDir() {
- char * dir = PHYSFS_getWriteDir();
- return bmx_char_to_bbstring(dir);
- }
- BBString * bmx_PHYSFS_getRealDir(BBString * filename) {
- char buf[1024];
- size_t len = 1024;
- bbStringToUTF8StringBuffer(filename, buf, &len);
- char * dir = PHYSFS_getRealDir(buf);
- return bmx_char_to_bbstring(dir);
- }
- BBString * bmx_PHYSFS_getMountPoint(BBString * dir) {
- char buf[1024];
- size_t len = 1024;
- bbStringToUTF8StringBuffer(dir, buf, &len);
- char * mount = PHYSFS_getMountPoint(buf);
- return bmx_char_to_bbstring(mount);
- }
- int bmx_PHYSFS_setRoot(BBString * archive, BBString * subdir) {
- char abuf[1024];
- size_t len = 1024;
- bbStringToUTF8StringBuffer(archive, abuf, &len);
- char sbuf[1024];
- size_t slen = 1024;
- if (subdir != &bbEmptyString) {
- bbStringToUTF8StringBuffer(subdir, sbuf, &slen);
- }
- return PHYSFS_setRoot(abuf, sbuf);
- }
- int bmx_PHYSFS_unmount(BBString * oldDir) {
- char buf[1024];
- size_t len = 1024;
- bbStringToUTF8StringBuffer(oldDir, buf, &len);
- return PHYSFS_unmount(buf);
- }
- BBArray * bmx_PHYSFS_getSearchPath() {
- char **list = PHYSFS_getSearchPath();
- int count = 0;
- char **i;
- for (i = list; *i != NULL; i++) {
- count++;
- }
- if (count == 0) {
- return &bbEmptyArray;
- }
- int n = 0;
- BBArray * p = bbArrayNew1D("$", count);
- BBString **s=(BBString**)BBARRAYDATA( p,p->dims );
- for (i = list; *i != NULL; i++) {
- s[n++]=bbStringFromUTF8String( *i );
- }
- PHYSFS_freeList(list);
- return p;
- }
|