[DGD] sscanf direction

Par Winzell zell at skotos.net
Mon Jan 12 21:44:10 CET 2004


> I have a question about sscanf (and yes I did check the archives, I 
> don't think the answer was there...), which I think is to do with the 
> direction in which the pattern is processed. For instance, if I wanted 
> to strip the filename from the end of a pathname string, would I do this?:
> 
> string path;
> 
> path = "/some/path/file.c";
> 
> sscanf(path, "%s/%*s, path);
> 
> 
> 
> Or would that set path to "path/file.c" ? If so, how would I make it do 
> it the other way around (set it to "/some/path/" ? If it does do that, 
> how would I make it do the other effect (set it to "path/file.c") ? I 
> hope that makes sense...

Unfortunately, sscanf cannot easily search backwards. The quickest way 
to do what you want to do is:

{
   string *bits;

   bits = explode(path, "/");

   return "/" + implode(bits[.. sizeof(bits)-2], "/");
}


If you're uneasy about explode/implode magic, you'd need to write the C 
style reinvent-your-own-string-librar-for-every-application solution...

{
   for (j = strlen(path)-1; j >= 0 && path[j] != '/'; j --);
   return path[.. j-1];
}

except you probably need to polish some boundary conditions there.

Zell

_________________________________________________________________
List config page:  http://list.imaginary.com/mailman/listinfo/dgd



More information about the DGD mailing list