Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. #include "Dir_or_File.hpp"
  2. #include <iostream>
  3.  
  4. Dir_or_File::Dir_or_File(std::string name)
  5. {
  6. // constructor stuff goes here
  7. // make sure that is_file is set according to the '/' rules -> make a check here!
  8.  
  9. // Set name as path name and split the name string
  10. std::vector<std::string> full_path = split_after_slash(name);
  11. this->name = full_path[0];
  12.  
  13. // Decide if directory or file
  14. if (full_path[0].at(full_path[0].size() - 1) != '/')
  15. {
  16. is_file = true;
  17. }
  18. else
  19. {
  20. is_file = false;
  21. }
  22.  
  23. // If there is a sub-folder, recursively call this constructor without the folder we are currently in
  24. if (full_path.size() > 1)
  25. {
  26. std::string new_param;
  27. for (unsigned int i = 1; i < full_path.size(); i++)
  28. {
  29. new_param.append(full_path[i]);
  30. }
  31. Dir_or_File* new_entry = new Dir_or_File(new_param);
  32. entries.push_back(new_entry);
  33. }
  34. }
  35.  
  36.  
  37. std::vector<std::string> Dir_or_File::split_after_slash(std::string full_path)
  38. {
  39. // take care of '/'
  40. // check what comes after a slash and decide what to do according to what you find
  41. // so iterate through your string and find slashes
  42. // and then decide what to do...
  43.  
  44. std::vector<std::string> ret;
  45. int substring_start = 0, substring_end = 0;
  46. for (unsigned int i = 0; i < full_path.size(); i++)
  47. {
  48. if (full_path[i] == '/' || i == full_path.size() - 1)
  49. {
  50. substring_end = i + 1;
  51. int substring_length = substring_end - substring_start;
  52. std::string new_substring = full_path.substr(substring_start, substring_length);
  53. ret.push_back(new_substring);
  54. substring_start = substring_end;
  55. }
  56. else
  57. {
  58. substring_end++;
  59. }
  60. }
  61. return ret;
  62. }
  63.  
  64.  
  65. void Dir_or_File::insert(std::vector<std::string> names)
  66. {
  67. // now comes the insertion of new entries
  68. // again make sure to distinguish between files and directories
  69. std::vector<std::string> full_path = names;
  70. for (unsigned int i = 0; i < entries.size(); i++)
  71. {
  72. if (full_path[0] == entries[i]->name)
  73. {
  74. full_path.erase(full_path.begin() + 1);
  75. entries[i]->insert(full_path);
  76. return;
  77. }
  78. }
  79. std::string new_param = "";
  80. for (unsigned int i = 1; i < full_path.size(); i++)
  81. {
  82. new_param.append(full_path[i]);
  83. }
  84. Dir_or_File* dir= new Dir_or_File(new_param);
  85. entries.push_back(dir);
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement