doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
getpart.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
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.
13  *
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.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * $Id: getpart.c,v 1.10 2004/03/10 08:12:09 bagder Exp $
22  ***************************************************************************/
23 
24 #include <stdio.h>
25 #include <ctype.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include "getpart.h"
29 
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++
32 
33 #ifdef DEBUG
34 #define show(x) printf x
35 #else
36 #define show(x)
37 #endif
38 
39 static
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 */
44 {
45  size_t len = strlen(buffer);
46  size_t needed_len = len + *stringlen;
47 
48  if(needed_len >= *stralloc) {
49  char *newptr;
50  size_t newsize = needed_len*2; /* get twice the needed size */
51 
52  newptr = realloc(string, newsize);
53  if(newptr) {
54  string = newptr;
55  *stralloc = newsize;
56  }
57  else
58  return NULL;
59  }
60  strcpy(&string[*stringlen], buffer);
61  *stringlen += len;
62 
63  return string;
64 }
65 
66 const char *spitout(FILE *stream,
67  const char *main,
68  const char *sub, int *size)
69 {
70  char buffer[8192]; /* big enough for anything */
71  char cmain[128]=""; /* current main section */
72  char csub[128]=""; /* current sub section */
73  char *ptr;
74  char *end;
75  char display = 0;
76 
77  char *string;
78  size_t stringlen=0;
79  size_t stralloc=256;
80 
81  enum {
82  STATE_OUTSIDE,
83  STATE_INMAIN,
84  STATE_INSUB,
85  STATE_ILLEGAL
86  } state = STATE_OUTSIDE;
87 
88  string = (char *)malloc(stralloc);
89  if(!string)
90  return NULL;
91 
92  string[0] = 0; /* zero first byte in case of no data */
93 
94  while(fgets(buffer, sizeof(buffer), stream)) {
95 
96  ptr = buffer;
97 
98  /* pass white spaces */
99  EAT_SPACE(ptr);
100 
101  if('<' != *ptr) {
102  if(display) {
103  show(("=> %s", buffer));
104  string = appendstring(string, buffer, &stringlen, &stralloc);
105  show(("* %s\n", buffer));
106  }
107  continue;
108  }
109 
110  ptr++;
111  EAT_SPACE(ptr);
112 
113  if('/' == *ptr) {
114  /* end of a section */
115  ptr++;
116  EAT_SPACE(ptr);
117 
118  end = ptr;
119  EAT_WORD(end);
120  *end = 0;
121 
122  if((state == STATE_INSUB) &&
123  !strcmp(csub, ptr)) {
124  /* this is the end of the currently read sub section */
125  state--;
126  csub[0]=0; /* no sub anymore */
127  display=0;
128  }
129  else if((state == STATE_INMAIN) &&
130  !strcmp(cmain, ptr)) {
131  /* this is the end of the currently read main section */
132  state--;
133  cmain[0]=0; /* no main anymore */
134  display=0;
135  }
136  }
137  else if(!display) {
138  /* this is the beginning of a section */
139  end = ptr;
140  EAT_WORD(end);
141 
142  *end = 0;
143  switch(state) {
144  case STATE_OUTSIDE:
145  strcpy(cmain, ptr);
146  state = STATE_INMAIN;
147  break;
148  case STATE_INMAIN:
149  strcpy(csub, ptr);
150  state = STATE_INSUB;
151  break;
152  default:
153  break;
154  }
155  }
156  if(display) {
157  string = appendstring(string, buffer, &stringlen, &stralloc);
158  show(("* %s\n", buffer));
159  }
160 
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 */
166  }
167  else {
168  show(("%d (%s/%s): %s\n", state, cmain, csub, buffer));
169  display = 0; /* no display */
170  }
171  }
172 
173  *size = stringlen;
174  return string;
175 }
176 
177 #ifdef TEST
178 int main(int argc, char **argv)
179 {
180  if(argc< 3) {
181  printf("./moo main sub\n");
182  }
183  else {
184  int size;
185  char *buffer = spitout(stdin, argv[1], argv[2], &size);
186  }
187  return 0;
188 }
189 #endif
#define strcmp
Definition: Str.h:41
#define EAT_SPACE(ptr)
Definition: getpart.c:30
#define main(x, y)
Definition: config-mac.h:9
#define EAT_WORD(ptr)
Definition: getpart.c:31
GLenum GLsizei const GLvoid * string
Definition: glext.h:3472
GLenum GLsizei len
Definition: glext.h:3472
GLuint GLuint end
Definition: glext.h:2845
#define NULL
Definition: Lib.h:88
GLuint buffer
Definition: glext.h:3108
#define show(x)
Definition: getpart.c:36
GLsizeiptr size
Definition: glext.h:3112
const char * spitout(FILE *stream, const char *main, const char *sub, int *size)
Definition: getpart.c:66