-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_splitpath.cpp
More file actions
75 lines (69 loc) · 2 KB
/
_splitpath.cpp
File metadata and controls
75 lines (69 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#if !defined _WIN32 && !defined _WIN64
#include <string.h>
//Wraps the system implementation of splitpath(), and guarantees that the
// resulting directory path contains only '/' instead of '\\'.
void _splitpath(const char *path, char *drive, char *directory, char *filename, char *extension)
{
// The drive name is never valid on a Mac.
drive[0] = '\0';
// Copy the full filename to the directory buffer. That's where we'll
// start screwing with it.
strcpy(directory, path);
// Fix any backslashes that occur in the path.
for (unsigned int i=0; i<strlen(path); i++)
if (directory[i] == '\\')
directory[i] = '/';
// Try to find the last '/' in the filename.
char *pFilename = strchr(directory, '/');
int lastPos;
int len = strlen(directory);
while (pFilename)
{
lastPos = pFilename - directory;
if (lastPos == len - 1)
break;
char *lastFound = pFilename;
pFilename = strchr(pFilename + 1, '/');
if (!pFilename)
{
pFilename = lastFound;
break;
}
}
// If there is one, then the file name has to come after the '/'.
if (pFilename != 0)
{
strncpy(directory, path, lastPos + 1);
strcpy(filename, pFilename + 1);
directory[lastPos + 1] = 0;
}
else
{
directory[0] = 0;
strcpy(filename, path);
}
char *pExtension = strchr(filename, '.');
if (!pExtension)
extension[0] = 0;
else
{
lastPos = pExtension - filename;
len = strlen(filename);
while (pExtension)
{
lastPos = pExtension - filename;
if (lastPos == len - 1)
break;
char *lastFound = pExtension;
pExtension = strchr(pExtension + 1, '.');
if (!pExtension)
{
pExtension = lastFound;
break;
}
}
strcpy(extension, pExtension);
filename[lastPos] = 0;
}
}
#endif