- 论坛徽章:
- 2
|
- #include <ctype.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct s_exp {
- struct s_exp *previous, *next;
- char *name;
- long value;
- };
- int get_char(FILE *fp, long offset)
- {
- char c;
- if (fseek(fp, offset, SEEK_SET) == 0) {
- if (fread(&c, 1, 1, fp) == 1) {
- return c;
- }
- }
- return EOF;
- }
- int get_name_length(FILE *fp, long offset)
- {
- int n;
- int c;
- for (n = 0; (c = get_char(fp, offset)) != EOF; n++) {
- if (c == '=') {
- break;
- }
- offset++;
- }
- return n;
- }
- void trim_right(char *s)
- {
- int len;
- for (len = strlen(s) - 1; len >= 0; len--) {
- if (!isspace(s[len])) {
- s[len + 1] = '\0';
- break;
- }
- }
- }
- int get_name(FILE *fp, long *offset, char *buff, int len)
- {
- long i;
- int n;
- int c;
- i = *offset;
- for (n = 0; (c = get_char(fp, i)) != EOF && n < len; n++) {
- if (c == '=') {
- buff[n] = '\0';
- trim_right(buff);
- break;
- }
- buff[n] = c;
- i++;
- }
- *offset = i;
- return 0;
- }
- int get_text(FILE *fp, long *offset, const char *text)
- {
- long i;
- int c;
- i = *offset;
- while (*text != '\0') {
- c = get_char(fp, i);
- if (c != EOF) {
- if (c == *text) {
- text++;
- i++;
- continue;
- }
- }
- return -1;
- }
- *offset = i;
- return 0;
- }
- int get_space(FILE *fp, long *offset)
- {
- int i;
- int c;
- i = *offset;
- c = get_char(fp, i);
- if (c != EOF) {
- if (c == ' ' || c == '\t') {
- while ((c = get_char(fp, ++i)) != EOF && c == ' ' || c == '\t') {
- }
- *offset = i;
- }
- return 0;
- }
- return -1;
- }
- int get_return(FILE *fp, long *offset)
- {
- if (get_text(fp, offset, "\r\n") == 0) {
- return 0;
- }
- return -1;
- }
- int get_long(FILE *fp, long *offset, long *value)
- {
- long i;
- long val;
- int c;
- i = *offset;
- c = get_char(fp, i);
- if (c != EOF) {
- if (isdigit(c)) {
- val = c - '0';
- while ((c = get_char(fp, ++i)) != EOF && isdigit(c)) {
- val = val * 10 + c - '0';
- }
- *value = val;
- *offset = i;
- }
- return 0;
- }
- return -1;
- }
- struct s_exp *search_exp(struct s_exp **list, const char *name)
- {
- struct s_exp *node;
- if (*list != NULL) {
- node = *list;
- do {
- if (strcmp(node->name, name) == 0) {
- return node;
- }
- node = node->next;
- }while (node != *list);
- }
- return NULL;
- }
- struct s_exp *get_exp(FILE *fp, long *offset)
- {
- long i;
- struct s_exp exp, *pexp;
- int len;
- i = *offset;
- if (get_space(fp, &i) == 0) {
- len = get_name_length(fp, i);
- if (len > 0) {
- memset(&exp, 0, sizeof(exp));
- exp.name = (char *)malloc(len + 1);
- if (exp.name != NULL) {
- if (get_name(fp, &i, exp.name, len + 1) == 0) {
- if (get_text(fp, &i, "=") == 0) {
- if (get_space(fp, &i) == 0) {
- if (get_long(fp, &i, &exp.value) == 0) {
- if (get_space(fp, &i) == 0) {
- if (get_text(fp, &i, ",") == 0
- || get_return(fp, &i) == 0) {
- pexp = (struct s_exp *)malloc(sizeof(*pexp));
- if (pexp != NULL) {
- *pexp = exp;
- *offset = i;
- return pexp;
- }
- }
- }
- }
- }
- }
- }
- free(exp.name);
- }
- }
- }
- return NULL;
- }
- int insert_exp(struct s_exp **list, struct s_exp *node)
- {
- if (search_exp(list, node->name) == NULL) {
- if (*list == NULL) {
- node->previous = node;
- node->next = node;
- *list = node;
- }else {
- node->previous = (*list)->previous;
- node->next = *list;
- (*list)->previous->next = node;
- (*list)->previous = node;
- }
- return 0;
- }else {
- free(node->name);
- free(node);
- return 0;
- }
- }
- int update_exp_value(struct s_exp **list, const char *name, long value)
- {
- struct s_exp *node;
- node = search_exp(list, name);
- if (node != NULL) {
- node->value = value;
- return 0;
- }
- return -1;
- }
- int free_all_exp(struct s_exp **list)
- {
- struct s_exp *node, *n2;
- if (*list != NULL) {
- node = *list;
- do {
- n2 = node;
- node = node->next;
- free(n2->name);
- free(n2);
- }while (node != *list);
- *list = NULL;
- return 0;
- }
- return -1;
- }
- int write_exp_to_file(struct s_exp **list, const char *fname)
- {
- struct s_exp *node;
- FILE *fp;
- if (*list != NULL) {
- fp = fopen(fname, "w");
- if (fp != NULL) {
- node = *list;
- do {
- fprintf(fp, "%s=%lu\n", node->name, node->value);
- node = node->next;
- }while (node != *list);
- fclose(fp);
- return 0;
- }
- }
- return -1;
- }
- int read_exp_from_file(struct s_exp **list, const char *fname)
- {
- struct s_exp *node;
- FILE *fp;
- long offset;
- fp = fopen(fname, "rb");
- if (fp != NULL) {
- *list = NULL;
- offset = 0;
- while ((node = get_exp(fp, &offset)) != NULL) {
- insert_exp(list, node);
- }
- fclose(fp);
- return 0;
- }
- return -1;
- }
- int main(void)
- {
- struct s_exp *list;
- if (read_exp_from_file(&list, "a.txt") == 0) {
- update_exp_value(&list, "c", 10);
- write_exp_to_file(&list, "con");
- free_all_exp(&list);
- return 0;
- }
- return -1;
- }
复制代码 |
|