stdafx.h 14 KB

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