Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <deque>
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #define its_not_string "NO string"
- #define its_string "string"
- #define its_object "object"
- #define its_array "array"
- #define what "whaaaaaat"
- void get_back(std::string &cur_path) {
- int j = cur_path.size() - 1;
- while (j == cur_path.size() - 1 || cur_path[j] != '/') --j;
- cur_path.erase(j + 1, cur_path.size() - 1 - j);
- }
- void sq_brackets(std::vector<std::string> &path, std::vector<std::string> &info, std::vector<std::string> &ford,
- int &i, std::string &line, std::string &cur_path, std::string &cur_info, std::ifstream &input, int &count) {
- cur_path += '/';
- int arr = 1;
- path.push_back(cur_path);
- ford.push_back(its_array);
- cur_info = "";
- info.push_back(cur_info);
- std::vector<int> name(1);
- while (arr) {
- ++count;
- cur_info = "";
- // std::cout << count << '\n';
- ++name[name.size() - 1];
- int j = 0;
- std::getline(input, line);
- while (line[j] == ' ') ++j;
- if (line[j] == ']') {
- name.erase(name.begin() + name.size() - 1);
- --arr;
- get_back(cur_path);
- continue;
- }
- cur_path += std::to_string(name[name.size() - 1]);
- if (line[j] == '[') {
- cur_path += '/';
- ++arr;
- name.push_back(0);
- path.push_back(cur_path);
- ford.push_back(its_array);
- info.push_back(cur_info);
- continue;
- }
- else {
- int ke = line.size() - 1;
- if (line[j] == '"') {
- ford.push_back(its_string);
- ++j;
- --ke;
- }
- else {
- ford.push_back(its_not_string);
- }
- while (j != ke) {
- cur_info += line[j++];
- }
- if (line[j] != '"' && line[j] != ',')
- cur_info += line[j];
- path.push_back(cur_path);
- info.push_back(cur_info);
- }
- //std::cout << cur_path << '\n';
- get_back(cur_path);
- }
- }
- void parsing(std::vector<std::string> &path, std::vector<std::string> &info, std::vector<std::string> &ford) {
- std::ifstream input("json_FS.json");
- std::string line, cur_path = "./", cur_info;
- std::getline(input, line);
- int open = 1, count = 1;
- while (open) {
- ++count;
- // std::cout << count << '\n';
- cur_info = "";
- std::getline(input, line);
- int i = 0;
- while (line[i] == ' ') ++i;
- if (line[i] == '}') {
- --open;
- if (!open)
- continue;
- get_back(cur_path);
- continue;
- }
- //std::cout << line << '\n';
- while (line[i] != ':') {
- if (line[i] != '"') {
- cur_path += line[i++];
- }
- else ++i;
- }
- i += 2;
- if (line[i] == '[') {
- sq_brackets(path, info, ford, i, line, cur_path, cur_info, input, count);
- continue;
- }
- if (line[i] == '{') {
- ++open;
- cur_path += '/';
- path.push_back(cur_path);
- ford.push_back(its_object);
- info.push_back(cur_info);
- continue;
- }
- else {
- int ke = line.size() - 1, type = 0;
- if (line[line.size() - 1] == ',') --ke;
- if (line[i] == '"') {
- ++i;
- ford.push_back(its_string);
- }
- else {
- ford.push_back(its_not_string);
- }
- while (i != ke) {
- cur_info += line[i++];
- }
- if (line[i] != ',' && line[i] != '"')
- cur_info += line[i];
- path.push_back(cur_path);
- info.push_back(cur_info);
- }
- // std::cout << cur_path << '\n';
- get_back(cur_path);
- }
- input.close();
- }
- // static struct fuse_operations tg_operations = {
- // .init = tgfs_init,
- // .getattr = tgfs_getattr,
- // .readdir = tgfs_readdir,
- // .utimens = tgfs_utimens,
- // // .open = tgfs_open,
- // .read = tgfs_read,
- // .mknod = tgfs_mknod,
- // .unlink = tgfs_unlink,
- // };
- void slash_quan(std::string &a, int &c) {
- c = 0;
- for (size_t x = 0; x < a.size(); ++x)
- if (a[x] == '/') ++c;
- }
- void get_directory_name(std::string &cur_path, std::string &line) {
- std::string part_path = "";
- int j = cur_path.size() - 1;
- if (cur_path[j] == '/') --j;
- while (cur_path[j] != '/')
- part_path = cur_path[j--] + part_path;
- line = '"' + part_path + '"' + ": ";
- }
- void upgrade(std::vector<std::string> &path, std::vector<std::string> &info, std::vector<std::string> &ford) {
- std::ofstream output("UPD_json_FS.json");
- std::string cur_path = "./";
- output << "{\n";
- int i = 0, csb = 0, csc = 0, arr = 0;
- bool arrb = 0;
- std::string boof = cur_path, line = "";
- std::deque<char> brackets;
- brackets.push_back('{');
- while (i != path.size() - 2) {
- line = "";
- ++i;
- cur_path = path[i];
- std::cout << cur_path << '\n';
- slash_quan(boof, csb);
- slash_quan(cur_path, csc);
- if (csc > csb) {
- for (size_t z = 0; z < csc - 1; ++z)
- output << " "; //4 пробела
- if (ford[i] == its_array) {
- brackets.push_back('[');
- if (!arrb) {
- get_directory_name(cur_path, line);
- +arr;
- arrb = 1;
- }
- line += '[';
- }
- if (ford[i] == its_object) {
- brackets.push_back('{');
- get_directory_name(cur_path, line);
- line += '{';
- }
- output << line << '\n';
- }
- if (csc < csb) {
- for (size_t z = 0; z < csb - csc; ++z) {
- line = "";
- for (size_t x = 0; x < csb - 1 - z; ++x)
- output << " "; //4 пробела
- if (brackets.back() == '[') {
- line += ']';
- --arr;
- if (!arr) arrb = 0;
- } else {
- line += '}';
- }
- brackets.pop_back();
- if (z == csb - csc - 1)
- line += ',';
- output << line << '\n';
- }
- }
- if (csc == csb) {
- for (size_t z = 0; z < csc; ++z)
- output << " ";
- if (cur_path[cur_path.size() - 1] == '/') {
- if (ford[i] == its_object) {
- get_directory_name(cur_path, line);
- line += '{';
- brackets.push_back('{');
- }
- else {
- line += '[';
- brackets.push_back('[');
- }
- continue;
- }
- if (brackets.back() == '{') {
- get_directory_name(cur_path, line);
- }
- if (ford[i] == its_string)
- line += '"' + info[i] + '"';
- if (ford[i] == its_not_string)
- line += info[i];
- int next = 0;
- slash_quan(path[i], next);
- if (next - csc == 0)
- line += ',';
- output << line << '\n';
- }
- boof = cur_path;
- }
- output.close();
- }
- int main(int argc, char const *argv[]) {
- std::vector<std::string> path, info;
- std::vector<std::string> ford;
- path.push_back("./");
- info.push_back("");
- ford.push_back("ROOT");
- parsing(path, info, ford);
- std::cout << "/* message */" << '\n';
- for (size_t i = 0; i < path.size(); ++i)
- std::cout << path[i] << " - " << info[i] << " - " << ford[i] << '\n';
- std::cout << "\n\n\n\n\n\n\n\n";
- upgrade(path, info, ford);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement