fileman.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "../include/fileman.h"
  2. #include "../include/base64.h"
  3. FileMan::FileMan() { islisting = false; }
  4. FileMan::~FileMan() {
  5. cancelGet();
  6. cancelPut();
  7. cancelList();
  8. }
  9. bool FileMan::isGetting() { return getfile.is_open(); }
  10. bool FileMan::isPutting() { return putfile.is_open(); }
  11. bool FileMan::isListing() { return islisting; }
  12. bool FileMan::openPut(const std::string &path) {
  13. putpath = path;
  14. putname = pathToFilename(path);
  15. putfile.open(path, std::ios::ate | std::ios::binary);
  16. if (putfile.is_open()) {
  17. size_t size = putfile.tellg();
  18. putsize = size;
  19. putchunks = size / max_read_len + ((size % max_read_len) ? 1 : 0);
  20. putchunksRemaining = putchunks;
  21. putfile.seekg(std::ios::beg);
  22. return true;
  23. }
  24. return false;
  25. }
  26. bool FileMan::openGet(const std::string &path) {
  27. getpath = path;
  28. getname = pathToFilename(path);
  29. getchunks = 0;
  30. getchunksRemaining = 0;
  31. getfile.open(path, std::ios::app | std::ios::binary);
  32. if (getfile.tellp() != std::ios::beg) {
  33. closeGet();
  34. return false;
  35. }
  36. return true;
  37. }
  38. bool FileMan::openList() {
  39. if (isListing()) {
  40. return false;
  41. }
  42. listdata = std::vector<std::string>();
  43. islisting = true;
  44. return true;
  45. }
  46. void FileMan::closePut() { putfile.close(); }
  47. void FileMan::closeGet() { getfile.close(); }
  48. void FileMan::closeList() { islisting = false; }
  49. void FileMan::cancelPut() {
  50. if (isPutting()) {
  51. closePut();
  52. }
  53. }
  54. void FileMan::cancelGet() {
  55. if (isGetting()) {
  56. closeGet();
  57. std::remove(getname.c_str());
  58. }
  59. }
  60. void FileMan::cancelList() {
  61. if (isListing()) {
  62. closeList();
  63. }
  64. }
  65. void FileMan::writeGet(const std::vector<char> data) {
  66. getfile.write(data.data(), data.size());
  67. getchunksRemaining--;
  68. }
  69. void FileMan::writeBase64(std::string data) {
  70. writeGet(base64::decodeVector(data));
  71. }
  72. void FileMan::putListData(std::vector<std::string> names) {
  73. listdata.insert(listdata.end(), names.begin(), names.end());
  74. listchunksRemaining--;
  75. }
  76. std::vector<std::string> FileMan::getListData() { return listdata; }
  77. int FileMan::getGetChunks() { return getchunks; }
  78. int FileMan::getGetRemainingChunks() { return getchunksRemaining; }
  79. void FileMan::setGetChunks(int chunks) {
  80. getchunks = chunks;
  81. getchunksRemaining = chunks - 1;
  82. }
  83. void FileMan::setListChunks(int chunks) {
  84. listchunks = chunks;
  85. getchunksRemaining = chunks - 1;
  86. }
  87. std::vector<char> FileMan::readPut() {
  88. char buf[max_read_len];
  89. std::vector<char> data;
  90. std::streamoff read = this->putfile.tellg();
  91. if (read + max_read_len > this->putsize) {
  92. read = this->putsize % max_read_len;
  93. } else {
  94. read = max_read_len;
  95. }
  96. putfile.read(buf, read);
  97. data.assign(buf, buf + read);
  98. putchunksRemaining--;
  99. return data;
  100. }
  101. std::string FileMan::readBase64() { return base64::encodeVector(readPut()); }
  102. std::string FileMan::getPutName() { return putname; }
  103. std::string FileMan::getGetName() { return getname; }
  104. int FileMan::getPutChunks() { return putchunks; }
  105. int FileMan::getPutRemainingChunks() { return putchunksRemaining; }
  106. int FileMan::getPutSize() { return putsize; }
  107. int FileMan::getListRemainingChunks() { return listchunksRemaining; }
  108. int FileMan::getListChunks() { return listchunks; }
  109. std::string FileMan::pathToFilename(std::string path) {
  110. int lastFoundIndex = -1;
  111. for (int currentIndex = path.find("/"); currentIndex != std::string::npos;
  112. currentIndex = path.find("/", currentIndex + 1)) {
  113. // check if the "/" was escaped
  114. if (currentIndex > 0 && path[currentIndex - 1] == '\\')
  115. continue;
  116. /* // check unnecessary, because error occurs when trying to open file
  117. anyways
  118. // check if the "/" is at the end of path name
  119. if (currentIndex + 1 == path.length()) {
  120. // ERROR: INVALID FILENAME, BECAUSE ENDS WITH "/"
  121. }
  122. */
  123. // otherwise we found a valid "/"
  124. lastFoundIndex = currentIndex;
  125. }
  126. return path.substr(lastFoundIndex + 1);
  127. }