File.cpp 21 KB

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