Advertisement
Guest User

Untitled

a guest
Feb 11th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. // Считываение dir и time_rule из файла
  2. bool parse_config(char *dir, char *cron_expr) {
  3. log_message(LOG_FILE, "Reading config file.", INFO_STATUS);
  4. FILE *fp;
  5. char *line = (char *) malloc(100 * sizeof(char));
  6. size_t len = 100;
  7. ssize_t read;
  8.  
  9. fp = fopen(CONFIG_FILENAME, "r");
  10.  
  11. if (fp == NULL) {
  12. log_message(LOG_FILE, "Error while opening the config.", ERROR_STATUS);
  13. return false;
  14. }
  15.  
  16. int key_id = 0;
  17. while ((read = getline(&line, &len, fp)) != -1) {
  18. size_t quote = 0;
  19. int j = 0;
  20. for (int i = 0; i < read; ++i) {
  21. if (line[i] == '"') {
  22. quote++;
  23. if (quote == 4) {
  24. if (key_id == 0) {
  25. dir[j] = '\0';
  26. } else {
  27. cron_expr[j] = '\0';
  28. }
  29. }
  30. continue;
  31. }
  32. if (quote == 3) {
  33. switch (key_id) {
  34. case 0:dir[j] = line[i];
  35. j++;
  36. break;
  37. case 1:cron_expr[j] = line[i];
  38. j++;
  39. break;
  40. }
  41. }
  42. if (quote == 4) {
  43. key_id = 1;
  44. j = 0;
  45. }
  46. }
  47. }
  48.  
  49. char *buf = (char *) malloc(100 * sizeof(char));
  50. sprintf(buf, "dir: %s", dir);
  51. log_message(LOG_FILE, buf, DEBUG_STATUS);
  52. free(buf);
  53.  
  54. buf = (char *) malloc(100 * sizeof(char));
  55. sprintf(buf, "cron expression: %s", cron_expr);
  56. log_message(LOG_FILE, buf, DEBUG_STATUS);
  57. free(buf);
  58.  
  59. fclose(fp);
  60. if (line) {
  61. free(line);
  62. }
  63.  
  64. return true;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement