folder.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include <fstream>
  2. #include <string>
  3. #include <filesystem>
  4. #include <algorithm>
  5. #include <cstring>
  6. #include "folder.h"
  7. #include "page.h"
  8. using namespace std;
  9. using namespace filesystem;
  10. bool contains(vector<string> v, string s){
  11. return (v.size() > 0) and (find(v.begin(), v.end(), s) != v.end());
  12. }
  13. folder::~folder(){}
  14. folder::folder(){}
  15. folder::folder(string path){
  16. this->path = path;
  17. for(const auto& entry : directory_iterator(path)){
  18. string path = entry.path();
  19. if(is_directory(entry.path()))
  20. folder(entry.path()).convert();
  21. else{
  22. if(path[path.size() - 7] == '/'){
  23. string check_string = path.substr(path.size() - 6);
  24. cout << check_string << endl;
  25. if(check_string == "header"){
  26. _header_path = path;
  27. }
  28. }else if(path[path.size() - 4] == '/'){
  29. string check_string = path.substr(path.size() - 3);
  30. cout << check_string << endl;
  31. if(check_string == "end"){
  32. _end_path = path;
  33. }
  34. }else if(path[path.size() - 7] == '.'){
  35. string check_string = path.substr(path.size() - 6);
  36. //cout << check_string << endl;
  37. cout << path << endl;
  38. if(check_string == "matedi"){
  39. _dot_matedi_path = path;
  40. }
  41. }
  42. }
  43. }
  44. }
  45. void folder::get_dot_matedi_config(string dot_matedi_path){
  46. ifstream in(dot_matedi_path);
  47. string line;
  48. while(getline(in,line)){
  49. size_t aux = line.find("header: ");
  50. if(aux != string::npos){
  51. line = line.substr(aux + 8);
  52. _header_path = line;
  53. }
  54. aux = line.find("end: ");
  55. if(aux != string::npos){
  56. line = line.substr(aux + 5);
  57. _end_path = line;
  58. }
  59. char * pch;
  60. aux = line.find("header_only: ");
  61. if(aux != string::npos){
  62. line = line.substr(aux + 13);
  63. const char * str = line.c_str();
  64. pch = strtok((char*)str, ", ");
  65. while(pch != NULL){
  66. _header_only.push_back(string(pch));
  67. pch = strtok (NULL, ", ");
  68. }
  69. }
  70. aux = line.find("end_only: ");
  71. if(aux != string::npos){
  72. line = line.substr(aux + 10);
  73. const char * str = line.c_str();
  74. pch = strtok((char *)str, ", ");
  75. while(pch != NULL){
  76. _end_only.push_back(string(pch));
  77. pch = strtok (NULL, ", ");
  78. }
  79. }
  80. aux = line.find("clear: ");
  81. if(aux != string::npos){
  82. line = line.substr(aux + 7);
  83. const char * str = line.c_str();
  84. pch = strtok((char *)str, ", ");
  85. while(pch != NULL){
  86. _clear.push_back(string(pch));
  87. pch = strtok (NULL, ", ");
  88. }
  89. }
  90. }
  91. }
  92. void folder::convert(){
  93. string header;
  94. string end;
  95. if(!_dot_matedi_path.empty()){
  96. get_dot_matedi_config(_dot_matedi_path);
  97. }
  98. string current_folder = current_path();
  99. if(!_header_path.empty() && !_end_path.empty()){
  100. header = load_file(_header_path);
  101. end = load_file(_end_path);
  102. for(const auto& entry : directory_iterator(path)){
  103. if(!is_directory(entry)){
  104. string aux_path = entry.path();
  105. string folder_and_name = aux_path.substr(current_folder.size() + 1);
  106. string filename = aux_path.substr(aux_path.size() - 6);
  107. if((filename.find("matedi") == string::npos) &&
  108. (filename.find("header") == string::npos) &&
  109. (filename.find("end") == string::npos)){
  110. if(contains(_header_only, filename)){
  111. page p(aux_path, header, "");
  112. p.save();
  113. }else if(contains(_end_only, filename)){
  114. end = load_file(_end_path);
  115. page p(aux_path, "", end);
  116. p.save();
  117. }else if(contains(_clear, filename)){
  118. page p(aux_path, "", "");
  119. p.save();
  120. }else{
  121. page p(aux_path, header, end);
  122. p.save();
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }
  129. string folder::load_file(string path){
  130. ifstream reader(path);
  131. string line;
  132. string result;
  133. while(getline(reader,line))
  134. result += line + "\n";
  135. reader.close();
  136. return result;
  137. }
  138. folder& folder::operator =(const folder& F){
  139. if(this != &F) // avoid self-allocation
  140. path = F.path;
  141. return *this;
  142. }