stdafx.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. //---------------------------------------------------------------------------
  2. #ifndef StdAfxH
  3. #define StdAfxH
  4. //---------------------------------------------------------------------------
  5. #define _int64 __int64
  6. //---------------------------------------------------------------------------
  7. #define MPEXT_NO_ZLIB
  8. #define MPEXT_NO_GSS
  9. #define _AFX_ENABLE_INLINES
  10. #define _AFX_NOFORCE_LIBS
  11. //---------------------------------------------------------------------------
  12. #define LENOF(x) ( (sizeof((x))) / (sizeof(*(x))))
  13. //---------------------------------------------------------------------------
  14. #include <afx.h>
  15. #include "wtypes.h"
  16. // STL includes
  17. #include <list>
  18. #include <map>
  19. #include <vector>
  20. #include <deque>
  21. #include <set>
  22. #include <algorithm>
  23. //---------------------------------------------------------------------------
  24. #pragma hdrstop
  25. //---------------------------------------------------------------------------
  26. #include <Global.h>
  27. // these create conflict with afxwin.h
  28. #undef BEGIN_MESSAGE_MAP
  29. #undef END_MESSAGE_MAP
  30. //---------------------------------------------------------------------------
  31. #include "MFC64bitFix.h"
  32. #include <ApiLog.h>
  33. #include <TextsFileZilla.h>
  34. //---------------------------------------------------------------------------
  35. #include <oleauto.h>
  36. #include <afxconv.h>
  37. //---------------------------------------------------------------------------
  38. #define _strlwr strlwr
  39. //---------------------------------------------------------------------------
  40. const int FILEEXISTS_OVERWRITE = 0;
  41. const int FILEEXISTS_RESUME = 1;
  42. const int FILEEXISTS_RENAME = 2;
  43. const int FILEEXISTS_SKIP = 3;
  44. const int FILEEXISTS_COMPLETE = 4;
  45. //---------------------------------------------------------------------------
  46. class t_ffam_statusmessage
  47. {
  48. public:
  49. CString status;
  50. int type;
  51. BOOL post;
  52. };
  53. //---------------------------------------------------------------------------
  54. typedef struct
  55. {
  56. __int64 bytes;
  57. __int64 transfersize;
  58. BOOL bFileTransfer;
  59. } t_ffam_transferstatus;
  60. //---------------------------------------------------------------------------
  61. struct CStringDataA
  62. {
  63. long nRefs; // reference count
  64. int nDataLength; // length of data (including terminator)
  65. int nAllocLength; // length of allocation
  66. // char data[nAllocLength];
  67. CHAR * data() // CHAR* to managed data
  68. {
  69. return (CHAR *)(this+1);
  70. }
  71. };
  72. //---------------------------------------------------------------------------
  73. extern LPCSTR _afxPchNilA;
  74. extern CStringDataA* _afxDataNilA;
  75. #define afxEmptyStringA ((CStringA&)*(CStringA*)&_afxPchNilA)
  76. //---------------------------------------------------------------------------
  77. class CStringA
  78. {
  79. public:
  80. CStringA()
  81. {
  82. m_pchData = afxEmptyStringA.m_pchData;
  83. }
  84. CStringA(const CStringA& stringSrc)
  85. {
  86. DebugAssert(stringSrc.GetData()->nRefs != 0);
  87. if (stringSrc.GetData()->nRefs >= 0)
  88. {
  89. DebugAssert(stringSrc.GetData() != _afxDataNilA);
  90. m_pchData = stringSrc.m_pchData;
  91. InterlockedIncrement(&GetData()->nRefs);
  92. }
  93. else
  94. {
  95. Init();
  96. *this = stringSrc.m_pchData;
  97. }
  98. }
  99. CStringA(LPCSTR lpsz)
  100. {
  101. Init();
  102. if (lpsz != NULL && HIWORD(lpsz) == NULL)
  103. {
  104. DebugFail();
  105. }
  106. else
  107. {
  108. int nLen = SafeStrlen(lpsz);
  109. if (nLen != 0)
  110. {
  111. AllocBuffer(nLen);
  112. memcpy(m_pchData, lpsz, nLen*sizeof(char));
  113. }
  114. }
  115. }
  116. ~CStringA()
  117. {
  118. if (GetData() != _afxDataNilA)
  119. {
  120. if (InterlockedDecrement(&GetData()->nRefs) <= 0)
  121. {
  122. FreeData(GetData());
  123. }
  124. }
  125. }
  126. int GetLength() const
  127. {
  128. return GetData()->nDataLength;
  129. }
  130. char operator[](int nIndex) const
  131. {
  132. // same as GetAt
  133. DebugAssert(nIndex >= 0);
  134. DebugAssert(nIndex < GetData()->nDataLength);
  135. return m_pchData[nIndex];
  136. }
  137. // ref-counted copy from another CString
  138. CStringA& operator=(const CStringA& stringSrc)
  139. {
  140. if (m_pchData != stringSrc.m_pchData)
  141. {
  142. if ((GetData()->nRefs < 0 && GetData() != _afxDataNilA) ||
  143. stringSrc.GetData()->nRefs < 0)
  144. {
  145. // actual copy necessary since one of the strings is locked
  146. AssignCopy(stringSrc.GetData()->nDataLength, stringSrc.m_pchData);
  147. }
  148. else
  149. {
  150. // can just copy references around
  151. Release();
  152. DebugAssert(stringSrc.GetData() != _afxDataNilA);
  153. m_pchData = stringSrc.m_pchData;
  154. InterlockedIncrement(&GetData()->nRefs);
  155. }
  156. }
  157. return *this;
  158. }
  159. const CStringA & operator=(LPCSTR lpsz)
  160. {
  161. AssignCopy(SafeStrlen(lpsz), lpsz);
  162. return *this;
  163. }
  164. const CStringA & operator+=(char ch)
  165. {
  166. ConcatInPlace(1, &ch);
  167. return *this;
  168. }
  169. friend CStringA AFXAPI operator+(const CStringA & string, char ch);
  170. operator LPCSTR() const
  171. {
  172. return m_pchData;
  173. }
  174. int Compare(LPCSTR lpsz) const
  175. {
  176. return strcmp(m_pchData, lpsz);
  177. }
  178. CStringA Mid(int nFirst, int nCount) const
  179. {
  180. // out-of-bounds requests return sensible things
  181. if (nFirst < 0)
  182. {
  183. nFirst = 0;
  184. }
  185. if (nCount < 0)
  186. {
  187. nCount = 0;
  188. }
  189. if (nFirst + nCount > GetData()->nDataLength)
  190. {
  191. nCount = GetData()->nDataLength - nFirst;
  192. }
  193. if (nFirst > GetData()->nDataLength)
  194. {
  195. nCount = 0;
  196. }
  197. DebugAssert(nFirst >= 0);
  198. DebugAssert(nFirst + nCount <= GetData()->nDataLength);
  199. // optimize case of returning entire string
  200. if (nFirst == 0 && nFirst + nCount == GetData()->nDataLength)
  201. {
  202. return *this;
  203. }
  204. CStringA dest;
  205. AllocCopy(dest, nCount, nFirst, 0);
  206. return dest;
  207. }
  208. CStringA Left(int nCount) const
  209. {
  210. if (nCount < 0)
  211. {
  212. nCount = 0;
  213. }
  214. if (nCount >= GetData()->nDataLength)
  215. {
  216. return *this;
  217. }
  218. CStringA dest;
  219. AllocCopy(dest, nCount, 0, 0);
  220. return dest;
  221. }
  222. int Find(char ch) const
  223. {
  224. return Find(ch, 0);
  225. }
  226. int Find(char ch, int nStart) const
  227. {
  228. int nLength = GetData()->nDataLength;
  229. if (nStart >= nLength)
  230. {
  231. return -1;
  232. }
  233. // find first single character
  234. LPSTR lpsz = strchr(m_pchData + nStart, (unsigned char)ch);
  235. // return -1 if not found and index otherwise
  236. return (lpsz == NULL) ? -1 : (int)(lpsz - m_pchData);
  237. }
  238. // find a sub-string (like strstr)
  239. int Find(LPCSTR lpszSub) const
  240. {
  241. return Find(lpszSub, 0);
  242. }
  243. int Find(LPCSTR lpszSub, int nStart) const
  244. {
  245. int nLength = GetData()->nDataLength;
  246. if (nStart > nLength)
  247. {
  248. return -1;
  249. }
  250. // find first matching substring
  251. LPSTR lpsz = strstr(m_pchData + nStart, lpszSub);
  252. // return -1 for not found, distance from beginning otherwise
  253. return (lpsz == NULL) ? -1 : (int)(lpsz - m_pchData);
  254. }
  255. void MakeUpper()
  256. {
  257. CopyBeforeWrite();
  258. strupr(m_pchData);
  259. }
  260. protected:
  261. LPSTR m_pchData; // pointer to ref counted string data
  262. CStringDataA * GetData() const
  263. {
  264. DebugAssert(m_pchData != NULL); return ((CStringDataA*)m_pchData)-1;
  265. }
  266. void Init()
  267. {
  268. m_pchData = afxEmptyStringA.m_pchData;
  269. }
  270. void AllocCopy(CStringA & dest, int nCopyLen, int nCopyIndex, int nExtraLen) const
  271. {
  272. // will clone the data attached to this string
  273. // allocating 'nExtraLen' characters
  274. // Places results in uninitialized string 'dest'
  275. // Will copy the part or all of original data to start of new string
  276. int nNewLen = nCopyLen + nExtraLen;
  277. if (nNewLen == 0)
  278. {
  279. dest.Init();
  280. }
  281. else
  282. {
  283. dest.AllocBuffer(nNewLen);
  284. memcpy(dest.m_pchData, m_pchData+nCopyIndex, nCopyLen*sizeof(char));
  285. }
  286. }
  287. void AllocBuffer(int nLen)
  288. // always allocate one extra character for '\0' termination
  289. // assumes [optimistically] that data length will equal allocation length
  290. {
  291. DebugAssert(nLen >= 0);
  292. DebugAssert(nLen <= INT_MAX-1); // max size (enough room for 1 extra)
  293. if (nLen == 0)
  294. {
  295. Init();
  296. }
  297. else
  298. {
  299. CStringDataA* pData;
  300. {
  301. pData = (CStringDataA*)
  302. new BYTE[sizeof(CStringDataA) + (nLen+1)*sizeof(char)];
  303. pData->nAllocLength = nLen;
  304. }
  305. pData->nRefs = 1;
  306. pData->data()[nLen] = '\0';
  307. pData->nDataLength = nLen;
  308. m_pchData = pData->data();
  309. }
  310. }
  311. void AssignCopy(int nSrcLen, LPCSTR lpszSrcData)
  312. {
  313. AllocBeforeWrite(nSrcLen);
  314. memcpy(m_pchData, lpszSrcData, nSrcLen*sizeof(char));
  315. GetData()->nDataLength = nSrcLen;
  316. m_pchData[nSrcLen] = '\0';
  317. }
  318. void FASTCALL FreeData(CStringDataA * pData)
  319. {
  320. delete[] (BYTE*)pData;
  321. }
  322. void PASCAL Release(CStringDataA * pData)
  323. {
  324. if (pData != _afxDataNilA)
  325. {
  326. DebugAssert(pData->nRefs != 0);
  327. if (InterlockedDecrement(&pData->nRefs) <= 0)
  328. {
  329. FreeData(pData);
  330. }
  331. }
  332. }
  333. void Release()
  334. {
  335. if (GetData() != _afxDataNilA)
  336. {
  337. DebugAssert(GetData()->nRefs != 0);
  338. if (InterlockedDecrement(&GetData()->nRefs) <= 0)
  339. {
  340. FreeData(GetData());
  341. }
  342. Init();
  343. }
  344. }
  345. void ConcatCopy(int nSrc1Len, LPCSTR lpszSrc1Data, int nSrc2Len, LPCSTR lpszSrc2Data)
  346. {
  347. // -- master concatenation routine
  348. // Concatenate two sources
  349. // -- assume that 'this' is a new CString object
  350. int nNewLen = nSrc1Len + nSrc2Len;
  351. if (nNewLen != 0)
  352. {
  353. AllocBuffer(nNewLen);
  354. memcpy(m_pchData, lpszSrc1Data, nSrc1Len*sizeof(char));
  355. memcpy(m_pchData+nSrc1Len, lpszSrc2Data, nSrc2Len*sizeof(char));
  356. }
  357. }
  358. void ConcatInPlace(int nSrcLen, LPCSTR lpszSrcData)
  359. {
  360. // -- the main routine for += operators
  361. // concatenating an empty string is a no-op!
  362. if (nSrcLen == 0)
  363. {
  364. return;
  365. }
  366. // if the buffer is too small, or we have a width mis-match, just
  367. // allocate a new buffer (slow but sure)
  368. if (GetData()->nRefs > 1 || GetData()->nDataLength + nSrcLen > GetData()->nAllocLength)
  369. {
  370. // we have to grow the buffer, use the ConcatCopy routine
  371. CStringDataA* pOldData = GetData();
  372. ConcatCopy(GetData()->nDataLength, m_pchData, nSrcLen, lpszSrcData);
  373. DebugAssert(pOldData != NULL);
  374. CStringA::Release(pOldData);
  375. }
  376. else
  377. {
  378. // fast concatenation when buffer big enough
  379. memcpy(m_pchData+GetData()->nDataLength, lpszSrcData, nSrcLen*sizeof(char));
  380. GetData()->nDataLength += nSrcLen;
  381. DebugAssert(GetData()->nDataLength <= GetData()->nAllocLength);
  382. m_pchData[GetData()->nDataLength] = '\0';
  383. }
  384. }
  385. void CopyBeforeWrite()
  386. {
  387. if (GetData()->nRefs > 1)
  388. {
  389. CStringDataA* pData = GetData();
  390. Release();
  391. AllocBuffer(pData->nDataLength);
  392. memcpy(m_pchData, pData->data(), (pData->nDataLength+1)*sizeof(char));
  393. }
  394. DebugAssert(GetData()->nRefs <= 1);
  395. }
  396. void AllocBeforeWrite(int nLen)
  397. {
  398. if (GetData()->nRefs > 1 || nLen > GetData()->nAllocLength)
  399. {
  400. Release();
  401. AllocBuffer(nLen);
  402. }
  403. DebugAssert(GetData()->nRefs <= 1);
  404. }
  405. static int PASCAL SafeStrlen(LPCSTR lpsz)
  406. {
  407. return (lpsz == NULL) ? 0 : strlen(lpsz);
  408. }
  409. };
  410. //---------------------------------------------------------------------------
  411. inline bool AFXAPI operator==(const CStringA & s1, LPCSTR s2)
  412. {
  413. return s1.Compare(s2) == 0;
  414. }
  415. //---------------------------------------------------------------------------
  416. inline bool AFXAPI operator!=(const CStringA & s1, LPCSTR s2)
  417. {
  418. return s1.Compare(s2) != 0;
  419. }
  420. //---------------------------------------------------------------------------
  421. inline CStringA AFXAPI operator+(const CStringA & string1, char ch)
  422. {
  423. CStringA s;
  424. s.ConcatCopy(string1.GetData()->nDataLength, string1.m_pchData, 1, &ch);
  425. return s;
  426. }
  427. //---------------------------------------------------------------------------
  428. #include <FileZillaApi.h>
  429. #include <FileZillaOpt.h>
  430. //---------------------------------------------------------------------------
  431. #endif