📄 config_api.c
字号:
fprintf(conf->fp, "%s",tmp); } fclose(tmp_fp); } /* get back to initial offset */ fseek(conf->fp, initial_offset, SEEK_SET); return 1;}int config_erase(config_t *conf, const char *section, const char *option){ off_t initial_offset; off_t section_offset; FILE *tmp_fp; char buff[MAX_LINE_SIZE]; char tmp[MAX_LINE_SIZE]; unsigned int copy = 0; unsigned int erased = 0; char template[] = "config_eraseXXXXXX"; if (!conf->openned) { if (conf->debug) fprintf(stderr, "config_erase(): config file not " "openned\n"); return 0; } /* save initial offset */ initial_offset = ftell(conf->fp); if ((section_offset = find_section(conf->fp, section)) == -1) return 0; if ((tmp_fp = my_tmpfile(template, "/tmp")) == NULL) { if (conf->debug) fprintf(stderr, "config_erase(): cannot open temporary " "file\n"); /* lets get back to initial offset before return */ fseek(conf->fp, initial_offset, SEEK_SET); return 0; } /* copy the rest of the file after erase_offset to the tmp_fp */ fseek(conf->fp, section_offset, SEEK_SET); while (!feof(conf->fp)) { memset(buff, 0, MAX_LINE_SIZE); my_fgets(buff, MAX_LINE_SIZE, conf->fp); fprintf(tmp_fp, "%s", buff); } /* after copy, trucate the rest of the file and get back to the offset */ ftruncate(fileno(conf->fp), section_offset); fseek(conf->fp, section_offset, SEEK_SET); rewind(tmp_fp); while (!feof(tmp_fp)) { /* get file buffer */ memset(buff, 0, MAX_LINE_SIZE); my_fgets(buff, MAX_LINE_SIZE, tmp_fp); /* copy comments */ if (*buff == '#' || *buff == '\r' || *buff == '\n') { fprintf(conf->fp, "%s", buff); continue; } /* if what we want were already erased, copy */ if (!erased) { if (option != NULL) { if (is_an_option(buff, tmp, MAX_OPTION_SIZE - 1, NULL, 0)) { if (!strcmp(tmp, option)) { erased = 1; copy = 0; } else { copy = 1; } } else { copy = 1; } } else { if (is_a_section(buff, tmp, MAX_OPTION_SIZE - 1 )) { if (!strcmp(tmp, section)) { copy = 0; } else { erased = 1; copy = 1; } } else { copy = 0; } } } else { copy = 1; } if (copy != 0) { fprintf(conf->fp, "%s", buff); } } fclose(tmp_fp); /* get back to initial offset */ fseek(conf->fp, initial_offset, SEEK_SET); return 1;}int config_get_num_sections(config_t *conf){ off_t initial_offset; char line_tmp[MAX_LINE_SIZE]; int count = 0; /* save initial offset */ initial_offset = ftell(conf->fp); while (!feof(conf->fp)) { /* get buffer */ memset(line_tmp, 0, MAX_LINE_SIZE - 1); my_fgets(line_tmp, MAX_LINE_SIZE - 1, conf->fp); /* ignore comments */ if (*line_tmp == '#' || *line_tmp == '\r' || *line_tmp == '\n') continue; /* ignore if its not a section */ if (is_a_section(line_tmp, NULL, 0)) { count++; } } /* get back to initial offset */ fseek(conf->fp, initial_offset, SEEK_SET); return count;}int config_get_num_options(config_t *conf, char *section) { off_t initial_offset; off_t section_offset; char line_tmp[MAX_LINE_SIZE]; int count = 0; /* save initial offset */ initial_offset = ftell(conf->fp); /* find the section */ section_offset = find_section(conf->fp, section); if (section_offset == -1) { if (conf->debug) fprintf(stderr, "config_get_options(): cannot find " "section '%s'\n", section); return -1; } /* ignore the current section line */ fseek(conf->fp, section_offset, SEEK_SET); my_fgets(line_tmp, MAX_LINE_SIZE, conf->fp); while (!feof(conf->fp)) { /* get the buffer */ memset(line_tmp, 0, MAX_LINE_SIZE); my_fgets(line_tmp, MAX_LINE_SIZE, conf->fp); /* if we find a new section, we are done */ if (is_a_section(line_tmp, NULL, 0)) { break; } if (is_an_option(line_tmp, NULL, 0, NULL, 0)) { count++; } } /* get back to initial offset */ fseek(conf->fp, initial_offset, SEEK_SET); return count;}char **config_get_sections(config_t *conf){ off_t initial_offset; char line_tmp[MAX_LINE_SIZE]; char section_tmp[MAX_SECTION_SIZE]; char **p_dest = NULL; static char **dest = NULL; /* save initial offset */ initial_offset = ftell(conf->fp); dest = malloc(config_get_num_sections(conf) * sizeof(char *)); p_dest = dest; if (conf->debug) fprintf(stderr, "base: %p\n", dest); if (dest == NULL) { if (conf->debug) { fprintf(stderr, "config_get_sections: cannot allocate space: %s", strerror(errno)); } return (char **)NULL; } while (!feof(conf->fp)) { /* get buffer */ memset(line_tmp, 0, MAX_LINE_SIZE - 1); my_fgets(line_tmp, MAX_LINE_SIZE - 1, conf->fp); /* ignore comments */ if (*line_tmp == '#' || *line_tmp == '\r' || *line_tmp == '\n') continue; /* ignore if its not a section */ if (is_a_section(line_tmp, section_tmp, MAX_SECTION_SIZE - 1)) { /* if dest is set, allocate space and copy the section name there */ *dest = malloc(strlen(section_tmp) + 1); if (*dest == NULL) { if (conf->debug) fprintf(stderr, "config_get_sections():" " cannot allocate space" ": %s\n", strerror(errno)); return (char **)NULL; } strcpy(*dest++, section_tmp); } } *dest = NULL; /* get back to initial offset */ fseek(conf->fp, initial_offset, SEEK_SET); return p_dest;} char **config_get_options(config_t *conf, char *section){ off_t initial_offset; off_t section_offset; char line_tmp[MAX_LINE_SIZE]; char option_tmp[MAX_OPTION_SIZE]; static char **dest; char **p_dest; /* save initial offset */ initial_offset = ftell(conf->fp); /* find the section */ section_offset = find_section(conf->fp, section); if (section_offset == -1) { if (conf->debug) fprintf(stderr, "config_get_options(): cannot find " "section '%s'\n", section); return (char **)NULL; } /* alloc memory for vector's base */ dest = malloc(config_get_num_options(conf, section) * sizeof(char *)); p_dest = dest; if (dest == NULL) { if (conf->debug) { fprintf(stderr, "config_get_options(): cannot allocat space: %s\n", strerror(errno)); } return (char **)NULL; } /* ignore the current section line */ fseek(conf->fp, section_offset, SEEK_SET); my_fgets(line_tmp, MAX_LINE_SIZE, conf->fp); while (!feof(conf->fp)) { /* get the buffer */ memset(line_tmp, 0, MAX_LINE_SIZE); my_fgets(line_tmp, MAX_LINE_SIZE, conf->fp); /* ignore if is a new section */ if (is_a_section(line_tmp, NULL, 0)) break; if (is_an_option(line_tmp, option_tmp, MAX_OPTION_SIZE - 1, NULL, 0)) { *dest = malloc(strlen(option_tmp) + 1); if (*dest == NULL) { if (conf->debug) fprintf(stderr, "config_get_options(): " "cannot allocate space:" " %s\n", strerror(errno)); return (char **)NULL; } strcpy(*dest++, option_tmp); } } *dest = NULL; /* get back to initial offset */ fseek(conf->fp, initial_offset, SEEK_SET); return p_dest;}void config_on_change(config_t *conf, void (*func)(void *), void *func_arg, unsigned int timeout){ pthread_t pth; /* get descriptor to stat() */ conf->fd = fileno(conf->fp); if (conf->fd == -1) return; conf->timeout = timeout; conf->func = func; conf->func_arg = func_arg; pthread_create(&pth, NULL, (void *)&on_change, conf); return;}int config_close(config_t *conf){ conf->openned = 0; if (fclose(conf->fp) == EOF) return 0; return 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -