Datei.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. #include "Datei.h"
  2. #include "Key.h"
  3. #include "Text.h"
  4. #include "Zeit.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. // Inhalt der Datei Klasse aus Datei.h
  17. // Konstruktor
  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. //! Konstruktor
  30. Datei::Datei(const char* pfad)
  31. : Datei()
  32. {
  33. setDatei(pfad);
  34. }
  35. //! Konstruktor
  36. Datei::Datei(Text* pfad)
  37. : Datei()
  38. {
  39. setDatei(pfad);
  40. }
  41. // Destruktor
  42. Datei::~Datei()
  43. {
  44. if (key) key->release();
  45. if (stream) delete stream;
  46. if (pfad) pfad->release();
  47. }
  48. // nicht constant
  49. void Datei::setDatei(const char* pfad) // setzt die Datei
  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(
  65. const char* pfad) // benennt die Datei um und verschiebt sie eventuell
  66. {
  67. if (!pfad) return 0;
  68. if (DateiUmbenennen(this->pfad->getText(), pfad))
  69. {
  70. this->pfad->setText(pfad);
  71. return 1;
  72. }
  73. return 0;
  74. }
  75. bool Datei::umbenennen(Text* pfad)
  76. {
  77. if (!this->pfad)
  78. {
  79. pfad->release();
  80. return 0;
  81. }
  82. if (DateiUmbenennen(this->pfad->getText(), pfad->getText()))
  83. {
  84. this->pfad->setText(*pfad);
  85. pfad->release();
  86. return 1;
  87. }
  88. pfad->release();
  89. return 0;
  90. }
  91. bool Datei::remove() // löscht die Datei
  92. {
  93. if (!pfad) return 0;
  94. return DateiRemove(dynamic_cast<Text*>(pfad->getThis()));
  95. }
  96. bool Datei::erstellen() // erstellt die Datei
  97. {
  98. if (!pfad) return 0;
  99. return DateiPfadErstellen(dynamic_cast<Text*>(pfad->getThis()));
  100. }
  101. bool Datei::open(int style) // öffnet die Datei
  102. {
  103. if (!pfad) return 0;
  104. if (stream) delete stream;
  105. stream = new std::fstream();
  106. std::ios_base::openmode om = std::ios::binary;
  107. if ((style | Style::lesen) == style) om |= std::ios::in;
  108. if ((style | Style::schreiben) == style) om |= std::ios::out;
  109. stream->open(pfad->getText(), om);
  110. if ((style | Style::ende) == style)
  111. {
  112. if ((style | Style::lesen) == style) stream->seekg(0, std::ios::end);
  113. if ((style | Style::schreiben) == style)
  114. stream->seekp(0, std::ios::end);
  115. }
  116. if (!stream->is_open() || !stream->good())
  117. {
  118. delete stream;
  119. stream = 0;
  120. return 0;
  121. }
  122. tmpLBPos = 7;
  123. tmpSBPos = -1;
  124. return 1;
  125. }
  126. void Datei::setLPosition(__int64 pos, bool ende) // setzt die Leseposition
  127. {
  128. if (!pfad) return;
  129. if (stream)
  130. {
  131. if (ende)
  132. stream->seekg(pos, std::ios::end);
  133. else
  134. stream->seekg(pos, std::ios::beg);
  135. }
  136. tmpLBPos = 7;
  137. }
  138. void Datei::setSPosition(__int64 pos, bool ende) // setzt die Schreibeposition
  139. {
  140. if (!pfad) return;
  141. if (stream)
  142. {
  143. if (ende)
  144. stream->seekp(pos, std::ios::end);
  145. else
  146. stream->seekp(pos, std::ios::beg);
  147. }
  148. tmpSBPos = -1;
  149. }
  150. void Datei::schreibe(const char* bytes, int len) // schreibt bytes in datei
  151. {
  152. if (!pfad || !stream) return;
  153. if (tmpSBPos >= 0)
  154. {
  155. tmpSBPos = -1;
  156. stream->write(&tmpSByte, 1);
  157. tmpSByte = 0;
  158. }
  159. if (key)
  160. {
  161. key->setPos(getSPosition());
  162. Bytes* n = new Bytes(bytes, len);
  163. key->codieren(dynamic_cast<Bytes*>(n->getThis()));
  164. stream->write(n->getBytes(), len);
  165. n->release();
  166. }
  167. else
  168. stream->write(bytes, len);
  169. }
  170. void Framework::Datei::flush()
  171. {
  172. if (!pfad || !stream) return;
  173. stream->flush();
  174. }
  175. void Datei::lese(char* bytes, int len) // ließt bytes aus datei
  176. {
  177. if (!pfad) return;
  178. if (stream)
  179. {
  180. __int64 tmp = getLPosition();
  181. stream->read(bytes, len);
  182. if (key)
  183. {
  184. key->setPos(tmp);
  185. Bytes* n = new Bytes();
  186. n->setBytesZ(bytes, len);
  187. key->decodieren(n);
  188. }
  189. }
  190. tmpLBPos = 7;
  191. tmpSBPos = -1;
  192. }
  193. Text* Datei::leseZeile() // ließt eine zeile
  194. {
  195. if (!pfad || !stream) return 0;
  196. if (istEnde()) return 0;
  197. Text* ret = new Text("");
  198. __int64 len = getSize();
  199. for (char c = 0; c != '\n' && stream->tellg() < len;)
  200. {
  201. __int64 tmp = getLPosition();
  202. stream->read(&c, 1);
  203. if (key)
  204. {
  205. key->setPos(tmp);
  206. Bytes* n = new Bytes();
  207. n->setBytesZ(&c, 1);
  208. key->decodieren(n);
  209. }
  210. if (c) ret->append(&c, 1);
  211. }
  212. tmpSBPos = 7;
  213. tmpSBPos = -1;
  214. return ret;
  215. }
  216. void Datei::close() // schließt die Datei
  217. {
  218. if (!pfad || !stream) return;
  219. if (tmpSBPos >= 0)
  220. {
  221. if (key)
  222. {
  223. key->setPos(getSPosition());
  224. Bytes* n = new Bytes(&tmpSByte, 1);
  225. key->codieren(dynamic_cast<Bytes*>(n->getThis()));
  226. stream->write(n->getBytes(), 1);
  227. n->release();
  228. }
  229. else
  230. stream->write(&tmpSByte, 1);
  231. }
  232. stream->close();
  233. delete stream;
  234. stream = 0;
  235. }
  236. #ifdef WIN32
  237. bool Datei::setLetzteÄnderung(Zeit* zeit) // setzt das änderungsdatum der Datei
  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) // Datei Bitweise auslesen
  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) // Datei Bitweise speichern
  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. // Setzt den Schlüssel für die Datei
  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 // prüft, ob die Datei ein Ordner ist
  348. {
  349. if (!pfad) return 0;
  350. return DateiIstVerzeichnis(dynamic_cast<Text*>(pfad->getThis()));
  351. }
  352. bool Datei::istOffen() const // prüft, ob die Datei geöffnet ist
  353. {
  354. if (!pfad) return 0;
  355. if (stream) return stream->is_open() && stream->good();
  356. return 0;
  357. }
  358. int Datei::getUnterdateiAnzahl() const // gibt die Anzahl der unterdateien an
  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>*
  397. Datei::getDateiListe() const // gibt eine Liste mit unterdateien zurück
  398. {
  399. #ifdef WIN32
  400. if (!pfad) return 0;
  401. if (!DateiIstVerzeichnis(dynamic_cast<Text*>(pfad->getThis()))) return 0;
  402. HANDLE fHandle;
  403. WIN32_FIND_DATA wfd;
  404. Text stxt = pfad->getText();
  405. stxt.ersetzen('/', '\\');
  406. if (stxt.positionVon('\\') == stxt.getLength() - 1)
  407. stxt.append("*");
  408. else
  409. stxt.append("\\*");
  410. fHandle = FindFirstFile(stxt.getText(), &wfd);
  411. FindNextFile(fHandle, &wfd);
  412. RCArray<Text>* ret = new RCArray<Text>();
  413. int count = 0;
  414. while (FindNextFile(fHandle, &wfd))
  415. {
  416. Text* txt = new Text(wfd.cFileName);
  417. ret->add(txt, count);
  418. ++count;
  419. }
  420. FindClose(fHandle);
  421. return ret;
  422. #else
  423. if (!pfad) return 0;
  424. if (!DateiIstVerzeichnis(dynamic_cast<Text*>(pfad->getThis()))) return 0;
  425. Text stxt = pfad->getText();
  426. stxt.ersetzen('\\', '/');
  427. if (stxt.positionVon('/') == stxt.getLength() - 1)
  428. stxt.remove(stxt.getLength() - 1);
  429. DIR* hdir;
  430. hdir = opendir(stxt.getText());
  431. if (hdir)
  432. {
  433. RCArray<Text>* ret = new RCArray<Text>();
  434. int count = 0;
  435. for (dirent* entry = readdir(hdir); entry; entry = readdir(hdir))
  436. {
  437. if (entry && entry->d_name[0] != '.')
  438. {
  439. ret->add(new Text(entry->d_name), count);
  440. ++count;
  441. }
  442. }
  443. closedir(hdir);
  444. return ret;
  445. }
  446. return 0;
  447. #endif
  448. }
  449. __int64 Datei::getSize() const // gibt die Größe der Datei zurück
  450. {
  451. if (!pfad) return 0;
  452. if (gr) return gr;
  453. if (!stream || !istOffen())
  454. {
  455. std::fstream* stream = new std::fstream();
  456. stream->open(pfad->getText(), std::ios::binary | std::ios::in);
  457. __int64 tmp = stream->tellg();
  458. stream->seekg(0, std::ios::end);
  459. __int64 ret = stream->tellg();
  460. stream->seekg(tmp, std::ios::beg);
  461. stream->close();
  462. delete stream;
  463. __int64* size = (__int64*)&gr;
  464. *size = ret;
  465. return ret;
  466. }
  467. __int64 tmp = stream->tellg();
  468. stream->seekg(0, std::ios::end);
  469. __int64 ret = stream->tellg();
  470. stream->seekg(tmp, std::ios::beg);
  471. __int64* size = (__int64*)&gr;
  472. *size = ret;
  473. return ret;
  474. }
  475. Zeit* Datei::getLastChange() const // gibt das Datum der letzten Änderung
  476. {
  477. if (!pfad) return 0;
  478. #ifdef WIN32
  479. HANDLE hFile = CreateFile(pfad->getText(),
  480. GENERIC_READ,
  481. FILE_SHARE_READ,
  482. NULL,
  483. OPEN_EXISTING,
  484. 0,
  485. NULL);
  486. if (hFile == INVALID_HANDLE_VALUE) return 0;
  487. FILETIME ftCreate, ftAccess, ftWrite;
  488. SYSTEMTIME stUTC, stLocal;
  489. if (!GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))
  490. {
  491. CloseHandle(hFile);
  492. return 0;
  493. }
  494. CloseHandle(hFile);
  495. if (!FileTimeToSystemTime(&ftWrite, &stUTC)) return 0;
  496. if (!SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal)) return 0;
  497. Zeit* ret = new Zeit();
  498. ret->setZeit(stLocal.wYear,
  499. stLocal.wMonth,
  500. stLocal.wDay,
  501. stLocal.wHour,
  502. stLocal.wMinute,
  503. stLocal.wSecond);
  504. return ret;
  505. #else
  506. struct stat attrib;
  507. if (stat(pfad->getText(), &attrib) != 0) return 0;
  508. tm* clock = gmtime(&(attrib.st_mtime));
  509. Zeit* ret = new Zeit();
  510. ret->setZeit(clock->tm_year + 1900,
  511. clock->tm_mon + 1,
  512. clock->tm_mday,
  513. clock->tm_hour,
  514. clock->tm_min,
  515. clock->tm_sec);
  516. return ret;
  517. #endif
  518. }
  519. bool Datei::existiert() const // prüft, ob die Datei existiert
  520. {
  521. if (!pfad) return 0;
  522. return DateiExistiert(dynamic_cast<Text*>(pfad->getThis()));
  523. }
  524. __int64 Datei::getLPosition() const // gibt die Leseposition zurück
  525. {
  526. if (!stream) return 0;
  527. return stream->tellg();
  528. }
  529. __int64 Datei::getSPosition() const // gibt die Schreibeposition zurück
  530. {
  531. if (!stream) return 0;
  532. return stream->tellp();
  533. }
  534. bool Datei::istEnde() const // prüft, ob die Datei zu ende ist
  535. {
  536. if (!stream || stream->tellg() < 0) return 1;
  537. __int64 i = getSize();
  538. return stream->tellg() >= i;
  539. }
  540. Text* Datei::getPfad() const // gibt den Dateipfad zurück
  541. {
  542. return pfad ? dynamic_cast<Text*>(pfad->getThis()) : 0;
  543. }
  544. Text* Datei::zPfad() const
  545. {
  546. return pfad;
  547. }
  548. // Datei Funktionen
  549. void Framework::GetFreePfad(Text* zPfad) // Sucht einen unbenutzten Dateinamen
  550. {
  551. Text txt = zPfad->getText();
  552. for (int i = 0; DateiExistiert(txt); i++)
  553. {
  554. txt = zPfad->getText();
  555. txt.append(i);
  556. }
  557. zPfad->setText(txt);
  558. }
  559. bool Framework::DateiPfadErstellen(
  560. Text* pfad) // Erstellt eine Datei in dem Pfad
  561. {
  562. bool ret = DateiPfadErstellen(pfad->getText());
  563. pfad->release();
  564. return ret;
  565. }
  566. bool Framework::DateiRemove(Text* pfad) // Löscht die angegebene Datei
  567. {
  568. bool ret = DateiRemove(pfad->getText());
  569. pfad->release();
  570. return ret;
  571. }
  572. bool Framework::DateiUmbenennen(
  573. Text* pfad_alt, Text* pfad_neu) // Benennt die Datei um
  574. {
  575. bool ret = DateiUmbenennen(pfad_alt->getText(), pfad_neu->getText());
  576. pfad_alt->release();
  577. pfad_neu->release();
  578. return ret;
  579. }
  580. bool Framework::DateiExistiert(Text* pfad) // Prüft, ob Datei existiert
  581. {
  582. bool ret = DateiExistiert(pfad->getText());
  583. pfad->release();
  584. return ret;
  585. }
  586. bool Framework::DateiIstVerzeichnis(
  587. Text* pfad) // prüft, ob pfad ein Verzeichnis ist
  588. {
  589. bool ret = DateiIstVerzeichnis(pfad->getText());
  590. pfad->release();
  591. return ret;
  592. }
  593. bool Framework::DateiPfadErstellen(
  594. const char* pfad) // Erstellt eine Datei in dem Pfad
  595. {
  596. Text pf = pfad;
  597. bool erst = 1;
  598. #ifdef WIN32
  599. pf.ersetzen("//", "\\"); // Pfadangaben korrigieren
  600. pf.ersetzen("/", "\\");
  601. for (int i = 0; i < pf.anzahlVon("\\");
  602. ++i) // Jeden ordner erstellen wenn er nicht existiert
  603. {
  604. Text* t = pf.getTeilText(0, pf.positionVon("\\", i));
  605. if (!t || !t->getLength())
  606. {
  607. if (t) t->release();
  608. continue;
  609. }
  610. if (!DateiExistiert(dynamic_cast<Text*>(t->getThis())))
  611. # pragma warning(suppress : 6031)
  612. _mkdir(t->getText());
  613. t->release();
  614. if (pf.positionVon("\\", i) == pf.getLength() - 1) erst = 0;
  615. }
  616. #else
  617. pf.ersetzen("\\", "/"); // Pfadangaben korrigieren
  618. for (int i = 0; i < pf.anzahlVon("/");
  619. ++i) // Jeden ordner erstellen wenn er nicht existiert
  620. {
  621. Text* t = pf.getTeilText(0, pf.positionVon("/", i));
  622. if (!t || !t->getLength())
  623. {
  624. if (t) t->release();
  625. continue;
  626. }
  627. if (!DateiExistiert(dynamic_cast<Text*>(t->getThis())))
  628. mkdir(t->getText(), 0777);
  629. t->release();
  630. if (pf.positionVon("\\", i) == pf.getLength() - 1) erst = 0;
  631. }
  632. #endif
  633. if (erst)
  634. {
  635. std::ofstream f(pf, std::ios::binary); // Datei erstellen
  636. f.close();
  637. }
  638. return DateiExistiert(pf);
  639. }
  640. bool Framework::DateiRemove(const char* pfad) // Löscht die angegebene Datei
  641. {
  642. Text pfa = pfad;
  643. #ifdef WIN32
  644. pfa.ersetzen('\\', '/');
  645. bool ret = 0;
  646. // prüfen ob Datei existiert
  647. if (!DateiIstVerzeichnis(dynamic_cast<Text*>(pfa.getThis())))
  648. ret = DeleteFile(pfa.getText()) == 1; // Datei löschen
  649. else
  650. {
  651. ret = 1;
  652. Datei* dat = new Datei();
  653. dat->setDatei(dynamic_cast<Text*>(pfa.getThis()));
  654. int anz = dat->getUnterdateiAnzahl();
  655. RCArray<Text>* liste = dat->getDateiListe();
  656. for (int i = 0; i < anz; ++i)
  657. {
  658. Text* pf = new Text(pfa.getText());
  659. if (pf->getText()[pf->getLength() - 1] != '/') pf->append("/");
  660. pf->append(*liste->z(i));
  661. if (ret)
  662. ret = DateiRemove(pf);
  663. else
  664. DateiRemove(pf);
  665. }
  666. liste->release();
  667. dat->release();
  668. if (ret)
  669. ret = RemoveDirectory(pfa.getText()) == 1;
  670. else
  671. RemoveDirectory(pfa.getText());
  672. }
  673. return ret;
  674. #else
  675. pfa.ersetzen('\\', '/');
  676. bool ret = 0;
  677. // pruefen ob Datei existiert
  678. if (!DateiIstVerzeichnis(dynamic_cast<Text*>(pfa.getThis())))
  679. ret = std::remove(pfa.getText()) == 0; // Datei loeschen
  680. else
  681. {
  682. ret = 1;
  683. Datei* dat = new Datei();
  684. dat->setDatei(dynamic_cast<Text*>(pfa.getThis()));
  685. int anz = dat->getUnterdateiAnzahl();
  686. RCArray<Text>* liste = dat->getDateiListe();
  687. for (int i = 0; i < anz; ++i)
  688. {
  689. Text* pf = new Text(pfa.getText());
  690. if (pf->getText()[pf->getLength() - 1] != '/') pf->append("/");
  691. pf->append(liste->get(i));
  692. if (ret)
  693. ret = DateiRemove(pf);
  694. else
  695. DateiRemove(pf);
  696. }
  697. liste->release();
  698. dat->release();
  699. if (ret)
  700. ret = std::remove(pfa.getText()) == 0;
  701. else
  702. std::remove(pfa.getText());
  703. }
  704. return ret;
  705. #endif
  706. }
  707. bool Framework::DateiUmbenennen(
  708. const char* pfad_alt, const char* pfad_neu) // Benennt die Datei um
  709. {
  710. #ifdef WIN32
  711. if (pfad_alt && pfad_neu && DateiExistiert(pfad_alt))
  712. {
  713. bool ret = 1;
  714. if (DateiIstVerzeichnis(pfad_alt))
  715. {
  716. if (!DateiExistiert(pfad_neu))
  717. {
  718. Text tmp = pfad_neu;
  719. tmp += "/a";
  720. DateiPfadErstellen(tmp);
  721. DateiRemove(tmp);
  722. }
  723. Datei d;
  724. d.setDatei(pfad_alt);
  725. RCArray<Text>* list = d.getDateiListe();
  726. int anz = list->getEintragAnzahl();
  727. for (int i = 0; i < anz; i++)
  728. {
  729. Text pf = pfad_neu;
  730. pf += "/";
  731. pf += list->z(i)->getText();
  732. Text pf_a = pfad_alt;
  733. pf_a += "/";
  734. pf_a += list->z(i)->getText();
  735. ret |= DateiUmbenennen(pf_a, pf);
  736. }
  737. d.remove();
  738. }
  739. else
  740. {
  741. if (DateiExistiert(pfad_neu)) return 0;
  742. }
  743. ret |= MoveFile(pfad_alt, pfad_neu) == 1; // Datei umbenennen
  744. return ret;
  745. }
  746. return 0;
  747. #else
  748. if (pfad_alt && pfad_neu && DateiExistiert(pfad_alt))
  749. {
  750. bool ret = 1;
  751. if (DateiIstVerzeichnis(pfad_alt))
  752. {
  753. if (!DateiExistiert(pfad_neu))
  754. {
  755. Text tmp = pfad_neu;
  756. tmp += "/a";
  757. DateiPfadErstellen(tmp);
  758. DateiRemove(tmp);
  759. }
  760. Datei d;
  761. d.setDatei(pfad_alt);
  762. RCArray<Text>* list = d.getDateiListe();
  763. int anz = list->getEintragAnzahl();
  764. for (int i = 0; i < anz; i++)
  765. {
  766. Text pf = pfad_neu;
  767. pf += "/";
  768. pf += list->z(i)->getText();
  769. Text pf_a = pfad_alt;
  770. pf_a += "/";
  771. pf_a += list->z(i)->getText();
  772. ret |= DateiUmbenennen(pf_a, pf);
  773. }
  774. d.remove();
  775. }
  776. else
  777. {
  778. if (DateiExistiert(pfad_neu)) return 0;
  779. }
  780. ret |= rename(pfad_alt, pfad_neu) == 1; // Datei umbenennen
  781. return ret;
  782. }
  783. return 0;
  784. #endif
  785. }
  786. bool Framework::DateiExistiert(const char* pfad) // Prüft, ob Datei existiert
  787. {
  788. #ifdef WIN32
  789. bool ret = PathFileExists(pfad) != 0;
  790. return ret;
  791. #else
  792. std::ifstream file(pfad);
  793. if (file.good()) return 1;
  794. return 0;
  795. #endif
  796. }
  797. bool Framework::DateiIstVerzeichnis(
  798. const char* pfad) // prüft, ob pfad ein Verzeichnis ist
  799. {
  800. #ifdef WIN32
  801. WIN32_FIND_DATA wfd;
  802. HANDLE handle = FindFirstFile(pfad, &wfd);
  803. if (handle == INVALID_HANDLE_VALUE) return 0;
  804. FindClose(handle);
  805. return (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
  806. #else
  807. struct stat path_stat;
  808. if (stat(pfad, &path_stat) != 0) return 0;
  809. if (S_ISDIR(path_stat.st_mode)) return 1;
  810. return 0;
  811. #endif
  812. }