/* 去除字符串两端空格 */ char *strtrim(char *pstr) { char *p; p = strtrimr(pstr); return strtriml(p); }
const char * get_property_from_string (char *str, const char *propname, const char prop_sep, const char comment_sep) { static char value_s[MAXBUFSIZE]; char str_end[8], *p = NULL, buf[MAXBUFSIZE]; int len = strlen (str); if (len >= MAXBUFSIZE) len = MAXBUFSIZE - 1; memcpy (buf, str, len); buf[len] = 0; p = strtriml (buf); if (*p == comment_sep || *p == '\n' || *p == '\r') return NULL; // text after comment_sep is comment sprintf (str_end, "%c\r\n", comment_sep); if ((p = strpbrk (buf, str_end)) != NULL) // strip *any* returns and comments *p = 0; p = strchr (buf, prop_sep); if (p) { *p = 0; // note that this "cuts" _buf_ ... p++; } strtriml (strtrimr (buf)); if (!stricmp (buf, propname)) // ...because we do _not_ use strnicmp() { // if no divider was found the propname must be a bool config entry // (present or not present) if (p) { strncpy (value_s, p, MAXBUFSIZE)[MAXBUFSIZE - 1] = 0; strtriml (strtrimr (value_s)); } else strcpy (value_s, "1"); } else return NULL; return value_s; }
/** * * Removes trailing and leading blanks * * @param *pszStr pointer to the string * * @return returns a pointer to the trimmed string * */ char* strtrim(char *pszStr) { char *ret; /*-------------------------------------------------*/ ret = strtrimr(pszStr); /* Remove trailing blanks */ ret = strtriml(ret); /* Remove leading blanks */ return ret; }
char strtrims(char *str, const char trim) { // just a combination of strtriml and strtrimr if(strtriml(str,trim)) /* if return is 1 */ { strtrimr(str,trim); return 1; } if(!strtrimr(str,trim)) /* if return is 0 */ return 0; else /* if return is 1 */ return 1; }
static int parse_templ (const char *templ_file, char *ip) { int filled_in[MAXBUFSIZE]; static char buf[MAXBUFSIZE]; int i; memset (filled_in, 0, sizeof (filled_in)); for (i = 0; templ[i].name; i++) { char *p = NULL; p = (char *) get_property (templ_file, templ[i].name, PROPERTY_MODE_TEXT); if (!p) p = templ[i].def; strncpy (buf, p, MAXBUFSIZE)[MAXBUFSIZE - 1] = 0; p = strtriml (strtrimr (buf)); if (!(*p)) continue; memset (ip + templ[i].pos, ' ', templ[i].len); if ((int) strlen (p) > templ[i].len) { fprintf (stderr, "ERROR: Data for field \"%s\" is too long... stripping to %d chars\n", templ[i].name, templ[i].len); p[templ[i].len] = 0; } memcpy (ip + templ[i].pos, p, strlen (p)); if (templ[i].extra_check) if (templ[i].extra_check (ip + templ[i].pos, templ[i].len) == -1) return -1; filled_in[i] = 1; } for (i = 0; templ[i].name; i++) if (!filled_in[i]) { fprintf (stderr, "ERROR: Missing value for \"%s\"\n", templ[i].name); return -1; } return 0; }
int set_property (const char *filename, const char *propname, const char *value_s, const char *comment_s) { int found = 0, result = 0, file_size = 0; char line[MAXBUFSIZE], line2[MAXBUFSIZE], *str = NULL, *p = NULL, line_end[6]; FILE *fh; struct stat fstate; if (stat (filename, &fstate) != 0) file_size = fstate.st_size; if (!(str = (char *) malloc (file_size + MAXBUFSIZE))) return -1; sprintf (line_end, "%c\r\n", PROPERTY_COMMENT); *str = 0; if ((fh = fopen (filename, "r")) != 0) // opening the file in text mode { // avoids trouble under DOS // update existing properties while (fgets (line, sizeof line, fh) != NULL) { strcpy (line2, line); if ((p = strpbrk (line2, line_end))) *p = 0; // note that this "cuts" _line2_ p = strchr (line2, PROPERTY_SEPARATOR); if (p) *p = 0; strtriml (strtrimr (line2)); if (!stricmp (line2, propname)) { found = 1; if (value_s) sprintf (line, "%s%c%s\n", propname, PROPERTY_SEPARATOR, value_s ? value_s : "1"); } strcat (str, line); } fclose (fh); } // completely new properties are added at the bottom if (!found && value_s) { if (comment_s) { sprintf (strchr (str, 0), "%c\n%c ", PROPERTY_COMMENT, PROPERTY_COMMENT); for (p = strchr (str, 0); *comment_s; comment_s++) switch (*comment_s) { case '\r': break; case '\n': sprintf (strchr (str, 0), "\n%c ", PROPERTY_COMMENT); break; default: p = strchr (str, 0); *p = *comment_s; *(++p) = 0; break; } sprintf (strchr (str, 0), "\n%c\n", PROPERTY_COMMENT); } sprintf (line, "%s%c%s\n", propname, PROPERTY_SEPARATOR, value_s); strcat (str, line); } if ((fh = fopen (filename, "w")) == NULL) // open in text mode return -1; result = fwrite (str, 1, strlen (str), fh); fclose (fh); return result; }