MarkUlrich
Joined: 03 Dec 2002 Posts: 436 Location: Karlsruhe, Germany
|
Posted: Sun May 20, 2007 9:51 am Post subject: cd and getdir (C-routines) |
 |
|
I am in /root
I type:
gtkbasic002 /stuff/test.bas
test.bas loads a picture or Glade-XML-file in the same folder, where the script is.
Something like:
loadpicture("mondrian.xpm")
This program will fail, as I am in /root , so test.bas does not find test.inc.
Same happens, if I run it the Unix-way:
/stuff/test.bas
In Linux, every program can be executable, if the first line looks like:
#!/usr/bin/gtkbasic002
So how can we solve this issue?
I wrote 2 functions in C, that help me.
They might be cross-platform, if you replace the "/" with a SEPERATOR constant. But I'm not shure.
I think wxBasic has such a constant somewhere, maybe main.c.
You pass a filename to it, and it will return the folder of that file.
This filename usually is command(2) , this variable holds the name of the script.
This method also return the full folder-path, if you start like:
./test.bas
This internally is the case for some filemanagers.
A Basic-program now would start like this:
| Code: | #!/usr/bin/gtkbasic002
a = xwin_getdir(command(2))
xwin_cd(a)
loadpicture("mondrian.xpm") |
xwin_getdir also resolves symbolic links.
So if you have a link /stuff/test.bas -> /usr/local/bin/test.bas , then it will return "/usr/local/bin"
This method will not work with
include "test.inc" , as includes are processed before the main-script.
In that case you would need external tools like a shellscript or start.exe I once posted.
I added this new function, to be able to create a "dialog" library that uses Gladefiles.
They will be relocatable like this.
Mark
| Code: |
void xwinreturnstring(char *thestring){
wVariant *myvariant;
myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );
//-- create a string
wVariantFromChar( myvariant, thestring ,4,4 );
//-- return it to wxBasic
wVariantMove( wStackPushNothing(), myvariant );
//-- cleanup
wVariantDeref( myvariant );
wFree(myvariant);
}
void wBuiltin_xwin_cd()
{
char *arg1;
wVariant *myarg;
myarg = (wVariant *)wMalloc( sizeof( wVariant ) );
myarg = wStackPopString();
arg1 = wVariantToChar( myarg, W_FALSE, 0 );
chdir(arg1);
wVariantDeref( myarg );
free(arg1);
}
void wBuiltin_xwin_getdir()
{
char *arg0;
wVariant *myarg;
myarg = (wVariant *)wMalloc( sizeof( wVariant ) );
myarg = wStackPopString();
arg0 = wVariantToChar( myarg, W_FALSE, 0 );
char result[4097];
char buf[4097];
int len;
if ((len = readlink(arg0, buf, sizeof(buf)-1)) != -1){
buf[len] = '\0';
sprintf (result , "%s", buf);
}else{
sprintf (result , "%s", arg0);
}
char thedir[4097];
int laenge;
int laenge2;
strcpy( thedir , "" );
char *rest;
if ( strstr(result, "/" ) != NULL ){
//printf("ok\n");
laenge = strlen( result );
rest = strrchr( result , '/' );
laenge2 = strlen( rest );
//printf("ok %d - %d\n" , laenge,laenge2);
if( laenge2 > 1 ){
strncat ( thedir , result , laenge - laenge2 );
}else{
strncat ( thedir , result , laenge );
}
}else{
strncat ( thedir , "." , 1 );
}
wVariantDeref( myarg );
char *here;
long size;
char *ptr;
char ret[4097];
sprintf(ret , "%s" , thedir);
size = pathconf(".", _PC_PATH_MAX);
if ((here = (char *)malloc((size_t)size)) != NULL)
ptr = getcwd(here, (size_t)size);
//printf("-script-- %s\n" , thedir);
//printf("-hier-- %s\n" , here);
if(! strcmp(thedir , here)){
//printf("-gleich-\n");
}else{
if(! strcmp(thedir , ".")){
sprintf(ret , "%s" , here);
//printf("-script ist . -\n");
}
}
// sprintf(arg0 , "%s" , ret);
//printf("-%d--- %s\n" ,size , ret);
free(ptr);
free(arg0);
xwinreturnstring(ret);
return;
}
|
|
|