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.

447 lines
10 KiB

  1. /*++
  2. Copyright (c) 1989-2001 Microsoft Corporation
  3. Module Name:
  4. CSTRING.cpp
  5. Abstract:
  6. Header for
  7. Author:
  8. kinshu created December 12,2001
  9. Revision History:
  10. --*/
  11. #ifndef _CSTRING_H
  12. #define _CSTRING_H
  13. #include "resource.h"
  14. #include <strsafe.h>
  15. #define MAX_PATH_BUFFSIZE (MAX_PATH+1)
  16. //
  17. // This also controls how much space we allocate during Sprintf implementation. Since large strings
  18. // like the commandlines and the app help messages are passed in Sprintf (See GetXML() in dbsupport.cpp)
  19. // Presently the commandlines are limited to 1024 chars and the app help messages also to 1024 chars
  20. // Note that when we call sprintf we pass additional strings along with these big strings and we are just
  21. // makign sure that the space is big enough.
  22. #define MAX_STRING_SIZE 1024 * 3
  23. //////////////////////// Externs //////////////////////////////////////////////
  24. extern struct _tagSpecialCharMap g_rgSpecialCharMap[4][2];
  25. extern TCHAR g_szAppName[];
  26. ///////////////////////////////////////////////////////////////////////////////
  27. //////////////////////// Defines //////////////////////////////////////////////
  28. #define MEM_ERR MessageBox(NULL, GetString(IDS_EXCEPTION, NULL, 0), g_szAppName,MB_ICONWARNING|MB_OK);
  29. #define SafeCpyN(pszDest, pszSource, nDestSize) StringCchCopy(pszDest, nDestSize, pszSource)
  30. ///////////////////////////////////////////////////////////////////////////////
  31. /*++
  32. Used to convert from special chars viz. <, >, &, " to the XML equivalents
  33. --*/
  34. typedef struct _tagSpecialCharMap {
  35. TCHAR* szString; // The string
  36. INT iLength; // Length of the string in TCHARs
  37. } SpecialCharMap;
  38. PTSTR
  39. GetString(
  40. UINT iResource,
  41. PTSTR szStr,
  42. int nLength
  43. );
  44. int
  45. CDECL
  46. MSGF(
  47. HWND hwndParent,
  48. PCTSTR pszCaption,
  49. UINT uType,
  50. PCTSTR pszFormat,
  51. ...
  52. );
  53. //
  54. // The string class
  55. //
  56. class CSTRING {
  57. public:
  58. WCHAR* pszString; // The wide string
  59. LPSTR pszANSI; // The ansi string
  60. public:
  61. CSTRING();
  62. CSTRING(CSTRING& Str);
  63. CSTRING(LPCTSTR szString);
  64. CSTRING(UINT uID);
  65. ~CSTRING();
  66. void Init(void);
  67. void Release(void);
  68. BOOL SetString(UINT uID);
  69. BOOL SetString(LPCTSTR szStringIn);
  70. CSTRING operator + (CSTRING& str)
  71. {
  72. return(*this + str.pszString);
  73. }
  74. CSTRING operator + (LPCTSTR szStr)
  75. {
  76. CSTRING strStr;
  77. strStr = *this;
  78. strStr.Strcat(szStr);
  79. return strStr;
  80. }
  81. CSTRING& operator += (LPCTSTR szString)
  82. {
  83. if (szString) {
  84. Strcat(szString);
  85. }
  86. return *this;
  87. }
  88. CSTRING& operator += (CSTRING& string)
  89. {
  90. Strcat((LPCTSTR)string);
  91. return *this;
  92. }
  93. BOOL
  94. ConvertToLongFileName()
  95. {
  96. TCHAR szLongPath[MAX_PATH];
  97. DWORD dwReturn = 0;
  98. BOOL bOk = TRUE;
  99. dwReturn = GetLongPathName(pszString, szLongPath, MAX_PATH);
  100. if (dwReturn > 0 && dwReturn <= sizeof(szLongPath) / sizeof(szLongPath[0])) {
  101. SetString(szLongPath);
  102. } else {
  103. ASSERT(FALSE);
  104. bOk = FALSE;
  105. }
  106. return bOk;
  107. }
  108. PCTSTR GetFileNamePointer()
  109. {
  110. if (pszString) {
  111. return PathFindFileName(pszString);
  112. }
  113. return NULL;
  114. }
  115. BOOL GetWindowsDirectory()
  116. /*++
  117. Desc: Gets the windows directory. Will always be appended by a slash
  118. --*/
  119. {
  120. TCHAR szPath[MAX_PATH];
  121. INT iLength;
  122. const size_t kszPath = sizeof(szPath) / sizeof(szPath[0]);
  123. UINT uResult = 0;
  124. *szPath = 0;
  125. uResult = ::GetWindowsDirectory(szPath, kszPath - 1);
  126. if (uResult > 0 && uResult < (kszPath - 1)) {
  127. iLength = lstrlen(szPath);
  128. if ((iLength < kszPath - 1 && iLength > 0) && szPath[iLength - 1] != TEXT('\\')) {
  129. *(szPath + iLength) = TEXT('\\');
  130. *(szPath + iLength + 1) = 0;
  131. SetString(szPath);
  132. return TRUE;
  133. }
  134. }
  135. return FALSE;
  136. }
  137. BOOL GetSystemWindowsDirectory()
  138. /*++
  139. Desc: Gets the system directory. Will always be appended by a slash
  140. --*/
  141. {
  142. TCHAR szPath[MAX_PATH];
  143. INT iLength;
  144. const size_t kszPath = sizeof(szPath) / sizeof(szPath[0]);
  145. UINT uResult = 0;
  146. *szPath = 0;
  147. uResult = ::GetSystemWindowsDirectory(szPath, kszPath - 1);
  148. if (uResult > 0 && uResult < (kszPath - 1)) {
  149. iLength = lstrlen(szPath);
  150. if ((iLength < kszPath - 1 && iLength > 0) && szPath[iLength - 1] != TEXT('\\')) {
  151. *(szPath + iLength) = TEXT('\\');
  152. *(szPath + iLength + 1) = 0;
  153. SetString(szPath);
  154. return TRUE;
  155. }
  156. }
  157. return FALSE;
  158. }
  159. BOOL GetSystemDirectory()
  160. /*++
  161. Desc: Gets the system directory. Will always be appended by a slash
  162. --*/
  163. {
  164. TCHAR szPath[MAX_PATH];
  165. INT iLength;
  166. const size_t kszPath = sizeof(szPath) / sizeof(szPath[0]);
  167. UINT uResult = 0;
  168. *szPath = 0;
  169. uResult = ::GetSystemDirectory(szPath, kszPath - 1);
  170. if (uResult > 0 && uResult < (kszPath - 1)) {
  171. iLength = lstrlen(szPath);
  172. if ((iLength < kszPath - 1 && iLength > 0) && szPath[iLength - 1] != TEXT('\\')) {
  173. *(szPath + iLength) = TEXT('\\');
  174. *(szPath + iLength + 1) = 0;
  175. SetString(szPath);
  176. return TRUE;
  177. }
  178. }
  179. return FALSE;
  180. }
  181. operator LPWSTR()
  182. {
  183. return pszString;
  184. }
  185. operator LPCWSTR()
  186. {
  187. return pszString;
  188. }
  189. CSTRING& operator =(LPCWSTR szStringIn)
  190. {
  191. SetString(szStringIn);
  192. return *this;
  193. }
  194. CSTRING& operator =(CSTRING & szStringIn)
  195. {
  196. SetString(szStringIn.pszString);
  197. return *this;
  198. }
  199. BOOL operator == (CSTRING & szString)
  200. {
  201. return(*this == szString.pszString);
  202. }
  203. BOOL operator == (LPCTSTR szString)
  204. {
  205. //
  206. // Both of them are NULL, we say that they are similar
  207. //
  208. if (NULL == pszString && NULL == szString) {
  209. return TRUE;
  210. }
  211. //
  212. // One of them is NULL, but the other one is NOT, we return dissimilar
  213. //
  214. if (NULL == pszString || NULL == szString) {
  215. return FALSE;
  216. }
  217. if (0 == lstrcmpi(szString, pszString)) {
  218. return TRUE;
  219. }
  220. return FALSE;
  221. }
  222. BOOL operator != (CSTRING& szString)
  223. {
  224. if (NULL == pszString && NULL == szString.pszString) {
  225. return FALSE;
  226. }
  227. if (NULL == pszString || NULL == szString.pszString) {
  228. return TRUE;
  229. }
  230. if (0 == lstrcmpi(szString.pszString,pszString)) {
  231. return FALSE;
  232. }
  233. return TRUE;
  234. }
  235. BOOL operator != (LPCTSTR szString)
  236. {
  237. return(! (*this == szString));
  238. }
  239. BOOL operator <= (CSTRING &szString)
  240. {
  241. return((lstrcmpi (*this,szString) <= 0) ? TRUE : FALSE);
  242. }
  243. BOOL operator < (CSTRING &szString)
  244. {
  245. return((lstrcmpi (*this,szString) < 0) ? TRUE : FALSE);
  246. }
  247. BOOL operator >= (CSTRING &szString)
  248. {
  249. return((lstrcmpi (*this,szString) >= 0) ? TRUE : FALSE);
  250. }
  251. BOOL operator > (CSTRING &szString)
  252. {
  253. return((lstrcmpi (*this, szString) > 0) ? TRUE : FALSE);
  254. }
  255. void __cdecl Sprintf(LPCTSTR szFormat, ...);
  256. UINT Trim(void);
  257. static INT Trim(IN OUT LPTSTR str);
  258. BOOL SetChar(int nPos, TCHAR chValue);
  259. BOOL GetChar(int nPos, TCHAR* chReturn);
  260. CSTRING SpecialCharToXML(BOOL bApphelpMessage = FALSE);
  261. TCHAR* XMLToSpecialChar(void);
  262. static TCHAR* StrStrI(const TCHAR* szString,const TCHAR* szMatch);
  263. BOOL BeginsWith(LPCTSTR szPrefix);
  264. BOOL EndsWith(LPCTSTR szSuffix);
  265. static BOOL EndsWith(LPCTSTR szString, LPCTSTR szSuffix);
  266. LPCTSTR Strcat(CSTRING & szStr);
  267. LPCTSTR Strcat(LPCTSTR pString);
  268. BOOL isNULL(void);
  269. int Length(void);
  270. void GUID(GUID& Guid);
  271. CSTRING& ShortFilename(void);
  272. BOOL RelativeFile(CSTRING& szPath);
  273. BOOL RelativeFile(LPCTSTR pExeFile);
  274. TCHAR* Replace(PCTSTR pszToFind, PCTSTR pszWith);
  275. };
  276. /*++
  277. CSTRINGLIST is a list of these
  278. --*/
  279. typedef struct _tagSList {
  280. CSTRING szStr; // The string
  281. int data ; // Any data that is associated with this string
  282. struct _tagSList * pNext; // The next string
  283. } STRLIST, *PSTRLIST;
  284. /*++
  285. A linked list of PSTRLIST
  286. --*/
  287. class CSTRINGLIST {
  288. public:
  289. UINT m_uCount; // The total number of elements
  290. PSTRLIST m_pHead; // The first element
  291. PSTRLIST m_pTail; // The last element
  292. public:
  293. CSTRINGLIST();
  294. ~CSTRINGLIST();
  295. BOOL IsEmpty(void);
  296. void DeleteAll(void);
  297. BOOL AddString(CSTRING& Str, int data = 0);
  298. BOOL AddStringAtBeg(LPCTSTR lpszStr,int data = 0);
  299. BOOL AddStringInOrder(LPCTSTR pStr,int data = 0);
  300. BOOL GetElement(UINT uPos, CSTRING& str);
  301. BOOL AddString(LPCTSTR pStr, int data = 0);
  302. CSTRINGLIST& operator =(CSTRINGLIST& strlTemp);
  303. BOOL operator != (CSTRINGLIST &strlTemp);
  304. BOOL operator == (CSTRINGLIST &strlTemp);
  305. BOOL Remove(CSTRING &str);
  306. void RemoveLast(void);
  307. };
  308. #endif