#include #include #include #ifdef DMALLOC #include #endif void str_rtrim(char *s) { // In-place trim char *p; p = s + strlen(s) - 1; while (isspace(*p)) { *p = 0; p--; } } void str_trimchars(char *s, char *chars) { int c; c = strspn(s, chars); // Initial trim portition memmove(s, s + c, strlen(s) - c); char *p = s + strlen(s) - 1; while (strchr(chars, *p)) *(p--) = '\0'; } int get_value(char *rawbuf, char *title, char *destbuf, int maxlen) { char *startp = rawbuf; while ((startp = strstr(startp, title)) != NULL) { if (*(startp - 1) == '\n' || startp == rawbuf) break; startp++; // Start search from next char } if (startp == NULL) return 0; char *valuestart = strstr(startp, "\t"); if (valuestart == NULL) return 0; int valuelen = strstr(valuestart, "\n") - valuestart; if (valuelen > maxlen) valuelen = maxlen; memcpy(destbuf, valuestart, valuelen); destbuf[valuelen] = '\0'; if (destbuf[valuelen - 1] == '\r') destbuf[valuelen - 1] = '\0'; return 1; } int get_field(char *buf, int indx) { // Get space separated integer field at indx int count = 0; char *p = buf; while (isspace(*p)) p++; while (*p != 0) { if (isspace(*p)) { count++; while (isspace(*p)) p++; } if (count == indx) { return atoi(p); } p++; } return -1; } void get_str_field(char *buf, int indx, char *dest, int buflen) { // Get space separated text field at indx int count = 0; char *p1 = buf, *p2 = dest, *endp = dest + buflen; while (isspace(*p1)) p1++; while (*p1 != 0) { if (isspace(*p1)) { count++; while (isspace(*p1) && *p1 != 0) p1++; } if (*p1 == 0) { break; } if (count == indx) { while (*p1 != 0 && p2 < endp - 1 && !isspace(*p1)) { *(p2++) = *(p1++); } *p2 = 0; return; } p1++; } dest[0] = 0; }