Datei.cpp 18 KB

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