wxbasic.sourceforge.net Forum Index wxbasic.sourceforge.net
A discussion board for the wxBasic programming language.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups 
 ProfileProfile   You have no new messagesYou have no new messages   Log out [ MarkUlrich ]Log out [ MarkUlrich ] 

cd and getdir (C-routines)

 
Post new topic   Reply to topic    wxbasic.sourceforge.net Forum Index -> Coding
View previous topic :: View next topic  
Author Message
MarkUlrich



Joined: 03 Dec 2002
Posts: 436
Location: Karlsruhe, Germany

PostPosted: Sun May 20, 2007 9:51 am    Post subject: cd and getdir (C-routines) Reply with quote Edit/Delete this post Delete this post View IP address of poster

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;
}
Back to top
View user's profile Send private message Send e-mail Visit poster's website  
ICQ Number
Display posts from previous:   
Post new topic   Reply to topic    wxbasic.sourceforge.net Forum Index -> Coding All times are GMT
Page 1 of 1
Stop watching this topic
 
Delete this topic Move this topic Lock this topic Split this topic 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum
You can moderate this forum


Powered by phpBB © 2001, 2005 phpBB Group