splitpath.c

Go to the documentation of this file.
00001 /*
00002  * Copyright 2000 Martin Fuchs
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00019 #include "utility.h"
00020 
00021 #ifdef UNICODE
00022 
00023 void _wsplitpath(const WCHAR* path, WCHAR* drv, WCHAR* dir, WCHAR* name, WCHAR* ext)
00024 {
00025         const WCHAR* end; /* end of processed string */
00026     const WCHAR* p;   /* search pointer */
00027     const WCHAR* s;   /* copy pointer */
00028 
00029     /* extract drive name */
00030     if (path[0] && path[1]==':') {
00031         if (drv) {
00032             *drv++ = *path++;
00033             *drv++ = *path++;
00034             *drv = L'\0';
00035         }
00036     } else if (drv)
00037         *drv = L'\0';
00038 
00039     /* search for end of string or stream separator */
00040     for(end=path; *end && *end!=L':'; )
00041         end++;
00042 
00043     /* search for begin of file extension */
00044     for(p=end; p>path && *--p!=L'\\' && *p!=L'/'; )
00045         if (*p == L'.') {
00046             end = p;
00047             break;
00048         }
00049 
00050     if (ext)
00051         for(s=end; *ext=*s++; )
00052             ext++;
00053 
00054     /* search for end of directory name */
00055     for(p=end; p>path; )
00056         if (*--p=='\\' || *p=='/') {
00057             p++;
00058             break;
00059         }
00060 
00061     if (name) {
00062         for(s=p; s<end; )
00063             *name++ = *s++;
00064 
00065         *name = L'\0';
00066     }
00067 
00068     if (dir) {
00069         for(s=path; s<p; )
00070             *dir++ = *s++;
00071 
00072         *dir = L'\0';
00073     }
00074 }
00075 
00076 #else
00077 
00078 void _splitpath(const CHAR* path, CHAR* drv, CHAR* dir, CHAR* name, CHAR* ext)
00079 {
00080         const CHAR* end; /* end of processed string */
00081     const CHAR* p;   /* search pointer */
00082     const CHAR* s;   /* copy pointer */
00083 
00084     /* extract drive name */
00085     if (path[0] && path[1]==':') {
00086         if (drv) {
00087             *drv++ = *path++;
00088             *drv++ = *path++;
00089             *drv = '\0';
00090         }
00091     } else if (drv)
00092         *drv = '\0';
00093 
00094     /* search for end of string or stream separator */
00095     for(end=path; *end && *end!=':'; )
00096         end++;
00097 
00098     /* search for begin of file extension */
00099     for(p=end; p>path && *--p!='\\' && *p!='/'; )
00100         if (*p == '.') {
00101             end = p;
00102             break;
00103         }
00104 
00105     if (ext)
00106         for(s=end; (*ext=*s++); )
00107             ext++;
00108 
00109     /* search for end of directory name */
00110     for(p=end; p>path; )
00111         if (*--p=='\\' || *p=='/') {
00112             p++;
00113             break;
00114         }
00115 
00116     if (name) {
00117         for(s=p; s<end; )
00118             *name++ = *s++;
00119 
00120         *name = '\0';
00121     }
00122 
00123     if (dir) {
00124         for(s=path; s<p; )
00125             *dir++ = *s++;
00126 
00127         *dir = '\0';
00128     }
00129 }
00130 
00131 #endif
00132 
00133 /*
00134 void main() // test splipath()
00135 {
00136     TCHAR drv[_MAX_DRIVE+1], dir[_MAX_DIR], name[_MAX_FNAME], ext[_MAX_EXT];
00137 
00138     _tsplitpath(L"x\\y", drv, dir, name, ext);
00139     _tsplitpath(L"x\\", drv, dir, name, ext);
00140     _tsplitpath(L"\\x", drv, dir, name, ext);
00141     _tsplitpath(L"x", drv, dir, name, ext);
00142     _tsplitpath(L"", drv, dir, name, ext);
00143     _tsplitpath(L".x", drv, dir, name, ext);
00144     _tsplitpath(L":x", drv, dir, name, ext);
00145     _tsplitpath(L"a:x", drv, dir, name, ext);
00146     _tsplitpath(L"a.b:x", drv, dir, name, ext);
00147     _tsplitpath(L"W:\\/\\abc/Z:~", drv, dir, name, ext);
00148     _tsplitpath(L"abc.EFGH:12345", drv, dir, name, ext);
00149     _tsplitpath(L"C:/dos/command.com", drv, dir, name, ext);
00150 }
00151 */

ROS Explorer Source Code Documentation
generated on 02.01.2013 by doxygen

ROS Explorer Homepage