From 56228a8c23955fdd3deb8374d00766260113d195 Mon Sep 17 00:00:00 2001 From: Galleon Date: Wed, 25 Mar 2015 03:14:53 -0700 Subject: [PATCH] Make function _CWD$ part of QB64's core Implemented _STARTDIR$ function --- internal/c/libqb.cpp | 116 +++++++++++++++--- .../c/parts/user_mods/include/luke_mods.h | 1 - internal/c/parts/user_mods/src/luke_mods.cpp | 53 -------- internal/c/qbx.cpp | 3 + .../extensions/Luke Stuff-ID_Includes.bas | 10 -- source/subs_functions/subs_functions.bas | 18 +++ 6 files changed, 121 insertions(+), 80 deletions(-) diff --git a/internal/c/libqb.cpp b/internal/c/libqb.cpp index e4e100d52..61b8e3367 100644 --- a/internal/c/libqb.cpp +++ b/internal/c/libqb.cpp @@ -28980,8 +28980,65 @@ void sub__maptriangle(int32 cull_options,float sx1,float sy1,float sx2,float sy2 return resize_event_y; } +//Get Current Working Directory +qbs *func__cwd(){ + qbs *final, *tqbs; + int length; + char *buf, *ret; +#if defined QB64_WINDOWS + length = GetCurrentDirectoryA(0, NULL); + buf = (char *)malloc(length); + if (!buf) { + error(7); //"Out of memory" + return tqbs; + } + if (GetCurrentDirectoryA(length, buf) != --length) { //Sanity check + free(buf); //It's good practice + tqbs = qbs_new(0, 1); + error(51); //"Internal error" + return tqbs; + } +#elif defined QB64_LINUX + length = 512; + while(1) { + buf = (char *)malloc(length); + if (!buf) { + tqbs = qbs_new(0, 1); + error(7); + return tqbs; + } + ret = getcwd(buf, length); + if (ret) break; + if (errno != ERANGE) { + tqbs = qbs_new(0, 1); + error(51); + return tqbs; + } + free(buf); + length += 512; + } + length = strlen(ret); + ret = (char *)realloc(ret, length); //Chops off the null byte + if (!ret) { + tqbs = qbs_new(0, 1); + error(7); + return tqbs; + } + buf = ret; +#endif + final = qbs_new(length, 1); + memcpy(final->chr, buf, length); + free(buf); + return final; +} +qbs *startDir=NULL;//set on startup +qbs *func__startdir(){ + qbs *temp=qbs_new(0, 1); + qbs_set(temp, startDir); + return temp; +} extern void set_dynamic_info(); @@ -29103,22 +29160,10 @@ render_state.cull_mode=CULL_MODE__UNKNOWN; ontimer[0].state=0; ontimer[0].active=0; - //switch to directory of this EXE file -#ifdef QB64_WINDOWS -#ifndef QB64_MICROSOFT - static char *exepath=(char*)malloc(65536); - GetModuleFileName(NULL,exepath,65536); - i=strlen(exepath); - for (i2=i-1;i2>=0;i2--){ - x=exepath[i2]; - if ((x==92)||(x==47)||(x==58)){ - if (x==58) exepath[i2+1]=0; else exepath[i2]=0; - break; - } - } - chdir(exepath); -#endif -#endif + + + + { @@ -29263,6 +29308,45 @@ render_state.cull_mode=CULL_MODE__UNKNOWN; singlespace=qbs_new_cmem(1,0); singlespace->chr[0]=32; +//store _CWD$ for recall using _STARTDIR$ in startDir +startDir=qbs_new(0,0); +qbs_set(startDir,func__cwd()); + +//switch to directory of this EXE file +//http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe +#ifdef QB64_WINDOWS +#ifndef QB64_MICROSOFT + static char *exepath=(char*)malloc(65536); + GetModuleFileName(NULL,exepath,65536); + i=strlen(exepath); + for (i2=i-1;i2>=0;i2--){ + x=exepath[i2]; + if ((x==92)||(x==47)||(x==58)){ + if (x==58) exepath[i2+1]=0; else exepath[i2]=0; + break; + } + } + chdir(exepath); +#endif +#endif +#ifdef QB64_LINUX + #ifdef QB64_MACOSX + { + char pathbuf[65536]; + uint32_t pathbufsize = sizeof(pathbuf); + _NSGetExecutablePath(pathbuf, &pathbufsize) + chdir(pathbuf); + } + #else + { + char pathbuf[65536]; + memset(pathbuf, 0, sizeof(pathbuf)); + readlink("/proc/self/exe", pathbuf, 65535); + chdir(pathbuf); + } + #endif +#endif + unknown_opcode_mess=qbs_new(0,0); qbs_set(unknown_opcode_mess,qbs_new_txt_len("Unknown Opcode ( )\0",20)); diff --git a/internal/c/parts/user_mods/include/luke_mods.h b/internal/c/parts/user_mods/include/luke_mods.h index b867289fd..8d1dd09c5 100644 --- a/internal/c/parts/user_mods/include/luke_mods.h +++ b/internal/c/parts/user_mods/include/luke_mods.h @@ -2,5 +2,4 @@ * The actual code is in luke_mods.cpp * Write me at if I broke something */ -qbs *func__cwd(); void sub__keyclear(int32 buf, int32 passed); diff --git a/internal/c/parts/user_mods/src/luke_mods.cpp b/internal/c/parts/user_mods/src/luke_mods.cpp index 20fe57d1b..fe52fef0e 100644 --- a/internal/c/parts/user_mods/src/luke_mods.cpp +++ b/internal/c/parts/user_mods/src/luke_mods.cpp @@ -4,59 +4,6 @@ #include "libqb.h" #include "luke_mods.h" -//Get Current Working Directory -qbs *func__cwd(){ - qbs *final, *tqbs; - int length; - char *buf, *ret; - -#if defined QB64_WINDOWS - length = GetCurrentDirectoryA(0, NULL); - buf = (char *)malloc(length); - if (!buf) { - error(7); //"Out of memory" - return tqbs; - } - if (GetCurrentDirectoryA(length, buf) != --length) { //Sanity check - free(buf); //It's good practice - tqbs = qbs_new(0, 1); - error(51); //"Internal error" - return tqbs; - } -#elif defined QB64_LINUX - length = 512; - while(1) { - buf = (char *)malloc(length); - if (!buf) { - tqbs = qbs_new(0, 1); - error(7); - return tqbs; - } - ret = getcwd(buf, length); - if (ret) break; - if (errno != ERANGE) { - tqbs = qbs_new(0, 1); - error(51); - return tqbs; - } - free(buf); - length += 512; - } - length = strlen(ret); - ret = (char *)realloc(ret, length); //Chops off the null byte - if (!ret) { - tqbs = qbs_new(0, 1); - error(7); - return tqbs; - } - buf = ret; -#endif - final = qbs_new(length, 1); - memcpy(final->chr, buf, length); - free(buf); - return final; -} - void sub__keyclear(int32 buf, int32 passed) { if (new_error) return; if (passed && (buf > 3 || buf < 1)) error(5); diff --git a/internal/c/qbx.cpp b/internal/c/qbx.cpp index 385353915..250dbb5dc 100644 --- a/internal/c/qbx.cpp +++ b/internal/c/qbx.cpp @@ -116,6 +116,9 @@ void TIMERTHREAD(); //extern functions +extern qbs *func__cwd(); +extern qbs *func__startdir(); + extern void sub__limit(double fps); extern void sub__fps(double fps, int32 passed); diff --git a/source/subs_functions/extensions/Luke Stuff-ID_Includes.bas b/source/subs_functions/extensions/Luke Stuff-ID_Includes.bas index df9b59c60..c2ec97125 100644 --- a/source/subs_functions/extensions/Luke Stuff-ID_Includes.bas +++ b/source/subs_functions/extensions/Luke Stuff-ID_Includes.bas @@ -1,13 +1,3 @@ -'Get Current Working Directory -clearid -id.n = "_CWD" -id.musthave = "$" -id.subfunc = 1 -id.callname = "func__cwd" -id.ret = STRINGTYPE - ISPOINTER -id.Dependency = DEPENDENCY_USER_MODS -regid - clearid id.n = "_KEYCLEAR" id.subfunc = 2 diff --git a/source/subs_functions/subs_functions.bas b/source/subs_functions/subs_functions.bas index ad0b60ecd..1b5e1b21c 100644 --- a/source/subs_functions/subs_functions.bas +++ b/source/subs_functions/subs_functions.bas @@ -2744,3 +2744,21 @@ id.arg = MKL$(LONGTYPE - ISPOINTER) id.ret = LONGTYPE - ISPOINTER id.NoCloud = 1 regid + +'Get Current Working Directory +clearid +id.n = "_CWD" +id.musthave = "$" +id.subfunc = 1 +id.callname = "func__cwd" +id.ret = STRINGTYPE - ISPOINTER +regid + +'Get the directory the program was started from (before the currenct directory is automatically changed to the executables directory) +clearid +id.n = "_STARTDIR" +id.musthave = "$" +id.subfunc = 1 +id.callname = "func__startdir" +id.ret = STRINGTYPE - ISPOINTER +regid