Guest User

Untitled

a guest
Aug 11th, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <limits.h>
  4. #include <string.h>
  5.  
  6. static void
  7. trim(char *dst, char *src)
  8. {
  9.     int i=0, j=0;
  10.  
  11.     while (isspace(src[i])) {
  12.         i++;
  13.     }
  14.     j = strlen(src) - 1;
  15.     while (isspace(src[j])) {
  16.         j--;
  17.     }
  18.     if (src[j] == ':') j--;
  19.     j++;
  20.     memcpy(dst, src + i, j - i);
  21.     dst[j - i] = '\0';
  22. }
  23.  
  24. int
  25. main()
  26. {
  27. #define HEADERS_MAX 100
  28.  
  29.     char headers[HEADERS_MAX][LINE_MAX];
  30.     char subheaders[HEADERS_MAX][LINE_MAX];
  31.  
  32.     char *header_map[LINE_MAX];
  33.     char *subheader_map[LINE_MAX];
  34.  
  35.     char line[LINE_MAX];
  36.     char header[LINE_MAX];
  37.     int i;
  38.     int subheader_done = 1;
  39.  
  40.     while (fgets(line, sizeof(line), stdin)) {
  41.         if (islower(line[0])) {
  42.             /* data */
  43.             char key[LINE_MAX];
  44.             int j = 0, start_x = -1;
  45.  
  46.             subheader_done = 1;
  47.             for (i=0; line[i]; i++) {
  48.                 if (!isspace(line[i])) {
  49.                     key[j++] = line[i];
  50.                     if (start_x < 0) start_x = i;
  51.                 } else if (j) {
  52.                     char *h = header_map[(start_x + i) / 2];
  53.                     char *sh = subheader_map[(start_x + i) / 2];
  54.                     key[j] = '\0';
  55.                     printf("%s: %s: %s\n", h, sh, key);
  56.                     j = 0;
  57.                     start_x = -1;
  58.                 }
  59.             }
  60.         } else if (isupper(line[0])) {
  61.             /* header */
  62.             subheader_done = 1;
  63.             strncpy(header, line, LINE_MAX);
  64.         } else if (isspace(line[0])) {
  65.             /* sub-header */
  66.             int nhdr = 0, hidx = 0;
  67.             char shdrbuf[LINE_MAX];
  68.  
  69.             if (subheader_done) {
  70.                 memset(subheader_map, 0, sizeof(char *) * LINE_MAX);
  71.                 memset(subheaders, 0, HEADERS_MAX * LINE_MAX);
  72.                 for (i=0; line[i]; i++) {
  73.                     if (!isspace(line[i])) {
  74.                         subheader_map[i] = subheaders[nhdr];
  75.                         shdrbuf[hidx++] = line[i];
  76.                     } else if (hidx) {
  77.                         shdrbuf[hidx] = '\0';
  78.                         trim(subheaders[nhdr], shdrbuf);
  79.                         hidx = 0;
  80.                         nhdr++;
  81.                     }
  82.                 }
  83.             } else {
  84.                 char key[LINE_MAX];
  85.                 int j = 0, start_x = -1;
  86.  
  87.                 for (i=0; line[i]; i++) {
  88.                     if (!isspace(line[i])) {
  89.                         key[j++] = line[i];
  90.                         if (start_x < 0) start_x = i;
  91.                     } else if (j) {
  92.                         char *h = subheader_map[(start_x + i) / 2];
  93.                         key[j] = '\0';
  94.                         /* concat subheaders */
  95.                         sprintf(h, "%s %s", h, key);
  96.                         j = 0;
  97.                         start_x = -1;
  98.                     }
  99.                 }
  100.             }
  101.             subheader_done = 0;
  102.         } else if (line[0] == '-') {
  103.             /* dashed line, mask for header */
  104.             int nhdr = 0, hidx = 0;
  105.             char hdrbuf[LINE_MAX];
  106.  
  107.             subheader_done = 1;
  108.             for (i=0; line[i]; i++) {
  109.                 if (!isspace(line[i])) {
  110.                     header_map[i] = headers[nhdr];
  111.                     hdrbuf[hidx++] = header[i];
  112.                 } else if (hidx) {
  113.                     hdrbuf[hidx] = '\0';
  114.                     trim(headers[nhdr], hdrbuf);
  115.                     hidx = 0;
  116.                     nhdr++;
  117.                 }
  118.             }
  119.         } else {
  120.             /* unknown */
  121.             printf("?\n");
  122.         }
  123.     }
  124.  
  125.     return 0;
  126. }
Add Comment
Please, Sign In to add comment