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.

423 lines
9.1 KiB

  1. /////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1998 Active Voice Corporation. All Rights Reserved.
  4. //
  5. // Active Agent(r) and Unified Communications(tm) are trademarks of Active Voice Corporation.
  6. //
  7. // Other brand and product names used herein are trademarks of their respective owners.
  8. //
  9. // The entire program and user interface including the structure, sequence, selection,
  10. // and arrangement of the dialog, the exclusively "yes" and "no" choices represented
  11. // by "1" and "2," and each dialog message are protected by copyrights registered in
  12. // the United States and by international treaties.
  13. //
  14. // Protected by one or more of the following United States patents: 5,070,526, 5,488,650,
  15. // 5,434,906, 5,581,604, 5,533,102, 5,568,540, 5,625,676, 5,651,054.
  16. //
  17. // Active Voice Corporation
  18. // Seattle, Washington
  19. // USA
  20. //
  21. /////////////////////////////////////////////////////////////////////////////////////////
  22. ////
  23. // str.c - string functions
  24. ////
  25. #include "winlocal.h"
  26. #include <stdlib.h>
  27. #include <stdarg.h>
  28. #include "str.h"
  29. #include "mem.h"
  30. ////
  31. // private definitions
  32. ////
  33. ////
  34. // public functions
  35. ////
  36. #ifndef NOTRACE
  37. // StrItoA - convert int nValue to ascii digits, the result stored in lpszDest
  38. // <nValue> (i) integer to convert
  39. // <lpszDest> (o) buffer to copy result (max 17 bytes)
  40. // <nRadix> (i) conversion radix (base-2 through base-36)
  41. // return <lpszDest>
  42. //
  43. LPTSTR DLLEXPORT WINAPI StrItoA(int nValue, LPTSTR lpszDest, int nRadix)
  44. {
  45. static TCHAR szDest[17];
  46. _itot(nValue, szDest, nRadix);
  47. if (lpszDest != NULL)
  48. StrCpy(lpszDest, szDest);
  49. return lpszDest;
  50. }
  51. // StrLtoA - convert long nValue to ascii digits, the result stored in lpszDest
  52. // <nValue> (i) integer to convert
  53. // <lpszDest> (o) buffer to copy result (max 33 bytes)
  54. // <nRadix> (i) conversion radix (base-2 through base-36)
  55. // return lpszDest
  56. //
  57. LPTSTR DLLEXPORT WINAPI StrLtoA(long nValue, LPTSTR lpszDest, int nRadix)
  58. {
  59. static TCHAR szDest[33];
  60. _ltot(nValue, szDest, nRadix);
  61. if (lpszDest != NULL)
  62. StrCpy(lpszDest, szDest);
  63. return lpszDest;
  64. }
  65. // StrAtoI - convert ascii digits to int
  66. // <lpszSrc> (i) string of digits to convert
  67. // return int
  68. //
  69. int DLLEXPORT WINAPI StrAtoI(LPCTSTR lpszSrc)
  70. {
  71. static TCHAR szSrc[17];
  72. StrNCpy(szSrc, lpszSrc, SIZEOFARRAY(szSrc));
  73. return _ttoi(szSrc);
  74. }
  75. // StrAtoL - convert ascii digits to long
  76. // <lpszSrc> (i) string of digits to convert
  77. // return long
  78. //
  79. long DLLEXPORT WINAPI StrAtoL(LPCTSTR lpszSrc)
  80. {
  81. static TCHAR szSrc[33];
  82. StrNCpy(szSrc, lpszSrc, SIZEOFARRAY(szSrc));
  83. return _ttol(szSrc);
  84. }
  85. // StrDup - create duplicate copy of specified string
  86. // <lpsz> (i) string to duplicate
  87. // return pointer to duplicate string (NULL if error)
  88. // NOTE: call StrDupFree to release allocated memory
  89. //
  90. LPTSTR DLLEXPORT WINAPI StrDup(LPCTSTR lpsz)
  91. {
  92. BOOL fSuccess = TRUE;
  93. LPTSTR lpszDup = NULL;
  94. int sizDup;
  95. if (lpsz == NULL)
  96. fSuccess = FALSE;
  97. else if ((lpszDup = (LPTSTR) MemAlloc(NULL,
  98. (sizDup = StrLen(lpsz) + 1) * sizeof(TCHAR), 0)) == NULL)
  99. fSuccess = FALSE;
  100. else
  101. MemCpy(lpszDup, lpsz, sizDup * sizeof(TCHAR));
  102. return fSuccess ? lpszDup : NULL;
  103. }
  104. // StrDupFree - free memory associated with duplicate string
  105. // <lpsz> (i) string returned by StrDup
  106. // return 0 if success
  107. //
  108. int DLLEXPORT WINAPI StrDupFree(LPTSTR lpsz)
  109. {
  110. BOOL fSuccess = TRUE;
  111. if (lpsz == NULL)
  112. fSuccess = FALSE;
  113. else if ((lpsz = MemFree(NULL, lpsz)) != NULL)
  114. fSuccess = FALSE;
  115. return fSuccess ? 0 : -1;
  116. }
  117. #endif // #ifndef NOTRACE
  118. // StrClean - copy up to n chars from string szSrc to string szDst,
  119. // except for leading and trailing white space
  120. // return szDst
  121. //
  122. LPTSTR DLLEXPORT WINAPI StrClean(LPTSTR szDst, LPCTSTR szSrc, size_t n)
  123. {
  124. szDst[n] = '\0';
  125. MemMove(szDst, szSrc, n);
  126. StrTrimWhite(szDst);
  127. StrTrimWhiteLeading(szDst);
  128. return (szDst);
  129. }
  130. // StrGetLastChr - return last char in string s
  131. //
  132. TCHAR DLLEXPORT WINAPI StrGetLastChr(LPCTSTR s)
  133. {
  134. TCHAR c = '\0';
  135. if (*s != '\0')
  136. c = *(s + StrLen(s) - 1);
  137. return (c);
  138. }
  139. // StrSetLastChr - replace last char in string s with c
  140. // return s
  141. //
  142. LPTSTR DLLEXPORT WINAPI StrSetLastChr(LPTSTR s, TCHAR c)
  143. {
  144. if (*s != '\0')
  145. *(s + StrLen(s) - 1) = c;
  146. return (s);
  147. }
  148. // StrTrimChr - strip trailing c chars from string s
  149. // return s
  150. //
  151. LPTSTR DLLEXPORT WINAPI StrTrimChr(LPTSTR s, TCHAR c)
  152. {
  153. LPTSTR p = StrChr(s, '\0');
  154. while (p > s && *(p = StrPrevChr(s, p)) == c)
  155. *p = '\0';
  156. return (s);
  157. }
  158. // StrTrimChrLeading - strip leading c chars from string s
  159. // return s
  160. //
  161. LPTSTR DLLEXPORT WINAPI StrTrimChrLeading(LPTSTR s, TCHAR c)
  162. {
  163. LPTSTR p = s;
  164. while (*p == c)
  165. p = StrNextChr(p);
  166. if (p > s)
  167. MemMove(s, p, StrLen(p) + 1);
  168. return (s);
  169. }
  170. // StrTrimWhite - strip trailing white space from string s
  171. // return s
  172. //
  173. LPTSTR DLLEXPORT WINAPI StrTrimWhite(LPTSTR s)
  174. {
  175. LPTSTR p = StrChr(s, '\0');
  176. while (p > s)
  177. {
  178. p = StrPrevChr(s, p);
  179. if (ChrIsAscii(*p) && ChrIsSpace(*p))
  180. *p = '\0';
  181. else
  182. break;
  183. }
  184. return (s);
  185. }
  186. // StrTrimWhiteLeading - strip leading white space from string s
  187. // return s
  188. //
  189. LPTSTR DLLEXPORT WINAPI StrTrimWhiteLeading(LPTSTR s)
  190. {
  191. LPTSTR p = s;
  192. while (ChrIsAscii(*p) && ChrIsSpace(*p))
  193. p = StrNextChr(p);
  194. if (p > s)
  195. MemMove(s, p, StrLen(p) + 1);
  196. return (s);
  197. }
  198. // StrTrimQuotes - strip leading and trailing quotes from string s
  199. // return s
  200. //
  201. LPTSTR DLLEXPORT WINAPI StrTrimQuotes(LPTSTR s)
  202. {
  203. StrTrimChrLeading(s, '\"');
  204. StrTrimChr(s, '\"');
  205. return s;
  206. }
  207. // StrChrCat - concatenate char c to end of string s
  208. // return s
  209. //
  210. LPTSTR DLLEXPORT WINAPI StrChrCat(LPTSTR s, TCHAR c)
  211. {
  212. LPTSTR p = StrChr(s, '\0');
  213. if( p == NULL )
  214. {
  215. return (NULL);
  216. }
  217. *p = c;
  218. p = StrNextChr(p);
  219. *p = '\0';
  220. return (s);
  221. }
  222. // StrChrCatLeft - concatenate char c to front of string s
  223. // return s
  224. //
  225. LPTSTR DLLEXPORT WINAPI StrChrCatLeft(LPTSTR s, TCHAR c)
  226. {
  227. MemMove(s + 1, s, StrLen(s) + 1);
  228. *s = c;
  229. return (s);
  230. }
  231. // StrInsert - insert string szSrc in front of szDst
  232. // return szDst
  233. //
  234. LPTSTR DLLEXPORT WINAPI StrInsert(LPTSTR szDst, LPTSTR szSrc)
  235. {
  236. MemMove(szDst + StrLen(szSrc), szDst, StrLen(szDst) + 1);
  237. MemMove(szDst, szSrc, StrLen(szSrc));
  238. return (szDst);
  239. }
  240. // StrSetN - set first n chars of string s to char c, null terminate s
  241. // return s
  242. //
  243. LPTSTR DLLEXPORT WINAPI StrSetN(LPTSTR s, TCHAR c, size_t n)
  244. {
  245. MemSet(s, c, n);
  246. *(s + n) = '\0';
  247. return (s);
  248. }
  249. // StrCpyXChr - copy string szSrc to string szDst, except for c chars
  250. // return szDst
  251. //
  252. LPTSTR DLLEXPORT WINAPI StrCpyXChr(LPTSTR szDst, LPCTSTR szSrc, TCHAR c)
  253. {
  254. TCHAR cTmp;
  255. if (c == '\0')
  256. MemMove(szDst, szSrc, StrLen(szSrc));
  257. else
  258. {
  259. while ((cTmp = *szSrc) != '\0')
  260. {
  261. if (cTmp != c)
  262. {
  263. *szDst = cTmp;
  264. szDst = StrNextChr(szDst);
  265. }
  266. szSrc = StrNextChr(szSrc);
  267. }
  268. *szDst = '\0';
  269. }
  270. return (szDst);
  271. }
  272. // StrGetRowColumnCount - calculate number of lines and longest line in string
  273. // <lpszText> (i) string to examine
  274. // <lpnRows> (o) int pointer to receive line count
  275. // <lpnColumnsMax> (o) int pointer to receive size of longest line
  276. // return 0 if success
  277. //
  278. int DLLEXPORT WINAPI StrGetRowColumnCount(LPCTSTR lpszText, LPINT lpnRows, LPINT lpnColumnsMax)
  279. {
  280. BOOL fSuccess = TRUE;
  281. int nRows = 0;
  282. int nColumnsMax = 0;
  283. if (lpszText == NULL)
  284. fSuccess = FALSE;
  285. else if (lpnRows == NULL)
  286. fSuccess = FALSE;
  287. else if (lpnColumnsMax == NULL)
  288. fSuccess = FALSE;
  289. else while (*lpszText != '\0')
  290. {
  291. int nColumns = 0;
  292. ++nRows;
  293. while (*lpszText != '\0')
  294. {
  295. if (*lpszText == '\n')
  296. {
  297. lpszText = StrNextChr(lpszText);
  298. break;
  299. }
  300. ++nColumns;
  301. lpszText = StrNextChr(lpszText);
  302. }
  303. if (nColumns > nColumnsMax)
  304. nColumnsMax = nColumns;
  305. }
  306. if (fSuccess)
  307. {
  308. *lpnRows = nRows;
  309. *lpnColumnsMax = nColumnsMax;
  310. }
  311. return fSuccess ? 0 : -1;
  312. }
  313. // StrGetRow - extract specified line from string
  314. // <lpszText> (i) string from which to extract line
  315. // <iRow> (i) index of line to extract (0 = first row, ...)
  316. // <lpszBuf> (o) buffer to copy line into
  317. // <sizBuf> (i) size of buffer
  318. // return 0 if success
  319. //
  320. int DLLEXPORT WINAPI StrGetRow(LPCTSTR lpszText, int iRow, LPTSTR lpszBuf, int sizBuf)
  321. {
  322. BOOL fSuccess = TRUE;
  323. int nRows = 0;
  324. if (lpszText == NULL)
  325. fSuccess = FALSE;
  326. else if (iRow < 0)
  327. fSuccess = FALSE;
  328. else if (lpszBuf == NULL)
  329. fSuccess = FALSE;
  330. else while (*lpszText != '\0')
  331. {
  332. int nColumns = 0;
  333. ++nRows;
  334. while (*lpszText != '\0')
  335. {
  336. if (*lpszText == '\n')
  337. {
  338. lpszText = StrNextChr(lpszText);
  339. break;
  340. }
  341. ++nColumns;
  342. if (iRow == nRows - 1 && nColumns < sizBuf - 1)
  343. {
  344. *lpszBuf = *lpszText;
  345. lpszBuf = StrNextChr(lpszBuf);
  346. }
  347. lpszText = StrNextChr(lpszText);
  348. }
  349. if (iRow == nRows - 1)
  350. {
  351. *lpszBuf = '\0';
  352. break;
  353. }
  354. }
  355. return fSuccess ? 0 : -1;
  356. }