Source code of Windows XP (NT5)
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.

710 lines
13 KiB

  1. /*++
  2. Copyright (c) 1994-1998 Microsoft Corporation
  3. Module Name :
  4. strfrn.h
  5. Abstract:
  6. String Functions
  7. Author:
  8. Ronald Meijer (ronaldm)
  9. Munged for setup by BoydM
  10. Project:
  11. Internet Services Manager
  12. Revision History:
  13. --*/
  14. #ifndef _STRFN_H
  15. #define _STRFN_H
  16. //
  17. // Memory Allocation Macros
  18. //
  19. #define AllocMem(cbSize)\
  20. ::LocalAlloc(LPTR, cbSize)
  21. #define FreeMem(lp)\
  22. ::LocalFree(lp)
  23. #define AllocMemByType(citems, type)\
  24. (type *)AllocMem(citems * sizeof(type))
  25. //
  26. // Debug Formatting Macros
  27. //
  28. #define TRACEOUT(x) { ; }
  29. #define TRACEEOL(x) { ; }
  30. #define TRACEEOLID(x) { ; }
  31. #define TRACEEOLERR(err,x) { ; }
  32. //
  33. // Helper Macros
  34. //
  35. //
  36. // Get number of array elements
  37. //
  38. #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
  39. //
  40. // Compute size of string array in characters. That is, don't count
  41. // the terminal null.
  42. //
  43. #define STRSIZE(str) (ARRAY_SIZE(str)-1)
  44. //
  45. // Get byte count of array
  46. //
  47. #define ARRAY_BYTES(a) (sizeof(a) * sizeof(a[0]))
  48. //
  49. // Get byte count of character elements of a string -- again
  50. // by not including the terminating NULL.
  51. //
  52. #define STRBYTES(str) (ARRAY_BYTES(str) - sizeof(str[0]))
  53. #define AllocTString(cch)\
  54. (LPTSTR)AllocMem((cch) * sizeof(TCHAR))
  55. #define AllocWString(cch)\
  56. (LPWSTR)AllocMem((cch) * sizeof(WCHAR))
  57. #define IS_NETBIOS_NAME(lpstr) (*lpstr == _T('\\'))
  58. //
  59. // Return the portion of a computer name without the backslashes
  60. //
  61. #define PURE_COMPUTER_NAME(lpstr) (IS_NETBIOS_NAME(lpstr) ? lpstr + 2 : lpstr)
  62. //
  63. // Convert CR/LF to LF
  64. //
  65. BOOL
  66. PCToUnixText(
  67. OUT LPWSTR & lpstrDestination,
  68. IN const CString strSource
  69. );
  70. //
  71. // Expand LF to CR/LF (no allocation necessary)
  72. //
  73. BOOL
  74. UnixToPCText(
  75. OUT CString & strDestination,
  76. IN LPCWSTR lpstrSource
  77. );
  78. //
  79. // Straight copy
  80. //
  81. BOOL
  82. TextToText(
  83. OUT LPWSTR & lpstrDestination,
  84. IN const CString & strSource
  85. );
  86. LPWSTR
  87. AllocWideString(
  88. IN LPCTSTR lpString
  89. );
  90. LPSTR AllocAnsiString(
  91. IN LPCTSTR lpString
  92. );
  93. LPTSTR AllocString(
  94. IN LPCTSTR lpString
  95. );
  96. #ifdef UNICODE
  97. //
  98. // Copy W string to T string
  99. //
  100. #define WTSTRCPY(dst, src, cch) \
  101. lstrcpy(dst, src)
  102. //
  103. // Copy T string to W string
  104. //
  105. #define TWSTRCPY(dst, src, cch) \
  106. lstrcpy(dst, src)
  107. //
  108. // Reference a T String as a W String (a nop in Unicode)
  109. //
  110. #define TWSTRREF(str) ((LPWSTR)str)
  111. #else
  112. //
  113. // Convert a T String to a temporary W Buffer, and
  114. // return a pointer to this internal buffer
  115. //
  116. LPWSTR ReferenceAsWideString(LPCTSTR str);
  117. //
  118. // Copy W string to T string
  119. //
  120. #define WTSTRCPY(dst, src, cch) \
  121. WideCharToMultiByte(CP_ACP, 0, src, -1, dst, cch, NULL, NULL)
  122. //
  123. // Copy T string to W string
  124. //
  125. #define TWSTRCPY(dst, src, cch) \
  126. MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, src, -1, dst, cch)
  127. //
  128. // Reference a T String as a W String
  129. //
  130. #define TWSTRREF(str) ReferenceAsWideString(str)
  131. #endif // UNICODE
  132. //
  133. // Determine if the given string is a UNC name
  134. //
  135. BOOL IsUNCName(
  136. IN const CString & strDirPath
  137. );
  138. //
  139. // Determine if the path is a fully qualified path in the context
  140. // of the local machine
  141. //
  142. BOOL IsFullyQualifiedPath(
  143. IN const CString & strDirPath
  144. );
  145. //
  146. // Determine if the given string is an URL path
  147. //
  148. BOOL IsURLName(
  149. IN const CString & strDirPath
  150. );
  151. //
  152. // Determine if the given string describes a relative URL path
  153. //
  154. inline BOOL IsRelURLPath(
  155. IN LPCTSTR lpPath
  156. )
  157. {
  158. ASSERT(lpPath != NULL);
  159. return *lpPath == _T('/');
  160. }
  161. //
  162. // Determine if the given path describes a wild-carded redirection
  163. // path (starts with *;)
  164. //
  165. inline BOOL IsWildcardedRedirectPath(
  166. IN LPCTSTR lpPath
  167. )
  168. {
  169. ASSERT(lpPath != NULL);
  170. return lpPath[0] == '*' && lpPath[1] == ';';
  171. }
  172. //
  173. // Determine if the account is local (doesn't have a computer name)
  174. //
  175. inline BOOL MyIsLocalAccount(
  176. IN CString & strAccount
  177. )
  178. {
  179. return strAccount.Find(_T('\\')) == -1;
  180. }
  181. //
  182. // Convert local path to UNC path
  183. //
  184. LPCTSTR MakeUNCPath(
  185. IN OUT CString & strDir,
  186. IN LPCTSTR lpszOwner,
  187. IN LPCTSTR lpszDirectory
  188. );
  189. //
  190. // Given domain\username, split into user name and domain
  191. //
  192. BOOL SplitUserNameAndDomain(
  193. IN OUT CString & strUserName,
  194. IN CString & strDomainName
  195. );
  196. //
  197. // Convert double-null terminated string to a CStringList
  198. //
  199. DWORD
  200. ConvertDoubleNullListToStringList(
  201. IN LPCTSTR lpstrSrc,
  202. OUT CStringList & strlDest,
  203. IN int cChars = -1
  204. );
  205. DWORD
  206. ConvertWDoubleNullListToStringList(
  207. IN PWCHAR lpstrSrc,
  208. OUT CStringList & strlDest,
  209. IN int cChars = -1
  210. );
  211. //
  212. // Go from a CStringList to a WIDE double null terminated list
  213. //
  214. DWORD
  215. ConvertStringListToWDoubleNullList(
  216. IN CStringList & strlSrc,
  217. OUT DWORD & cchDest,
  218. OUT PWCHAR & lpstrDest
  219. );
  220. //
  221. // Go from a CStringList to a double null terminated list
  222. //
  223. DWORD
  224. ConvertStringListToDoubleNullList(
  225. IN CStringList & strlSrc,
  226. OUT DWORD & cchDest,
  227. OUT LPTSTR & lpstrDest
  228. );
  229. //
  230. // Convert separated list of strings to CStringList
  231. //
  232. int
  233. ConvertSepLineToStringList(
  234. IN LPCTSTR lpstrIn,
  235. OUT CStringList & strlOut,
  236. IN LPCTSTR lpstrSep
  237. );
  238. //
  239. // Reverse function of the above
  240. //
  241. LPCTSTR
  242. ConvertStringListToSepLine(
  243. IN CStringList & strlIn,
  244. OUT CString & strOut,
  245. IN LPCTSTR lpstrSep
  246. );
  247. //
  248. // Private strtok
  249. //
  250. LPTSTR
  251. StringTok(
  252. IN LPTSTR string,
  253. IN LPCTSTR control
  254. );
  255. //
  256. // CString.Find() that's not case-sensitive
  257. //
  258. int
  259. CStringFindNoCase(
  260. IN const CString & strSrc,
  261. IN LPCTSTR lpszSub
  262. );
  263. //
  264. // Replace the first occurrance of one string
  265. // inside another one. Return error code
  266. //
  267. DWORD
  268. ReplaceStringInString(
  269. OUT IN CString & strBuffer,
  270. IN CString & strTarget,
  271. IN CString & strReplacement,
  272. IN BOOL fCaseSensitive
  273. );
  274. class CStringListEx : public CStringList
  275. /*++
  276. Class Description:
  277. Superclass of CStringList with comparison and assignment
  278. operators.
  279. Public Interface:
  280. operator == Comparison operator
  281. operator != Comparison operator
  282. operator = Assignment operator
  283. --*/
  284. {
  285. //
  286. // ctor
  287. //
  288. public:
  289. CStringListEx(int nBlockSize = 10) : CStringList(nBlockSize) {};
  290. //
  291. // Operators
  292. //
  293. public:
  294. BOOL operator == (const CStringList & strl);
  295. BOOL operator != (const CStringList & strl) { return !operator ==(strl); }
  296. CStringListEx & operator =(const CStringList & strl);
  297. };
  298. #ifdef _DOS
  299. typedef struct tagINTLFORMAT
  300. {
  301. WORD wDateFormat;
  302. CHAR szCurrencySymbol[5];
  303. CHAR szThousandSep[2];
  304. CHAR szDecimalPoint[2];
  305. CHAR szDateSep[2];
  306. CHAR szTimeSep[2];
  307. BYTE bCurrencyFormat;
  308. BYTE bCurrencyDecimals;
  309. BYTE bTimeFormat;
  310. DWORD dwMapCall;
  311. CHAR szDataSep[2];
  312. BYTE bReserved[5];
  313. } INTLFORMAT;
  314. BOOL _dos_getintlsettings(INTLFORMAT * pStruct);
  315. #endif // _DOS
  316. class CINumber
  317. /*++
  318. Class Description:
  319. Base class for international-friendly number formatting
  320. Public Interface:
  321. NOTES: Consider making this class a template
  322. --*/
  323. {
  324. public:
  325. static BOOL Initialize(BOOL fUserSetting = TRUE);
  326. static CString * s_pstrBadNumber;
  327. static BOOL UseSystemDefault();
  328. static BOOL UseUserDefault();
  329. static BOOL IsInitialized();
  330. static LPCTSTR QueryThousandSeperator();
  331. static LPCTSTR QueryDecimalPoint();
  332. static LPCTSTR QueryCurrency();
  333. static double BuildFloat(const LONG lInteger, const LONG lFraction);
  334. static LPCTSTR ConvertLongToString(const LONG lSrc, CString & str);
  335. static LPCTSTR ConvertFloatToString(
  336. IN const double flSrc,
  337. IN int nPrecision,
  338. OUT CString & str
  339. );
  340. static BOOL ConvertStringToLong(LPCTSTR lpsrc, LONG & lValue);
  341. static BOOL ConvertStringToFloat(LPCTSTR lpsrc, double & flValue);
  342. protected:
  343. CINumber();
  344. ~CINumber();
  345. protected:
  346. friend BOOL InitIntlSettings();
  347. friend void TerminateIntlSettings();
  348. static BOOL Allocate();
  349. static void DeAllocate();
  350. static BOOL IsAllocated();
  351. protected:
  352. static CString * s_pstr;
  353. private:
  354. static CString * s_pstrThousandSeperator;
  355. static CString * s_pstrDecimalPoint;
  356. static CString * s_pstrCurrency;
  357. static BOOL s_fCurrencyPrefix;
  358. static BOOL s_fInitialized;
  359. static BOOL s_fAllocated;
  360. };
  361. class CILong : public CINumber
  362. /*++
  363. Class Description:
  364. International-friendly LONG number
  365. Public Interface:
  366. --*/
  367. {
  368. public:
  369. //
  370. // Constructors
  371. //
  372. CILong();
  373. CILong(LONG lValue);
  374. CILong(LPCTSTR lpszValue);
  375. public:
  376. //
  377. // Assignment Operators
  378. //
  379. CILong & operator =(LONG lValue);
  380. CILong & operator =(LPCTSTR lpszValue);
  381. //
  382. // Shorthand Operators
  383. //
  384. CILong & operator +=(const LONG lValue);
  385. CILong & operator +=(const LPCTSTR lpszValue);
  386. CILong & operator +=(const CILong& value);
  387. CILong & operator -=(const LONG lValue);
  388. CILong & operator -=(const LPCTSTR lpszValue);
  389. CILong & operator -=(const CILong& value);
  390. CILong & operator *=(const LONG lValue);
  391. CILong & operator *=(const LPCTSTR lpszValue);
  392. CILong & operator *=(const CILong& value);
  393. CILong & operator /=(const LONG lValue);
  394. CILong & operator /=(const LPCTSTR lpszValue);
  395. CILong & operator /=(const CILong& value);
  396. //
  397. // Comparison operators
  398. //
  399. BOOL operator ==(LONG value);
  400. BOOL operator !=(CILong& value);
  401. //
  402. // Conversion operators
  403. //
  404. operator const LONG() const;
  405. operator LPCTSTR() const;
  406. inline friend CArchive & AFXAPI operator<<(CArchive & ar, CILong & value)
  407. {
  408. return (ar << value.m_lValue);
  409. }
  410. inline friend CArchive & AFXAPI operator>>(CArchive & ar, CILong & value)
  411. {
  412. return (ar >> value.m_lValue);
  413. }
  414. #ifdef _DEBUG
  415. //
  416. // CDumpContext stream operator
  417. //
  418. inline friend CDumpContext & AFXAPI operator<<(
  419. CDumpContext& dc,
  420. const CILong& value
  421. )
  422. {
  423. return (dc << value.m_lValue);
  424. }
  425. #endif // _DEBUG
  426. protected:
  427. LONG m_lValue;
  428. };
  429. class CIFloat : public CINumber
  430. /*++
  431. Class Description:
  432. International-friendly floating point number
  433. Public Interface:
  434. --*/
  435. {
  436. public:
  437. //
  438. // Constructors
  439. //
  440. CIFloat(int nPrecision = 2);
  441. CIFloat(double flValue, int nPrecision = 2);
  442. CIFloat(LONG lInteger, LONG lFraction, int nPrecision = 2);
  443. CIFloat(LPCTSTR lpszValue, int nPrecision = 2);
  444. public:
  445. //
  446. // Precision functions
  447. //
  448. int QueryPrecision() const;
  449. void SetPrecision(int nPrecision);
  450. //
  451. // Assignment Operators
  452. //
  453. CIFloat & operator =(double flValue);
  454. CIFloat & operator =(LPCTSTR lpszValue);
  455. //
  456. // Shorthand Operators
  457. //
  458. CIFloat & operator +=(const double flValue);
  459. CIFloat & operator +=(const LPCTSTR lpszValue);
  460. CIFloat & operator +=(const CIFloat& value);
  461. CIFloat & operator -=(const double flValue);
  462. CIFloat & operator -=(const LPCTSTR lpszValue);
  463. CIFloat & operator -=(const CIFloat& value);
  464. CIFloat & operator *=(const double flValue);
  465. CIFloat & operator *=(const LPCTSTR lpszValue);
  466. CIFloat & operator *=(const CIFloat& value);
  467. CIFloat & operator /=(const double flValue);
  468. CIFloat & operator /=(const LPCTSTR lpszValue);
  469. CIFloat & operator /=(const CIFloat& value);
  470. //
  471. // Conversion operators
  472. //
  473. operator const double() const;
  474. operator LPCTSTR() const;
  475. //
  476. // Persistence Operators
  477. //
  478. inline friend CArchive& AFXAPI operator<<(CArchive& ar, CIFloat& value)
  479. {
  480. return (ar << value.m_flValue);
  481. }
  482. inline friend CArchive& AFXAPI operator>>(CArchive& ar, CIFloat& value)
  483. {
  484. return (ar >> value.m_flValue);
  485. }
  486. #ifdef _DEBUG
  487. //
  488. // CDumpContext stream operator
  489. //
  490. inline friend CDumpContext& AFXAPI operator<<(
  491. CDumpContext& dc,
  492. const CIFloat& value
  493. )
  494. {
  495. return (dc << value.m_flValue);
  496. }
  497. #endif // _DEBUG
  498. protected:
  499. double m_flValue;
  500. int m_nPrecision;
  501. };
  502. //
  503. // Inline Expansion
  504. //
  505. // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  506. inline /* static */ BOOL CINumber::UseSystemDefault()
  507. {
  508. return Initialize(FALSE);
  509. }
  510. inline /* static */ BOOL CINumber::UseUserDefault()
  511. {
  512. return Initialize(TRUE);
  513. }
  514. inline /* static */ BOOL CINumber::IsInitialized()
  515. {
  516. return s_fInitialized;
  517. }
  518. inline /* static */ LPCTSTR CINumber::QueryThousandSeperator()
  519. {
  520. return (LPCTSTR)*s_pstrThousandSeperator;
  521. }
  522. inline /* static */ LPCTSTR CINumber::QueryDecimalPoint()
  523. {
  524. return (LPCTSTR)*s_pstrDecimalPoint;
  525. }
  526. inline /* static */ LPCTSTR CINumber::QueryCurrency()
  527. {
  528. return (LPCTSTR)*s_pstrCurrency;
  529. }
  530. inline /* static */ BOOL CINumber::IsAllocated()
  531. {
  532. return s_fAllocated;
  533. }
  534. inline BOOL CILong::operator ==(LONG value)
  535. {
  536. return m_lValue == value;
  537. }
  538. inline BOOL CILong::operator !=(CILong& value)
  539. {
  540. return m_lValue != value.m_lValue;
  541. }
  542. inline CILong::operator const LONG() const
  543. {
  544. return m_lValue;
  545. }
  546. inline CILong::operator LPCTSTR() const
  547. {
  548. return CINumber::ConvertLongToString(m_lValue, *CINumber::s_pstr);
  549. }
  550. inline int CIFloat::QueryPrecision() const
  551. {
  552. return m_nPrecision;
  553. }
  554. inline void CIFloat::SetPrecision(int nPrecision)
  555. {
  556. m_nPrecision = nPrecision;
  557. }
  558. inline CIFloat::operator const double() const
  559. {
  560. return m_flValue;
  561. }
  562. inline CIFloat::operator LPCTSTR() const
  563. {
  564. return CINumber::ConvertFloatToString(
  565. m_flValue,
  566. m_nPrecision,
  567. *CINumber::s_pstr
  568. );
  569. }
  570. #endif // _STRFN_H