Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <sstream>
- #include <vector>
- #include <iterator>
- #include <unordered_set>
- #include <boost/filesystem.hpp>
- #include <boost/range/iterator_range.hpp>
- #include <unordered_map>
- // 1) find highest below cur numbered item with same first line.
- // 2) this is checkin, get 2nd from last string = X
- // 3) find most recent (highest, but lower than cur) item with last string = X this is a checkout. Get 3rd from last string = Y
- // 4) find checkin with last item = Y and repeat
- // 5) if we dont find a checkin with last item = Y,
- // then give back the create with last item = Y
- // #5 is technically just find item lower (above) it with last item = Y
- std::string GetParent(std::string manifest) {
- // open the manifest file
- std::ifstream child(manifest);
- // get first line of manifest
- std::string line;
- std::getline(child, line);
- std::string str;
- char DELIMITER = ' ';
- std::stringstream lineOne(line);
- std::vector<std::string> lineOneVector;
- while (std::getline(lineOne, str, DELIMITER)) {
- lineOneVector.push_back(str);
- }
- // Was called on root create
- if (lineOneVector[1] == "create") {
- return manifest;
- }
- else if (lineOneVector[1] == "checkout") {
- // Todo
- return "";
- }
- else if (lineOneVector[1] == "checkin") {
- std::string parent_proj = lineOneVector[2];
- // TODO: if checkin manifest, get the project directory name (i.e last argument)
- std::string child_proj_dir = lineOneVector[3];
- child.close();
- // TODO: get list of manifests less recent than the argument
- std::vector<std::string> manifests;
- for(auto& entry : boost::make_iterator_range(boost::filesystem::directory_iterator(child_proj_dir), {})) {
- if (boost::filesystem::is_regular_file(entry)) {
- std::string str = entry.path().string();
- int subLen = child_proj_dir.length();
- int endLen = str.length() - subLen;
- std::string manifestFile = str.substr(subLen, endLen);
- manifests.push_back(manifestFile);
- }
- }
- std::unordered_map<int, std::string> mapManifests;
- std::vector<int> manifestNums;
- // Split manifests apart associating #:qualified path
- // Also storing just number for easier searching later
- for (int i = 0; i < manifests.size(); i++) {
- std::string str;
- char DELIM = '.';
- std::stringstream manifestFile(manifests[i]);
- std::vector<std::string> manifestVector;
- std::getline(manifestFile, str, DELIM);
- manifestVector.push_back(str);
- int manNum = std::stoi(manifestVector[0]);
- mapManifests[manNum] = manifests[i];
- manifestNums.push_back(manNum);
- }
- std::vector<std::string> sortedManifests;
- // Sort number of manifest files to make traversal easier
- std::sort(manifestNums.begin(), manifestNums.end());
- // Now sort the manifest files but with fully qualified path
- for (int i = 0;i < manifestNums.size(); i++) {
- std::string qualifiedManifest = (child_proj_dir + mapManifests[manifestNums[i]]);
- sortedManifests.push_back(qualifiedManifest);
- }
- // TODO: start from back of the list, find manifest direct parent
- std::string checkOutItem = "";
- while (parent_proj != checkOutItem) {
- // open the next potential parent
- std::ifstream parent(sortedManifests.back());
- // read first line from parent
- std::string line;
- std::getline(parent, line);
- std::string str;
- std::stringstream manifestLineOne(line);
- std::vector<std::string> manifestLineOneVector;
- while (std::getline(manifestLineOne, str, DELIMITER)) {
- manifestLineOneVector.push_back(str);
- }
- // We're currently seaching for checkin parent,
- // which is a checkout. so find the checkout's last item if it matches
- if (manifestLineOneVector[1] == "checkout") {
- checkOutItem = manifestLineOneVector[4];
- }
- parent.close();
- sortedManifests.pop_back();
- }
- return checkOutItem;
- }
- return "";
- }
- int main() {
- std::cout << GetParent("yofolder/3.manifest") << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement