ServerPath.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. // FileZilla - a Windows ftp client
  2. // Copyright (C) 2002-2004 - Tim Kosse <[email protected]>
  3. // This program is free software; you can redistribute it and/or
  4. // modify it under the terms of the GNU General Public License
  5. // as published by the Free Software Foundation; either version 2
  6. // of the License, or (at your option) any later version.
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU General Public License for more details.
  11. // You should have received a copy of the GNU General Public License
  12. // along with this program; if not, write to the Free Software
  13. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. // ServerPath.cpp: Implementierung der Klasse CServerPath.
  15. //
  16. //////////////////////////////////////////////////////////////////////
  17. #include "stdafx.h"
  18. #include "ServerPath.h"
  19. #include "structures.h"
  20. #ifdef _DEBUG
  21. #undef THIS_FILE
  22. static char THIS_FILE[]=__FILE__;
  23. #define new DEBUG_NEW
  24. #endif
  25. #define FTP_MVS_DOUBLE_QUOTA (TCHAR)0xDC
  26. //////////////////////////////////////////////////////////////////////
  27. // Konstruktion/Destruktion
  28. //////////////////////////////////////////////////////////////////////
  29. CServerPath::CServerPath()
  30. {
  31. m_nServerType = 0;
  32. m_bEmpty = TRUE;
  33. }
  34. CServerPath::CServerPath(int nServerType)
  35. {
  36. m_nServerType = nServerType;
  37. m_bEmpty = TRUE;
  38. }
  39. CServerPath::CServerPath(CString path)
  40. {
  41. m_nServerType = FZ_SERVERTYPE_FTP;
  42. path.TrimLeft( _T(" ") );
  43. path.TrimRight( _T(" ") );
  44. if (path == _MPT(""))
  45. {
  46. m_bEmpty = TRUE;
  47. return;
  48. }
  49. else
  50. m_bEmpty = FALSE;
  51. int pos1 = path.Find( _T(":[") );
  52. if (pos1 != -1 && path.Right(1) == _MPT("]") && pos1 != (path.GetLength()-1))
  53. m_nServerType |= FZ_SERVERTYPE_SUB_FTP_VMS;
  54. else if (path.GetLength() >= 3 && _istalpha(path[0]) && path[1] == _MPT(':') && (path[2] == _MPT('\\') || path[2] == _MPT('/')))
  55. m_nServerType |= FZ_SERVERTYPE_SUB_FTP_WINDOWS;
  56. else if (path[0] == FTP_MVS_DOUBLE_QUOTA && path[path.GetLength() - 1] == FTP_MVS_DOUBLE_QUOTA)
  57. m_nServerType |= FZ_SERVERTYPE_SUB_FTP_MVS;
  58. else if (path.GetLength() > 2 && path[0] == _MPT('\'') && path.Right(1) == _T("'") && path.Find(_MPT('/')) == -1 && path.Find(_MPT('\\')) == -1)
  59. m_nServerType |= FZ_SERVERTYPE_SUB_FTP_MVS;
  60. *this = CServerPath(path, m_nServerType);
  61. }
  62. CServerPath::CServerPath(CString path, int nServerType)
  63. {
  64. m_nServerType = nServerType;
  65. path.TrimLeft( _T(" ") );
  66. path.TrimRight( _T(" ") );
  67. if (path == _MPT(""))
  68. {
  69. m_bEmpty = TRUE;
  70. return;
  71. }
  72. else
  73. m_bEmpty = FALSE;
  74. switch (m_nServerType&FZ_SERVERTYPE_HIGHMASK)
  75. {
  76. case FZ_SERVERTYPE_FTP:
  77. switch(m_nServerType&FZ_SERVERTYPE_SUBMASK)
  78. {
  79. case FZ_SERVERTYPE_SUB_FTP_MVS:
  80. case FZ_SERVERTYPE_SUB_FTP_BS2000:
  81. {
  82. path.TrimLeft(FTP_MVS_DOUBLE_QUOTA);
  83. path.TrimRight(FTP_MVS_DOUBLE_QUOTA);
  84. path.TrimLeft(_MPT('\''));
  85. path.TrimRight(_MPT('\''));
  86. path.TrimLeft(_MPT('.'));
  87. while (path.Replace(_T(".."), _T(".")));
  88. int pos = path.Find(_T("."));
  89. while (pos != -1)
  90. {
  91. m_Segments.push_back(path.Left(pos));
  92. path = path.Mid(pos + 1);
  93. pos = path.Find( _T(".") );
  94. }
  95. if (path != _T(""))
  96. m_Segments.push_back(path);
  97. else
  98. m_Prefix = _T(".");
  99. }
  100. break;
  101. case FZ_SERVERTYPE_SUB_FTP_VMS:
  102. {
  103. int pos1 = path.Find( _T("[") );
  104. if (pos1 == -1 || path.Right(1) != _T("]"))
  105. {
  106. ASSERT(FALSE);
  107. m_bEmpty = TRUE;
  108. return;
  109. }
  110. path.TrimRight( _T("]") );
  111. if (pos1)
  112. m_Prefix = path.Left(pos1);
  113. path = path.Mid(pos1 + 1);
  114. int pos = path.Find( _T(".") );
  115. while (pos != -1)
  116. {
  117. m_Segments.push_back(path.Left(pos));
  118. path = path.Mid(pos+1);
  119. pos = path.Find( _T(".") );
  120. }
  121. if (path != _MPT(""))
  122. m_Segments.push_back(path);
  123. }
  124. break;
  125. default:
  126. path.Replace( _T("\\"), _T("/") );
  127. while (path.Replace( _T("//"), _T("/") ));
  128. path.TrimLeft( _T("/") );
  129. path.TrimRight( _T("/") );
  130. int pos = path.Find( _T("/") );
  131. while (pos != -1)
  132. {
  133. m_Segments.push_back(path.Left(pos));
  134. path = path.Mid(pos+1);
  135. pos = path.Find( _T("/") );
  136. }
  137. if (path != _MPT(""))
  138. m_Segments.push_back(path);
  139. break;
  140. }
  141. break;
  142. case FZ_SERVERTYPE_LOCAL:
  143. {
  144. path.TrimRight( _T("\\") );
  145. while (path.Replace( _T("\\\\"), _T("\\") ));
  146. int pos = path.Find( _T("\\") );
  147. if (pos == -1)
  148. {
  149. m_Prefix = path;
  150. return;
  151. }
  152. ASSERT(pos == 2);
  153. m_Prefix = path.Left(pos);
  154. path = path.Mid(pos + 1);
  155. pos = path.Find( _T("\\") );
  156. while (pos != -1)
  157. {
  158. m_Segments.push_back(path.Left(pos));
  159. path=path.Mid(pos + 1);
  160. pos=path.Find( _T("\\") );
  161. }
  162. if (path != _MPT(""))
  163. m_Segments.push_back(path);
  164. }
  165. break;
  166. default:
  167. ASSERT(FALSE);
  168. }
  169. }
  170. CServerPath::CServerPath(const CServerPath &path)
  171. {
  172. m_nServerType = path.m_nServerType;
  173. m_Prefix = path.m_Prefix;
  174. m_bEmpty = path.m_bEmpty;
  175. m_Segments = path.m_Segments;
  176. }
  177. CServerPath::~CServerPath()
  178. {
  179. }
  180. void CServerPath::SetServer(const t_server &server)
  181. {
  182. m_nServerType=server.nServerType;
  183. }
  184. BOOL CServerPath::SetPath(CString &newpath, BOOL bIsFile /*=FALSE*/)
  185. {
  186. CString file;
  187. CString path=newpath;
  188. path.TrimLeft( _T(" ") );
  189. path.TrimRight( _T(" ") );
  190. if (path != _MPT(""))
  191. m_bEmpty = FALSE;
  192. else
  193. m_bEmpty = TRUE;
  194. if (!(m_nServerType & FZ_SERVERTYPE_HIGHMASK))
  195. m_nServerType = FZ_SERVERTYPE_FTP;
  196. if (!(m_nServerType&FZ_SERVERTYPE_SUBMASK) && (m_nServerType&FZ_SERVERTYPE_HIGHMASK)==FZ_SERVERTYPE_FTP)
  197. {
  198. int pos1 = path.Find( _T(":[") );
  199. if (pos1!=-1 && pos1!=(path.GetLength()-2))
  200. {
  201. if (!bIsFile && path.Right(1)==_T("]"))
  202. m_nServerType|=FZ_SERVERTYPE_SUB_FTP_VMS;
  203. else if (bIsFile && path.ReverseFind(']')>(pos1+1))
  204. m_nServerType|=FZ_SERVERTYPE_SUB_FTP_VMS;
  205. }
  206. if (newpath.GetLength() >= 3 && _istalpha(newpath[0]) && newpath[1] == _MPT(':') && (newpath[2] == _MPT('\\') || newpath[2] == _MPT('/')))
  207. m_nServerType |= FZ_SERVERTYPE_SUB_FTP_WINDOWS;
  208. else if (path[0] == FTP_MVS_DOUBLE_QUOTA && path[path.GetLength() - 1] == FTP_MVS_DOUBLE_QUOTA)
  209. m_nServerType |= FZ_SERVERTYPE_SUB_FTP_MVS;
  210. }
  211. m_Segments.clear();
  212. m_Prefix = _MPT("");
  213. switch (m_nServerType&FZ_SERVERTYPE_HIGHMASK)
  214. {
  215. case FZ_SERVERTYPE_FTP:
  216. switch (m_nServerType&FZ_SERVERTYPE_SUBMASK)
  217. {
  218. case FZ_SERVERTYPE_SUB_FTP_MVS:
  219. case FZ_SERVERTYPE_SUB_FTP_BS2000:
  220. {
  221. path.TrimLeft(FTP_MVS_DOUBLE_QUOTA);
  222. path.TrimRight(FTP_MVS_DOUBLE_QUOTA);
  223. path.TrimLeft(_MPT('\''));
  224. path.TrimRight(_MPT('\''));
  225. path.TrimLeft(_MPT('.'));
  226. while (path.Replace(_T(".."), _T(".")));
  227. int pos = path.Find(_T("."));
  228. while (pos != -1)
  229. {
  230. m_Segments.push_back(path.Left(pos));
  231. path = path.Mid(pos + 1);
  232. pos = path.Find( _T(".") );
  233. }
  234. if (path != _MPT(""))
  235. m_Segments.push_back(path);
  236. else
  237. m_Prefix = _T(".");
  238. if (bIsFile)
  239. {
  240. if (m_Segments.empty())
  241. return FALSE;
  242. file = m_Segments.back();
  243. m_Segments.pop_back();
  244. if (file.Right(1) == _T("."))
  245. return FALSE;
  246. int pos = file.Find(_MPT('('));
  247. int pos2 = file.Find(_MPT(')'));
  248. if (pos != -1)
  249. {
  250. if (!pos || pos2 != file.GetLength() - 2)
  251. return FALSE;
  252. m_Prefix = _T("");
  253. m_Segments.push_back(file.Left(pos));
  254. file = file.Mid(pos + 1, pos2 - pos - 1);
  255. }
  256. else if (pos2 != -1)
  257. return FALSE;
  258. }
  259. }
  260. break;
  261. case FZ_SERVERTYPE_SUB_FTP_VMS:
  262. {
  263. int pos1=path.Find( _T("[") );
  264. if (pos1==-1)
  265. return FALSE;
  266. if (bIsFile)
  267. {
  268. int rpos=path.ReverseFind(_MPT(']'));
  269. if (rpos==-1)
  270. return FALSE;
  271. else if (rpos!=(path.GetLength()-1) )
  272. {
  273. file=file.Mid(rpos+1);
  274. path=path.Left(rpos+1);
  275. }
  276. else
  277. return FALSE;
  278. }
  279. if (path.Right(1)!=_MPT("]"))
  280. return FALSE;
  281. path.TrimRight( _T("]") );
  282. if (pos1)
  283. m_Prefix=path.Left(pos1);
  284. path=path.Mid(pos1+1);
  285. int pos=path.Find( _T(".") );
  286. while(pos!=-1)
  287. {
  288. m_Segments.push_back(path.Left(pos));
  289. path=path.Mid(pos+1);
  290. pos=path.Find( _T(".") );
  291. }
  292. if (path!=_MPT(""))
  293. m_Segments.push_back(path);
  294. }
  295. break;
  296. default:
  297. path.Replace( _T("\\"), _T("/") );
  298. while(path.Replace( _T("//"), _T("/") ));
  299. path.TrimLeft( _T("/") );
  300. if (bIsFile)
  301. {
  302. if (path.Right(1)!= _T("/") )
  303. {
  304. int rpos=path.ReverseFind(_MPT('/'));
  305. if (rpos==-1)
  306. {
  307. newpath=path;
  308. m_bEmpty=TRUE;
  309. return TRUE;
  310. }
  311. file=path.Mid(rpos+1);
  312. path=path.Left(rpos);
  313. }
  314. else
  315. return FALSE;
  316. }
  317. path.TrimRight( _T("/") );
  318. int pos=path.Find( _T("/") );
  319. while(pos!=-1)
  320. {
  321. m_Segments.push_back(path.Left(pos));
  322. path=path.Mid(pos+1);
  323. pos=path.Find( _T("/") );
  324. }
  325. if (path!=_MPT(""))
  326. m_Segments.push_back(path);
  327. break;
  328. }
  329. break;
  330. case FZ_SERVERTYPE_LOCAL:
  331. {
  332. if (bIsFile)
  333. {
  334. if (path.Right(1)!= _T("\\") )
  335. {
  336. int rpos=path.ReverseFind(_MPT('\\'));
  337. if (rpos==-1)
  338. return FALSE;
  339. file=path.Mid(rpos+1);
  340. path=path.Left(rpos);
  341. }
  342. else
  343. return FALSE;
  344. }
  345. path.TrimRight( _T("\\") );
  346. while (path.Replace( _T("\\\\"), _T("\\") ));
  347. int pos=path.Find( _T(":\\") );
  348. if (pos==-1 || pos!=1)
  349. return FALSE;
  350. else
  351. {
  352. m_Prefix=path.Left(pos+1);
  353. path=path.Mid(pos+2);
  354. }
  355. pos=path.Find( _T("\\") );
  356. while (pos!=-1)
  357. {
  358. m_Segments.push_back(path.Left(pos));
  359. path=path.Mid(pos+1);
  360. pos=path.Find( _T("\\") );
  361. }
  362. if (path!=_MPT(""))
  363. m_Segments.push_back(path);
  364. }
  365. break;
  366. }
  367. if (bIsFile)
  368. newpath = file;
  369. return TRUE;
  370. }
  371. const CString CServerPath::GetPath() const
  372. {
  373. if (m_bEmpty)
  374. return _MPT("");
  375. CString path;
  376. tConstIter iter;
  377. switch (m_nServerType&FZ_SERVERTYPE_HIGHMASK)
  378. {
  379. case FZ_SERVERTYPE_FTP:
  380. switch (m_nServerType&FZ_SERVERTYPE_SUBMASK)
  381. {
  382. case FZ_SERVERTYPE_SUB_FTP_MVS:
  383. case FZ_SERVERTYPE_SUB_FTP_BS2000:
  384. path = _MPT("'");
  385. for (iter = m_Segments.begin(); iter != m_Segments.end(); iter++)
  386. {
  387. if (iter != m_Segments.begin())
  388. path += _T(".");
  389. path += *iter;
  390. }
  391. path += m_Prefix + _MPT("'");
  392. break;
  393. case FZ_SERVERTYPE_SUB_FTP_VMS:
  394. path = m_Prefix + _MPT("[");
  395. for (iter = m_Segments.begin(); iter != m_Segments.end(); iter++)
  396. path += *iter + _T(".");
  397. path.TrimRight( _T(".") );
  398. path += _MPT("]");
  399. break;
  400. default:
  401. if (!(m_nServerType & FZ_SERVERTYPE_SUB_FTP_WINDOWS))
  402. path=_MPT("/");
  403. for (iter=m_Segments.begin(); iter!=m_Segments.end(); iter++)
  404. path+=*iter + _T("/");
  405. break;
  406. }
  407. break;
  408. case FZ_SERVERTYPE_LOCAL:
  409. path=m_Prefix;
  410. if (!m_Segments.empty())
  411. path+=_MPT("\\");
  412. for (iter=m_Segments.begin(); iter!=m_Segments.end(); iter++)
  413. path+=*iter + _T("\\");
  414. break;
  415. default:
  416. ASSERT(FALSE);
  417. }
  418. return path;
  419. }
  420. CServerPath& CServerPath::operator=(const CServerPath &op)
  421. {
  422. if (this == &op)
  423. return *this;
  424. m_Segments.clear();
  425. m_nServerType = op.m_nServerType;
  426. m_Prefix = op.m_Prefix;
  427. m_bEmpty = op.m_bEmpty;
  428. m_Segments = op.m_Segments;
  429. return *this;
  430. }
  431. const bool CServerPath::operator==(const CServerPath &op) const
  432. {
  433. if (this == &op)
  434. return true;
  435. if (m_bEmpty != op.m_bEmpty)
  436. return false;
  437. if (m_Prefix != op.m_Prefix)
  438. return false;
  439. // excluding FZ_SERVERTYPE_LAYERMASK from comparison,
  440. // as this part of server type is not set in TFileZillaIntf
  441. const int CompareMask = FZ_SERVERTYPE_HIGHMASK | FZ_SERVERTYPE_SUBMASK;
  442. if ((m_nServerType & CompareMask) != (op.m_nServerType & CompareMask))
  443. return false;
  444. tConstIter iter1 = m_Segments.begin();
  445. tConstIter iter2 = op.m_Segments.begin();
  446. while (iter1 != m_Segments.end())
  447. {
  448. if (iter2 == op.m_Segments.end())
  449. return false;
  450. if (*iter1 != *iter2)
  451. return false;
  452. iter1++;
  453. iter2++;
  454. }
  455. if (iter2 != op.m_Segments.end())
  456. return false;
  457. return true;
  458. }
  459. const BOOL operator==(const CServerPath &a, const CString &b)
  460. {
  461. CServerPath path(b);
  462. return a==path;
  463. }
  464. const bool CServerPath::operator!=(const CServerPath &op) const
  465. {
  466. if (!this)
  467. return false;
  468. if (*this == op)
  469. return false;
  470. else
  471. return true;
  472. }
  473. CString CServerPath::GetLastSegment() const
  474. {
  475. if (!HasParent())
  476. return _T("");
  477. if (m_Segments.empty())
  478. return _T("");
  479. else
  480. return m_Segments.back();
  481. }
  482. CServerPath CServerPath::GetParent() const
  483. {
  484. ASSERT(HasParent());
  485. CServerPath path;
  486. path = *this;
  487. path.m_Segments.pop_back();
  488. if (m_nServerType & (FZ_SERVERTYPE_SUB_FTP_MVS | FZ_SERVERTYPE_SUB_FTP_BS2000))
  489. path.m_Prefix = _T(".");
  490. return path;
  491. }
  492. BOOL CServerPath::HasParent() const
  493. {
  494. if (!m_Segments.empty())
  495. return TRUE;
  496. else
  497. return FALSE;
  498. }
  499. const BOOL CServerPath::IsEmpty() const
  500. {
  501. return m_bEmpty;
  502. }
  503. CString CServerPath::GetSafePath() const
  504. {
  505. if (m_bEmpty)
  506. return _T("");
  507. CString safepath;
  508. safepath.Format(_T("%d %d "), m_nServerType, m_Prefix.GetLength());
  509. if (m_Prefix!=_MPT(""))
  510. safepath+=m_Prefix+_MPT(" ");
  511. tConstIter iter = m_Segments.begin();
  512. while(iter!=m_Segments.end())
  513. {
  514. CString len;
  515. len.Format(_T("%d "), iter->GetLength());
  516. safepath+=len;
  517. safepath+=*iter;
  518. iter++;
  519. if (iter!=m_Segments.end())
  520. safepath+=_MPT(" ");
  521. }
  522. return safepath;
  523. }
  524. BOOL CServerPath::AddSubdir(CString subdir)
  525. {
  526. subdir.TrimLeft( _T(" ") );
  527. subdir.TrimRight( _T(" ") );
  528. if (subdir == _MPT(""))
  529. return FALSE;
  530. if (m_nServerType & (FZ_SERVERTYPE_SUB_FTP_MVS | FZ_SERVERTYPE_SUB_FTP_BS2000) && m_Prefix != _T("."))
  531. return FALSE;
  532. m_Segments.push_back(subdir);
  533. if (m_nServerType & (FZ_SERVERTYPE_SUB_FTP_MVS | FZ_SERVERTYPE_SUB_FTP_BS2000) && !m_Segments.empty())
  534. {
  535. if (m_Segments.back().Right(1) == _T("."))
  536. {
  537. m_Segments.back().TrimRight(_MPT('.'));
  538. m_Prefix = _T(".");
  539. }
  540. else
  541. m_Prefix = _T("");
  542. }
  543. m_bEmpty = FALSE;
  544. return TRUE;
  545. }
  546. CServerPath::CServerPath(CString subdir, const CServerPath &parent)
  547. {
  548. *this=parent;
  549. subdir.TrimLeft( _T(" ") );
  550. subdir.TrimRight( _T(" ") );
  551. if ( subdir==_T("") )
  552. {
  553. if (IsEmpty())
  554. ASSERT(FALSE);
  555. else
  556. return;
  557. }
  558. if (!(m_nServerType&FZ_SERVERTYPE_HIGHMASK))
  559. m_nServerType=FZ_SERVERTYPE_FTP;
  560. m_bEmpty = FALSE;
  561. switch (m_nServerType&FZ_SERVERTYPE_HIGHMASK)
  562. {
  563. case FZ_SERVERTYPE_FTP:
  564. switch(m_nServerType&FZ_SERVERTYPE_SUBMASK)
  565. {
  566. case FZ_SERVERTYPE_SUB_FTP_MVS:
  567. case FZ_SERVERTYPE_SUB_FTP_BS2000:
  568. {
  569. subdir.TrimLeft(FTP_MVS_DOUBLE_QUOTA);
  570. subdir.TrimRight(FTP_MVS_DOUBLE_QUOTA);
  571. if (subdir.Left(1) == _MPT("'"))
  572. {
  573. if (subdir.Right(1) == _MPT("'"))
  574. {
  575. if (!SetPath(subdir))
  576. m_bEmpty = true;
  577. }
  578. else
  579. m_bEmpty = true;
  580. }
  581. else if (subdir.Right(1) == _MPT("'"))
  582. m_bEmpty = true;
  583. else if (!m_bEmpty)
  584. {
  585. if (m_Prefix != _T("."))
  586. m_bEmpty = true;
  587. else
  588. {
  589. subdir.TrimLeft(_MPT('.'));
  590. while (subdir.Replace(_T(".."), _T(".")));
  591. int pos = subdir.Find(_MPT('.'));
  592. while (pos != -1)
  593. {
  594. m_Segments.push_back(subdir.Left(pos));
  595. subdir = subdir.Mid(pos + 1);
  596. pos = subdir.Find(_MPT('.'));
  597. }
  598. if (subdir != _T(""))
  599. {
  600. m_Prefix = _T("");
  601. m_Segments.push_back(subdir);
  602. }
  603. }
  604. }
  605. else if (!SetPath(subdir))
  606. m_bEmpty = true;
  607. }
  608. break;
  609. case FZ_SERVERTYPE_SUB_FTP_VMS:
  610. {
  611. int pos1=subdir.Find( _T("[") );
  612. if (pos1==-1)
  613. {
  614. ASSERT( subdir.Right(1)!=_T("]") );
  615. while ( subdir.Replace( _T(".."), _T(".") ) );
  616. }
  617. else
  618. {
  619. if (subdir.Right(1)!=_MPT("]"))
  620. ASSERT(FALSE);
  621. subdir=subdir.Left(subdir.GetLength()-1);
  622. if (pos1)
  623. m_Prefix=subdir.Left(pos1);
  624. else
  625. m_Prefix=_MPT("");
  626. m_Segments.clear();
  627. subdir=subdir.Mid(pos1+1);
  628. pos1=subdir.Find( _T("[") );
  629. int pos2=subdir.Find( _T("]") );
  630. if (pos1!=-1 || pos2!=-1)
  631. ASSERT(FALSE);
  632. }
  633. int pos=subdir.Find( _T(".") );
  634. while(pos!=-1)
  635. {
  636. m_Segments.push_back(subdir.Left(pos));
  637. subdir=subdir.Mid(pos+1);
  638. pos=subdir.Find( _T(".") );
  639. }
  640. if (subdir!=_MPT(""))
  641. m_Segments.push_back(subdir);
  642. }
  643. break;
  644. case FZ_SERVERTYPE_SUB_FTP_WINDOWS:
  645. {
  646. subdir.Replace( _T("\\"), _T("/") );
  647. while(subdir.Replace( _T("//"), _T("/") ));
  648. if (subdir.GetLength() >= 2 && subdir[1] == _MPT(':'))
  649. m_Segments.clear();
  650. else if (subdir[0]==_MPT('/'))
  651. {
  652. CString firstSegment;
  653. if (m_Segments.empty())
  654. firstSegment = _MPT("C:");
  655. else
  656. firstSegment = m_Segments.front();
  657. m_Segments.clear();
  658. m_Segments.push_back(firstSegment);
  659. subdir.TrimLeft( _T("/") );
  660. }
  661. subdir.TrimRight( _T("/") );
  662. int pos = subdir.Find( _T("/") );
  663. while(pos!=-1)
  664. {
  665. m_Segments.push_back(subdir.Left(pos));
  666. subdir=subdir.Mid(pos+1);
  667. pos=subdir.Find( _T("/") );
  668. }
  669. if (subdir!=_MPT(""))
  670. m_Segments.push_back(subdir);
  671. break;
  672. }
  673. default:
  674. subdir.Replace( _T("\\"), _T("/") );
  675. while(subdir.Replace( _T("//"), _T("/") ));
  676. if (subdir[0]==_MPT('/'))
  677. {
  678. m_Segments.clear();
  679. subdir.TrimLeft( _T("/") );
  680. }
  681. subdir.TrimRight( _T("/") );
  682. int pos=subdir.Find( _T("/") );
  683. while(pos!=-1)
  684. {
  685. m_Segments.push_back(subdir.Left(pos));
  686. subdir=subdir.Mid(pos+1);
  687. pos=subdir.Find( _T("/") );
  688. }
  689. if (subdir!=_MPT(""))
  690. m_Segments.push_back(subdir);
  691. break;
  692. }
  693. break;
  694. case FZ_SERVERTYPE_LOCAL:
  695. {
  696. subdir.TrimRight( _T("\\") );
  697. while (subdir.Replace( _T("\\\\"), _T("\\") ));
  698. subdir.TrimLeft( _T("\\") );
  699. int pos=subdir.Find( _T(":") );
  700. if (pos==1) //subdir is absolute path
  701. {
  702. m_Segments.clear();
  703. m_Prefix=subdir.Left(pos+1);
  704. subdir=subdir.Mid(pos+1);
  705. subdir.TrimLeft( _T("\\") );
  706. if (subdir.Find( _T(":") )!=-1)
  707. ASSERT(FALSE);
  708. }
  709. if (pos==-1 || pos==1)
  710. {
  711. pos=subdir.Find( _T("\\") );
  712. while (pos!=-1)
  713. {
  714. m_Segments.push_back(subdir.Left(pos));
  715. subdir=subdir.Mid(pos+1);
  716. pos=subdir.Find( _T("\\") );
  717. }
  718. if ( subdir!=_T("") )
  719. m_Segments.push_back(subdir);
  720. }
  721. else
  722. ASSERT(FALSE);
  723. }
  724. break;
  725. default:
  726. ASSERT(FALSE);
  727. }
  728. }
  729. BOOL CServerPath::SetPath(CString newpath)
  730. {
  731. return SetPath(newpath, FALSE);
  732. }
  733. CString CServerPath::FormatFilename(CString fn, bool omitPath /*=false*/) const
  734. {
  735. if (m_bEmpty)
  736. return fn;
  737. if (fn == _MPT(""))
  738. return _MPT("");
  739. CString path;
  740. tConstIter iter;
  741. switch (m_nServerType&FZ_SERVERTYPE_HIGHMASK)
  742. {
  743. case FZ_SERVERTYPE_FTP:
  744. switch (m_nServerType&FZ_SERVERTYPE_SUBMASK)
  745. {
  746. case FZ_SERVERTYPE_SUB_FTP_MVS:
  747. case FZ_SERVERTYPE_SUB_FTP_BS2000:
  748. if (omitPath && m_Prefix == _T("."))
  749. return fn;
  750. path = _MPT("'");
  751. for (iter = m_Segments.begin(); iter != m_Segments.end(); iter++)
  752. path += *iter + _T(".");
  753. if (m_Prefix != _T("."))
  754. {
  755. path.TrimRight(_MPT('.'));
  756. path += _T("(") + fn + _T(")");
  757. }
  758. else
  759. path += fn;
  760. path += _MPT("'");
  761. break;
  762. case FZ_SERVERTYPE_SUB_FTP_VMS:
  763. if (omitPath)
  764. return fn;
  765. path = m_Prefix + _MPT("[");
  766. for (iter = m_Segments.begin(); iter != m_Segments.end(); iter++)
  767. path += *iter + _T(".");
  768. path.TrimRight( _T(".") );
  769. path += _MPT("]");
  770. path += fn;
  771. break;
  772. default:
  773. if (omitPath)
  774. return fn;
  775. if (!(m_nServerType & FZ_SERVERTYPE_SUB_FTP_WINDOWS))
  776. path=_MPT("/");
  777. for (iter = m_Segments.begin(); iter != m_Segments.end(); iter++)
  778. path+=*iter + _T("/");
  779. path += fn;
  780. break;
  781. }
  782. break;
  783. case FZ_SERVERTYPE_LOCAL:
  784. if (omitPath)
  785. return fn;
  786. path=m_Prefix;
  787. if (!m_Segments.empty())
  788. path+=_MPT("\\");
  789. for (iter=m_Segments.begin(); iter!=m_Segments.end(); iter++)
  790. path+=*iter + _T("\\");
  791. path += fn;
  792. break;
  793. default:
  794. ASSERT(FALSE);
  795. }
  796. return path;
  797. }