1 /* 2 * ReactOS kernel 3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 */ 19 /* 20 * PROJECT: ReactOS user32.dll 21 * FILE: win32ss/user/user32/windows/text.c 22 * PURPOSE: Input 23 * PROGRAMMER: Casper S. Hornstrup (chorns@users.sourceforge.net) 24 * UPDATE HISTORY: 25 * 09-05-2001 CSH Created 26 */ 27 28 #include <user32.h> 29 30 static WORD 31 GetC1Type(WCHAR Ch) 32 { 33 WORD CharType; 34 35 if (! GetStringTypeW(CT_CTYPE1, &Ch, 1, &CharType)) 36 { 37 return 0; 38 } 39 40 return CharType; 41 } 42 43 /* 44 * @implemented 45 */ 46 LPSTR 47 WINAPI 48 CharLowerA(LPSTR str) 49 { 50 if (!HIWORD(str)) 51 { 52 char ch = LOWORD(str); 53 CharLowerBuffA( &ch, 1 ); 54 return (LPSTR)(UINT_PTR)(BYTE)ch; 55 } 56 57 _SEH2_TRY 58 { 59 CharLowerBuffA( str, strlen(str) ); 60 } 61 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) 62 { 63 SetLastError( ERROR_INVALID_PARAMETER ); 64 _SEH2_YIELD(return NULL); 65 } 66 _SEH2_END; 67 68 return str; 69 } 70 71 /* 72 * @implemented 73 */ 74 DWORD 75 WINAPI 76 CharLowerBuffA(LPSTR str, DWORD len) 77 { 78 DWORD lenW; 79 WCHAR *strW; 80 if (!str) return 0; /* YES */ 81 82 lenW = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0); 83 strW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR)); 84 if (strW) { 85 MultiByteToWideChar(CP_ACP, 0, str, len, strW, lenW); 86 CharLowerBuffW(strW, lenW); 87 len = WideCharToMultiByte(CP_ACP, 0, strW, lenW, str, len, NULL, NULL); 88 HeapFree(GetProcessHeap(), 0, strW); 89 return len; 90 } 91 return 0; 92 } 93 94 /* 95 * @implemented 96 */ 97 DWORD 98 WINAPI 99 CharLowerBuffW(LPWSTR str, DWORD len) 100 { 101 DWORD ret = len; 102 if (!str) return 0; /* YES */ 103 for (; len; len--, str++) *str = towlower(*str); 104 return ret; 105 } 106 107 /* 108 * @implemented 109 */ 110 LPWSTR 111 WINAPI 112 CharLowerW(LPWSTR x) 113 { 114 if (HIWORD(x)) return strlwrW(x); 115 else return (LPWSTR)((UINT_PTR)tolowerW(LOWORD(x))); 116 } 117 118 /* 119 * @implemented 120 */ 121 LPWSTR 122 WINAPI 123 CharPrevW(LPCWSTR start, LPCWSTR x) 124 { 125 if (x > start) return (LPWSTR)(x-1); 126 else return (LPWSTR)x; 127 } 128 129 /* 130 * @implemented 131 */ 132 LPSTR 133 WINAPI 134 CharNextA(LPCSTR ptr) 135 { 136 if (!*ptr) return (LPSTR)ptr; 137 if (IsDBCSLeadByte(ptr[0]) && ptr[1]) return (LPSTR)(ptr + 2); 138 return (LPSTR)(ptr + 1); 139 } 140 141 /* 142 * @implemented 143 */ 144 LPSTR 145 WINAPI 146 CharNextExA(WORD codepage, LPCSTR ptr, DWORD flags) 147 { 148 if (!*ptr) return (LPSTR)ptr; 149 if (IsDBCSLeadByteEx(codepage, ptr[0]) && ptr[1]) return (LPSTR)(ptr + 2); 150 return (LPSTR)(ptr + 1); 151 } 152 153 /* 154 * @implemented 155 */ 156 LPWSTR 157 WINAPI 158 CharNextW(LPCWSTR x) 159 { 160 if (*x) x++; 161 return (LPWSTR)x; 162 } 163 164 /* 165 * @implemented 166 */ 167 LPSTR 168 WINAPI 169 CharPrevA(LPCSTR start, LPCSTR ptr) 170 { 171 while (*start && (start < ptr)) { 172 LPCSTR next = CharNextA(start); 173 if (next >= ptr) break; 174 start = next; 175 } 176 return (LPSTR)start; 177 } 178 179 /* 180 * @implemented 181 */ 182 LPSTR WINAPI CharPrevExA( WORD codepage, LPCSTR start, LPCSTR ptr, DWORD flags ) 183 { 184 while (*start && (start < ptr)) 185 { 186 LPCSTR next = CharNextExA( codepage, start, flags ); 187 if (next > ptr) break; 188 start = next; 189 } 190 return (LPSTR)start; 191 } 192 193 /* 194 * @implemented 195 */ 196 BOOL 197 WINAPI 198 CharToOemA(LPCSTR s, LPSTR d) 199 { 200 if (!s || !d) return FALSE; 201 return CharToOemBuffA(s, d, strlen(s) + 1); 202 } 203 204 /* 205 * @implemented 206 */ 207 BOOL 208 WINAPI 209 CharToOemBuffA(LPCSTR s, LPSTR d, DWORD len) 210 { 211 WCHAR* bufW; 212 213 if ( !s || !d ) return FALSE; 214 215 bufW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)); 216 if (bufW) { 217 MultiByteToWideChar(CP_ACP, 0, s, len, bufW, len); 218 WideCharToMultiByte(CP_OEMCP, 0, bufW, len, d, len, NULL, NULL); 219 HeapFree(GetProcessHeap(), 0, bufW); 220 } 221 return TRUE; 222 } 223 224 /* 225 * @implemented 226 */ 227 BOOL 228 WINAPI 229 CharToOemBuffW(LPCWSTR s, LPSTR d, DWORD len) 230 { 231 if (!s || !d) 232 return FALSE; 233 WideCharToMultiByte(CP_OEMCP, 0, s, len, d, len, NULL, NULL); 234 return TRUE; 235 } 236 237 /* 238 * @implemented 239 */ 240 BOOL 241 WINAPI 242 CharToOemW(LPCWSTR s, LPSTR d) 243 { 244 if ( !s || !d ) return FALSE; 245 return CharToOemBuffW(s, d, wcslen(s) + 1); 246 } 247 248 /* 249 * @implemented 250 */ 251 LPSTR WINAPI CharUpperA(LPSTR str) 252 { 253 if (!HIWORD(str)) 254 { 255 char ch = LOWORD(str); 256 CharUpperBuffA( &ch, 1 ); 257 return (LPSTR)(UINT_PTR)(BYTE)ch; 258 } 259 260 _SEH2_TRY 261 { 262 CharUpperBuffA( str, strlen(str) ); 263 } 264 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) 265 { 266 SetLastError( ERROR_INVALID_PARAMETER ); 267 _SEH2_YIELD(return NULL); 268 } 269 _SEH2_END; 270 271 return str; 272 } 273 274 /* 275 * @implemented 276 */ 277 DWORD 278 WINAPI 279 CharUpperBuffA(LPSTR str, DWORD len) 280 { 281 DWORD lenW; 282 WCHAR* strW; 283 if (!str) return 0; /* YES */ 284 285 lenW = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0); 286 strW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR)); 287 if (strW) { 288 MultiByteToWideChar(CP_ACP, 0, str, len, strW, lenW); 289 CharUpperBuffW(strW, lenW); 290 len = WideCharToMultiByte(CP_ACP, 0, strW, lenW, str, len, NULL, NULL); 291 HeapFree(GetProcessHeap(), 0, strW); 292 return len; 293 } 294 return 0; 295 } 296 297 /* 298 * @implemented 299 */ 300 DWORD 301 WINAPI 302 CharUpperBuffW(LPWSTR str, DWORD len) 303 { 304 DWORD ret = len; 305 if (!str) return 0; /* YES */ 306 for (; len; len--, str++) *str = towupper(*str); 307 return ret; 308 } 309 310 /* 311 * @implemented 312 */ 313 LPWSTR 314 WINAPI 315 CharUpperW(LPWSTR x) 316 { 317 if (HIWORD(x)) return struprW(x); 318 else return (LPWSTR)((UINT_PTR)toupperW(LOWORD(x))); 319 } 320 321 /* 322 * @implemented 323 */ 324 BOOL 325 WINAPI 326 IsCharAlphaA(CHAR Ch) 327 { 328 WCHAR WCh; 329 330 MultiByteToWideChar(CP_ACP, 0, &Ch, 1, &WCh, 1); 331 return IsCharAlphaW(WCh); 332 } 333 334 /* 335 * @implemented 336 */ 337 BOOL 338 WINAPI 339 IsCharAlphaNumericA(CHAR Ch) 340 { 341 WCHAR WCh; 342 343 MultiByteToWideChar(CP_ACP, 0, &Ch, 1, &WCh, 1); 344 return IsCharAlphaNumericW(WCh); 345 } 346 347 /* 348 * @implemented 349 */ 350 BOOL 351 WINAPI 352 IsCharAlphaNumericW(WCHAR Ch) 353 { 354 return (GetC1Type(Ch) & (C1_ALPHA|C1_DIGIT)) != 0; 355 } 356 357 /* 358 * @implemented 359 */ 360 BOOL 361 WINAPI 362 IsCharAlphaW(WCHAR Ch) 363 { 364 return (GetC1Type(Ch) & C1_ALPHA) != 0; 365 } 366 367 /* 368 * @implemented 369 */ 370 BOOL 371 WINAPI 372 IsCharLowerA(CHAR Ch) 373 { 374 WCHAR WCh; 375 376 MultiByteToWideChar(CP_ACP, 0, &Ch, 1, &WCh, 1); 377 return IsCharLowerW(WCh); 378 } 379 380 /* 381 * @implemented 382 */ 383 BOOL 384 WINAPI 385 IsCharLowerW(WCHAR Ch) 386 { 387 return (GetC1Type(Ch) & C1_LOWER) != 0; 388 } 389 390 /* 391 * @implemented 392 */ 393 BOOL 394 WINAPI 395 IsCharUpperA(CHAR Ch) 396 { 397 WCHAR WCh; 398 399 MultiByteToWideChar(CP_ACP, 0, &Ch, 1, &WCh, 1); 400 return IsCharUpperW(WCh); 401 } 402 403 /* 404 * @implemented 405 */ 406 BOOL 407 WINAPI 408 IsCharUpperW(WCHAR Ch) 409 { 410 return (GetC1Type(Ch) & C1_UPPER) != 0; 411 } 412 413 /* 414 * @implemented 415 */ 416 BOOL 417 WINAPI 418 OemToCharA(LPCSTR s, LPSTR d) 419 { 420 if ( !s || !d ) return FALSE; 421 return OemToCharBuffA(s, d, strlen(s) + 1); 422 } 423 424 /* 425 * @implemented 426 */ 427 BOOL WINAPI OemToCharBuffA(LPCSTR s, LPSTR d, DWORD len) 428 { 429 WCHAR* bufW; 430 431 if ( !s || !d ) return FALSE; 432 433 bufW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)); 434 if (bufW) { 435 MultiByteToWideChar(CP_OEMCP, 0, s, len, bufW, len); 436 WideCharToMultiByte(CP_ACP, 0, bufW, len, d, len, NULL, NULL); 437 HeapFree(GetProcessHeap(), 0, bufW); 438 } 439 return TRUE; 440 } 441 442 /* 443 * @implemented 444 */ 445 BOOL 446 WINAPI 447 OemToCharBuffW(LPCSTR s, LPWSTR d, DWORD len) 448 { 449 if ( !s || !d ) return FALSE; 450 MultiByteToWideChar(CP_OEMCP, 0, s, len, d, len); 451 return TRUE; 452 } 453 454 /* 455 * @implemented 456 */ 457 BOOL WINAPI OemToCharW(LPCSTR s, LPWSTR d) 458 { 459 if ( !s || !d ) return FALSE; 460 return OemToCharBuffW(s, d, strlen(s) + 1); 461 } 462 463 /* EOF */ 464