News:

Download Pelles C here: http://www.smorgasbordet.com/pellesc/

Main Menu

basename ext

Started by TimoVJL, April 27, 2012, 09:46:36 PM

Previous topic - Next topic

TimoVJL

#include <stdio.h>

char *FindLast(char *s, char c)
{
char *p, *p2 = 0;
for(p=s; *p; p++)
if (*p == c) p2 = p;
if (p2) p2++;
else if (c == '\\') return s;
return p2;
}

int main(int argc, char **argv)
{
char *name = "C:\\WINDOWS\\system32\\kernel32.dll";
//char *name = "kernel32.dll";
printf("%s %s %s\n", name, FindLast(name, '\\'), FindLast(name, '.'));
return 0;
}
May the source be with you

CommonTater

Nice snippet!

Also if you include shlwapi.h you can use PathFindFileName() when working in WinAPI.


TimoVJL

No, if i think size of code  ;D
May the source be with you

CommonTater

Quote from: timovjl on April 27, 2012, 10:36:08 PM
No, if i think size of code  ;D

True... linking into a huge DLL isn't always the best plan... But if you've got it loaded *anyway* ;)

TimoVJL

#4
actually shlwapi.dll is quite small, only about 350 kb.
But what about to kill two flies with one beat (function)  :)
May the source be with you

CommonTater

Quote from: timovjl on April 27, 2012, 11:07:41 PM
actually shlwapi.dll is quite small, only about 350 kb.
But what about to kill two flies with one beat (function)  :)

LOL... Yep, like I said... Nice snippet! ....

czerny


char *FindLast(char *s, char c)
{
char *p=s;
while(*p++);
while(--p>s)
if(c==*p) return ++p;
return c=='\\' ? s : 0;
}


Don't know if it is not quicker reverse?

czerny

TimoVJL

It is better than mine.
May the source be with you

Vortex

Hi timovjl,

Searching for both of the dot and slash is a good idea. Maybe, you can create a lookup table to perform a faster search.
Code it... That's all...

JohnF

A little more care is needed if the filename does not have an extension and path has a period '.' embedded. I had this situation recently.

e.g.

char * name = "C:\\a.folder\\tem\\thefile";

John


czerny

#10
Quote
A little more care is needed if the filename does not have an extension and path has a period '.' embedded. I had this situation recently.

You are right, if the function should find basename and extension.

But if it should find last appearance of character c it should even be shorter:


char *FindLast(char *s, char c)
{
char *p=s;
while(*p++);
while(--p>s)
if(c==*p) return ++p;
return 0;
}


It can be followed by some  tests:


b=FindLast(path,'\\');
e=FindLast(path,'.');

if (!b) ...
if (!e) ...
if (e<b) ...

JohnF

Yes.

I posted because the example was for basename and ext.

John

TimoVJL

char *FindLast(char *s, char c)
{
char *p=s;
while(*p++);
while(--p>s) {
if(*p=='\\' && c=='.') return 0;
if(c==*p) return ++p;
}
return c=='\\' ? s : 0;
}
May the source be with you

czerny

Hallo,

is there a good reason to use the api function PathMatchSpec compared with the following  PathMatchSpec2


BOOL PathMatchSpec2(char *fn, char *ext)
{
return !strcmp(FindLast(fn,'.'),ext);
}

PathMatchSpec(fn,"*.txt");
PathMatchSpec2(fn,"txt");


czerny

CommonTater

Quote from: czerny on May 07, 2012, 10:33:10 PM
Hallo,

is there a good reason to use the api function PathMatchSpec compared with the following  PathMatchSpec2


BOOL PathMatchSpec2(char *fn, char *ext)
{
return !strcmp(FindLast(fn,'.'),ext);
}

PathMatchSpec(fn,"*.txt");
PathMatchSpec2(fn,"txt");


czerny

That works if you're looking for a specific extension....

You can also use PathFindFilename() and PathFindExtension() to search your strings.  I use them fairly often and so long as you're careful to check whether it's a file in advance ( using PathIsDirectory() ) it gives very good results.