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.

594 lines
11 KiB

  1. /*++
  2. Copyright (c) 1994-1999 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 (Cluster Edition)
  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. //
  37. // Size in bits
  38. //
  39. #define SIZE_IN_BITS(u) (sizeof(u) * 8)
  40. #define AllocTString(cch)\
  41. (LPTSTR)AllocMem((cch) * sizeof(TCHAR))
  42. #define IS_NETBIOS_NAME(lpstr) (*lpstr == _T('\\'))
  43. //
  44. // Return the portion of a computer name without the backslashes
  45. //
  46. #define PURE_COMPUTER_NAME(lpstr) (IS_NETBIOS_NAME(lpstr) ? lpstr + 2 : lpstr)
  47. //
  48. // Convert CR/LF to LF
  49. //
  50. BOOL
  51. COMDLL
  52. PCToUnixText(
  53. OUT LPWSTR & lpstrDestination,
  54. IN const CString strSource
  55. );
  56. //
  57. // Expand LF to CR/LF (no allocation necessary)
  58. //
  59. BOOL
  60. COMDLL
  61. UnixToPCText(
  62. OUT CString & strDestination,
  63. IN LPCWSTR lpstrSource
  64. );
  65. /*
  66. //
  67. // Straight copy
  68. //
  69. BOOL
  70. COMDLL
  71. TextToText(
  72. OUT LPWSTR & lpstrDestination,
  73. IN const CString & strSource
  74. );
  75. */
  76. LPSTR
  77. COMDLL
  78. AllocAnsiString(
  79. IN LPCTSTR lpString
  80. );
  81. LPTSTR
  82. COMDLL
  83. AllocString(
  84. IN LPCTSTR lpString,
  85. IN int nLen = -1 // -1 to autodetermine
  86. );
  87. /*
  88. #ifdef UNICODE
  89. //
  90. // Copy W string to T string
  91. //
  92. #define WTSTRCPY(dst, src, cch) \
  93. lstrcpy(dst, src)
  94. //
  95. // Copy T string to W string
  96. //
  97. #define TWSTRCPY(dst, src, cch) \
  98. lstrcpy(dst, src)
  99. //
  100. // Reference a T String as a W String (a nop in Unicode)
  101. //
  102. #define TWSTRREF(str) ((LPWSTR)str)
  103. #else
  104. //
  105. // Convert a T String to a temporary W Buffer, and
  106. // return a pointer to this internal buffer
  107. //
  108. LPWSTR ReferenceAsWideString(LPCTSTR str);
  109. //
  110. // Copy W string to T string
  111. //
  112. #define WTSTRCPY(dst, src, cch) \
  113. WideCharToMultiByte(CP_ACP, 0, src, -1, dst, cch, NULL, NULL)
  114. //
  115. // Copy T string to W string
  116. //
  117. #define TWSTRCPY(dst, src, cch) \
  118. MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, src, -1, dst, cch)
  119. //
  120. // Reference a T String as a W String
  121. //
  122. #define TWSTRREF(str) ReferenceAsWideString(str)
  123. #endif // UNICODE
  124. */
  125. //
  126. // Determine if the given string is a UNC name
  127. //
  128. BOOL
  129. COMDLL
  130. IsUNCName(
  131. IN const CString & strDirPath
  132. );
  133. //
  134. // Determine if the path is e.g. an IFS path
  135. //
  136. BOOL
  137. COMDLL
  138. IsDevicePath(
  139. IN const CString & strDirPath
  140. );
  141. //
  142. // Determine if the path is a fully qualified path in the context
  143. // of the local machine
  144. //
  145. BOOL
  146. COMDLL
  147. IsFullyQualifiedPath(
  148. IN const CString & strDirPath
  149. );
  150. BOOL
  151. COMDLL
  152. PathIsValid(LPCTSTR path);
  153. //
  154. // Determine if the path exists on a network directory in the context
  155. // of the local machine
  156. //
  157. BOOL
  158. COMDLL
  159. IsNetworkPath(
  160. IN const CString & strDirPath,
  161. OUT CString * pstrDrive = NULL,
  162. OUT CString * pstrUNC = NULL
  163. );
  164. //
  165. // Determine if the given string is an URL path
  166. //
  167. BOOL
  168. COMDLL
  169. IsURLName(
  170. IN const CString & strDirPath
  171. );
  172. //
  173. // Determine if the given string describes a relative URL path
  174. //
  175. inline BOOL IsRelURLPath(
  176. IN LPCTSTR lpPath
  177. )
  178. {
  179. ASSERT_READ_PTR(lpPath);
  180. return *lpPath == _T('/');
  181. }
  182. //
  183. // Determine if the given path describes a wild-carded redirection
  184. // path (starts with *;)
  185. //
  186. inline BOOL IsWildcardedRedirectPath(
  187. IN LPCTSTR lpPath
  188. )
  189. {
  190. ASSERT_READ_PTR(lpPath);
  191. return lpPath[0] == '*' && lpPath[1] == ';';
  192. }
  193. //
  194. // Determine if the account is local (doesn't have a computer name)
  195. //
  196. inline BOOL IsLocalAccount(
  197. IN CString & strAccount
  198. )
  199. {
  200. return strAccount.Find(_T('\\')) == -1;
  201. }
  202. //
  203. // Convert local path to UNC path
  204. //
  205. LPCTSTR COMDLL MakeUNCPath(
  206. IN OUT CString & strDir,
  207. IN LPCTSTR lpszOwner,
  208. IN LPCTSTR lpszDirectory
  209. );
  210. //
  211. // Convert GUID to a CString
  212. //
  213. LPCTSTR COMDLL GUIDToCString(
  214. IN REFGUID guid,
  215. OUT CString & str
  216. );
  217. //
  218. // Convert double-null terminated string to a CStringList
  219. //
  220. DWORD COMDLL ConvertDoubleNullListToStringList(
  221. IN LPCTSTR lpstrSrc,
  222. OUT CStringList & strlDest,
  223. IN int cChars = -1
  224. );
  225. //
  226. // Go from a CStringList to a double null terminated list
  227. //
  228. DWORD COMDLL ConvertStringListToDoubleNullList(
  229. IN CStringList & strlSrc,
  230. OUT DWORD & cchDest,
  231. OUT LPTSTR & lpstrDest
  232. );
  233. //
  234. // Convert separated list of strings to CStringList
  235. //
  236. int COMDLL ConvertSepLineToStringList(
  237. IN LPCTSTR lpstrIn,
  238. OUT CStringList & strlOut,
  239. IN LPCTSTR lpstrSep
  240. );
  241. //
  242. // Reverse function of the above
  243. //
  244. LPCTSTR COMDLL ConvertStringListToSepLine(
  245. IN CStringList & strlIn,
  246. OUT CString & strOut,
  247. IN LPCTSTR lpstrSep
  248. );
  249. //
  250. // Advanced atol which recognises hex strings
  251. //
  252. BOOL COMDLL CvtStringToLong(
  253. IN LPCTSTR lpNumber,
  254. OUT DWORD * pdwValue
  255. );
  256. //
  257. // GMT string to time_t
  258. //
  259. BOOL COMDLL CvtGMTStringToInternal(
  260. IN LPCTSTR lpTime,
  261. OUT time_t * ptValue
  262. );
  263. //
  264. // time_t to GMT string
  265. //
  266. void COMDLL CvtInternalToGMTString(
  267. IN time_t tm,
  268. OUT CString & str
  269. );
  270. //
  271. // CString.Find() that's not case-sensitive
  272. //
  273. int COMDLL CStringFindNoCase(
  274. IN const CString & strSrc,
  275. IN LPCTSTR lpszSub
  276. );
  277. //
  278. // Replace the first occurrance of one string
  279. // inside another one. Return error code
  280. //
  281. DWORD COMDLL ReplaceStringInString(
  282. OUT IN CString & strBuffer,
  283. IN CString & strTarget,
  284. IN CString & strReplacement,
  285. IN BOOL fCaseSensitive
  286. );
  287. //
  288. // Replace a path in strTarget with the
  289. // environment variable lpszEnvVar if that
  290. // strTarget path is a superset of the path
  291. // pointed to by lpszEnvVar
  292. //
  293. DWORD COMDLL DeflateEnvironmentVariablePath(
  294. IN LPCTSTR lpszEnvVar,
  295. IN OUT CString & strTarget
  296. );
  297. class COMDLL CStringListEx : public CStringList
  298. /*++
  299. Class Description:
  300. Superclass of CStringList with comparison and assignment
  301. operators.
  302. Public Interface:
  303. operator == Comparison operator
  304. operator != Comparison operator
  305. operator = Assignment operator
  306. --*/
  307. {
  308. //
  309. // ctor
  310. //
  311. public:
  312. CStringListEx(int nBlockSize = 10) : CStringList(nBlockSize) {};
  313. //
  314. // Operators
  315. //
  316. public:
  317. BOOL operator == (const CStringList & strl);
  318. BOOL operator != (const CStringList & strl) { return !operator ==(strl); }
  319. CStringListEx & operator =(const CStringList & strl);
  320. };
  321. class COMDLL CINumber
  322. /*++
  323. Class Description:
  324. Base class for international-friendly number formatting
  325. Public Interface:
  326. NOTES: Consider making this class a template
  327. --*/
  328. {
  329. public:
  330. static BOOL Initialize(BOOL fUserSetting = TRUE);
  331. static CString * _pstrBadNumber;
  332. static BOOL UseSystemDefault();
  333. static BOOL UseUserDefault();
  334. static BOOL IsInitialized();
  335. static LPCTSTR QueryThousandSeparator();
  336. static LPCTSTR QueryDecimalPoint();
  337. static LPCTSTR QueryCurrency();
  338. static double BuildFloat(const LONG lInteger, const LONG lFraction);
  339. static LPCTSTR ConvertLongToString(const LONG lSrc, CString & str);
  340. static LPCTSTR ConvertFloatToString(
  341. IN const double flSrc,
  342. IN int nPrecision,
  343. OUT CString & str
  344. );
  345. static BOOL ConvertStringToLong(LPCTSTR lpsrc, LONG & lValue);
  346. static BOOL ConvertStringToFloat(LPCTSTR lpsrc, double & flValue);
  347. protected:
  348. CINumber();
  349. ~CINumber();
  350. protected:
  351. friend BOOL InitIntlSettings();
  352. friend void TerminateIntlSettings();
  353. static BOOL Allocate();
  354. static void DeAllocate();
  355. static BOOL IsAllocated();
  356. protected:
  357. static CString * _pstr;
  358. private:
  359. static CString * _pstrThousandSeparator;
  360. static CString * _pstrDecimalPoint;
  361. static CString * _pstrCurrency;
  362. static BOOL _fCurrencyPrefix;
  363. static BOOL _fInitialized;
  364. static BOOL _fAllocated;
  365. };
  366. class COMDLL CILong : public CINumber
  367. /*++
  368. Class Description:
  369. International-friendly LONG number
  370. Public Interface:
  371. --*/
  372. {
  373. public:
  374. //
  375. // Constructors
  376. //
  377. CILong();
  378. CILong(LONG lValue);
  379. CILong(LPCTSTR lpszValue);
  380. public:
  381. //
  382. // Assignment Operators
  383. //
  384. CILong & operator =(LONG lValue);
  385. CILong & operator =(LPCTSTR lpszValue);
  386. //
  387. // Shorthand Operators
  388. //
  389. CILong & operator +=(const LONG lValue);
  390. CILong & operator +=(const LPCTSTR lpszValue);
  391. CILong & operator +=(const CILong & value);
  392. CILong & operator -=(const LONG lValue);
  393. CILong & operator -=(const LPCTSTR lpszValue);
  394. CILong & operator -=(const CILong & value);
  395. CILong & operator *=(const LONG lValue);
  396. CILong & operator *=(const LPCTSTR lpszValue);
  397. CILong & operator *=(const CILong & value);
  398. CILong & operator /=(const LONG lValue);
  399. CILong & operator /=(const LPCTSTR lpszValue);
  400. CILong & operator /=(const CILong & value);
  401. //
  402. // Comparison operators
  403. //
  404. BOOL operator ==(LONG value);
  405. BOOL operator !=(CILong& value);
  406. //
  407. // Conversion operators
  408. //
  409. operator const LONG() const;
  410. operator LPCTSTR() const;
  411. inline friend CArchive & AFXAPI operator <<(CArchive & ar, CILong & value)
  412. {
  413. return (ar << value.m_lValue);
  414. }
  415. inline friend CArchive & AFXAPI operator >>(CArchive & ar, CILong & value)
  416. {
  417. return (ar >> value.m_lValue);
  418. }
  419. #if defined(_DEBUG) || DBG
  420. //
  421. // CDumpContext stream operator
  422. //
  423. inline friend CDumpContext & AFXAPI operator<<(
  424. CDumpContext & dc,
  425. const CILong & value
  426. )
  427. {
  428. return (dc << value.m_lValue);
  429. }
  430. #endif // _DEBUG
  431. protected:
  432. LONG m_lValue;
  433. };
  434. //
  435. // Inline Expansion
  436. //
  437. // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  438. inline /* static */ BOOL CINumber::UseSystemDefault()
  439. {
  440. return Initialize(FALSE);
  441. }
  442. inline /* static */ BOOL CINumber::UseUserDefault()
  443. {
  444. return Initialize(TRUE);
  445. }
  446. inline /* static */ BOOL CINumber::IsInitialized()
  447. {
  448. return _fInitialized;
  449. }
  450. inline /* static */ LPCTSTR CINumber::QueryThousandSeparator()
  451. {
  452. return (LPCTSTR)*_pstrThousandSeparator;
  453. }
  454. inline /* static */ LPCTSTR CINumber::QueryDecimalPoint()
  455. {
  456. return (LPCTSTR)*_pstrDecimalPoint;
  457. }
  458. inline /* static */ LPCTSTR CINumber::QueryCurrency()
  459. {
  460. return (LPCTSTR)*_pstrCurrency;
  461. }
  462. inline /* static */ BOOL CINumber::IsAllocated()
  463. {
  464. return _fAllocated;
  465. }
  466. inline BOOL CILong::operator ==(LONG value)
  467. {
  468. return m_lValue == value;
  469. }
  470. inline BOOL CILong::operator !=(CILong& value)
  471. {
  472. return m_lValue != value.m_lValue;
  473. }
  474. inline CILong::operator const LONG() const
  475. {
  476. return m_lValue;
  477. }
  478. inline CILong::operator LPCTSTR() const
  479. {
  480. return CINumber::ConvertLongToString(m_lValue, *CINumber::_pstr);
  481. }
  482. #endif // _STRFN_H