7500c920f9690aaeebd63af8617522755c9db9b5
[reactos.git] / reactos / lib / sdk / crt / string / splitp.c
1 /*
2 * PROJECT: ReactOS Kernel
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * PURPOSE: CRT: implementation of _splitpath / _wsplitpath
5 * PROGRAMMERS: Timo Kreuzer
6 */
7
8 #include <precomp.h>
9 #include <tchar.h>
10
11 /*
12 * @implemented
13 */
14 void _tsplitpath(const _TCHAR* path, _TCHAR* drive, _TCHAR* dir, _TCHAR* fname, _TCHAR* ext)
15 {
16 const _TCHAR *src, *dir_start, *file_start = 0, *ext_start = 0;
17
18 /* Truncate all output strings */
19 if (drive) drive[0] = '\0';
20 if (dir) dir[0] = '\0';
21 if (fname) fname[0] = '\0';
22 if (ext) ext[0] = '\0';
23
24 #if WINVER >= 0x600
25 /* Check parameter */
26 if (!path)
27 {
28 #ifndef _LIBCNT_
29 _set_errno(EINVAL);
30 #endif
31 return;
32 }
33 #endif
34
35 _Analysis_assume_(path != 0);
36
37 #if WINVER == 0x600
38 /* Skip '\\?\' prefix */
39 if ((path[0] == '\\') && (path[1] == '\\') &&
40 (path[2] == '?') && (path[3] == '\\')) path += 4;
41 #endif
42
43 if (path[0] == '\0') return;
44
45 /* Check if we have a drive letter (only 1 char supported) */
46 if (path[1] == ':')
47 {
48 if (drive)
49 {
50 drive[0] = path[0];
51 drive[1] = ':';
52 drive[2] = '\0';
53 }
54 path += 2;
55 }
56
57 /* Scan the rest of the string */
58 dir_start = path;
59 while (*path != '\0')
60 {
61 /* Remember last path seperator and last dot */
62 if ((*path == '\\') || (*path == '/')) file_start = path + 1;
63 if (*path == '.') ext_start = path;
64 path++;
65 }
66
67 /* Check if we got a file name / extension */
68 if (!file_start)
69 file_start = dir_start;
70 if (!ext_start || ext_start < file_start)
71 ext_start = path;
72
73 if (dir)
74 {
75 src = dir_start;
76 while (src < file_start) *dir++ = *src++;
77 *dir = '\0';
78 }
79
80 if (fname)
81 {
82 src = file_start;
83 while (src < ext_start) *fname++ = *src++;
84 *fname = '\0';
85 }
86
87 if (ext)
88 {
89 src = ext_start;
90 while (*src != '\0') *ext++ = *src++;
91 *ext = '\0';
92 }
93 }
94