Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.10 KB | None | 0 0
  1. #include <algorithm>
  2. #include <deque>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6. #include <vector>
  7.  
  8. #define its_not_string "NO string"
  9. #define its_string "string"
  10. #define its_object "object"
  11. #define its_array "array"
  12. #define what "whaaaaaat"
  13.  
  14. void get_back(std::string &cur_path) {
  15. int j = cur_path.size() - 1;
  16. while (j == cur_path.size() - 1 || cur_path[j] != '/') --j;
  17. cur_path.erase(j + 1, cur_path.size() - 1 - j);
  18. }
  19.  
  20. void sq_brackets(std::vector<std::string> &path, std::vector<std::string> &info, std::vector<std::string> &ford,
  21. int &i, std::string &line, std::string &cur_path, std::string &cur_info, std::ifstream &input, int &count) {
  22. cur_path += '/';
  23. int arr = 1;
  24. path.push_back(cur_path);
  25. ford.push_back(its_array);
  26. cur_info = "";
  27. info.push_back(cur_info);
  28. std::vector<int> name(1);
  29. while (arr) {
  30. ++count;
  31. cur_info = "";
  32. // std::cout << count << '\n';
  33. ++name[name.size() - 1];
  34. int j = 0;
  35. std::getline(input, line);
  36. while (line[j] == ' ') ++j;
  37. if (line[j] == ']') {
  38. name.erase(name.begin() + name.size() - 1);
  39. --arr;
  40. get_back(cur_path);
  41. continue;
  42. }
  43. cur_path += std::to_string(name[name.size() - 1]);
  44. if (line[j] == '[') {
  45. cur_path += '/';
  46. ++arr;
  47. name.push_back(0);
  48. path.push_back(cur_path);
  49. ford.push_back(its_array);
  50. info.push_back(cur_info);
  51. continue;
  52. }
  53. else {
  54. int ke = line.size() - 1;
  55. if (line[j] == '"') {
  56. ford.push_back(its_string);
  57. ++j;
  58. --ke;
  59. }
  60. else {
  61. ford.push_back(its_not_string);
  62. }
  63. while (j != ke) {
  64. cur_info += line[j++];
  65. }
  66. if (line[j] != '"' && line[j] != ',')
  67. cur_info += line[j];
  68. path.push_back(cur_path);
  69. info.push_back(cur_info);
  70. }
  71. //std::cout << cur_path << '\n';
  72. get_back(cur_path);
  73. }
  74. }
  75.  
  76. void parsing(std::vector<std::string> &path, std::vector<std::string> &info, std::vector<std::string> &ford) {
  77. std::ifstream input("json_FS.json");
  78. std::string line, cur_path = "./", cur_info;
  79. std::getline(input, line);
  80. int open = 1, count = 1;
  81. while (open) {
  82. ++count;
  83. // std::cout << count << '\n';
  84. cur_info = "";
  85. std::getline(input, line);
  86. int i = 0;
  87. while (line[i] == ' ') ++i;
  88. if (line[i] == '}') {
  89. --open;
  90. if (!open)
  91. continue;
  92. get_back(cur_path);
  93. continue;
  94. }
  95. //std::cout << line << '\n';
  96. while (line[i] != ':') {
  97. if (line[i] != '"') {
  98. cur_path += line[i++];
  99. }
  100. else ++i;
  101. }
  102. i += 2;
  103. if (line[i] == '[') {
  104. sq_brackets(path, info, ford, i, line, cur_path, cur_info, input, count);
  105. continue;
  106. }
  107. if (line[i] == '{') {
  108. ++open;
  109. cur_path += '/';
  110. path.push_back(cur_path);
  111. ford.push_back(its_object);
  112. info.push_back(cur_info);
  113. continue;
  114. }
  115. else {
  116. int ke = line.size() - 1, type = 0;
  117. if (line[line.size() - 1] == ',') --ke;
  118. if (line[i] == '"') {
  119. ++i;
  120. ford.push_back(its_string);
  121. }
  122. else {
  123. ford.push_back(its_not_string);
  124. }
  125. while (i != ke) {
  126. cur_info += line[i++];
  127. }
  128. if (line[i] != ',' && line[i] != '"')
  129. cur_info += line[i];
  130. path.push_back(cur_path);
  131. info.push_back(cur_info);
  132. }
  133. // std::cout << cur_path << '\n';
  134. get_back(cur_path);
  135. }
  136. input.close();
  137. }
  138.  
  139. // static struct fuse_operations tg_operations = {
  140. // .init = tgfs_init,
  141. // .getattr = tgfs_getattr,
  142. // .readdir = tgfs_readdir,
  143. // .utimens = tgfs_utimens,
  144. // // .open = tgfs_open,
  145. // .read = tgfs_read,
  146. // .mknod = tgfs_mknod,
  147. // .unlink = tgfs_unlink,
  148. // };
  149.  
  150. void slash_quan(std::string &a, int &c) {
  151. c = 0;
  152. for (size_t x = 0; x < a.size(); ++x)
  153. if (a[x] == '/') ++c;
  154. }
  155.  
  156. void get_directory_name(std::string &cur_path, std::string &line) {
  157. std::string part_path = "";
  158. int j = cur_path.size() - 1;
  159. if (cur_path[j] == '/') --j;
  160. while (cur_path[j] != '/')
  161. part_path = cur_path[j--] + part_path;
  162. line = '"' + part_path + '"' + ": ";
  163. }
  164.  
  165. void upgrade(std::vector<std::string> &path, std::vector<std::string> &info, std::vector<std::string> &ford) {
  166. std::ofstream output("UPD_json_FS.json");
  167. std::string cur_path = "./";
  168. output << "{\n";
  169. int i = 0, csb = 0, csc = 0, arr = 0;
  170. bool arrb = 0;
  171. std::string boof = cur_path, line = "";
  172. std::deque<char> brackets;
  173. brackets.push_back('{');
  174. while (i != path.size() - 2) {
  175. line = "";
  176. ++i;
  177. cur_path = path[i];
  178. std::cout << cur_path << '\n';
  179. slash_quan(boof, csb);
  180. slash_quan(cur_path, csc);
  181. if (csc > csb) {
  182. for (size_t z = 0; z < csc - 1; ++z)
  183. output << " "; //4 пробела
  184. if (ford[i] == its_array) {
  185. brackets.push_back('[');
  186. if (!arrb) {
  187. get_directory_name(cur_path, line);
  188. +arr;
  189. arrb = 1;
  190. }
  191. line += '[';
  192. }
  193. if (ford[i] == its_object) {
  194. brackets.push_back('{');
  195. get_directory_name(cur_path, line);
  196. line += '{';
  197. }
  198. output << line << '\n';
  199. }
  200. if (csc < csb) {
  201. for (size_t z = 0; z < csb - csc; ++z) {
  202. line = "";
  203. for (size_t x = 0; x < csb - 1 - z; ++x)
  204. output << " "; //4 пробела
  205. if (brackets.back() == '[') {
  206. line += ']';
  207. --arr;
  208. if (!arr) arrb = 0;
  209. } else {
  210. line += '}';
  211. }
  212. brackets.pop_back();
  213. if (z == csb - csc - 1)
  214. line += ',';
  215. output << line << '\n';
  216. }
  217. }
  218. if (csc == csb) {
  219. for (size_t z = 0; z < csc; ++z)
  220. output << " ";
  221. if (cur_path[cur_path.size() - 1] == '/') {
  222. if (ford[i] == its_object) {
  223. get_directory_name(cur_path, line);
  224. line += '{';
  225. brackets.push_back('{');
  226. }
  227. else {
  228. line += '[';
  229. brackets.push_back('[');
  230. }
  231. continue;
  232. }
  233. if (brackets.back() == '{') {
  234. get_directory_name(cur_path, line);
  235. }
  236. if (ford[i] == its_string)
  237. line += '"' + info[i] + '"';
  238. if (ford[i] == its_not_string)
  239. line += info[i];
  240. int next = 0;
  241. slash_quan(path[i], next);
  242. if (next - csc == 0)
  243. line += ',';
  244. output << line << '\n';
  245. }
  246. boof = cur_path;
  247. }
  248. output.close();
  249. }
  250.  
  251. int main(int argc, char const *argv[]) {
  252. std::vector<std::string> path, info;
  253. std::vector<std::string> ford;
  254. path.push_back("./");
  255. info.push_back("");
  256. ford.push_back("ROOT");
  257. parsing(path, info, ford);
  258. std::cout << "/* message */" << '\n';
  259. for (size_t i = 0; i < path.size(); ++i)
  260. std::cout << path[i] << " - " << info[i] << " - " << ford[i] << '\n';
  261. std::cout << "\n\n\n\n\n\n\n\n";
  262. upgrade(path, info, ford);
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement