Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Dir_or_File.hpp"
- #include <iostream>
- Dir_or_File::Dir_or_File(std::string name)
- {
- // constructor stuff goes here
- // make sure that is_file is set according to the '/' rules -> make a check here!
- // Set name as path name and split the name string
- std::vector<std::string> full_path = split_after_slash(name);
- this->name = full_path[0];
- // Decide if directory or file
- if (full_path[0].at(full_path[0].size() - 1) != '/')
- {
- is_file = true;
- }
- else
- {
- is_file = false;
- }
- // If there is a sub-folder, recursively call this constructor without the folder we are currently in
- if (full_path.size() > 1)
- {
- std::string new_param;
- for (unsigned int i = 1; i < full_path.size(); i++)
- {
- new_param.append(full_path[i]);
- }
- Dir_or_File* new_entry = new Dir_or_File(new_param);
- entries.push_back(new_entry);
- }
- }
- std::vector<std::string> Dir_or_File::split_after_slash(std::string full_path)
- {
- // take care of '/'
- // check what comes after a slash and decide what to do according to what you find
- // so iterate through your string and find slashes
- // and then decide what to do...
- std::vector<std::string> ret;
- int substring_start = 0, substring_end = 0;
- for (unsigned int i = 0; i < full_path.size(); i++)
- {
- if (full_path[i] == '/' || i == full_path.size() - 1)
- {
- substring_end = i + 1;
- int substring_length = substring_end - substring_start;
- std::string new_substring = full_path.substr(substring_start, substring_length);
- ret.push_back(new_substring);
- substring_start = substring_end;
- }
- else
- {
- substring_end++;
- }
- }
- return ret;
- }
- void Dir_or_File::insert(std::vector<std::string> names)
- {
- // now comes the insertion of new entries
- // again make sure to distinguish between files and directories
- std::vector<std::string> full_path = names;
- for (unsigned int i = 0; i < entries.size(); i++)
- {
- if (full_path[0] == entries[i]->name)
- {
- full_path.erase(full_path.begin() + 1);
- entries[i]->insert(full_path);
- return;
- }
- }
- std::string new_param = "";
- for (unsigned int i = 1; i < full_path.size(); i++)
- {
- new_param.append(full_path[i]);
- }
- Dir_or_File* dir= new Dir_or_File(new_param);
- entries.push_back(dir);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement