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