1
1
Fork 0
mirror of https://github.com/QB64Official/qb64.git synced 2024-07-03 08:51:22 +00:00

Make function _CWD$ part of QB64's core

Implemented _STARTDIR$ function
This commit is contained in:
Galleon 2015-03-25 03:14:53 -07:00
parent 57f6ee159a
commit 56228a8c23
6 changed files with 121 additions and 80 deletions

View file

@ -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));

View file

@ -2,5 +2,4 @@
* The actual code is in luke_mods.cpp
* Write me at <flukiluke@gmail.com> if I broke something
*/
qbs *func__cwd();
void sub__keyclear(int32 buf, int32 passed);

View file

@ -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);

View file

@ -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);

View file

@ -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

View file

@ -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