Pelles C forum
C language => Tips & tricks => Topic started by: TimoVJL on April 27, 2012, 09:46:36 PM
-
#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;
}
-
Nice snippet!
Also if you include shlwapi.h you can use PathFindFileName() when working in WinAPI.
-
No, if i think size of code ;D
-
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* ;)
-
actually shlwapi.dll is quite small, only about 350 kb.
But what about to kill two flies with one beat (function) :)
-
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! ....
-
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
-
It is better than mine.
-
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.
-
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
-
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) ...
-
Yes.
I posted because the example was for basename and ext.
John
-
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;
}
-
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
-
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.
-
This should be a little bit quicker
BOOL PathMatchSpec3(char *fn, char *ext)
{
char *p=fn, *q=ext;
while (*p++);
while (*q++);
while (--q>=ext)
if (*--p != *q) return FALSE;
return (*--p == '.');
}