fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. #include <string>
  5.  
  6. std::string num2str(int num) {
  7. std::string str;
  8. if (num == 0) {
  9. str = "0";
  10. } else {
  11. while (num > 0) {
  12. str += num % 10 + '0';
  13. num /= 10;
  14. }
  15. }
  16. while (str.size() < 2) {
  17. str += '0';
  18. }
  19. for (int i = 0; i < (int)str.size()/2; ++i) {
  20. std::swap(str[i], str[(int)str.size()-1-i]);
  21. }
  22. return str;
  23. }
  24.  
  25. void extractDirAndFileName(const std::string& path, std::string& dirPath, std::string& fileName) {
  26. int pos = (int)path.size() - 1;
  27. for (; pos >= 0; --pos) {
  28. if (path[pos] == '/') {
  29. break;
  30. }
  31. }
  32. dirPath = path.substr(0, pos+1);
  33. fileName = path.substr(pos+1);
  34. }
  35.  
  36. void removeExtension(std::string& fileName) {
  37. while (!fileName.empty() && fileName.back() != '.') {
  38. fileName.pop_back();
  39. }
  40. if (!fileName.empty()) {
  41. fileName.pop_back();
  42. }
  43. }
  44.  
  45. void formatDirPath(std::string& dirPath) {
  46. if (!dirPath.empty() || dirPath.back() != '/') {
  47. dirPath += '/';
  48. }
  49. }
  50.  
  51. int getFileSize(std::ifstream& fin) {
  52. fin.seekg(0, fin.end);
  53. int fileSize = fin.tellg();
  54. fin.seekg(0);
  55. return fileSize;
  56. }
  57.  
  58. bool readBinFile(const std::string& path, int& bufferSize, char*& buffer) {
  59. std::ifstream fin(path, std::ios::binary);
  60. if (!fin.is_open()) {
  61. return false;
  62. }
  63. bufferSize = getFileSize(fin);
  64. buffer = new char[bufferSize];
  65. fin.read(buffer, bufferSize);
  66. fin.close();
  67. return true;
  68. }
  69.  
  70. int main(int argc, char** argv) {
  71. std::string filePath;
  72. std::string outDir;
  73.  
  74. if (argc != 5) {
  75. std::cout << "Invalid arguments\n";
  76. return 0;
  77. } else {
  78. for (int i = 1; i < 5; i+=2) {
  79. if (strcmp(argv[i], "-s") == 0) {
  80. filePath = argv[i+1];
  81. } else if (strcmp(argv[i], "-d") == 0) {
  82. outDir = argv[i+1];
  83. }
  84. }
  85. if (filePath.empty()) {
  86. std::cout << "Source file not found in arguments!\n";
  87. return 0;
  88. } else if (outDir.empty()) {
  89. std::cout << "Destination not found in arguments!\n";
  90. return 0;
  91. }
  92. }
  93.  
  94.  
  95. std::string srcPath, fileName;
  96. extractDirAndFileName(filePath, srcPath, fileName);
  97. removeExtension(fileName);
  98.  
  99. formatDirPath(outDir);
  100.  
  101. int bufferSize;
  102. char* buffer = nullptr;
  103. std::string outFile = outDir + fileName;
  104. std::ofstream fout;
  105.  
  106. for (int i = 1; ; ++i) {
  107. std::string partPath = srcPath + fileName + ".part" + num2str(i);
  108. if (readBinFile(partPath, bufferSize, buffer)) {
  109. if (i == 1) {
  110. fout.open(outFile, std::ios::binary);
  111. }
  112. if (!fout.is_open()) {
  113. std::cout << "Cannot write into " << outFile << '\n';
  114. return 0;
  115. }
  116. fout.write(buffer, bufferSize);
  117. delete[] buffer;
  118. buffer = nullptr;
  119. } else {
  120. if (i == 1) {
  121. std::cout << "ERROR: No part exists!\n";
  122. }
  123. break;
  124. }
  125. }
  126.  
  127. fout.close();
  128.  
  129. return 0;
  130. }
Success #stdin #stdout 0.01s 5540KB
stdin
Standard input is empty
stdout
Invalid arguments