Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-06-22 22:49:16

0001 #include <iostream>
0002 
0003 #include <filesystem>
0004 #include <fstream>
0005 #include <array>
0006 
0007 int main(int argc, char** argv) {
0008   if (argc != 3) {
0009     return 0;
0010   }
0011 
0012   std::string directoryName = argv[1];
0013 
0014   std::string baseName = argv[2];
0015 
0016   const std::filesystem::path readDirectory{directoryName};
0017 
0018   if (not is_directory(readDirectory)) {
0019     std::cerr << "Error: " << readDirectory << " is not a directory";
0020     return 1;
0021   }
0022 
0023   std::ofstream mergedFile(baseName + ".bin", std::ios::binary);
0024   std::ofstream indexFile(baseName + ".index");
0025 
0026   unsigned long int offset = 0;
0027   for (auto const& dir_entry : std::filesystem::recursive_directory_iterator{readDirectory}) {
0028     if (dir_entry.is_regular_file()) {
0029       std::string fileName = relative(dir_entry.path(), readDirectory);
0030       //std::cout <<" "<<relative(dir_entry.path(), readDirectory)<<"  "<<offset<<"\n";
0031       indexFile << fileName << " " << offset << "\n";
0032       offset += dir_entry.file_size();
0033 
0034       std::ifstream toRead(dir_entry.path(), std::ios::binary);
0035 
0036       std::array<char, 1024> buffer;
0037       while (toRead.read(buffer.data(), buffer.size())) {
0038         mergedFile.write(buffer.data(), buffer.size());
0039       }
0040       if (toRead.bad()) {
0041         std::cerr << " problem copying file " << dir_entry.path();
0042         return 1;
0043       }
0044       if (not toRead.eof()) {
0045         std::cerr << " failed to read end of file " << dir_entry.path();
0046         return 1;
0047       }
0048       int lastRead = toRead.gcount();
0049       if (lastRead != 1024) {
0050         mergedFile.write(buffer.data(), lastRead);
0051       }
0052     }
0053   }
0054   return 0;
0055 }