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.

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