ServerPath.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. //---------------------------------------------------------------------------
  2. #include "stdafx.h"
  3. #include "ServerPath.h"
  4. #include "structures.h"
  5. #define FTP_MVS_DOUBLE_QUOTA (TCHAR)0xDC
  6. //////////////////////////////////////////////////////////////////////
  7. // Konstruktion/Destruktion
  8. //////////////////////////////////////////////////////////////////////
  9. CServerPath::CServerPath()
  10. {
  11. m_nServerType = 0;
  12. m_bEmpty = TRUE;
  13. }
  14. CServerPath::CServerPath(CString path, bool trim)
  15. {
  16. m_nServerType = FZ_SERVERTYPE_FTP;
  17. if (trim)
  18. {
  19. path.TrimLeft( L" " );
  20. path.TrimRight( L" " );
  21. }
  22. if (path == L"")
  23. {
  24. m_bEmpty = TRUE;
  25. return;
  26. }
  27. else
  28. m_bEmpty = FALSE;
  29. int pos1 = path.Find( L":[" );
  30. if (pos1 != -1 && path.Right(1) == L"]" && pos1 != (path.GetLength()-1))
  31. m_nServerType |= FZ_SERVERTYPE_SUB_FTP_VMS;
  32. else if (path.GetLength() >= 3 && _istalpha(path[0]) && path[1] == L':' && (path[2] == L'\\' || path[2] == L'/'))
  33. m_nServerType |= FZ_SERVERTYPE_SUB_FTP_WINDOWS;
  34. else if (path[0] == FTP_MVS_DOUBLE_QUOTA && path[path.GetLength() - 1] == FTP_MVS_DOUBLE_QUOTA)
  35. m_nServerType |= FZ_SERVERTYPE_SUB_FTP_MVS;
  36. else if (path.GetLength() > 2 && path[0] == L'\'' && path.Right(1) == L"'" && path.Find(L'/') == -1 && path.Find(L'\\') == -1)
  37. m_nServerType |= FZ_SERVERTYPE_SUB_FTP_MVS;
  38. *this = CServerPath(path, m_nServerType, trim);
  39. }
  40. CServerPath::CServerPath(CString path, int nServerType, bool trim)
  41. {
  42. m_nServerType = nServerType;
  43. if (trim)
  44. {
  45. path.TrimLeft( L" " );
  46. path.TrimRight( L" " );
  47. }
  48. if (path == L"")
  49. {
  50. m_bEmpty = TRUE;
  51. return;
  52. }
  53. else
  54. m_bEmpty = FALSE;
  55. switch (m_nServerType&FZ_SERVERTYPE_HIGHMASK)
  56. {
  57. case FZ_SERVERTYPE_FTP:
  58. switch(m_nServerType&FZ_SERVERTYPE_SUBMASK)
  59. {
  60. case FZ_SERVERTYPE_SUB_FTP_MVS:
  61. case FZ_SERVERTYPE_SUB_FTP_BS2000:
  62. {
  63. path.TrimLeft(FTP_MVS_DOUBLE_QUOTA);
  64. path.TrimRight(FTP_MVS_DOUBLE_QUOTA);
  65. path.TrimLeft(L'\'');
  66. path.TrimRight(L'\'');
  67. path.TrimLeft(L'.');
  68. while (path.Replace(L"..", L"."));
  69. int pos = path.Find(L".");
  70. while (pos != -1)
  71. {
  72. m_Segments.push_back(path.Left(pos));
  73. path = path.Mid(pos + 1);
  74. pos = path.Find( L"." );
  75. }
  76. if (path != L"")
  77. m_Segments.push_back(path);
  78. else
  79. m_Prefix = L".";
  80. }
  81. break;
  82. case FZ_SERVERTYPE_SUB_FTP_VMS:
  83. {
  84. int pos1 = path.Find( L"[" );
  85. if (pos1 == -1 || path.Right(1) != L"]")
  86. {
  87. DebugFail();
  88. m_bEmpty = TRUE;
  89. return;
  90. }
  91. path.TrimRight( L"]" );
  92. if (pos1)
  93. m_Prefix = path.Left(pos1);
  94. path = path.Mid(pos1 + 1);
  95. int pos = path.Find( L"." );
  96. while (pos != -1)
  97. {
  98. m_Segments.push_back(path.Left(pos));
  99. path = path.Mid(pos+1);
  100. pos = path.Find( L"." );
  101. }
  102. if (path != L"")
  103. m_Segments.push_back(path);
  104. }
  105. break;
  106. default:
  107. path.Replace( L"\\", L"/" );
  108. while (path.Replace( L"//", L"/" ));
  109. path.TrimLeft( L"/" );
  110. path.TrimRight( L"/" );
  111. int pos = path.Find( L"/" );
  112. while (pos != -1)
  113. {
  114. m_Segments.push_back(path.Left(pos));
  115. path = path.Mid(pos+1);
  116. pos = path.Find( L"/" );
  117. }
  118. if (path != L"")
  119. m_Segments.push_back(path);
  120. break;
  121. }
  122. break;
  123. case FZ_SERVERTYPE_LOCAL:
  124. {
  125. path.TrimRight( L"\\" );
  126. while (path.Replace( L"\\\\", L"\\" ));
  127. int pos = path.Find( L"\\" );
  128. if (pos == -1)
  129. {
  130. m_Prefix = path;
  131. return;
  132. }
  133. DebugAssert(pos == 2);
  134. m_Prefix = path.Left(pos);
  135. path = path.Mid(pos + 1);
  136. pos = path.Find( L"\\" );
  137. while (pos != -1)
  138. {
  139. m_Segments.push_back(path.Left(pos));
  140. path=path.Mid(pos + 1);
  141. pos=path.Find( L"\\" );
  142. }
  143. if (path != L"")
  144. m_Segments.push_back(path);
  145. }
  146. break;
  147. default:
  148. DebugFail();
  149. }
  150. }
  151. CServerPath::CServerPath(const CServerPath &path)
  152. {
  153. m_nServerType = path.m_nServerType;
  154. m_Prefix = path.m_Prefix;
  155. m_bEmpty = path.m_bEmpty;
  156. m_Segments = path.m_Segments;
  157. }
  158. CServerPath::~CServerPath()
  159. {
  160. }
  161. void CServerPath::SetServer(const t_server &server)
  162. {
  163. m_nServerType=server.nServerType;
  164. }
  165. BOOL CServerPath::SetPath(CString &newpath, BOOL bIsFile /*=FALSE*/)
  166. {
  167. CString file;
  168. CString path=newpath;
  169. path.TrimLeft( L" " );
  170. path.TrimRight( L" " );
  171. if (path != L"")
  172. m_bEmpty = FALSE;
  173. else
  174. m_bEmpty = TRUE;
  175. if (!(m_nServerType & FZ_SERVERTYPE_HIGHMASK))
  176. m_nServerType = FZ_SERVERTYPE_FTP;
  177. if (!(m_nServerType&FZ_SERVERTYPE_SUBMASK) && (m_nServerType&FZ_SERVERTYPE_HIGHMASK)==FZ_SERVERTYPE_FTP)
  178. {
  179. int pos1 = path.Find( L":[" );
  180. if (pos1!=-1 && pos1!=(path.GetLength()-2))
  181. {
  182. if (!bIsFile && path.Right(1)==L"]")
  183. m_nServerType|=FZ_SERVERTYPE_SUB_FTP_VMS;
  184. else if (bIsFile && path.ReverseFind(']')>(pos1+1))
  185. m_nServerType|=FZ_SERVERTYPE_SUB_FTP_VMS;
  186. }
  187. if (newpath.GetLength() >= 3 && _istalpha(newpath[0]) && newpath[1] == L':' && (newpath[2] == L'\\' || newpath[2] == L'/'))
  188. m_nServerType |= FZ_SERVERTYPE_SUB_FTP_WINDOWS;
  189. else if (path[0] == FTP_MVS_DOUBLE_QUOTA && path[path.GetLength() - 1] == FTP_MVS_DOUBLE_QUOTA)
  190. m_nServerType |= FZ_SERVERTYPE_SUB_FTP_MVS;
  191. }
  192. m_Segments.clear();
  193. m_Prefix = L"";
  194. switch (m_nServerType&FZ_SERVERTYPE_HIGHMASK)
  195. {
  196. case FZ_SERVERTYPE_FTP:
  197. switch (m_nServerType&FZ_SERVERTYPE_SUBMASK)
  198. {
  199. case FZ_SERVERTYPE_SUB_FTP_MVS:
  200. case FZ_SERVERTYPE_SUB_FTP_BS2000:
  201. {
  202. path.TrimLeft(FTP_MVS_DOUBLE_QUOTA);
  203. path.TrimRight(FTP_MVS_DOUBLE_QUOTA);
  204. path.TrimLeft(L'\'');
  205. path.TrimRight(L'\'');
  206. path.TrimLeft(L'.');
  207. while (path.Replace(L"..", L"."));
  208. int pos = path.Find(L".");
  209. while (pos != -1)
  210. {
  211. m_Segments.push_back(path.Left(pos));
  212. path = path.Mid(pos + 1);
  213. pos = path.Find( L"." );
  214. }
  215. if (path != L"")
  216. m_Segments.push_back(path);
  217. else
  218. m_Prefix = L".";
  219. if (bIsFile)
  220. {
  221. if (m_Segments.empty())
  222. return FALSE;
  223. file = m_Segments.back();
  224. m_Segments.pop_back();
  225. if (file.Right(1) == L".")
  226. return FALSE;
  227. int pos = file.Find(L'(');
  228. int pos2 = file.Find(L')');
  229. if (pos != -1)
  230. {
  231. if (!pos || pos2 != file.GetLength() - 2)
  232. return FALSE;
  233. m_Prefix = L"";
  234. m_Segments.push_back(file.Left(pos));
  235. file = file.Mid(pos + 1, pos2 - pos - 1);
  236. }
  237. else if (pos2 != -1)
  238. return FALSE;
  239. }
  240. }
  241. break;
  242. case FZ_SERVERTYPE_SUB_FTP_VMS:
  243. {
  244. int pos1=path.Find( L"[" );
  245. if (pos1==-1)
  246. return FALSE;
  247. if (bIsFile)
  248. {
  249. int rpos=path.ReverseFind(L']');
  250. if (rpos==-1)
  251. return FALSE;
  252. else if (rpos!=(path.GetLength()-1) )
  253. {
  254. file=file.Mid(rpos+1);
  255. path=path.Left(rpos+1);
  256. }
  257. else
  258. return FALSE;
  259. }
  260. if (path.Right(1)!=L"]")
  261. return FALSE;
  262. path.TrimRight( L"]" );
  263. if (pos1)
  264. m_Prefix=path.Left(pos1);
  265. path=path.Mid(pos1+1);
  266. int pos=path.Find( L"." );
  267. while(pos!=-1)
  268. {
  269. m_Segments.push_back(path.Left(pos));
  270. path=path.Mid(pos+1);
  271. pos=path.Find( L"." );
  272. }
  273. if (path!=L"")
  274. m_Segments.push_back(path);
  275. }
  276. break;
  277. default:
  278. path.Replace( L"\\", L"/" );
  279. while(path.Replace( L"//", L"/" ));
  280. path.TrimLeft( L"/" );
  281. if (bIsFile)
  282. {
  283. if (path.Right(1)!= L"/" )
  284. {
  285. int rpos=path.ReverseFind(L'/');
  286. if (rpos==-1)
  287. {
  288. newpath=path;
  289. m_bEmpty=TRUE;
  290. return TRUE;
  291. }
  292. file=path.Mid(rpos+1);
  293. path=path.Left(rpos);
  294. }
  295. else
  296. return FALSE;
  297. }
  298. path.TrimRight( L"/" );
  299. int pos=path.Find( L"/" );
  300. while(pos!=-1)
  301. {
  302. m_Segments.push_back(path.Left(pos));
  303. path=path.Mid(pos+1);
  304. pos=path.Find( L"/" );
  305. }
  306. if (path!=L"")
  307. m_Segments.push_back(path);
  308. break;
  309. }
  310. break;
  311. case FZ_SERVERTYPE_LOCAL:
  312. {
  313. if (bIsFile)
  314. {
  315. if (path.Right(1)!= L"\\" )
  316. {
  317. int rpos=path.ReverseFind(L'\\');
  318. if (rpos==-1)
  319. return FALSE;
  320. file=path.Mid(rpos+1);
  321. path=path.Left(rpos);
  322. }
  323. else
  324. return FALSE;
  325. }
  326. path.TrimRight( L"\\" );
  327. while (path.Replace( L"\\\\", L"\\" ));
  328. int pos=path.Find( L":\\" );
  329. if (pos==-1 || pos!=1)
  330. return FALSE;
  331. else
  332. {
  333. m_Prefix=path.Left(pos+1);
  334. path=path.Mid(pos+2);
  335. }
  336. pos=path.Find( L"\\" );
  337. while (pos!=-1)
  338. {
  339. m_Segments.push_back(path.Left(pos));
  340. path=path.Mid(pos+1);
  341. pos=path.Find( L"\\" );
  342. }
  343. if (path!=L"")
  344. m_Segments.push_back(path);
  345. }
  346. break;
  347. }
  348. if (bIsFile)
  349. newpath = file;
  350. return TRUE;
  351. }
  352. const CString CServerPath::GetPath() const
  353. {
  354. if (m_bEmpty)
  355. return L"";
  356. CString path;
  357. tConstIter iter;
  358. switch (m_nServerType&FZ_SERVERTYPE_HIGHMASK)
  359. {
  360. case FZ_SERVERTYPE_FTP:
  361. switch (m_nServerType&FZ_SERVERTYPE_SUBMASK)
  362. {
  363. case FZ_SERVERTYPE_SUB_FTP_MVS:
  364. case FZ_SERVERTYPE_SUB_FTP_BS2000:
  365. path = L"'";
  366. for (iter = m_Segments.begin(); iter != m_Segments.end(); iter++)
  367. {
  368. if (iter != m_Segments.begin())
  369. path += L".";
  370. path += *iter;
  371. }
  372. path += m_Prefix + L"'";
  373. break;
  374. case FZ_SERVERTYPE_SUB_FTP_VMS:
  375. path = m_Prefix + L"[";
  376. for (iter = m_Segments.begin(); iter != m_Segments.end(); iter++)
  377. path += *iter + L".";
  378. path.TrimRight( L"." );
  379. path += L"]";
  380. break;
  381. default:
  382. if (!(m_nServerType & FZ_SERVERTYPE_SUB_FTP_WINDOWS))
  383. path=L"/";
  384. for (iter=m_Segments.begin(); iter!=m_Segments.end(); iter++)
  385. path+=*iter + L"/";
  386. break;
  387. }
  388. break;
  389. case FZ_SERVERTYPE_LOCAL:
  390. path=m_Prefix;
  391. if (!m_Segments.empty())
  392. path+=L"\\";
  393. for (iter=m_Segments.begin(); iter!=m_Segments.end(); iter++)
  394. path+=*iter + L"\\";
  395. break;
  396. default:
  397. DebugFail();
  398. }
  399. return path;
  400. }
  401. CServerPath& CServerPath::operator=(const CServerPath &op)
  402. {
  403. if (this == &op)
  404. return *this;
  405. m_Segments.clear();
  406. m_nServerType = op.m_nServerType;
  407. m_Prefix = op.m_Prefix;
  408. m_bEmpty = op.m_bEmpty;
  409. m_Segments = op.m_Segments;
  410. return *this;
  411. }
  412. const bool CServerPath::operator==(const CServerPath &op) const
  413. {
  414. if (this == &op)
  415. return true;
  416. if (m_bEmpty != op.m_bEmpty)
  417. return false;
  418. if (m_Prefix != op.m_Prefix)
  419. return false;
  420. // excluding FZ_SERVERTYPE_LAYERMASK from comparison,
  421. // as this part of server type is not set in TFileZillaIntf
  422. const int CompareMask = FZ_SERVERTYPE_HIGHMASK | FZ_SERVERTYPE_SUBMASK;
  423. if ((m_nServerType & CompareMask) != (op.m_nServerType & CompareMask))
  424. return false;
  425. tConstIter iter1 = m_Segments.begin();
  426. tConstIter iter2 = op.m_Segments.begin();
  427. while (iter1 != m_Segments.end())
  428. {
  429. if (iter2 == op.m_Segments.end())
  430. return false;
  431. if (*iter1 != *iter2)
  432. return false;
  433. iter1++;
  434. iter2++;
  435. }
  436. if (iter2 != op.m_Segments.end())
  437. return false;
  438. return true;
  439. }
  440. const BOOL operator==(const CServerPath &a, const CString &b)
  441. {
  442. CServerPath path(b);
  443. return a==path;
  444. }
  445. const bool CServerPath::operator!=(const CServerPath &op) const
  446. {
  447. if (!this)
  448. return false;
  449. if (*this == op)
  450. return false;
  451. else
  452. return true;
  453. }
  454. CString CServerPath::GetLastSegment() const
  455. {
  456. if (!HasParent())
  457. return L"";
  458. if (m_Segments.empty())
  459. return L"";
  460. else
  461. return m_Segments.back();
  462. }
  463. CServerPath CServerPath::GetParent() const
  464. {
  465. DebugAssert(HasParent());
  466. CServerPath path;
  467. path = *this;
  468. path.m_Segments.pop_back();
  469. if (m_nServerType & (FZ_SERVERTYPE_SUB_FTP_MVS | FZ_SERVERTYPE_SUB_FTP_BS2000))
  470. path.m_Prefix = L".";
  471. return path;
  472. }
  473. BOOL CServerPath::HasParent() const
  474. {
  475. if (!m_Segments.empty())
  476. return TRUE;
  477. else
  478. return FALSE;
  479. }
  480. const BOOL CServerPath::IsEmpty() const
  481. {
  482. return m_bEmpty;
  483. }
  484. CString CServerPath::GetSafePath() const
  485. {
  486. if (m_bEmpty)
  487. return L"";
  488. CString safepath;
  489. safepath.Format(L"%d %d ", m_nServerType, m_Prefix.GetLength());
  490. if (m_Prefix!=L"")
  491. safepath+=m_Prefix+L" ";
  492. tConstIter iter = m_Segments.begin();
  493. while(iter!=m_Segments.end())
  494. {
  495. CString len;
  496. len.Format(L"%d ", iter->GetLength());
  497. safepath+=len;
  498. safepath+=*iter;
  499. iter++;
  500. if (iter!=m_Segments.end())
  501. safepath+=L" ";
  502. }
  503. return safepath;
  504. }
  505. BOOL CServerPath::AddSubdir(CString subdir)
  506. {
  507. subdir.TrimLeft( L" " );
  508. subdir.TrimRight( L" " );
  509. if (subdir == L"")
  510. return FALSE;
  511. if (m_nServerType & (FZ_SERVERTYPE_SUB_FTP_MVS | FZ_SERVERTYPE_SUB_FTP_BS2000) && m_Prefix != L".")
  512. return FALSE;
  513. m_Segments.push_back(subdir);
  514. if (m_nServerType & (FZ_SERVERTYPE_SUB_FTP_MVS | FZ_SERVERTYPE_SUB_FTP_BS2000) && !m_Segments.empty())
  515. {
  516. if (m_Segments.back().Right(1) == L".")
  517. {
  518. m_Segments.back().TrimRight(L'.');
  519. m_Prefix = L".";
  520. }
  521. else
  522. m_Prefix = L"";
  523. }
  524. m_bEmpty = FALSE;
  525. return TRUE;
  526. }
  527. BOOL CServerPath::SetPath(CString newpath)
  528. {
  529. return SetPath(newpath, FALSE);
  530. }
  531. CString CServerPath::FormatFilename(CString fn, bool omitPath /*=false*/) const
  532. {
  533. if (m_bEmpty)
  534. return fn;
  535. if (fn == L"")
  536. return L"";
  537. CString path;
  538. tConstIter iter;
  539. switch (m_nServerType&FZ_SERVERTYPE_HIGHMASK)
  540. {
  541. case FZ_SERVERTYPE_FTP:
  542. switch (m_nServerType&FZ_SERVERTYPE_SUBMASK)
  543. {
  544. case FZ_SERVERTYPE_SUB_FTP_MVS:
  545. case FZ_SERVERTYPE_SUB_FTP_BS2000:
  546. if (omitPath && m_Prefix == L".")
  547. return fn;
  548. path = L"'";
  549. for (iter = m_Segments.begin(); iter != m_Segments.end(); iter++)
  550. path += *iter + L".";
  551. if (m_Prefix != L".")
  552. {
  553. path.TrimRight(L'.');
  554. path += L"(" + fn + L")";
  555. }
  556. else
  557. path += fn;
  558. path += L"'";
  559. break;
  560. case FZ_SERVERTYPE_SUB_FTP_VMS:
  561. if (omitPath)
  562. return fn;
  563. path = m_Prefix + L"[";
  564. for (iter = m_Segments.begin(); iter != m_Segments.end(); iter++)
  565. path += *iter + L".";
  566. path.TrimRight( L"." );
  567. path += L"]";
  568. path += fn;
  569. break;
  570. default:
  571. if (omitPath)
  572. return fn;
  573. if (!(m_nServerType & FZ_SERVERTYPE_SUB_FTP_WINDOWS))
  574. path=L"/";
  575. for (iter = m_Segments.begin(); iter != m_Segments.end(); iter++)
  576. path+=*iter + L"/";
  577. path += fn;
  578. break;
  579. }
  580. break;
  581. case FZ_SERVERTYPE_LOCAL:
  582. if (omitPath)
  583. return fn;
  584. path=m_Prefix;
  585. if (!m_Segments.empty())
  586. path+=L"\\";
  587. for (iter=m_Segments.begin(); iter!=m_Segments.end(); iter++)
  588. path+=*iter + L"\\";
  589. path += fn;
  590. break;
  591. default:
  592. DebugFail();
  593. }
  594. return path;
  595. }