1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * $Id: getpart.c,v 1.10 2004/03/10 08:12:09 bagder Exp $
22 ***************************************************************************/
30 #define EAT_SPACE(ptr) while( ptr && *ptr && isspace((int)*ptr) ) ptr++
31 #define EAT_WORD(ptr) while( ptr && *ptr && !isspace((int)*ptr) && ('>' != *ptr)) ptr++
34 #define show(x) printf x
40 char *appendstring(char *string, /* original string */
41 char *buffer, /* to append */
42 size_t *stringlen, /* length of string */
43 size_t *stralloc) /* allocated size */
45 size_t len = strlen(buffer);
46 size_t needed_len = len + *stringlen;
48 if(needed_len >= *stralloc) {
50 size_t newsize = needed_len*2; /* get twice the needed size */
52 newptr = realloc(string, newsize);
60 strcpy(&string[*stringlen], buffer);
66 const char *spitout(FILE *stream,
68 const char *sub, int *size)
70 char buffer[8192]; /* big enough for anything */
71 char cmain[128]=""; /* current main section */
72 char csub[128]=""; /* current sub section */
86 } state = STATE_OUTSIDE;
88 string = (char *)malloc(stralloc);
92 string[0] = 0; /* zero first byte in case of no data */
94 while(fgets(buffer, sizeof(buffer), stream)) {
98 /* pass white spaces */
103 show(("=> %s", buffer));
104 string = appendstring(string, buffer, &stringlen, &stralloc);
105 show(("* %s\n", buffer));
114 /* end of a section */
122 if((state == STATE_INSUB) &&
123 !strcmp(csub, ptr)) {
124 /* this is the end of the currently read sub section */
126 csub[0]=0; /* no sub anymore */
129 else if((state == STATE_INMAIN) &&
130 !strcmp(cmain, ptr)) {
131 /* this is the end of the currently read main section */
133 cmain[0]=0; /* no main anymore */
138 /* this is the beginning of a section */
146 state = STATE_INMAIN;
157 string = appendstring(string, buffer, &stringlen, &stralloc);
158 show(("* %s\n", buffer));
161 if((STATE_INSUB == state) &&
162 !strcmp(cmain, main) &&
163 !strcmp(csub, sub)) {
164 show(("* (%d bytes) %s\n", stringlen, buffer));
165 display = 1; /* start displaying */
168 show(("%d (%s/%s): %s\n", state, cmain, csub, buffer));
169 display = 0; /* no display */
178 int main(int argc, char **argv)
181 printf("./moo main sub\n");
185 char *buffer = spitout(stdin, argv[1], argv[2], &size);