Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Считываение dir и time_rule из файла
- bool parse_config(char *dir, char *cron_expr) {
- log_message(LOG_FILE, "Reading config file.", INFO_STATUS);
- FILE *fp;
- char *line = (char *) malloc(100 * sizeof(char));
- size_t len = 100;
- ssize_t read;
- fp = fopen(CONFIG_FILENAME, "r");
- if (fp == NULL) {
- log_message(LOG_FILE, "Error while opening the config.", ERROR_STATUS);
- return false;
- }
- int key_id = 0;
- while ((read = getline(&line, &len, fp)) != -1) {
- size_t quote = 0;
- int j = 0;
- for (int i = 0; i < read; ++i) {
- if (line[i] == '"') {
- quote++;
- if (quote == 4) {
- if (key_id == 0) {
- dir[j] = '\0';
- } else {
- cron_expr[j] = '\0';
- }
- }
- continue;
- }
- if (quote == 3) {
- switch (key_id) {
- case 0:dir[j] = line[i];
- j++;
- break;
- case 1:cron_expr[j] = line[i];
- j++;
- break;
- }
- }
- if (quote == 4) {
- key_id = 1;
- j = 0;
- }
- }
- }
- char *buf = (char *) malloc(100 * sizeof(char));
- sprintf(buf, "dir: %s", dir);
- log_message(LOG_FILE, buf, DEBUG_STATUS);
- free(buf);
- buf = (char *) malloc(100 * sizeof(char));
- sprintf(buf, "cron expression: %s", cron_expr);
- log_message(LOG_FILE, buf, DEBUG_STATUS);
- free(buf);
- fclose(fp);
- if (line) {
- free(line);
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement