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.

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