remove empty dir
[reactos.git] / rosapps / winfile / splitpath.c
1 /*
2 * ReactOS winfile
3 *
4 * splitpath.c
5 *
6 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
24 #include <windows.h>
25 #include <commctrl.h>
26 #include <stdlib.h>
27 #include <malloc.h>
28 #include <memory.h>
29 #include <tchar.h>
30 #include <process.h>
31 #include <stdio.h>
32
33 #include "main.h"
34
35
36 #ifdef UNICODE
37
38 void _wsplitpath(const WCHAR* path, WCHAR* drv, WCHAR* dir, WCHAR* name, WCHAR* ext)
39 {
40 const WCHAR* end; // end of processed string
41 const WCHAR* p; // search pointer
42 const WCHAR* s; // copy pointer
43
44 // extract drive name
45 if (path[0] && path[1]==':') {
46 if (drv) {
47 *drv++ = *path++;
48 *drv++ = *path++;
49 *drv = L'\0';
50 }
51 } else if (drv)
52 *drv = L'\0';
53
54 // search for end of string or stream separator
55 for(end=path; *end && *end!=L':'; )
56 end++;
57
58 // search for begin of file extension
59 for(p=end; p>path && *--p!=L'\\' && *p!=L'/'; )
60 if (*p == L'.') {
61 end = p;
62 break;
63 }
64
65 if (ext)
66 for(s=end; *ext=*s++; )
67 ext++;
68
69 // search for end of directory name
70 for(p=end; p>path; )
71 if (*--p=='\\' || *p=='/') {
72 p++;
73 break;
74 }
75
76 if (name) {
77 for(s=p; s<end; )
78 *name++ = *s++;
79
80 *name = L'\0';
81 }
82
83 if (dir) {
84 for(s=path; s<p; )
85 *dir++ = *s++;
86
87 *dir = L'\0';
88 }
89 }
90
91 #else
92
93 void _splitpath(const CHAR* path, CHAR* drv, CHAR* dir, CHAR* name, CHAR* ext)
94 {
95 const CHAR* end; // end of processed string
96 const CHAR* p; // search pointer
97 const CHAR* s; // copy pointer
98
99 // extract drive name
100 if (path[0] && path[1]==':') {
101 if (drv) {
102 *drv++ = *path++;
103 *drv++ = *path++;
104 *drv = '\0';
105 }
106 } else if (drv)
107 *drv = '\0';
108
109 // search for end of string or stream separator
110 for(end=path; *end && *end!=':'; )
111 end++;
112
113 // search for begin of file extension
114 for(p=end; p>path && *--p!='\\' && *p!='/'; )
115 if (*p == '.') {
116 end = p;
117 break;
118 }
119
120 if (ext)
121 for(s=end; (*ext=*s++); )
122 ext++;
123
124 // search for end of directory name
125 for(p=end; p>path; )
126 if (*--p=='\\' || *p=='/') {
127 p++;
128 break;
129 }
130
131 if (name) {
132 for(s=p; s<end; )
133 *name++ = *s++;
134
135 *name = '\0';
136 }
137
138 if (dir) {
139 for(s=path; s<p; )
140 *dir++ = *s++;
141
142 *dir = '\0';
143 }
144 }
145
146 #endif
147
148 /*
149 void main() // test splipath()
150 {
151 TCHAR drv[_MAX_DRIVE+1], dir[_MAX_DIR], name[_MAX_FNAME], ext[_MAX_EXT];
152
153 _tsplitpath(L"x\\y", drv, dir, name, ext);
154 _tsplitpath(L"x\\", drv, dir, name, ext);
155 _tsplitpath(L"\\x", drv, dir, name, ext);
156 _tsplitpath(L"x", drv, dir, name, ext);
157 _tsplitpath(L"", drv, dir, name, ext);
158 _tsplitpath(L".x", drv, dir, name, ext);
159 _tsplitpath(L":x", drv, dir, name, ext);
160 _tsplitpath(L"a:x", drv, dir, name, ext);
161 _tsplitpath(L"a.b:x", drv, dir, name, ext);
162 _tsplitpath(L"W:\\/\\abc/Z:~", drv, dir, name, ext);
163 _tsplitpath(L"abc.EFGH:12345", drv, dir, name, ext);
164 _tsplitpath(L"C:/dos/command.com", drv, dir, name, ext);
165 }
166 */