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.

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