Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenff_string.c
Go to the documentation of this file.
00001 /***************************************************************************** 00002 * FullFAT - High Performance, Thread-Safe Embedded FAT File-System * 00003 * Copyright (C) 2009 James Walmsley (james@worm.me.uk) * 00004 * * 00005 * This program is free software: you can redistribute it and/or modify * 00006 * it under the terms of the GNU General Public License as published by * 00007 * the Free Software Foundation, either version 3 of the License, or * 00008 * (at your option) any later version. * 00009 * * 00010 * This program is distributed in the hope that it will be useful, * 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00013 * GNU General Public License for more details. * 00014 * * 00015 * You should have received a copy of the GNU General Public License * 00016 * along with this program. If not, see <http://www.gnu.org/licenses/>. * 00017 * * 00018 * IMPORTANT NOTICE: * 00019 * ================= * 00020 * Alternative Licensing is available directly from the Copyright holder, * 00021 * (James Walmsley). For more information consult LICENSING.TXT to obtain * 00022 * a Commercial license. * 00023 * * 00024 * See RESTRICTIONS.TXT for extra restrictions on the use of FullFAT. * 00025 * * 00026 * Removing the above notice is illegal and will invalidate this license. * 00027 ***************************************************************************** 00028 * See http://worm.me.uk/fullfat for more information. * 00029 * Or http://fullfat.googlecode.com/ for latest releases and the wiki. * 00030 *****************************************************************************/ 00031 00043 #include <stdlib.h> 00044 #include <string.h> 00045 #include <ctype.h> 00046 #include "ff_string.h" 00047 #include "ff_error.h" 00048 00049 #ifdef FF_UNICODE_SUPPORT 00050 #include <wchar.h> 00051 #include <wctype.h> 00052 #endif 00053 00054 /* 00055 * These will eventually be moved into a platform independent string 00056 * library. Which will be optional. (To allow the use of system specific versions). 00057 */ 00058 00059 #ifdef FF_UNICODE_SUPPORT 00060 00061 void FF_cstrntowcs(FF_T_WCHAR *wcsDest, const FF_T_INT8 *szpSource, FF_T_UINT32 len) { 00062 while(*szpSource && len--) { 00063 *wcsDest++ = *szpSource++; 00064 } 00065 *wcsDest = '\0'; 00066 } 00067 00068 void FF_cstrtowcs(FF_T_WCHAR *wcsDest, const FF_T_INT8 *szpSource) { 00069 while(*szpSource) { 00070 *wcsDest++ = (FF_T_WCHAR) *szpSource++; 00071 } 00072 *wcsDest = '\0'; 00073 } 00074 00075 void FF_wcstocstr(FF_T_INT8 *szpDest, const FF_T_WCHAR *wcsSource) { 00076 while(*wcsSource) { 00077 *szpDest++ = (FF_T_INT8) *wcsSource++; 00078 } 00079 *szpDest = '\0'; 00080 } 00081 00082 void FF_wcsntocstr(FF_T_INT8 *szpDest, const FF_T_WCHAR *wcsSource, FF_T_UINT32 len) { 00083 while(*wcsSource && len--) { 00084 *szpDest++ = (FF_T_INT8) *wcsSource++; 00085 } 00086 *szpDest = '\0'; 00087 } 00088 00089 #endif 00090 00095 #ifndef FF_UNICODE_SUPPORT 00096 00100 void FF_toupper(FF_T_INT8 *string, FF_T_UINT32 strLen) { 00101 FF_T_UINT32 i; 00102 for(i = 0; i < strLen; i++) { 00103 if(string[i] >= 'a' && string[i] <= 'z') 00104 string[i] -= 32; 00105 if(string[i] == '\0') 00106 break; 00107 } 00108 } 00109 void FF_tolower(FF_T_INT8 *string, FF_T_UINT32 strLen) { 00110 FF_T_UINT32 i; 00111 for(i = 0; i < strLen; i++) { 00112 if(string[i] >= 'A' && string[i] <= 'Z') 00113 string[i] += 32; 00114 if(string[i] == '\0') 00115 break; 00116 } 00117 } 00118 00119 #else 00120 void FF_toupper(FF_T_WCHAR *string, FF_T_UINT32 strLen) { 00121 FF_T_UINT32 i; 00122 for(i = 0; i < strLen; i++) { 00123 string[i] = towupper(string[i]); 00124 } 00125 } 00126 void FF_tolower(FF_T_WCHAR *string, FF_T_UINT32 strLen) { 00127 FF_T_UINT32 i; 00128 for(i = 0; i < strLen; i++) { 00129 string[i] = towlower(string[i]); 00130 } 00131 } 00132 #endif 00133 00134 00135 00136 00144 #ifndef FF_UNICODE_SUPPORT 00145 FF_T_BOOL FF_strmatch(const FF_T_INT8 *str1, const FF_T_INT8 *str2, FF_T_UINT16 len) { 00146 register FF_T_UINT16 i; 00147 register FF_T_INT8 char1, char2; 00148 00149 if(!len) { 00150 if(strlen(str1) != strlen(str2)) { 00151 return FF_FALSE; 00152 } 00153 len = (FF_T_UINT16) strlen(str1); 00154 } 00155 00156 for(i = 0; i < len; i++) { 00157 char1 = str1[i]; 00158 char2 = str2[i]; 00159 if(char1 >= 'A' && char1 <= 'Z') { 00160 char1 += 32; 00161 } 00162 if(char2 >= 'A' && char2 <= 'Z') { 00163 char2 += 32; 00164 } 00165 00166 if(char1 != char2) { 00167 return FF_FALSE; 00168 } 00169 } 00170 00171 return FF_TRUE; 00172 } 00173 #else 00174 00175 FF_T_BOOL FF_strmatch(const FF_T_WCHAR *str1, const FF_T_WCHAR *str2, FF_T_UINT16 len) { 00176 register FF_T_UINT16 i; 00177 register FF_T_WCHAR char1, char2; 00178 00179 if(!len) { 00180 if(wcslen(str1) != wcslen(str2)) { 00181 return FF_FALSE; 00182 } 00183 len = (FF_T_UINT16) wcslen(str1); 00184 } 00185 00186 for(i = 0; i < len; i++) { 00187 char1 = towlower(str1[i]); 00188 char2 = towlower(str2[i]); 00189 if(char1 != char2) { 00190 return FF_FALSE; 00191 } 00192 } 00193 00194 return FF_TRUE; 00195 } 00196 #endif 00197 00204 #ifndef FF_UNICODE_SUPPORT 00205 FF_T_INT8 *FF_strtok(const FF_T_INT8 *string, FF_T_INT8 *token, FF_T_UINT16 *tokenNumber, FF_T_BOOL *last, FF_T_UINT16 Length) { 00206 FF_T_UINT16 strLen = Length; 00207 FF_T_UINT16 i,y, tokenStart, tokenEnd = 0; 00208 00209 i = 0; 00210 y = 0; 00211 00212 if(string[i] == '\\' || string[i] == '/') { 00213 i++; 00214 } 00215 00216 tokenStart = i; 00217 00218 while(i < strLen) { 00219 if(string[i] == '\\' || string[i] == '/') { 00220 y++; 00221 if(y == *tokenNumber) { 00222 tokenStart = (FF_T_UINT16)(i + 1); 00223 } 00224 if(y == (*tokenNumber + 1)) { 00225 tokenEnd = i; 00226 break; 00227 } 00228 } 00229 i++; 00230 } 00231 00232 if(!tokenEnd) { 00233 if(*last == FF_TRUE) { 00234 return NULL; 00235 } else { 00236 *last = FF_TRUE; 00237 } 00238 tokenEnd = i; 00239 } 00240 if((tokenEnd - tokenStart) < FF_MAX_FILENAME) { 00241 memcpy(token, (string + tokenStart), (FF_T_UINT32)(tokenEnd - tokenStart)); 00242 token[tokenEnd - tokenStart] = '\0'; 00243 } else { 00244 memcpy(token, (string + tokenStart), (FF_T_UINT32)(FF_MAX_FILENAME)); 00245 token[FF_MAX_FILENAME-1] = '\0'; 00246 } 00247 //token[tokenEnd - tokenStart] = '\0'; 00248 *tokenNumber += 1; 00249 00250 return token; 00251 } 00252 00253 #else 00254 FF_T_WCHAR *FF_strtok(const FF_T_WCHAR *string, FF_T_WCHAR *token, FF_T_UINT16 *tokenNumber, FF_T_BOOL *last, FF_T_UINT16 Length) { 00255 FF_T_UINT16 strLen = Length; 00256 FF_T_UINT16 i,y, tokenStart, tokenEnd = 0; 00257 00258 i = 0; 00259 y = 0; 00260 00261 if(string[i] == '\\' || string[i] == '/') { 00262 i++; 00263 } 00264 00265 tokenStart = i; 00266 00267 while(i < strLen) { 00268 if(string[i] == '\\' || string[i] == '/') { 00269 y++; 00270 if(y == *tokenNumber) { 00271 tokenStart = (FF_T_UINT16)(i + 1); 00272 } 00273 if(y == (*tokenNumber + 1)) { 00274 tokenEnd = i; 00275 break; 00276 } 00277 } 00278 i++; 00279 } 00280 00281 if(!tokenEnd) { 00282 if(*last == FF_TRUE) { 00283 return NULL; 00284 } else { 00285 *last = FF_TRUE; 00286 } 00287 tokenEnd = i; 00288 } 00289 if((tokenEnd - tokenStart) < FF_MAX_FILENAME) { 00290 memcpy(token, (string + tokenStart), (FF_T_UINT32)(tokenEnd - tokenStart) * sizeof(FF_T_WCHAR)); 00291 token[tokenEnd - tokenStart] = '\0'; 00292 } else { 00293 memcpy(token, (string + tokenStart), (FF_T_UINT32)(FF_MAX_FILENAME) * sizeof(FF_T_WCHAR)); 00294 token[FF_MAX_FILENAME-1] = '\0'; 00295 } 00296 //token[tokenEnd - tokenStart] = '\0'; 00297 *tokenNumber += 1; 00298 00299 return token; 00300 } 00301 #endif 00302 00303 /* 00304 A Wild-Card Comparator Library function, Provided by Adam Fullerton. 00305 This can be extended or altered to improve or advance wildCard matching 00306 of the FF_FindFirst() and FF_FindNext() API's. 00307 */ 00308 #ifdef FF_FINDAPI_ALLOW_WILDCARDS 00309 /*FF_T_BOOL FF_wildcompare(const FF_T_INT8 *pszWildCard, const FF_T_INT8 *pszString) { 00310 // Check to see if the string contains the wild card 00311 if (!memchr(pszWildCard, '*', strlen(pszWildCard))) 00312 { 00313 // if it does not then do a straight string compare 00314 if (strcmp(pszWildCard, pszString)) 00315 { 00316 return FF_FALSE; 00317 } 00318 } 00319 else 00320 { 00321 while ((*pszWildCard) 00322 && (*pszString)) 00323 { 00324 // Test for the wild card 00325 if (*pszWildCard == '*') 00326 { 00327 // Eat more than one 00328 while (*pszWildCard == '*') 00329 { 00330 pszWildCard++; 00331 } 00332 // If there are more chars in the string 00333 if (*pszWildCard) 00334 { 00335 // Search for the next char 00336 pszString = memchr(pszString, (int)*pszWildCard, strlen(pszString)); 00337 // if it does not exist then the strings don't match 00338 if (!pszString) 00339 { 00340 return FF_FALSE; 00341 } 00342 00343 } 00344 else 00345 { 00346 if (*pszWildCard) 00347 { 00348 // continue 00349 break; 00350 } 00351 else 00352 { 00353 return FF_TRUE; 00354 } 00355 } 00356 } 00357 else 00358 { 00359 // Fail if they don't match 00360 if (*pszWildCard != *pszString) 00361 { 00362 return FF_FALSE; 00363 } 00364 } 00365 // Bump both pointers 00366 pszWildCard++; 00367 pszString++; 00368 } 00369 // fail if different lengths 00370 if (*pszWildCard != *pszString) 00371 { 00372 return FF_FALSE; 00373 } 00374 } 00375 00376 return FF_TRUE; 00377 }*/ 00378 /* 00379 This is a better Wild-card compare function, that works perfectly, and is much more efficient. 00380 This function was contributed by one of our commercial customers. 00381 */ 00382 #ifdef FF_UNICODE_SUPPORT 00383 FF_T_BOOL FF_wildcompare(const FF_T_WCHAR *pszWildCard, const FF_T_WCHAR *pszString) { 00384 register const FF_T_WCHAR *pszWc = NULL; 00385 register const FF_T_WCHAR *pszStr = NULL; // Encourage the string pointers to be placed in memory. 00386 do { 00387 if ( *pszWildCard == '*' ) { 00388 while(*(1 + pszWildCard++) == '*'); // Eat up multiple '*''s 00389 pszWc = (pszWildCard - 1); 00390 pszStr = pszString; 00391 } 00392 if (*pszWildCard == '?' && !*pszString) { 00393 return FF_FALSE; // False when the string is ended, yet a ? charachter is demanded. 00394 } 00395 #ifdef FF_WILDCARD_CASE_INSENSITIVE 00396 if (*pszWildCard != '?' && tolower(*pszWildCard) != tolower(*pszString)) { 00397 #else 00398 if (*pszWildCard != '?' && *pszWildCard != *pszString) { 00399 #endif 00400 if (pszWc == NULL) { 00401 return FF_FALSE; 00402 } 00403 pszWildCard = pszWc; 00404 pszString = pszStr++; 00405 } 00406 } while ( *pszWildCard++ && *pszString++ ); 00407 00408 while(*pszWildCard == '*') { 00409 pszWildCard++; 00410 } 00411 00412 if(!*(pszWildCard - 1)) { // WildCard is at the end. (Terminated) 00413 return FF_TRUE; // Therefore this must be a match. 00414 } 00415 00416 return FF_FALSE; // If not, then return FF_FALSE! 00417 } 00418 #else 00419 FF_T_BOOL FF_wildcompare(const FF_T_INT8 *pszWildCard, const FF_T_INT8 *pszString) { 00420 register const FF_T_INT8 *pszWc = NULL; 00421 register const FF_T_INT8 *pszStr = NULL; // Encourage the string pointers to be placed in memory. 00422 do { 00423 if ( *pszWildCard == '*' ) { 00424 while(*(1 + pszWildCard++) == '*'); // Eat up multiple '*''s 00425 pszWc = (pszWildCard - 1); 00426 pszStr = pszString; 00427 } 00428 if (*pszWildCard == '?' && !*pszString) { 00429 return FF_FALSE; // False when the string is ended, yet a ? charachter is demanded. 00430 } 00431 #ifdef FF_WILDCARD_CASE_INSENSITIVE 00432 if (*pszWildCard != '?' && tolower(*pszWildCard) != tolower(*pszString)) { 00433 #else 00434 if (*pszWildCard != '?' && *pszWildCard != *pszString) { 00435 #endif 00436 if (pszWc == NULL) { 00437 return FF_FALSE; 00438 } 00439 pszWildCard = pszWc; 00440 pszString = pszStr++; 00441 } 00442 } while ( *pszWildCard++ && *pszString++ ); 00443 00444 while(*pszWildCard == '*') { 00445 pszWildCard++; 00446 } 00447 00448 if(!*(pszWildCard - 1)) { // WildCard is at the end. (Terminated) 00449 return FF_TRUE; // Therefore this must be a match. 00450 } 00451 00452 return FF_FALSE; // If not, then return FF_FALSE! 00453 } 00454 #endif 00455 00456 #endif Generated on Mon May 28 2012 04:33:57 for ReactOS by
1.7.6.1
|