splitpath.c 2.29 KB
Newer Older
1
/*
Martin Fuchs's avatar
Martin Fuchs committed
2
 * Copyright 2000, 2004 Martin Fuchs
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 18
 */

Mike McCormack's avatar
Mike McCormack committed
19
#include "winefile.h"
Martin Fuchs's avatar
Martin Fuchs committed
20 21


22 23
void _wsplitpath(const WCHAR* path, WCHAR* drv, WCHAR* dir, WCHAR* name, WCHAR* ext)
{
24 25 26
        const WCHAR* end; /* end of processed string */
	const WCHAR* p;	  /* search pointer */
	const WCHAR* s;	  /* copy pointer */
27

28
	/* extract drive name */
29 30 31 32
	if (path[0] && path[1]==':') {
		if (drv) {
			*drv++ = *path++;
			*drv++ = *path++;
33
			*drv = '\0';
34 35
		}
	} else if (drv)
36
		*drv = '\0';
37

38
        end = path + lstrlenW(path);
39

40
	/* search for begin of file extension */
41 42
	for(p=end; p>path && *--p!='\\' && *p!='/'; )
		if (*p == '.') {
43 44 45 46 47
			end = p;
			break;
		}

	if (ext)
Martin Fuchs's avatar
Martin Fuchs committed
48
		for(s=end; (*ext=*s++); )
49 50
			ext++;

51
	/* search for end of directory name */
52 53 54 55 56 57 58 59 60 61
	for(p=end; p>path; )
		if (*--p=='\\' || *p=='/') {
			p++;
			break;
		}

	if (name) {
		for(s=p; s<end; )
			*name++ = *s++;

62
		*name = '\0';
63 64 65 66 67 68
	}

	if (dir) {
		for(s=path; s<p; )
			*dir++ = *s++;

69
		*dir = '\0';
70 71
	}
}
Martin Fuchs's avatar
Martin Fuchs committed
72

73 74 75 76

/*
void main()	// test splipath()
{
77 78 79 80 81 82 83 84 85 86 87 88 89 90
	WCHAR drv[_MAX_DRIVE+1], dir[_MAX_DIR], name[_MAX_FNAME], ext[_MAX_EXT];

	_wsplitpath(L"x\\y", drv, dir, name, ext);
	_wsplitpath(L"x\\", drv, dir, name, ext);
	_wsplitpath(L"\\x", drv, dir, name, ext);
	_wsplitpath(L"x", drv, dir, name, ext);
	_wsplitpath(L"", drv, dir, name, ext);
	_wsplitpath(L".x", drv, dir, name, ext);
	_wsplitpath(L":x", drv, dir, name, ext);
	_wsplitpath(L"a:x", drv, dir, name, ext);
	_wsplitpath(L"a.b:x", drv, dir, name, ext);
	_wsplitpath(L"W:\\/\\abc/Z:~", drv, dir, name, ext);
	_wsplitpath(L"abc.EFGH:12345", drv, dir, name, ext);
	_wsplitpath(L"C:/dos/command.com", drv, dir, name, ext);
91 92
}
*/