fork download
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. void itrim(char **val, size_t *len) {
  7. while (*val && **val && isspace(**val)) {
  8. (*val)++;
  9. (*len)--;
  10. }
  11.  
  12. while ((*len) && isspace((*val)[(*len) - 1])) {
  13. (*len)--;
  14. }
  15. }
  16.  
  17. void iuse(char *key, size_t klen, char *value, size_t vlen) {
  18. /* original values, original limits */
  19. printf("k: %.*s(%d)\n", (int) klen, key, (int) klen);
  20. printf("v: %.*s(%d)\n", (int) vlen, value, (int) vlen);
  21.  
  22. /* adjust values and limits in place */
  23. itrim(&key, &klen);
  24. itrim(&value, &vlen);
  25.  
  26. /* adjusted values and limits */
  27. printf("tk: %.*s(%d)\n", (int) klen, key, (int) klen);
  28. printf("tv: %.*s(%d)\n", (int) vlen, value, (int) vlen);
  29. }
  30.  
  31. int main(int argc, char **argv) {
  32. char str[] = "Foo : bar\n"; /* you have the header already */
  33. size_t len = strlen(str); /* and it's length */
  34.  
  35. char *split = strchr(str, ':');
  36.  
  37. if (split) {
  38. iuse(
  39. str,
  40. split - str,
  41. &str[split - str + 1],
  42. len - (split - str) - 1
  43. );
  44. }
  45.  
  46. return 0;
  47. }
Success #stdin #stdout 0s 10320KB
stdin
Standard input is empty
stdout
k: Foo (4)
v:  bar
(5)
tk: Foo(3)
tv: bar(3)