ServerPath.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. //---------------------------------------------------------------------------
  2. #include "FileZillaPCH.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. default:
  124. DebugFail();
  125. }
  126. }
  127. CServerPath::CServerPath(const CServerPath &path)
  128. {
  129. m_nServerType = path.m_nServerType;
  130. m_Prefix = path.m_Prefix;
  131. m_bEmpty = path.m_bEmpty;
  132. m_Segments = path.m_Segments;
  133. }
  134. CServerPath::~CServerPath()
  135. {
  136. }
  137. void CServerPath::SetServer(const t_server &server)
  138. {
  139. m_nServerType=server.nServerType;
  140. }
  141. BOOL CServerPath::SetPath(CString &newpath, BOOL bIsFile /*=FALSE*/)
  142. {
  143. CString file;
  144. CString path=newpath;
  145. path.TrimLeft( L" " );
  146. path.TrimRight( L" " );
  147. if (path != L"")
  148. m_bEmpty = FALSE;
  149. else
  150. m_bEmpty = TRUE;
  151. if (!(m_nServerType & FZ_SERVERTYPE_HIGHMASK))
  152. m_nServerType = FZ_SERVERTYPE_FTP;
  153. if (!(m_nServerType&FZ_SERVERTYPE_SUBMASK) && (m_nServerType&FZ_SERVERTYPE_HIGHMASK)==FZ_SERVERTYPE_FTP)
  154. {
  155. int pos1 = path.Find( L":[" );
  156. if (pos1!=-1 && pos1!=(path.GetLength()-2))
  157. {
  158. if (!bIsFile && path.Right(1)==L"]")
  159. m_nServerType|=FZ_SERVERTYPE_SUB_FTP_VMS;
  160. else if (bIsFile && path.ReverseFind(']')>(pos1+1))
  161. m_nServerType|=FZ_SERVERTYPE_SUB_FTP_VMS;
  162. }
  163. if (newpath.GetLength() >= 3 && _istalpha(newpath[0]) && newpath[1] == L':' && (newpath[2] == L'\\' || newpath[2] == L'/'))
  164. m_nServerType |= FZ_SERVERTYPE_SUB_FTP_WINDOWS;
  165. else if (path[0] == FTP_MVS_DOUBLE_QUOTA && path[path.GetLength() - 1] == FTP_MVS_DOUBLE_QUOTA)
  166. m_nServerType |= FZ_SERVERTYPE_SUB_FTP_MVS;
  167. }
  168. m_Segments.clear();
  169. m_Prefix = L"";
  170. switch (m_nServerType&FZ_SERVERTYPE_HIGHMASK)
  171. {
  172. case FZ_SERVERTYPE_FTP:
  173. switch (m_nServerType&FZ_SERVERTYPE_SUBMASK)
  174. {
  175. case FZ_SERVERTYPE_SUB_FTP_MVS:
  176. case FZ_SERVERTYPE_SUB_FTP_BS2000:
  177. {
  178. path.TrimLeft(FTP_MVS_DOUBLE_QUOTA);
  179. path.TrimRight(FTP_MVS_DOUBLE_QUOTA);
  180. path.TrimLeft(L'\'');
  181. path.TrimRight(L'\'');
  182. path.TrimLeft(L'.');
  183. while (path.Replace(L"..", L"."));
  184. int pos = path.Find(L".");
  185. while (pos != -1)
  186. {
  187. m_Segments.push_back(path.Left(pos));
  188. path = path.Mid(pos + 1);
  189. pos = path.Find( L"." );
  190. }
  191. if (path != L"")
  192. m_Segments.push_back(path);
  193. else
  194. m_Prefix = L".";
  195. if (bIsFile)
  196. {
  197. if (m_Segments.empty())
  198. return FALSE;
  199. file = m_Segments.back();
  200. m_Segments.pop_back();
  201. if (file.Right(1) == L".")
  202. return FALSE;
  203. int pos = file.Find(L'(');
  204. int pos2 = file.Find(L')');
  205. if (pos != -1)
  206. {
  207. if (!pos || pos2 != file.GetLength() - 2)
  208. return FALSE;
  209. m_Prefix = L"";
  210. m_Segments.push_back(file.Left(pos));
  211. file = file.Mid(pos + 1, pos2 - pos - 1);
  212. }
  213. else if (pos2 != -1)
  214. return FALSE;
  215. }
  216. }
  217. break;
  218. case FZ_SERVERTYPE_SUB_FTP_VMS:
  219. {
  220. int pos1=path.Find( L"[" );
  221. if (pos1==-1)
  222. return FALSE;
  223. if (bIsFile)
  224. {
  225. int rpos=path.ReverseFind(L']');
  226. if (rpos==-1)
  227. return FALSE;
  228. else if (rpos!=(path.GetLength()-1) )
  229. {
  230. file=path.Mid(rpos+1);
  231. path=path.Left(rpos+1);
  232. }
  233. else
  234. return FALSE;
  235. }
  236. if (path.Right(1)!=L"]")
  237. return FALSE;
  238. path.TrimRight( L"]" );
  239. if (pos1)
  240. m_Prefix=path.Left(pos1);
  241. path=path.Mid(pos1+1);
  242. int pos=path.Find( L"." );
  243. while(pos!=-1)
  244. {
  245. m_Segments.push_back(path.Left(pos));
  246. path=path.Mid(pos+1);
  247. pos=path.Find( L"." );
  248. }
  249. if (path!=L"")
  250. m_Segments.push_back(path);
  251. }
  252. break;
  253. default:
  254. path.Replace( L"\\", L"/" );
  255. while(path.Replace( L"//", L"/" ));
  256. path.TrimLeft( L"/" );
  257. if (bIsFile)
  258. {
  259. if (path.Right(1)!= L"/" )
  260. {
  261. int rpos=path.ReverseFind(L'/');
  262. if (rpos==-1)
  263. {
  264. newpath=path;
  265. m_bEmpty=TRUE;
  266. return TRUE;
  267. }
  268. file=path.Mid(rpos+1);
  269. path=path.Left(rpos);
  270. }
  271. else
  272. return FALSE;
  273. }
  274. path.TrimRight( L"/" );
  275. int pos=path.Find( L"/" );
  276. while(pos!=-1)
  277. {
  278. m_Segments.push_back(path.Left(pos));
  279. path=path.Mid(pos+1);
  280. pos=path.Find( L"/" );
  281. }
  282. if (path!=L"")
  283. m_Segments.push_back(path);
  284. break;
  285. }
  286. break;
  287. }
  288. if (bIsFile)
  289. newpath = file;
  290. return TRUE;
  291. }
  292. const CString CServerPath::DoGetPath(bool unterminated) const
  293. {
  294. if (m_bEmpty)
  295. return L"";
  296. CString path;
  297. tConstIter iter;
  298. switch (m_nServerType&FZ_SERVERTYPE_HIGHMASK)
  299. {
  300. case FZ_SERVERTYPE_FTP:
  301. switch (m_nServerType&FZ_SERVERTYPE_SUBMASK)
  302. {
  303. case FZ_SERVERTYPE_SUB_FTP_MVS:
  304. case FZ_SERVERTYPE_SUB_FTP_BS2000:
  305. path = L"'";
  306. for (iter = m_Segments.begin(); iter != m_Segments.end(); iter++)
  307. {
  308. if (iter != m_Segments.begin())
  309. path += L".";
  310. path += *iter;
  311. }
  312. path += m_Prefix + L"'";
  313. break;
  314. case FZ_SERVERTYPE_SUB_FTP_VMS:
  315. path = m_Prefix + L"[";
  316. for (iter = m_Segments.begin(); iter != m_Segments.end(); iter++)
  317. path += *iter + L".";
  318. path.TrimRight( L"." );
  319. path += L"]";
  320. break;
  321. default:
  322. if (!(m_nServerType & FZ_SERVERTYPE_SUB_FTP_WINDOWS))
  323. path=L"/";
  324. for (iter=m_Segments.begin(); iter!=m_Segments.end(); iter++)
  325. path+=*iter + L"/";
  326. if (unterminated)
  327. {
  328. if (path.GetLength() >= 2)
  329. {
  330. path.Delete(path.GetLength() - 1, 1);
  331. }
  332. }
  333. break;
  334. }
  335. break;
  336. default:
  337. DebugFail();
  338. }
  339. return path;
  340. }
  341. const CString CServerPath::GetPath() const
  342. {
  343. return DoGetPath(false);
  344. }
  345. const CString CServerPath::GetPathUnterminated() const
  346. {
  347. return DoGetPath(true);
  348. }
  349. CServerPath& CServerPath::operator=(const CServerPath &op)
  350. {
  351. if (this == &op)
  352. return *this;
  353. m_Segments.clear();
  354. m_nServerType = op.m_nServerType;
  355. m_Prefix = op.m_Prefix;
  356. m_bEmpty = op.m_bEmpty;
  357. m_Segments = op.m_Segments;
  358. return *this;
  359. }
  360. const bool CServerPath::operator==(const CServerPath &op) const
  361. {
  362. if (this == &op)
  363. return true;
  364. if (m_bEmpty != op.m_bEmpty)
  365. return false;
  366. if (m_Prefix != op.m_Prefix)
  367. return false;
  368. // excluding FZ_SERVERTYPE_LAYERMASK from comparison,
  369. // as this part of server type is not set in TFileZillaIntf
  370. const int CompareMask = FZ_SERVERTYPE_HIGHMASK | FZ_SERVERTYPE_SUBMASK;
  371. if ((m_nServerType & CompareMask) != (op.m_nServerType & CompareMask))
  372. return false;
  373. tConstIter iter1 = m_Segments.begin();
  374. tConstIter iter2 = op.m_Segments.begin();
  375. while (iter1 != m_Segments.end())
  376. {
  377. if (iter2 == op.m_Segments.end())
  378. return false;
  379. if (*iter1 != *iter2)
  380. return false;
  381. iter1++;
  382. iter2++;
  383. }
  384. if (iter2 != op.m_Segments.end())
  385. return false;
  386. return true;
  387. }
  388. const bool CServerPath::operator!=(const CServerPath &op) const
  389. {
  390. if (*this == op)
  391. return false;
  392. else
  393. return true;
  394. }
  395. CString CServerPath::GetLastSegment() const
  396. {
  397. if (!HasParent())
  398. return L"";
  399. if (m_Segments.empty())
  400. return L"";
  401. else
  402. return m_Segments.back();
  403. }
  404. CServerPath CServerPath::GetParent() const
  405. {
  406. DebugAssert(HasParent());
  407. CServerPath path;
  408. path = *this;
  409. path.m_Segments.pop_back();
  410. if (m_nServerType & (FZ_SERVERTYPE_SUB_FTP_MVS | FZ_SERVERTYPE_SUB_FTP_BS2000))
  411. path.m_Prefix = L".";
  412. return path;
  413. }
  414. BOOL CServerPath::HasParent() const
  415. {
  416. if (!m_Segments.empty())
  417. return TRUE;
  418. else
  419. return FALSE;
  420. }
  421. const BOOL CServerPath::IsEmpty() const
  422. {
  423. return m_bEmpty;
  424. }
  425. BOOL CServerPath::AddSubdir(CString subdir)
  426. {
  427. if (subdir == L"")
  428. return FALSE;
  429. if (m_nServerType & (FZ_SERVERTYPE_SUB_FTP_MVS | FZ_SERVERTYPE_SUB_FTP_BS2000) && m_Prefix != L".")
  430. return FALSE;
  431. m_Segments.push_back(subdir);
  432. if (m_nServerType & (FZ_SERVERTYPE_SUB_FTP_MVS | FZ_SERVERTYPE_SUB_FTP_BS2000) && !m_Segments.empty())
  433. {
  434. if (m_Segments.back().Right(1) == L".")
  435. {
  436. m_Segments.back().TrimRight(L'.');
  437. m_Prefix = L".";
  438. }
  439. else
  440. m_Prefix = L"";
  441. }
  442. m_bEmpty = FALSE;
  443. return TRUE;
  444. }
  445. BOOL CServerPath::SetPath(CString newpath)
  446. {
  447. return SetPath(newpath, FALSE);
  448. }
  449. CString CServerPath::FormatFilename(CString fn, bool omitPath /*=false*/) const
  450. {
  451. if (m_bEmpty)
  452. return fn;
  453. if (fn == L"")
  454. return L"";
  455. CString path;
  456. tConstIter iter;
  457. switch (m_nServerType&FZ_SERVERTYPE_HIGHMASK)
  458. {
  459. case FZ_SERVERTYPE_FTP:
  460. switch (m_nServerType&FZ_SERVERTYPE_SUBMASK)
  461. {
  462. case FZ_SERVERTYPE_SUB_FTP_MVS:
  463. case FZ_SERVERTYPE_SUB_FTP_BS2000:
  464. if (omitPath && m_Prefix == L".")
  465. return fn;
  466. path = L"'";
  467. for (iter = m_Segments.begin(); iter != m_Segments.end(); iter++)
  468. path += *iter + L".";
  469. if (m_Prefix != L".")
  470. {
  471. path.TrimRight(L'.');
  472. path += L"(" + fn + L")";
  473. }
  474. else
  475. path += fn;
  476. path += L"'";
  477. break;
  478. case FZ_SERVERTYPE_SUB_FTP_VMS:
  479. if (omitPath)
  480. return fn;
  481. path = m_Prefix + L"[";
  482. for (iter = m_Segments.begin(); iter != m_Segments.end(); iter++)
  483. path += *iter + L".";
  484. path.TrimRight( L"." );
  485. path += L"]";
  486. path += fn;
  487. break;
  488. default:
  489. if (omitPath)
  490. return fn;
  491. if (!(m_nServerType & FZ_SERVERTYPE_SUB_FTP_WINDOWS))
  492. path=L"/";
  493. for (iter = m_Segments.begin(); iter != m_Segments.end(); iter++)
  494. path+=*iter + L"/";
  495. path += fn;
  496. break;
  497. }
  498. break;
  499. default:
  500. DebugFail();
  501. }
  502. return path;
  503. }