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.

508 lines
12 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Forms
  4. // Copyright (C) Microsoft Corporation, 1994-2000
  5. //
  6. // File: cstrinout.h
  7. //
  8. // Contents: shell-wide string thunkers, for use by unicode wrappers
  9. //
  10. //----------------------------------------------------------------------------
  11. #ifndef _CSTRINOUT_HXX_
  12. #define _CSTRINOUT_HXX_
  13. #include "uniansi.h"
  14. #define CP_ATOM 0xFFFFFFFF /* not a string at all */
  15. //+---------------------------------------------------------------------------
  16. //
  17. // Class: CConvertStr (CStr)
  18. //
  19. // Purpose: Base class for conversion classes.
  20. //
  21. //----------------------------------------------------------------------------
  22. class CConvertStr
  23. {
  24. public:
  25. operator char *();
  26. inline BOOL IsAtom() { return _uCP == CP_ATOM; }
  27. protected:
  28. CConvertStr(UINT uCP);
  29. ~CConvertStr();
  30. void Free();
  31. UINT _uCP;
  32. LPSTR _pstr;
  33. char _ach[MAX_PATH * sizeof(WCHAR)];
  34. };
  35. //+---------------------------------------------------------------------------
  36. //
  37. // Member: CConvertStr::CConvertStr
  38. //
  39. // Synopsis: ctor.
  40. //
  41. //----------------------------------------------------------------------------
  42. inline
  43. CConvertStr::CConvertStr(UINT uCP)
  44. {
  45. _uCP = uCP;
  46. _pstr = NULL;
  47. }
  48. //+---------------------------------------------------------------------------
  49. //
  50. // Member: CConvertStr::~CConvertStr
  51. //
  52. // Synopsis: dtor.
  53. //
  54. //----------------------------------------------------------------------------
  55. inline
  56. CConvertStr::~CConvertStr()
  57. {
  58. Free();
  59. }
  60. //+---------------------------------------------------------------------------
  61. //
  62. // Member: CConvertStr::operator char *
  63. //
  64. // Synopsis: Returns the string.
  65. //
  66. //----------------------------------------------------------------------------
  67. inline
  68. CConvertStr::operator char *()
  69. {
  70. return _pstr;
  71. }
  72. //+---------------------------------------------------------------------------
  73. //
  74. // Class: CStrIn (CStrI)
  75. //
  76. // Purpose: Converts string function arguments which are passed into
  77. // a Windows API.
  78. //
  79. //----------------------------------------------------------------------------
  80. class CStrIn : public CConvertStr
  81. {
  82. public:
  83. CStrIn(LPCWSTR pwstr);
  84. CStrIn(LPCWSTR pwstr, int cwch);
  85. CStrIn(UINT uCP, LPCWSTR pwstr);
  86. CStrIn(UINT uCP, LPCWSTR pwstr, int cwch);
  87. int strlen();
  88. protected:
  89. CStrIn();
  90. void Init(LPCWSTR pwstr, int cwch);
  91. int _cchLen;
  92. };
  93. //+---------------------------------------------------------------------------
  94. //
  95. // Member: CStrIn::CStrIn
  96. //
  97. // Synopsis: Inits the class with a given length
  98. //
  99. //----------------------------------------------------------------------------
  100. inline
  101. CStrIn::CStrIn(LPCWSTR pwstr, int cwch) : CConvertStr(CP_ACP)
  102. {
  103. Init(pwstr, cwch);
  104. }
  105. inline
  106. CStrIn::CStrIn(UINT uCP, LPCWSTR pwstr, int cwch) : CConvertStr(uCP)
  107. {
  108. Init(pwstr, cwch);
  109. }
  110. //+---------------------------------------------------------------------------
  111. //
  112. // Member: CStrIn::CStrIn
  113. //
  114. // Synopsis: Initialization for derived classes which call Init.
  115. //
  116. //----------------------------------------------------------------------------
  117. inline
  118. CStrIn::CStrIn() : CConvertStr(CP_ACP)
  119. {
  120. }
  121. //+---------------------------------------------------------------------------
  122. //
  123. // Member: CStrIn::strlen
  124. //
  125. // Synopsis: Returns the length of the string in characters, excluding
  126. // the terminating NULL.
  127. //
  128. //----------------------------------------------------------------------------
  129. inline int
  130. CStrIn::strlen()
  131. {
  132. return _cchLen;
  133. }
  134. //+---------------------------------------------------------------------------
  135. //
  136. // Class: CStrInMulti (CStrIM)
  137. //
  138. // Purpose: Converts multiple strings which are terminated by two NULLs,
  139. // e.g. "Foo\0Bar\0\0"
  140. //
  141. //----------------------------------------------------------------------------
  142. class CStrInMulti : public CStrIn
  143. {
  144. public:
  145. CStrInMulti(LPCWSTR pwstr);
  146. };
  147. //+---------------------------------------------------------------------------
  148. //
  149. // Class: CPPFIn
  150. //
  151. // Purpose: Converts string function arguments which are passed into
  152. // a Win9x PrivateProfile API. Win9x DBCS has a bug where
  153. // passing a string longer than MAX_PATH will fault kernel.
  154. //
  155. // PPF = Private Profile Filename
  156. //
  157. //----------------------------------------------------------------------------
  158. class CPPFIn
  159. {
  160. public:
  161. operator char *();
  162. CPPFIn(LPCWSTR pwstr);
  163. private:
  164. char _ach[MAX_PATH];
  165. };
  166. //+---------------------------------------------------------------------------
  167. //
  168. // Member: CPPFIn::operator char *
  169. //
  170. // Synopsis: Returns the string.
  171. //
  172. //----------------------------------------------------------------------------
  173. inline
  174. CPPFIn::operator char *()
  175. {
  176. return _ach;
  177. }
  178. //+---------------------------------------------------------------------------
  179. //
  180. // Class: CStrOut (CStrO)
  181. //
  182. // Purpose: Converts string function arguments which are passed out
  183. // from a Windows API.
  184. //
  185. //----------------------------------------------------------------------------
  186. class CStrOut : public CConvertStr
  187. {
  188. public:
  189. CStrOut(LPWSTR pwstr, int cwchBuf);
  190. CStrOut(UINT uCP, LPWSTR pwstr, int cwchBuf);
  191. ~CStrOut();
  192. int BufSize();
  193. int ConvertIncludingNul();
  194. int ConvertExcludingNul();
  195. int CopyNoConvert(int srcBytes);
  196. protected:
  197. void Init(LPWSTR pwstr, int cwchBuf);
  198. private:
  199. LPWSTR _pwstr;
  200. int _cwchBuf;
  201. };
  202. //+---------------------------------------------------------------------------
  203. //
  204. // Member: CStrOut::BufSize
  205. //
  206. // Synopsis: Returns the size of the buffer to receive an out argument,
  207. // including the terminating NULL.
  208. //
  209. //----------------------------------------------------------------------------
  210. inline int
  211. CStrOut::BufSize()
  212. {
  213. return _cwchBuf * sizeof(WCHAR);
  214. }
  215. //
  216. // Multi-Byte ---> Unicode conversion
  217. //
  218. //+---------------------------------------------------------------------------
  219. //
  220. // Class: CConvertStrW (CStr)
  221. //
  222. // Purpose: Base class for multibyte conversion classes.
  223. //
  224. //----------------------------------------------------------------------------
  225. class CConvertStrW
  226. {
  227. public:
  228. operator WCHAR *();
  229. protected:
  230. CConvertStrW();
  231. ~CConvertStrW();
  232. void Free();
  233. LPWSTR _pwstr;
  234. WCHAR _awch[MAX_PATH * sizeof(WCHAR)];
  235. };
  236. //+---------------------------------------------------------------------------
  237. //
  238. // Member: CConvertStrW::CConvertStrW
  239. //
  240. // Synopsis: ctor.
  241. //
  242. //----------------------------------------------------------------------------
  243. inline
  244. CConvertStrW::CConvertStrW()
  245. {
  246. _pwstr = NULL;
  247. }
  248. //+---------------------------------------------------------------------------
  249. //
  250. // Member: CConvertStrW::~CConvertStrW
  251. //
  252. // Synopsis: dtor.
  253. //
  254. //----------------------------------------------------------------------------
  255. inline
  256. CConvertStrW::~CConvertStrW()
  257. {
  258. Free();
  259. }
  260. //+---------------------------------------------------------------------------
  261. //
  262. // Member: CConvertStrW::operator WCHAR *
  263. //
  264. // Synopsis: Returns the string.
  265. //
  266. //----------------------------------------------------------------------------
  267. inline
  268. CConvertStrW::operator WCHAR *()
  269. {
  270. return _pwstr;
  271. }
  272. //+---------------------------------------------------------------------------
  273. //
  274. // Class: CStrInW (CStrI)
  275. //
  276. // Purpose: Converts multibyte strings into UNICODE
  277. //
  278. //----------------------------------------------------------------------------
  279. class CStrInW : public CConvertStrW
  280. {
  281. public:
  282. CStrInW(LPCSTR pstr) { Init(pstr, -1); }
  283. CStrInW(LPCSTR pstr, int cch) { Init(pstr, cch); }
  284. int strlen();
  285. protected:
  286. CStrInW();
  287. void Init(LPCSTR pstr, int cch);
  288. int _cwchLen;
  289. };
  290. //+---------------------------------------------------------------------------
  291. //
  292. // Member: CStrInW::CStrInW
  293. //
  294. // Synopsis: Initialization for derived classes which call Init.
  295. //
  296. //----------------------------------------------------------------------------
  297. inline
  298. CStrInW::CStrInW()
  299. {
  300. }
  301. //+---------------------------------------------------------------------------
  302. //
  303. // Member: CStrInW::strlen
  304. //
  305. // Synopsis: Returns the length of the string in characters, excluding
  306. // the terminating NULL.
  307. //
  308. //----------------------------------------------------------------------------
  309. inline int
  310. CStrInW::strlen()
  311. {
  312. return _cwchLen;
  313. }
  314. //+---------------------------------------------------------------------------
  315. //
  316. // Class: CStrOutW (CStrO)
  317. //
  318. // Purpose: Converts returned unicode strings into ANSI. Used for [out]
  319. // params (so we initialize with a buffer that should later be
  320. // filled with the correct ansi data)
  321. //
  322. //
  323. //----------------------------------------------------------------------------
  324. class CStrOutW : public CConvertStrW
  325. {
  326. public:
  327. CStrOutW(LPSTR pstr, int cchBuf);
  328. ~CStrOutW();
  329. int BufSize();
  330. int ConvertIncludingNul();
  331. int ConvertExcludingNul();
  332. private:
  333. LPSTR _pstr;
  334. int _cchBuf;
  335. };
  336. //+---------------------------------------------------------------------------
  337. //
  338. // Member: CStrOutW::BufSize
  339. //
  340. // Synopsis: Returns the size of the buffer to receive an out argument,
  341. // including the terminating NULL.
  342. //
  343. //----------------------------------------------------------------------------
  344. inline int
  345. CStrOutW::BufSize()
  346. {
  347. return _cchBuf;
  348. }
  349. //+---------------------------------------------------------------------------
  350. //
  351. // Class: CWin32FindDataInOut
  352. //
  353. // Purpose: Converts WIN32_FIND_DATA structures from UNICODE to ANSI
  354. // on the way in, then ANSI to UNICODE on the way out.
  355. //
  356. //----------------------------------------------------------------------------
  357. class CWin32FindDataInOut
  358. {
  359. public:
  360. operator LPWIN32_FIND_DATAA();
  361. CWin32FindDataInOut(LPWIN32_FIND_DATAW pfdW);
  362. ~CWin32FindDataInOut();
  363. protected:
  364. LPWIN32_FIND_DATAW _pfdW;
  365. WIN32_FIND_DATAA _fdA;
  366. };
  367. //+---------------------------------------------------------------------------
  368. //
  369. // Member: CWin32FindDataInOut::CWin32FindDataInOut
  370. //
  371. // Synopsis: Convert the non-string fields to ANSI. You'd think this
  372. // isn't necessary, but it is, because Win95 puts secret
  373. // goo into the dwReserved fields that must be preserved.
  374. //
  375. //----------------------------------------------------------------------------
  376. inline
  377. CWin32FindDataInOut::CWin32FindDataInOut(LPWIN32_FIND_DATAW pfdW) :
  378. _pfdW(pfdW)
  379. {
  380. memcpy(&_fdA, _pfdW, FIELD_OFFSET(WIN32_FIND_DATA, cFileName));
  381. _fdA.cFileName[0] = '\0';
  382. _fdA.cAlternateFileName[0] = '\0';
  383. }
  384. //+---------------------------------------------------------------------------
  385. //
  386. // Member: CWin32FindDataInOut::~CWin32FindDataInOut
  387. //
  388. // Synopsis: Convert all the fields from ANSI back to UNICODE.
  389. //
  390. //----------------------------------------------------------------------------
  391. inline
  392. CWin32FindDataInOut::~CWin32FindDataInOut()
  393. {
  394. memcpy(_pfdW, &_fdA, FIELD_OFFSET(WIN32_FIND_DATA, cFileName));
  395. SHAnsiToUnicode(_fdA.cFileName, _pfdW->cFileName, ARRAYSIZE(_pfdW->cFileName));
  396. SHAnsiToUnicode(_fdA.cAlternateFileName, _pfdW->cAlternateFileName, ARRAYSIZE(_pfdW->cAlternateFileName));
  397. }
  398. //+---------------------------------------------------------------------------
  399. //
  400. // Member: CWin32FindDataInOut::operator LPWIN32_FIND_DATAA
  401. //
  402. // Synopsis: Returns the WIN32_FIND_DATAA.
  403. //
  404. //----------------------------------------------------------------------------
  405. inline
  406. CWin32FindDataInOut::operator LPWIN32_FIND_DATAA()
  407. {
  408. return &_fdA;
  409. }
  410. #endif // _CSTRINOUT_HXX_