/
Boxapp
/
yadisk-helper
Обзор
Документация
Войти
/
Boxapp
/
yadisk-helper
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
excl.cpp
102 строки
2 KB
DebianCougar
test build 2026.02-beta
30 янв 2026, 21:25
30 янв 2026, 21:25
eb3551d
Код
Авторство
О чём код?
#include <fstream> #include <iostream> #include <vector> #include "parser.hpp" using std::endl; using std::ofstream; using std::ostream; using std::pair; using std::string; using std::vector; enum Quotation { ALWAYS_QUOTE, NEVER_QUOTE, QUOTE_IF_NEEDED }; static bool canBeInName(char c) { return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' || c <= 'z') || c == '_' || c == '.' || c == '-'; } static string writeWord(const string &word, Quotation mode) { string res; switch (mode) { case ALWAYS_QUOTE: { res += '"'; for (char c : word) { if (c == '"' || c == '\\') res += '\\'; res += c; } res += '"'; } break; case NEVER_QUOTE: { for (char c : word) { if (!canBeInName(c)) res += '\\'; res += c; } } break; case QUOTE_IF_NEEDED: { bool needed = false; for (char c : word) if (!canBeInName(c)) { needed = true; break; } if (needed) { res += '"'; for (char c : word) { if (c == '"' || c == '\\') res += '\\'; res += c; } res += '"'; } else res = word; } break; } return res; } ConfigMap params; // WARNING global variable! vector<string> readExcludeDirs(const string &file) { params = get_config(file); string paths = params.find("exclude-dirs") == params.end() ? "" : params.at("exclude-dirs"); vector<string> res; string path; for (char c : paths) { if (c == ',') { // WARNING check escaping like in CSV (i.e. through quotation) if (!path.empty()) res.push_back(path); path.clear(); } else path.push_back(c); } if (!path.empty()) res.push_back(path); return res; } static void writeConfig(const ConfigMap &conf, ostream &out) { for (const pair<const string, string> ¶m : conf) out << writeWord(param.first, NEVER_QUOTE) << " = " << writeWord(param.second, ALWAYS_QUOTE) << endl; } static string unslash(const string &path) { // FIXME temporary patch, try to handle paths more carefully size_t p = 0; for (; p < path.size() && path[p] == '/'; ++p) {} if (p < path.size()) return path.substr(p); return "/"; } void writeExcludeDirs(const vector<string> &excl_list, const string &file) { string value; // WARNING commas in file names cannot be escaped if (!excl_list.empty()) { value = unslash(excl_list[0]); for (size_t i = 1; i < excl_list.size(); ++i) (value += ',') += unslash(excl_list[i]); } params["exclude-dirs"] = value; ofstream fout(file); writeConfig(params, fout); }