Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <sstream>
  5. #include <vector>
  6. #include <iterator>
  7. #include <unordered_set>
  8. #include <boost/filesystem.hpp>
  9. #include <boost/range/iterator_range.hpp>
  10. #include <unordered_map>
  11.  
  12.  
  13.  
  14. // 1) find highest below cur numbered item with same first line.
  15. // 2) this is checkin, get 2nd from last string = X
  16. // 3) find most recent (highest, but lower than cur) item with last string = X this is a checkout. Get 3rd from last string = Y
  17. // 4) find checkin with last item = Y and repeat
  18. // 5) if we dont find a checkin with last item = Y,
  19. //     then give back the create with last item = Y
  20.  
  21. // #5 is technically just find item lower (above) it with last item = Y
  22.  
  23.  
  24. std::string GetParent(std::string manifest) {
  25.   // open the manifest file
  26.   std::ifstream child(manifest);
  27.  
  28.   // get first line of manifest
  29.   std::string line;
  30.   std::getline(child, line);
  31.  
  32.   std::string str;
  33.   char DELIMITER = ' ';
  34.   std::stringstream lineOne(line);
  35.   std::vector<std::string> lineOneVector;
  36.   while (std::getline(lineOne, str, DELIMITER)) {
  37.       lineOneVector.push_back(str);
  38.   }
  39.  
  40.   // Was called on root create
  41.   if (lineOneVector[1] == "create") {
  42.       return manifest;
  43.   }
  44.   else if (lineOneVector[1] == "checkout") {
  45.       // Todo
  46.       return "";
  47.   }
  48.  
  49.   else if (lineOneVector[1] == "checkin") {
  50.  
  51.  
  52.     std::string parent_proj = lineOneVector[2];
  53.     // TODO: if checkin manifest, get the project directory name (i.e last argument)
  54.     std::string child_proj_dir = lineOneVector[3];
  55.     child.close();
  56.  
  57.     // TODO: get list of manifests less recent than the argument
  58.     std::vector<std::string> manifests;
  59.     for(auto& entry : boost::make_iterator_range(boost::filesystem::directory_iterator(child_proj_dir), {})) {
  60.         if (boost::filesystem::is_regular_file(entry)) {
  61.             std::string str = entry.path().string();
  62.             int subLen = child_proj_dir.length();
  63.             int endLen = str.length() - subLen;
  64.             std::string manifestFile = str.substr(subLen, endLen);
  65.             manifests.push_back(manifestFile);
  66.         }
  67.     }
  68.  
  69.     std::unordered_map<int, std::string> mapManifests;
  70.     std::vector<int> manifestNums;
  71.  
  72.     // Split manifests apart associating #:qualified path
  73.     // Also storing just number for easier searching later
  74.     for (int i = 0; i < manifests.size(); i++) {
  75.         std::string str;
  76.         char DELIM = '.';
  77.         std::stringstream manifestFile(manifests[i]);
  78.         std::vector<std::string> manifestVector;
  79.         std::getline(manifestFile, str, DELIM);
  80.             manifestVector.push_back(str);
  81.             int manNum = std::stoi(manifestVector[0]);
  82.             mapManifests[manNum] = manifests[i];
  83.             manifestNums.push_back(manNum);
  84.        
  85.     }
  86.  
  87.     std::vector<std::string> sortedManifests;
  88.  
  89.     // Sort number of manifest files to make traversal easier
  90.     std::sort(manifestNums.begin(), manifestNums.end());
  91.  
  92.    
  93.     // Now sort the manifest files but with fully qualified path
  94.     for (int i = 0;i < manifestNums.size(); i++) {
  95.         std::string qualifiedManifest = (child_proj_dir + mapManifests[manifestNums[i]]);
  96.         sortedManifests.push_back(qualifiedManifest);
  97.     }
  98.    
  99.  
  100.     //  TODO: start from back of the list, find manifest direct parent
  101.  
  102.     std::string checkOutItem = "";
  103.     while (parent_proj != checkOutItem) {
  104.         // open the next potential parent
  105.         std::ifstream parent(sortedManifests.back());
  106.  
  107.         // read first line from parent
  108.         std::string line;
  109.         std::getline(parent, line);
  110.         std::string str;
  111.         std::stringstream manifestLineOne(line);
  112.         std::vector<std::string> manifestLineOneVector;
  113.         while (std::getline(manifestLineOne, str, DELIMITER)) {
  114.         manifestLineOneVector.push_back(str);
  115.         }
  116.         // We're currently seaching for checkin parent,
  117.         // which is a checkout. so find the checkout's last item if it matches
  118.         if (manifestLineOneVector[1] == "checkout") {
  119.             checkOutItem = manifestLineOneVector[4];
  120.         }
  121.         parent.close();
  122.         sortedManifests.pop_back();
  123.  
  124.   }
  125.  
  126.   return checkOutItem;
  127.  
  128.   }
  129.   return "";
  130. }
  131.  
  132. int main() {
  133.     std::cout << GetParent("yofolder/3.manifest") << std::endl;
  134.     return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement