filestring.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. Copyright (C) 2004-2005 Michael Liebscher <johnnycanuck@users.sourceforge.net>
  3. Copyright (C) 1997-2001 Id Software, Inc.
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. /*
  17. * filestring.c: Portable file path/name manipulation methods.
  18. *
  19. * Author: Michael Liebscher <johnnycanuck@users.sourceforge.net>
  20. *
  21. * Acknowledgement:
  22. * This code was derived from Quake II, and was originally
  23. * written by Id Software, Inc.
  24. *
  25. */
  26. #include "../wolfiphone.h"
  27. /*
  28. -----------------------------------------------------------------------------
  29. Function: FS_CreatePath -Creates given path.
  30. Parameters: path -[in] Pointer to NULL terminated string that contains directory
  31. path.
  32. Returns: Nothing.
  33. Notes: Creates any directories needed to store the given filename.
  34. -----------------------------------------------------------------------------
  35. */
  36. PUBLIC void FS_CreatePath( char *path )
  37. {
  38. char *ofs;
  39. for( ofs = path + 1; *ofs; ofs++ )
  40. {
  41. if( *ofs == '/' )
  42. { // create the directory
  43. *ofs = '\0';
  44. FS_CreateDirectory( path );
  45. *ofs = '/';
  46. }
  47. }
  48. }
  49. PUBLIC void FS_FilePath( char *in, char *out )
  50. {
  51. char *s;
  52. s = in + strlen( in ) - 1;
  53. while( s != in && *s != '/' )
  54. {
  55. s--;
  56. }
  57. strncpy( out, in, s-in );
  58. out[ s - in ] = '\0'; // NUL-terminate string.
  59. }
  60. PUBLIC char *FS_SkipPath( char *pathname )
  61. {
  62. char *last;
  63. last = pathname;
  64. while( *pathname )
  65. {
  66. if( *pathname == '/' )
  67. {
  68. last = pathname + 1;
  69. }
  70. pathname++;
  71. }
  72. return last;
  73. }
  74. PUBLIC void FS_StripExtension( char *in, char *out )
  75. {
  76. while( *in && *in != '.' )
  77. {
  78. *out++ = *in++;
  79. }
  80. *out = '\0'; // NUL-terminate string.
  81. }
  82. PUBLIC char *FS_FileExtension( char *in )
  83. {
  84. static char exten[ 8 ];
  85. int i;
  86. while( *in && *in != '.' )
  87. {
  88. in++;
  89. }
  90. if( ! *in )
  91. {
  92. return "";
  93. }
  94. in++;
  95. for( i = 0 ; i < 7 && *in ; i++, in++ )
  96. {
  97. exten[ i ] = *in;
  98. }
  99. exten[ i ] = '\0'; // NUL-terminate string.
  100. return exten;
  101. }
  102. PUBLIC void FS_FileBase( char *in, char *out )
  103. {
  104. char *s, *s2;
  105. s = in + strlen( in ) - 1;
  106. while( s != in && *s != '.' )
  107. {
  108. s--;
  109. }
  110. for( s2 = s ; s2 != in && *s2 != '/' ; s2-- )
  111. {
  112. ;
  113. }
  114. if( s - s2 < 2 )
  115. {
  116. out[ 0 ] = '\0'; // NUL-terminate string.
  117. }
  118. else
  119. {
  120. s--;
  121. strncpy( out, s2 + 1, s - s2 );
  122. out[ s - s2 ] = '\0'; // NUL-terminate string.
  123. }
  124. }