Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

834 lines
21 KiB

  1. //#pragma title( "UString.hpp - Common string and character functions" )
  2. /*
  3. Copyright (c) 1995-1998, Mission Critical Software, Inc. All rights reserved.
  4. ===============================================================================
  5. Module - UString.hpp
  6. System - Common
  7. Author - Tom Bernhardt, Rich Denham
  8. Created - 1995-08-25
  9. Description - Common string and character functions.
  10. Many string and character functions defined in "string.h" are redefined
  11. here as overloaded inline functions with several extensions:
  12. o Both ANSI and UNICODE strings are supported.
  13. o Both ANSI and UNICODE characters are supported.
  14. o For ANSI, characters can be "char signed" or "char unsigned".
  15. o Functions that allow a length field, such as "lstrcpy" vs "lstrcpyn",
  16. are implemented as overloaded functions with optional arguments.
  17. o Some functions, UStrCpy in particular, can perform conversion between
  18. ANSI and UNICODE strings.
  19. The function names defined here consist of "U" concatenated to the base
  20. name from "string.h". The first letter of words or word abbreviations
  21. are capitalized, e.g. "strcpy" becomes "UStrCpy".
  22. Updates -
  23. ===============================================================================
  24. */
  25. #ifndef MCSINC_UString_hpp
  26. #define MCSINC_UString_hpp
  27. #define safecopy(trg,src) ((src) ? UStrCpy(trg,src,DIM(trg)) : trg[0] = 0)
  28. #ifdef WIN16_VERSION
  29. #ifndef UCHAR
  30. #define UCHAR unsigned char
  31. #endif
  32. #include <string.h>
  33. #include <ctype.h>
  34. #endif // WIN16_VERSION
  35. int _inline // ret-length in chars
  36. UStrLen(
  37. char const * s1 // in -ANSI string
  38. )
  39. {
  40. return strlen( s1 );
  41. }
  42. int _inline // ret-length in chars
  43. UStrLen(
  44. UCHAR const * s1 // in -ANSI string
  45. )
  46. {
  47. return strlen( (char const *) s1 );
  48. }
  49. void _inline
  50. UStrCpy(
  51. char * aTarget ,// out-ANSI target string
  52. char const * aSource // in -ANSI source string
  53. )
  54. {
  55. strcpy( aTarget, aSource );
  56. }
  57. void _inline
  58. UStrCpy(
  59. char * aTarget ,// out-ANSI target string
  60. UCHAR const * aSource // in -ANSI source string
  61. )
  62. {
  63. UStrCpy( aTarget, (char const *) aSource );
  64. }
  65. void _inline
  66. UStrCpy(
  67. UCHAR * aTarget ,// out-ANSI target string
  68. char const * aSource // in -ANSI source string
  69. )
  70. {
  71. UStrCpy( (char *) aTarget, aSource );
  72. }
  73. void _inline
  74. UStrCpy(
  75. UCHAR * aTarget ,// out-ANSI target string
  76. UCHAR const * aSource // in -ANSI source string
  77. )
  78. {
  79. UStrCpy( (char *) aTarget, (char const *) aSource );
  80. }
  81. void _inline
  82. UStrCpy(
  83. char * aTarget ,// out-ANSI target string
  84. char const * aSource ,// in -ANSI source string
  85. int len // in -copy length in chars
  86. )
  87. {
  88. int copylen = UStrLen( aSource ) + 1;
  89. copylen = min( copylen, len );
  90. if ( copylen )
  91. {
  92. strncpy( aTarget, aSource, copylen );
  93. aTarget[copylen-1] = '\0';
  94. }
  95. }
  96. void _inline
  97. UStrCpy(
  98. char * aTarget ,// out-ANSI target string
  99. UCHAR const * aSource ,// in -ANSI source string
  100. int len // in -copy length in chars
  101. )
  102. {
  103. UStrCpy( aTarget, (char const *) aSource, len );
  104. }
  105. void _inline
  106. UStrCpy(
  107. UCHAR * aTarget ,// out-ANSI target string
  108. char const * aSource ,// in -ANSI source string
  109. int len // in -copy length in chars
  110. )
  111. {
  112. UStrCpy( (char *) aTarget, aSource, len );
  113. }
  114. void _inline
  115. UStrCpy(
  116. UCHAR * aTarget ,// out-ANSI target string
  117. UCHAR const * aSource ,// in -ANSI source string
  118. int len // in -copy length in chars
  119. )
  120. {
  121. UStrCpy( (char *) aTarget, (char const *) aSource, len );
  122. }
  123. int _inline // ret-compare result
  124. UStrCmp(
  125. char const * s1 ,// in -ANSI string 1
  126. char const * s2 // in -ANSI string 2
  127. )
  128. {
  129. if (s1 && s2)
  130. {
  131. return CompareStringA(LOCALE_SYSTEM_DEFAULT, 0, s1, -1, s2, -1) - 2;
  132. }
  133. else
  134. {
  135. return s1 ? 1 : (s2 ? -1 : 0);
  136. }
  137. }
  138. int _inline // ret-compare result
  139. UStrCmp(
  140. char const * s1 ,// in -ANSI string 1
  141. UCHAR const * s2 // in -ANSI string 2
  142. )
  143. {
  144. if (s1 && s2)
  145. {
  146. return CompareStringA(LOCALE_SYSTEM_DEFAULT, 0, s1, -1, (char const *) s2, -1) - 2;
  147. }
  148. else
  149. {
  150. return s1 ? 1 : (s2 ? -1 : 0);
  151. }
  152. }
  153. int _inline // ret-compare result
  154. UStrCmp(
  155. UCHAR const * s1 ,// in -ANSI string 1
  156. char const * s2 // in -ANSI string 2
  157. )
  158. {
  159. if (s1 && s2)
  160. {
  161. return CompareStringA(LOCALE_SYSTEM_DEFAULT, 0, (char const *) s1, -1, s2, -1) - 2;
  162. }
  163. else
  164. {
  165. return s1 ? 1 : (s2 ? -1 : 0);
  166. }
  167. }
  168. int _inline // ret-compare result
  169. UStrCmp(
  170. UCHAR const * s1 ,// in -ANSI string 1
  171. UCHAR const * s2 // in -ANSI string 2
  172. )
  173. {
  174. if (s1 && s2)
  175. {
  176. return CompareStringA(LOCALE_SYSTEM_DEFAULT, 0, (char const *) s1, -1, (char const *) s2, -1) - 2;
  177. }
  178. else
  179. {
  180. return s1 ? 1 : (s2 ? -1 : 0);
  181. }
  182. }
  183. int _inline // ret-compare result
  184. UStrCmp(
  185. char const * s1 ,// in -ANSI string 1
  186. char const * s2 ,// in -ANSI string 2
  187. int len // in -compare length in chars
  188. )
  189. {
  190. if (s1 && s2)
  191. {
  192. return CompareStringA(LOCALE_SYSTEM_DEFAULT, 0, s1, len, s2, len) - 2;
  193. }
  194. else
  195. {
  196. return s1 ? 1 : (s2 ? -1 : 0);
  197. }
  198. }
  199. int _inline // ret-compare result
  200. UStrCmp(
  201. char const * s1 ,// in -ANSI string 1
  202. UCHAR const * s2 ,// in -ANSI string 2
  203. int len // in -compare length in chars
  204. )
  205. {
  206. if (s1 && s2)
  207. {
  208. return CompareStringA(LOCALE_SYSTEM_DEFAULT, 0, s1, len, (char const *) s2, len) - 2;
  209. }
  210. else
  211. {
  212. return s1 ? 1 : (s2 ? -1 : 0);
  213. }
  214. }
  215. int _inline // ret-compare result
  216. UStrCmp(
  217. UCHAR const * s1 ,// in -ANSI string 1
  218. char const * s2 ,// in -ANSI string 2
  219. int len // in -compare length in chars
  220. )
  221. {
  222. if (s1 && s2)
  223. {
  224. return CompareStringA(LOCALE_SYSTEM_DEFAULT, 0, (char const *) s1, len, s2, len) - 2;
  225. }
  226. else
  227. {
  228. return s1 ? 1 : (s2 ? -1 : 0);
  229. }
  230. }
  231. int _inline // ret-compare result
  232. UStrCmp(
  233. UCHAR const * s1 ,// in -ANSI string 1
  234. UCHAR const * s2 ,// in -ANSI string 2
  235. int len // in -compare length in chars
  236. )
  237. {
  238. if (s1 && s2)
  239. {
  240. return CompareStringA(LOCALE_SYSTEM_DEFAULT, 0, (char const *) s1, len, (char const *) s2, len) - 2;
  241. }
  242. else
  243. {
  244. return s1 ? 1 : (s2 ? -1 : 0);
  245. }
  246. }
  247. int _inline // ret-compare result
  248. UStrICmp(
  249. char const * s1 ,// in -ANSI string 1
  250. char const * s2 // in -ANSI string 2
  251. )
  252. {
  253. if (s1 && s2)
  254. {
  255. return CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, s1, -1, s2, -1 ) - 2;
  256. }
  257. else
  258. {
  259. return s1 ? 1 : (s2 ? -1 : 0);
  260. }
  261. }
  262. int _inline // ret-compare result
  263. UStrICmp(
  264. char const * s1 ,// in -ANSI string 1
  265. UCHAR const * s2 // in -ANSI string 2
  266. )
  267. {
  268. if (s1 && s2)
  269. {
  270. return CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, s1, -1, (char const *) s2, -1 ) - 2;
  271. }
  272. else
  273. {
  274. return s1 ? 1 : (s2 ? -1 : 0);
  275. }
  276. }
  277. int _inline // ret-compare result
  278. UStrICmp(
  279. UCHAR const * s1 ,// in -ANSI string 1
  280. char const * s2 // in -ANSI string 2
  281. )
  282. {
  283. if (s1 && s2)
  284. {
  285. return CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, (char const *) s1, -1, s2, -1 ) - 2;
  286. }
  287. else
  288. {
  289. return s1 ? 1 : (s2 ? -1 : 0);
  290. }
  291. }
  292. int _inline // ret-compare result
  293. UStrICmp(
  294. UCHAR const * s1 ,// in -ANSI string 1
  295. UCHAR const * s2 // in -ANSI string 2
  296. )
  297. {
  298. if (s1 && s2)
  299. {
  300. return CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, (char const *) s1, -1, (char const *) s2, -1 ) - 2;
  301. }
  302. else
  303. {
  304. return s1 ? 1 : (s2 ? -1 : 0);
  305. }
  306. }
  307. int _inline // ret-compare result
  308. UStrICmp(
  309. char const * s1 ,// in -ANSI string 1
  310. char const * s2 ,// in -ANSI string 2
  311. int len // in -compare length in chars
  312. )
  313. {
  314. if (s1 && s2)
  315. {
  316. return CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, s1, len, s2, len) - 2;
  317. }
  318. else
  319. {
  320. return s1 ? 1 : (s2 ? -1 : 0);
  321. }
  322. }
  323. int _inline // ret-compare result
  324. UStrICmp(
  325. char const * s1 ,// in -ANSI string 1
  326. UCHAR const * s2 ,// in -ANSI string 2
  327. int len // in -compare length in chars
  328. )
  329. {
  330. if (s1 && s2)
  331. {
  332. return CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, s1, len, (char const *) s2, len) - 2;
  333. }
  334. else
  335. {
  336. return s1 ? 1 : (s2 ? -1 : 0);
  337. }
  338. }
  339. int _inline // ret-compare result
  340. UStrICmp(
  341. UCHAR const * s1 ,// in -ANSI string 1
  342. char const * s2 ,// in -ANSI string 2
  343. int len // in -compare length in chars
  344. )
  345. {
  346. if (s1 && s2)
  347. {
  348. return CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, (char const *) s1, len, s2, len) - 2;
  349. }
  350. else
  351. {
  352. return s1 ? 1 : (s2 ? -1 : 0);
  353. }
  354. }
  355. int _inline // ret-compare result
  356. UStrICmp(
  357. UCHAR const * s1 ,// in -ANSI string 1
  358. UCHAR const * s2 ,// in -ANSI string 2
  359. int len // in -compare length in chars
  360. )
  361. {
  362. if (s1 && s2)
  363. {
  364. return CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, (char const *) s1, len, (char const *) s2, len) - 2;
  365. }
  366. else
  367. {
  368. return s1 ? 1 : (s2 ? -1 : 0);
  369. }
  370. }
  371. char _inline *
  372. UStrLwr(
  373. char * s // i/o-ANSI string
  374. )
  375. {
  376. return strlwr( s );
  377. }
  378. UCHAR _inline *
  379. UStrLwr(
  380. UCHAR * s // i/o-ANSI string
  381. )
  382. {
  383. return (UCHAR *) strlwr( (char *) s );
  384. }
  385. char _inline *
  386. UStrUpr(
  387. char * s // i/o-ANSI string
  388. )
  389. {
  390. return strupr( s );
  391. }
  392. UCHAR _inline *
  393. UStrUpr(
  394. UCHAR * s // i/o-ANSI string
  395. )
  396. {
  397. return (UCHAR *) strupr( (char *) s );
  398. }
  399. char _inline
  400. UToLower(
  401. char c // in -ANSI char
  402. )
  403. {
  404. return (char) tolower( c );
  405. }
  406. UCHAR _inline
  407. UToLower(
  408. UCHAR c // in -ANSI char
  409. )
  410. {
  411. return (UCHAR) tolower( (char) c );
  412. }
  413. char _inline
  414. UToUpper(
  415. char c // in -ANSI char
  416. )
  417. {
  418. return (char) toupper( c );
  419. }
  420. UCHAR _inline
  421. UToUpper(
  422. UCHAR c // in -ANSI char
  423. )
  424. {
  425. return (UCHAR) toupper( (char) c );
  426. }
  427. // Left-trim string in place
  428. _inline UCHAR *
  429. LTrim(
  430. UCHAR * s // i/o-ANSI string
  431. )
  432. {
  433. UCHAR * strorg = s;
  434. while ( *strorg == ' ' )
  435. strorg++;
  436. if ( strorg > s )
  437. {
  438. while ( *(strorg-1) )
  439. *s++ = *strorg++;
  440. }
  441. return s;
  442. }
  443. _inline char *
  444. LTrim(
  445. char * s // i/o-ANSI string
  446. )
  447. {
  448. return (char *) LTrim( (UCHAR *) s );
  449. }
  450. // Right-trim string in place
  451. _inline UCHAR *
  452. RTrim(
  453. UCHAR * s // i/o-ANSI string
  454. )
  455. {
  456. UCHAR * strend = s + UStrLen(s);
  457. while ( (strend > s) && (*(strend-1) == ' ') )
  458. strend--;
  459. *strend = '\0';
  460. return s;
  461. }
  462. _inline char *
  463. RTrim(
  464. char * s // i/o-ANSI string
  465. )
  466. {
  467. return (char *) RTrim( (UCHAR *) s );
  468. }
  469. // Trim string in place
  470. _inline UCHAR *
  471. Trim(
  472. UCHAR * s // i/o-ANSI string
  473. )
  474. {
  475. return LTrim( RTrim( s ) );
  476. }
  477. _inline char *
  478. Trim(
  479. char * s // i/o-ANSI string
  480. )
  481. {
  482. return LTrim( RTrim( s ) );
  483. }
  484. #ifndef WIN16_VERSION
  485. int _inline // ret-length in chars
  486. UStrLen(
  487. WCHAR const * s1 // in -UNICODE string
  488. )
  489. {
  490. return lstrlenW( s1 );
  491. }
  492. void _inline
  493. UStrCpy(
  494. char * aTarget ,// out-ANSI target string
  495. WCHAR const * wSource // in -UNICODE source string
  496. )
  497. {
  498. int copylen = UStrLen( wSource ) + 1;
  499. if ( copylen )
  500. {
  501. WideCharToMultiByte( CP_ACP, 0, wSource, copylen, aTarget, copylen, NULL, NULL );
  502. aTarget[copylen-1] = '\0';
  503. }
  504. }
  505. void _inline
  506. UStrCpy(
  507. UCHAR * aTarget ,// out-ANSI target string
  508. WCHAR const * wSource // in -UNICODE source string
  509. )
  510. {
  511. UStrCpy( (char *) aTarget, wSource );
  512. }
  513. void _inline
  514. UStrCpy(
  515. WCHAR * wTarget ,// out-UNICODE target string
  516. char const * aSource // in -ANSI source string
  517. )
  518. {
  519. int copylen = UStrLen( aSource ) + 1;
  520. if ( copylen )
  521. {
  522. MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, aSource, copylen, wTarget, copylen );
  523. wTarget[copylen-1] = L'\0';
  524. }
  525. }
  526. void _inline
  527. UStrCpy(
  528. WCHAR * wTarget ,// out-UNICODE target string
  529. UCHAR const * aSource // in -ANSI source string
  530. )
  531. {
  532. UStrCpy( wTarget, (char const *) aSource );
  533. }
  534. void _inline
  535. UStrCpy(
  536. WCHAR * wTarget ,// out-UNICODE target string
  537. WCHAR const * wSource // in -UNICODE source string
  538. )
  539. {
  540. lstrcpyW( wTarget, wSource );
  541. }
  542. void _inline
  543. UStrCpy(
  544. char * aTarget ,// out-ANSI target string
  545. WCHAR const * wSource ,// in -UNICODE source string
  546. int len // in -copy length in chars
  547. )
  548. {
  549. int copylen = UStrLen( wSource ) + 1;
  550. copylen = min( copylen, len );
  551. if ( copylen )
  552. {
  553. WideCharToMultiByte( CP_ACP, 0, wSource, copylen, aTarget, copylen, NULL, NULL );
  554. aTarget[copylen-1] = '\0';
  555. }
  556. }
  557. void _inline
  558. UStrCpy(
  559. UCHAR * aTarget ,// out-ANSI target string
  560. WCHAR const * wSource ,// in -UNICODE source string
  561. int len // in -copy length in chars
  562. )
  563. {
  564. UStrCpy( (char *) aTarget, wSource, len );
  565. }
  566. void _inline
  567. UStrCpy(
  568. WCHAR * wTarget ,// out-UNICODE target string
  569. char const * aSource ,// in -ANSI source string
  570. int len // in -copy length in chars
  571. )
  572. {
  573. int copylen = UStrLen( aSource ) + 1;
  574. copylen = min( copylen, len );
  575. if ( copylen )
  576. {
  577. MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, aSource, copylen, wTarget, copylen );
  578. wTarget[copylen-1] = L'\0';
  579. }
  580. }
  581. void _inline
  582. UStrCpy(
  583. WCHAR * wTarget ,// out-UNICODE target string
  584. UCHAR const * aSource ,// in -ANSI source string
  585. int len // in -copy length in chars
  586. )
  587. {
  588. UStrCpy( wTarget, (char const *) aSource, len );
  589. }
  590. void _inline
  591. UStrCpy(
  592. WCHAR * wTarget ,// out-UNICODE target string
  593. WCHAR const * wSource ,// in -UNICODE source string
  594. int len // in -copy length in chars
  595. )
  596. {
  597. int copylen = UStrLen( wSource ) + 1;
  598. copylen = min( copylen, len );
  599. if ( copylen )
  600. {
  601. lstrcpynW( wTarget, wSource, copylen );
  602. wTarget[copylen-1] = L'\0';
  603. }
  604. }
  605. int _inline // ret-compare result
  606. UStrCmp(
  607. WCHAR const * s1 ,// in -UNICODE string 1
  608. WCHAR const * s2 // in -UNICODE string 2
  609. )
  610. {
  611. if (s1 && s2)
  612. {
  613. return CompareStringW(LOCALE_SYSTEM_DEFAULT, 0, s1, -1, s2, -1) - 2;
  614. }
  615. else
  616. {
  617. return s1 ? 1 : (s2 ? -1 : 0);
  618. }
  619. }
  620. int _inline // ret-compare result
  621. UStrCmp(
  622. WCHAR const * s1 ,// in -UNICODE string 1
  623. WCHAR const * s2 ,// in -UNICODE string 2
  624. int len // in -compare length in chars
  625. )
  626. {
  627. if (s1 && s2)
  628. {
  629. return CompareStringW(LOCALE_SYSTEM_DEFAULT, 0, s1, len, s2, len) - 2;
  630. }
  631. else
  632. {
  633. return s1 ? 1 : (s2 ? -1 : 0);
  634. }
  635. }
  636. int _inline // ret-compare result
  637. UStrICmp(
  638. WCHAR const * s1 ,// in -UNICODE string 1
  639. WCHAR const * s2 // in -UNICODE string 2
  640. )
  641. {
  642. if (s1 && s2)
  643. {
  644. return CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, s1, -1, s2, -1) - 2;
  645. }
  646. else
  647. {
  648. return s1 ? 1 : (s2 ? -1 : 0);
  649. }
  650. }
  651. int _inline // ret-compare result
  652. UStrICmp(
  653. WCHAR const * s1 ,// in -UNICODE string 1
  654. WCHAR const * s2 ,// in -UNICODE string 2
  655. int len // in -compare length in chars
  656. )
  657. {
  658. if (s1 && s2)
  659. {
  660. return CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, s1, len, s2, len) - 2;
  661. }
  662. else
  663. {
  664. return s1 ? 1 : (s2 ? -1 : 0);
  665. }
  666. }
  667. WCHAR _inline *
  668. UStrLwr(
  669. WCHAR * s // i/o-UNICODE string
  670. )
  671. {
  672. return _wcslwr( s );
  673. }
  674. WCHAR _inline *
  675. UStrUpr(
  676. WCHAR * s // i/o-UNICODE string
  677. )
  678. {
  679. return _wcsupr( s );
  680. }
  681. WCHAR _inline
  682. UToLower(
  683. WCHAR c // in -UNICODE char
  684. )
  685. {
  686. return towlower( c );
  687. }
  688. WCHAR _inline
  689. UToUpper(
  690. WCHAR c // in -UNICODE char
  691. )
  692. {
  693. return towupper( c );
  694. }
  695. // Left-trim string in place
  696. _inline WCHAR *
  697. LTrim(
  698. WCHAR * s // i/o-UNICODE string
  699. )
  700. {
  701. WCHAR * strorg = s;
  702. while ( *strorg == L' ' )
  703. strorg++;
  704. if ( strorg > s )
  705. {
  706. while ( *(strorg-1) )
  707. *s++ = *strorg++;
  708. }
  709. return s;
  710. }
  711. // Right-trim string in place
  712. _inline WCHAR *
  713. RTrim(
  714. WCHAR * s // i/o-UNICODE string
  715. )
  716. {
  717. WCHAR * strend = s + UStrLen(s);
  718. while ( (strend > s) && (*(strend-1) == L' ') )
  719. strend--;
  720. *strend = L'\0';
  721. return s;
  722. }
  723. // Trim string in place
  724. _inline WCHAR *
  725. Trim(
  726. WCHAR * s // i/o-UNICODE string
  727. )
  728. {
  729. return LTrim( RTrim( s ) );
  730. }
  731. char * _cdecl // ret-target string
  732. UStrJoin(
  733. char * target ,// out-target string
  734. size_t sizeTarget ,// in -maximum size of target in chars
  735. char const * source1 ,// in -first source string or NULL
  736. ... // in -remainder of source strings
  737. );
  738. WCHAR * _cdecl // ret-target string
  739. UStrJoin(
  740. WCHAR * target ,// out-target string
  741. size_t sizeTarget ,// in -maximum size of target in chars
  742. WCHAR const * source1 ,// in -first source string or NULL
  743. ... // in -remainder of source strings
  744. );
  745. #endif // WIN16_VERSION
  746. #endif // MCSINC_UString_hpp
  747. // UString.hpp - end of file