int IniWriteString (const char *section, const char *indent, char *content, int contentlen, FILE *fp) { struct stringlist *head, *node, *p, *t; char tempstr[MAXSTRLEN]; char sectionstr[MAXSTRLEN]; int i, n, len; int sectionfinded;
if (fp == NULL) { return 0; }
bzero (sectionstr, MAXSTRLEN); sprintf (sectionstr, "[%s]", section);
lockf(fileno(fp), F_LOCK, 0l); fseek(fp, 0, SEEK_SET); head = (struct stringlist *) malloc (sizeof (struct stringlist)); p = head; bzero (tempstr, MAXSTRLEN); while (fgets (tempstr, MAXSTRLEN, fp) != NULL) { node = (struct stringlist *) malloc (sizeof (struct stringlist)); node->next = NULL; bzero (node->string, MAXSTRLEN); strcpy (node->string, tempstr); p->next = node; p = p->next; bzero (tempstr, MAXSTRLEN); } lockf(fileno(fp), F_ULOCK, 0l);
p = head; sectionfinded = 0; while (p->next != NULL) { p = p->next; if (sectionfinded == 0) { if (strncmp (p->string, sectionstr, strlen(sectionstr)) == 0 && (p->string[strlen(sectionstr)] == '\r' || p->string[strlen(sectionstr)] == '\n' || p->string[strlen(sectionstr)] == ' ')) sectionfinded = 1; } else { if (p->string[0] == '[') { continue; } else { if (strncmp (p->string, indent, strlen (indent)) == 0) { if (p->string[strlen (indent)] == '=') { memset(p->string, 0, sizeof(p->string)); sprintf(p->string, "%s=%s\n", indent, content); break; } } } } }
lockf(fileno(fp), F_LOCK, 0l); ftruncate(fileno(fp), 0); fseek(fp, 0, SEEK_SET); p = head; while (p->next != NULL) { p = p->next; fputs(p->string, fp); } lockf(fileno(fp), F_ULOCK, 0l);
while (head->next != NULL) { t = head->next; head->next = t->next; free (t); } free (head);
return 0; } /***************************************************************************** 函 数 名 : IniReadString *****************************************************************************/ int IniReadString (const char *section, const char *indent, const char *defaultresult, char *result, int resultlen, FILE *fp) { struct stringlist *head, *node, *p, *t; char tempstr[MAXSTRLEN]; char sectionstr[MAXSTRLEN]; int i, n, len; int sectionfinded;
if (fp == NULL) { strcpy (result, defaultresult); return 0; }
bzero (sectionstr, MAXSTRLEN); sprintf (sectionstr, "[%s]", section);
lockf(fileno(fp), F_LOCK, 0l); fseek(fp, 0, SEEK_SET); head = (struct stringlist *) malloc (sizeof (struct stringlist)); p = head; bzero (tempstr, MAXSTRLEN); while (fgets (tempstr, MAXSTRLEN, fp) != NULL) { node = (struct stringlist *) malloc (sizeof (struct stringlist)); node->next = NULL; bzero (node->string, MAXSTRLEN); strcpy (node->string, tempstr); p->next = node; p = p->next; bzero (tempstr, MAXSTRLEN); } lockf(fileno(fp), F_ULOCK, 0l);
p = head; while (p->next != NULL) { t = p->next; len = strlen (t->string); bzero (tempstr, MAXSTRLEN); n = 0; for (i = 0; i < len; i++) { if ((t->string[i] != '\r') && (t->string[i] != '\n') && (t->string[i] != ' ')) { tempstr[n] = t->string[i]; n++; } } if (strlen (tempstr) == 0) { p->next = t->next; free (t); } else { bzero (t->string, MAXSTRLEN); strcpy (t->string, tempstr); p = p->next; } }
p = head; sectionfinded = 0; while (p->next != NULL) { p = p->next; if (sectionfinded == 0) { if (strcmp (p->string, sectionstr) == 0) sectionfinded = 1; } else { if (p->string[0] == '[') { strcpy (result, defaultresult); goto end; } else { if (strncmp (p->string, indent, strlen (indent)) == 0) { if (p->string[strlen (indent)] == '=') { strncpy (result, p->string + strlen (indent) + 1, resultlen); goto end; } } } } } strcpy (result, defaultresult);
end: while (head->next != NULL) { t = head->next; head->next = t->next; free (t); } free (head);
return 0; }