splitpath.c 5.5 KB

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