forked from dlang/phobos
-
Notifications
You must be signed in to change notification settings - Fork 1
/
filename.d
91 lines (78 loc) · 1.59 KB
/
filename.d
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) 2001 by Digital Mars
// All Rights Reserved
// www.digitalmars.com
// File name parsing
class FileName
{
/**************************
* Get extension.
* For example, "d:\path\foo.bat" returns "bat".
*/
static char[] getExt(char[] fullname)
{
uint i;
i = fullname.length;
while (i > 0)
{
i--;
if (fullname[i] == '.')
break;
version(Win32)
{
if (fullname[i] == ':' || fullname[i] == '\')
return null;
}
version(linux)
{
if (fullname[i] == '/')
return null;
}
}
return fullname[i .. fullname.length];
}
/**************************
* Get name.
* For example, "d:\path\foo.bat" returns "foo.bat".
*/
static char[] getName(char[] fullname)
out (result)
{
assert(result.length <= fullname.length);
}
body
{
uint i;
for (i = fullname.length; i > 0; i--)
{
version(Win32)
{
if (fullname[i - 1] == ':' || fullname[i - 1] == '\')
return null;
}
version(linux)
{
if (fullname[i - 1] == '/')
return null;
}
}
return fullname[i .. fullname.length];
}
/****************************
* Put a default extension on fullname if it doesn't already
* have an extension.
*/
static char[] defaultExt(char[] fullname, char[] ext)
{
char[] existing;
existing = getExt(fullname);
if (existing.length == 0)
{
// Check for fullname ending in '.'
if (fullname.length && fullname[fullname.length - 1] == '.')
fullname ~= ext;
else
fullname = fullname ~ '.' ~ ext;
}
return fullname;
}
}