File.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. #include "File.h"
  2. #include "Key.h"
  3. #include "Text.h"
  4. #include "Time.h"
  5. #ifdef WIN32
  6. # include <direct.h>
  7. # include <Shlwapi.h>
  8. # pragma comment(lib, "Shlwapi.lib")
  9. #else
  10. # include <dirent.h>
  11. # include <stdio.h>
  12. # include <sys/stat.h>
  13. #endif
  14. using namespace Framework;
  15. using namespace Encryption;
  16. // Content of the Datei class from File.h
  17. // Constructor
  18. Datei::Datei()
  19. : ReferenceCounter(),
  20. stream(0),
  21. pfad(0),
  22. gr(0),
  23. tmpLByte(0),
  24. tmpLBPos(7),
  25. tmpSByte(0),
  26. tmpSBPos(-1),
  27. key(0)
  28. {}
  29. //! Constructor
  30. Datei::Datei(const char* pfad)
  31. : Datei()
  32. {
  33. setDatei(pfad);
  34. }
  35. //! Constructor
  36. Datei::Datei(Text* pfad)
  37. : Datei()
  38. {
  39. setDatei(pfad);
  40. }
  41. // Destructor
  42. Datei::~Datei()
  43. {
  44. if (key) key->release();
  45. if (stream) delete stream;
  46. if (pfad) pfad->release();
  47. }
  48. // non-constant
  49. void Datei::setDatei(const char* pfad) // sets the file
  50. {
  51. if (istOffen()) close();
  52. if (!this->pfad) this->pfad = new Text();
  53. this->pfad->setText(pfad);
  54. gr = 0;
  55. }
  56. void Datei::setDatei(Text* pfad)
  57. {
  58. if (istOffen()) close();
  59. if (!this->pfad) this->pfad = new Text();
  60. this->pfad->setText(*pfad);
  61. pfad->release();
  62. gr = 0;
  63. }
  64. bool Datei::umbenennen(const char* pfad) // renames and possibly moves the file
  65. {
  66. if (!pfad) return 0;
  67. if (DateiUmbenennen(this->pfad->getText(), pfad))
  68. {
  69. this->pfad->setText(pfad);
  70. return 1;
  71. }
  72. return 0;
  73. }
  74. bool Datei::umbenennen(Text* pfad)
  75. {
  76. if (!this->pfad)
  77. {
  78. pfad->release();
  79. return 0;
  80. }
  81. if (DateiUmbenennen(this->pfad->getText(), pfad->getText()))
  82. {
  83. this->pfad->setText(*pfad);
  84. pfad->release();
  85. return 1;
  86. }
  87. pfad->release();
  88. return 0;
  89. }
  90. bool Datei::remove() // deletes the file
  91. {
  92. if (!pfad) return 0;
  93. return DateiRemove(dynamic_cast<Text*>(pfad->getThis()));
  94. }
  95. bool Datei::erstellen() // creates the file
  96. {
  97. if (!pfad) return 0;
  98. return DateiPfadErstellen(dynamic_cast<Text*>(pfad->getThis()));
  99. }
  100. bool Datei::open(int style) // opens the file
  101. {
  102. if (!pfad) return 0;
  103. if (stream) delete stream;
  104. stream = new std::fstream();
  105. std::ios_base::openmode om = std::ios::binary;
  106. if ((style | Style::lesen) == style) om |= std::ios::in;
  107. if ((style | Style::schreiben) == style) om |= std::ios::out;
  108. stream->open(pfad->getText(), om);
  109. if ((style | Style::ende) == style)
  110. {
  111. if ((style | Style::lesen) == style) stream->seekg(0, std::ios::end);
  112. if ((style | Style::schreiben) == style)
  113. stream->seekp(0, std::ios::end);
  114. }
  115. if (!stream->is_open() || !stream->good())
  116. {
  117. delete stream;
  118. stream = 0;
  119. return 0;
  120. }
  121. tmpLBPos = 7;
  122. tmpSBPos = -1;
  123. return 1;
  124. }
  125. void Datei::setLPosition(__int64 pos, bool ende) // sets the read position
  126. {
  127. if (!pfad) return;
  128. if (stream)
  129. {
  130. if (ende)
  131. stream->seekg(pos, std::ios::end);
  132. else
  133. stream->seekg(pos, std::ios::beg);
  134. }
  135. tmpLBPos = 7;
  136. }
  137. void Datei::setSPosition(__int64 pos, bool ende) // sets the write position
  138. {
  139. if (!pfad) return;
  140. if (stream)
  141. {
  142. if (ende)
  143. stream->seekp(pos, std::ios::end);
  144. else
  145. stream->seekp(pos, std::ios::beg);
  146. }
  147. tmpSBPos = -1;
  148. }
  149. void Datei::schreibe(const char* bytes, int len) // writes bytes to file
  150. {
  151. if (!pfad || !stream) return;
  152. if (tmpSBPos >= 0)
  153. {
  154. tmpSBPos = -1;
  155. stream->write(&tmpSByte, 1);
  156. tmpSByte = 0;
  157. }
  158. if (key)
  159. {
  160. key->setPos(getSPosition());
  161. Bytes* n = new Bytes(bytes, len);
  162. key->codieren(dynamic_cast<Bytes*>(n->getThis()));
  163. stream->write(n->getBytes(), len);
  164. n->release();
  165. }
  166. else
  167. stream->write(bytes, len);
  168. }
  169. void Framework::Datei::flush()
  170. {
  171. if (!pfad || !stream) return;
  172. stream->flush();
  173. }
  174. void Datei::lese(char* bytes, int len) // reads bytes from file
  175. {
  176. if (!pfad) return;
  177. if (stream)
  178. {
  179. __int64 tmp = getLPosition();
  180. stream->read(bytes, len);
  181. if (key)
  182. {
  183. key->setPos(tmp);
  184. Bytes* n = new Bytes();
  185. n->setBytesZ(bytes, len);
  186. key->decodieren(n);
  187. }
  188. }
  189. tmpLBPos = 7;
  190. tmpSBPos = -1;
  191. }
  192. Text* Datei::leseZeile() // reads a line
  193. {
  194. if (!pfad || !stream) return 0;
  195. if (istEnde()) return 0;
  196. Text* ret = new Text("");
  197. __int64 len = getSize();
  198. for (char c = 0; c != '\n' && stream->tellg() < len;)
  199. {
  200. __int64 tmp = getLPosition();
  201. stream->read(&c, 1);
  202. if (key)
  203. {
  204. key->setPos(tmp);
  205. Bytes* n = new Bytes();
  206. n->setBytesZ(&c, 1);
  207. key->decodieren(n);
  208. }
  209. if (c) ret->append(&c, 1);
  210. }
  211. tmpSBPos = 7;
  212. tmpSBPos = -1;
  213. return ret;
  214. }
  215. void Datei::close() // closes the file
  216. {
  217. if (!pfad || !stream) return;
  218. if (tmpSBPos >= 0)
  219. {
  220. if (key)
  221. {
  222. key->setPos(getSPosition());
  223. Bytes* n = new Bytes(&tmpSByte, 1);
  224. key->codieren(dynamic_cast<Bytes*>(n->getThis()));
  225. stream->write(n->getBytes(), 1);
  226. n->release();
  227. }
  228. else
  229. stream->write(&tmpSByte, 1);
  230. }
  231. stream->close();
  232. delete stream;
  233. stream = 0;
  234. }
  235. #ifdef WIN32
  236. bool Datei::setLetzteAEnderung(
  237. Zeit* zeit) // sets the modification date of the file
  238. {
  239. if (!pfad)
  240. {
  241. zeit->release();
  242. return 0;
  243. }
  244. HANDLE hFile = CreateFile(pfad->getText(),
  245. GENERIC_READ,
  246. FILE_SHARE_READ,
  247. NULL,
  248. OPEN_EXISTING,
  249. 0,
  250. NULL);
  251. if (hFile == INVALID_HANDLE_VALUE)
  252. {
  253. zeit->release();
  254. return 0;
  255. }
  256. FILETIME ftCreate, ftAccess, ftWrite;
  257. if (!GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))
  258. {
  259. CloseHandle(hFile);
  260. zeit->release();
  261. return 0;
  262. }
  263. SYSTEMTIME stUTC, stLocal;
  264. stLocal.wMilliseconds = 0;
  265. stLocal.wSecond = zeit->zUhrzeit()->getSekunde();
  266. stLocal.wMinute = zeit->zUhrzeit()->getMinute();
  267. stLocal.wHour = zeit->zUhrzeit()->getStunde();
  268. stLocal.wDay = zeit->zDatum()->getTag();
  269. stLocal.wMonth = zeit->zDatum()->getMonat();
  270. stLocal.wYear = zeit->zDatum()->getJahr();
  271. zeit->release();
  272. if (!TzSpecificLocalTimeToSystemTime(NULL, &stLocal, &stUTC))
  273. {
  274. CloseHandle(hFile);
  275. return 0;
  276. }
  277. if (!SystemTimeToFileTime(&stUTC, &ftWrite))
  278. {
  279. CloseHandle(hFile);
  280. return 0;
  281. }
  282. if (!SetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))
  283. {
  284. CloseHandle(hFile);
  285. return 0;
  286. }
  287. CloseHandle(hFile);
  288. return 1;
  289. }
  290. #endif
  291. bool Datei::getNextBit(bool& bit) // read file bit by bit
  292. {
  293. if (!pfad || !stream) return 0;
  294. if (tmpLBPos == 7)
  295. {
  296. tmpLBPos = -1;
  297. __int64 tmp = getLPosition();
  298. stream->read(&tmpLByte, 1);
  299. if (key)
  300. {
  301. key->setPos(tmp);
  302. Bytes* n = new Bytes();
  303. n->setBytesZ(&tmpLByte, 1);
  304. key->decodieren(n);
  305. }
  306. }
  307. tmpLBPos++;
  308. bit = (tmpLByte >> (7 - tmpLBPos)) & 1;
  309. return 1;
  310. }
  311. bool Datei::setNextBit(bool bit) // write file bit by bit
  312. {
  313. if (!pfad || !stream) return 0;
  314. tmpSBPos++;
  315. tmpSByte |= (char)(((char)bit << (7 - tmpSBPos)) & (1 << (7 - tmpSBPos)));
  316. if (tmpSBPos == 7)
  317. {
  318. tmpSBPos = -1;
  319. if (key)
  320. {
  321. key->setPos(getSPosition());
  322. Bytes* n = new Bytes(&tmpSByte, 1);
  323. key->codieren(dynamic_cast<Bytes*>(n->getThis()));
  324. stream->write(n->getBytes(), 1);
  325. n->release();
  326. }
  327. else
  328. stream->write(&tmpSByte, 1);
  329. tmpSByte = 0;
  330. }
  331. return 1;
  332. }
  333. // Sets the encryption key for the file
  334. void Datei::setKey(char* s, int l)
  335. {
  336. if (l == 0)
  337. {
  338. key = (Key*)key->release();
  339. return;
  340. }
  341. if (key)
  342. key->setKey(s, l);
  343. else
  344. key = new Key(s, l);
  345. }
  346. // constant
  347. bool Datei::istOrdner() const // checks if the file is a directory
  348. {
  349. if (!pfad) return 0;
  350. return DateiIstVerzeichnis(dynamic_cast<Text*>(pfad->getThis()));
  351. }
  352. bool Datei::istOffen() const // checks if the file is open
  353. {
  354. if (!pfad) return 0;
  355. if (stream) return stream->is_open() && stream->good();
  356. return 0;
  357. }
  358. int Datei::getUnterdateiAnzahl() const // returns the number of sub-files
  359. {
  360. #ifdef WIN32
  361. if (!pfad) return 0;
  362. if (!DateiIstVerzeichnis(dynamic_cast<Text*>(pfad->getThis()))) return 0;
  363. int ret = 0;
  364. HANDLE fHandle;
  365. WIN32_FIND_DATA wfd;
  366. Text stxt = pfad->getText();
  367. stxt.ersetzen('/', '\\');
  368. if (stxt.positionVon('\\') == stxt.getLength() - 1)
  369. stxt.append("*");
  370. else
  371. stxt.append("\\*");
  372. fHandle = FindFirstFile(stxt.getText(), &wfd);
  373. FindNextFile(fHandle, &wfd);
  374. while (FindNextFile(fHandle, &wfd))
  375. ++ret;
  376. FindClose(fHandle);
  377. return ret;
  378. #else
  379. if (!pfad) return 0;
  380. if (!DateiIstVerzeichnis(dynamic_cast<Text*>(pfad->getThis()))) return 0;
  381. int ret = 0;
  382. Text stxt = pfad->getText();
  383. stxt.ersetzen('\\', '/');
  384. if (stxt.positionVon('/') == stxt.getLength() - 1)
  385. stxt.remove(stxt.getLength() - 1);
  386. DIR* hdir;
  387. hdir = opendir(stxt.getText());
  388. for (dirent* entry = readdir(hdir); entry; entry = readdir(hdir))
  389. {
  390. if (entry && entry->d_name[0] != '.') ++ret;
  391. }
  392. closedir(hdir);
  393. return ret;
  394. #endif
  395. }
  396. RCArray<Text>* Datei::getDateiListe() const // returns a list of sub-files
  397. {
  398. #ifdef WIN32
  399. if (!pfad) return 0;
  400. if (!DateiIstVerzeichnis(dynamic_cast<Text*>(pfad->getThis()))) return 0;
  401. HANDLE fHandle;
  402. WIN32_FIND_DATA wfd;
  403. Text stxt = pfad->getText();
  404. stxt.ersetzen('/', '\\');
  405. if (stxt.positionVon('\\') == stxt.getLength() - 1)
  406. stxt.append("*");
  407. else
  408. stxt.append("\\*");
  409. fHandle = FindFirstFile(stxt.getText(), &wfd);
  410. FindNextFile(fHandle, &wfd);
  411. RCArray<Text>* ret = new RCArray<Text>();
  412. int count = 0;
  413. while (FindNextFile(fHandle, &wfd))
  414. {
  415. Text* txt = new Text(wfd.cFileName);
  416. ret->add(txt, count);
  417. ++count;
  418. }
  419. FindClose(fHandle);
  420. return ret;
  421. #else
  422. if (!pfad) return 0;
  423. if (!DateiIstVerzeichnis(dynamic_cast<Text*>(pfad->getThis()))) return 0;
  424. Text stxt = pfad->getText();
  425. stxt.ersetzen('\\', '/');
  426. if (stxt.positionVon('/') == stxt.getLength() - 1)
  427. stxt.remove(stxt.getLength() - 1);
  428. DIR* hdir;
  429. hdir = opendir(stxt.getText());
  430. if (hdir)
  431. {
  432. RCArray<Text>* ret = new RCArray<Text>();
  433. int count = 0;
  434. for (dirent* entry = readdir(hdir); entry; entry = readdir(hdir))
  435. {
  436. if (entry && entry->d_name[0] != '.')
  437. {
  438. ret->add(new Text(entry->d_name), count);
  439. ++count;
  440. }
  441. }
  442. closedir(hdir);
  443. return ret;
  444. }
  445. return 0;
  446. #endif
  447. }
  448. __int64 Datei::getSize() const // returns the size of the file
  449. {
  450. if (!pfad) return 0;
  451. if (gr) return gr;
  452. if (!stream || !istOffen())
  453. {
  454. std::fstream* stream = new std::fstream();
  455. stream->open(pfad->getText(), std::ios::binary | std::ios::in);
  456. __int64 tmp = stream->tellg();
  457. stream->seekg(0, std::ios::end);
  458. __int64 ret = stream->tellg();
  459. stream->seekg(tmp, std::ios::beg);
  460. stream->close();
  461. delete stream;
  462. __int64* size = (__int64*)&gr;
  463. *size = ret;
  464. return ret;
  465. }
  466. __int64 tmp = stream->tellg();
  467. stream->seekg(0, std::ios::end);
  468. __int64 ret = stream->tellg();
  469. stream->seekg(tmp, std::ios::beg);
  470. __int64* size = (__int64*)&gr;
  471. *size = ret;
  472. return ret;
  473. }
  474. Zeit* Datei::getLastChange() const // returns the date of the last modification
  475. {
  476. if (!pfad) return 0;
  477. #ifdef WIN32
  478. HANDLE hFile = CreateFile(pfad->getText(),
  479. GENERIC_READ,
  480. FILE_SHARE_READ,
  481. NULL,
  482. OPEN_EXISTING,
  483. 0,
  484. NULL);
  485. if (hFile == INVALID_HANDLE_VALUE) return 0;
  486. FILETIME ftCreate, ftAccess, ftWrite;
  487. SYSTEMTIME stUTC, stLocal;
  488. if (!GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))
  489. {
  490. CloseHandle(hFile);
  491. return 0;
  492. }
  493. CloseHandle(hFile);
  494. if (!FileTimeToSystemTime(&ftWrite, &stUTC)) return 0;
  495. if (!SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal)) return 0;
  496. Zeit* ret = new Zeit();
  497. ret->setZeit(stLocal.wYear,
  498. stLocal.wMonth,
  499. stLocal.wDay,
  500. stLocal.wHour,
  501. stLocal.wMinute,
  502. stLocal.wSecond);
  503. return ret;
  504. #else
  505. struct stat attrib;
  506. if (stat(pfad->getText(), &attrib) != 0) return 0;
  507. tm* clock = gmtime(&(attrib.st_mtime));
  508. Zeit* ret = new Zeit();
  509. ret->setZeit(clock->tm_year + 1900,
  510. clock->tm_mon + 1,
  511. clock->tm_mday,
  512. clock->tm_hour,
  513. clock->tm_min,
  514. clock->tm_sec);
  515. return ret;
  516. #endif
  517. }
  518. bool Datei::existiert() const // checks if the file exists
  519. {
  520. if (!pfad) return 0;
  521. return DateiExistiert(dynamic_cast<Text*>(pfad->getThis()));
  522. }
  523. __int64 Datei::getLPosition() const // returns the read position
  524. {
  525. if (!stream) return 0;
  526. return stream->tellg();
  527. }
  528. __int64 Datei::getSPosition() const // returns the write position
  529. {
  530. if (!stream) return 0;
  531. return stream->tellp();
  532. }
  533. bool Datei::istEnde() const // checks if the end of file is reached
  534. {
  535. if (!stream || stream->tellg() < 0) return 1;
  536. __int64 i = getSize();
  537. return stream->tellg() >= i;
  538. }
  539. Text* Datei::getPfad() const // returns the file path
  540. {
  541. return pfad ? dynamic_cast<Text*>(pfad->getThis()) : 0;
  542. }
  543. Text* Datei::zPfad() const
  544. {
  545. return pfad;
  546. }
  547. // File Functions
  548. void Framework::GetFreePfad(Text* zPfad) // Searches for an unused filename
  549. {
  550. Text txt = zPfad->getText();
  551. for (int i = 0; DateiExistiert(txt); i++)
  552. {
  553. txt = zPfad->getText();
  554. txt.append(i);
  555. }
  556. zPfad->setText(txt);
  557. }
  558. bool Framework::DateiPfadErstellen(Text* pfad) // Creates a file in the path
  559. {
  560. bool ret = DateiPfadErstellen(pfad->getText());
  561. pfad->release();
  562. return ret;
  563. }
  564. bool Framework::DateiRemove(Text* pfad) // Deletes the specified file
  565. {
  566. bool ret = DateiRemove(pfad->getText());
  567. pfad->release();
  568. return ret;
  569. }
  570. bool Framework::DateiUmbenennen(
  571. Text* pfad_alt, Text* pfad_neu) // Renames the file
  572. {
  573. bool ret = DateiUmbenennen(pfad_alt->getText(), pfad_neu->getText());
  574. pfad_alt->release();
  575. pfad_neu->release();
  576. return ret;
  577. }
  578. bool Framework::DateiExistiert(Text* pfad) // Checks if the file exists
  579. {
  580. bool ret = DateiExistiert(pfad->getText());
  581. pfad->release();
  582. return ret;
  583. }
  584. bool Framework::DateiIstVerzeichnis(
  585. Text* pfad) // checks if the path is a directory
  586. {
  587. bool ret = DateiIstVerzeichnis(pfad->getText());
  588. pfad->release();
  589. return ret;
  590. }
  591. bool Framework::DateiPfadErstellen(
  592. const char* pfad) // Creates a file in the path
  593. {
  594. Text pf = pfad;
  595. bool erst = 1;
  596. #ifdef WIN32
  597. pf.ersetzen("//", "\\"); // Correct path separators
  598. pf.ersetzen("/", "\\");
  599. for (int i = 0; i < pf.anzahlVon("\\");
  600. ++i) // Create each directory if it does not exist
  601. {
  602. Text* t = pf.getTeilText(0, pf.positionVon("\\", i));
  603. if (!t || !t->getLength())
  604. {
  605. if (t) t->release();
  606. continue;
  607. }
  608. if (!DateiExistiert(dynamic_cast<Text*>(t->getThis())))
  609. # pragma warning(suppress : 6031)
  610. _mkdir(t->getText());
  611. t->release();
  612. if (pf.positionVon("\\", i) == pf.getLength() - 1) erst = 0;
  613. }
  614. #else
  615. pf.ersetzen("\\", "/"); // Correct path separators
  616. for (int i = 0; i < pf.anzahlVon("/");
  617. ++i) // Create each directory if it does not exist
  618. {
  619. Text* t = pf.getTeilText(0, pf.positionVon("/", i));
  620. if (!t || !t->getLength())
  621. {
  622. if (t) t->release();
  623. continue;
  624. }
  625. if (!DateiExistiert(dynamic_cast<Text*>(t->getThis())))
  626. mkdir(t->getText(), 0777);
  627. t->release();
  628. if (pf.positionVon("\\", i) == pf.getLength() - 1) erst = 0;
  629. }
  630. #endif
  631. if (erst)
  632. {
  633. std::ofstream f(pf, std::ios::binary); // Create file
  634. f.close();
  635. }
  636. return DateiExistiert(pf);
  637. }
  638. bool Framework::DateiRemove(const char* pfad) // Deletes the specified file
  639. {
  640. Text pfa = pfad;
  641. #ifdef WIN32
  642. pfa.ersetzen('\\', '/');
  643. bool ret = 0;
  644. // check if file exists
  645. if (!DateiIstVerzeichnis(dynamic_cast<Text*>(pfa.getThis())))
  646. ret = DeleteFile(pfa.getText()) == 1; // delete file
  647. else
  648. {
  649. ret = 1;
  650. Datei* dat = new Datei();
  651. dat->setDatei(dynamic_cast<Text*>(pfa.getThis()));
  652. int anz = dat->getUnterdateiAnzahl();
  653. RCArray<Text>* liste = dat->getDateiListe();
  654. for (int i = 0; i < anz; ++i)
  655. {
  656. Text* pf = new Text(pfa.getText());
  657. if (pf->getText()[pf->getLength() - 1] != '/') pf->append("/");
  658. pf->append(*liste->z(i));
  659. if (ret)
  660. ret = DateiRemove(pf);
  661. else
  662. DateiRemove(pf);
  663. }
  664. liste->release();
  665. dat->release();
  666. if (ret)
  667. ret = RemoveDirectory(pfa.getText()) == 1;
  668. else
  669. RemoveDirectory(pfa.getText());
  670. }
  671. return ret;
  672. #else
  673. pfa.ersetzen('\\', '/');
  674. bool ret = 0;
  675. // check if file exists
  676. if (!DateiIstVerzeichnis(dynamic_cast<Text*>(pfa.getThis())))
  677. ret = std::remove(pfa.getText()) == 0; // delete file
  678. else
  679. {
  680. ret = 1;
  681. Datei* dat = new Datei();
  682. dat->setDatei(dynamic_cast<Text*>(pfa.getThis()));
  683. int anz = dat->getUnterdateiAnzahl();
  684. RCArray<Text>* liste = dat->getDateiListe();
  685. for (int i = 0; i < anz; ++i)
  686. {
  687. Text* pf = new Text(pfa.getText());
  688. if (pf->getText()[pf->getLength() - 1] != '/') pf->append("/");
  689. pf->append(liste->get(i));
  690. if (ret)
  691. ret = DateiRemove(pf);
  692. else
  693. DateiRemove(pf);
  694. }
  695. liste->release();
  696. dat->release();
  697. if (ret)
  698. ret = std::remove(pfa.getText()) == 0;
  699. else
  700. std::remove(pfa.getText());
  701. }
  702. return ret;
  703. #endif
  704. }
  705. bool Framework::DateiUmbenennen(
  706. const char* pfad_alt, const char* pfad_neu) // Renames the file
  707. {
  708. #ifdef WIN32
  709. if (pfad_alt && pfad_neu && DateiExistiert(pfad_alt))
  710. {
  711. bool ret = 1;
  712. if (DateiIstVerzeichnis(pfad_alt))
  713. {
  714. if (!DateiExistiert(pfad_neu))
  715. {
  716. Text tmp = pfad_neu;
  717. tmp += "/a";
  718. DateiPfadErstellen(tmp);
  719. DateiRemove(tmp);
  720. }
  721. Datei d;
  722. d.setDatei(pfad_alt);
  723. RCArray<Text>* list = d.getDateiListe();
  724. int anz = list->getEintragAnzahl();
  725. for (int i = 0; i < anz; i++)
  726. {
  727. Text pf = pfad_neu;
  728. pf += "/";
  729. pf += list->z(i)->getText();
  730. Text pf_a = pfad_alt;
  731. pf_a += "/";
  732. pf_a += list->z(i)->getText();
  733. ret |= DateiUmbenennen(pf_a, pf);
  734. }
  735. d.remove();
  736. }
  737. else
  738. {
  739. if (DateiExistiert(pfad_neu)) return 0;
  740. }
  741. ret |= MoveFile(pfad_alt, pfad_neu) == 1; // rename file
  742. return ret;
  743. }
  744. return 0;
  745. #else
  746. if (pfad_alt && pfad_neu && DateiExistiert(pfad_alt))
  747. {
  748. bool ret = 1;
  749. if (DateiIstVerzeichnis(pfad_alt))
  750. {
  751. if (!DateiExistiert(pfad_neu))
  752. {
  753. Text tmp = pfad_neu;
  754. tmp += "/a";
  755. DateiPfadErstellen(tmp);
  756. DateiRemove(tmp);
  757. }
  758. Datei d;
  759. d.setDatei(pfad_alt);
  760. RCArray<Text>* list = d.getDateiListe();
  761. int anz = list->getEintragAnzahl();
  762. for (int i = 0; i < anz; i++)
  763. {
  764. Text pf = pfad_neu;
  765. pf += "/";
  766. pf += list->z(i)->getText();
  767. Text pf_a = pfad_alt;
  768. pf_a += "/";
  769. pf_a += list->z(i)->getText();
  770. ret |= DateiUmbenennen(pf_a, pf);
  771. }
  772. d.remove();
  773. }
  774. else
  775. {
  776. if (DateiExistiert(pfad_neu)) return 0;
  777. }
  778. ret |= rename(pfad_alt, pfad_neu) == 1; // rename file
  779. return ret;
  780. }
  781. return 0;
  782. #endif
  783. }
  784. bool Framework::DateiExistiert(const char* pfad) // Checks if the file exists
  785. {
  786. #ifdef WIN32
  787. bool ret = PathFileExists(pfad) != 0;
  788. return ret;
  789. #else
  790. std::ifstream file(pfad);
  791. if (file.good()) return 1;
  792. return 0;
  793. #endif
  794. }
  795. bool Framework::DateiIstVerzeichnis(
  796. const char* pfad) // checks if the path is a directory
  797. {
  798. #ifdef WIN32
  799. WIN32_FIND_DATA wfd;
  800. HANDLE handle = FindFirstFile(pfad, &wfd);
  801. if (handle == INVALID_HANDLE_VALUE) return 0;
  802. FindClose(handle);
  803. return (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
  804. #else
  805. struct stat path_stat;
  806. if (stat(pfad, &path_stat) != 0) return 0;
  807. if (S_ISDIR(path_stat.st_mode)) return 1;
  808. return 0;
  809. #endif
  810. }