File indexing completed on 2018-03-02 18:44:59 UTC
view on githubraw file Latest commit e768bd12 on 2008-02-26 17:05:00 UTC
ec6cf3b09d Ed H*0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 #include <stdio.h>
e768bd1221 Jean*0030 #include <stdlib.h>
ec6cf3b09d Ed H*0031 #include <string.h>
0032
0033 #include "part.h"
0034 #include "xmalloc.h"
0035
0036 #define BUFSIZE 1024
0037 #define GROWBOUNDARY 20
0038
0039 static int pendingboundary(struct part *part);
0040
0041
0042
0043
0044
0045 struct part *part_init(FILE *infile)
0046 {
0047 static struct part zeropart;
0048 struct part *newpart;
0049
0050 newpart = (struct part *)xmalloc(sizeof(struct part));
0051 *newpart = zeropart;
0052 newpart->infile = infile;
0053 newpart->buf = (unsigned char *)xmalloc(BUFSIZE);
0054 newpart->buf_alloc = BUFSIZE;
0055
0056 return newpart;
0057 }
0058
0059
0060
0061
e768bd1221 Jean*0062 void part_close(struct part *part)
ec6cf3b09d Ed H*0063 {
0064 fclose(part->infile);
0065 if (part->buf) free(part->buf);
0066 if (part->boundary) free(part->boundary);
0067 }
0068
0069
0070
0071
0072 int
0073 part_depth(struct part *part)
0074 {
0075 return part->boundary_num;
0076 }
0077
0078
0079
0080
e768bd1221 Jean*0081 void part_addboundary(struct part *part, char *boundary)
ec6cf3b09d Ed H*0082 {
0083
0084 if (part->boundary_num == part->boundary_alloc) {
0085 part->boundary_alloc += GROWBOUNDARY;
0086 part->boundary = (char (*)[PART_MAX_BOUNDARY_LEN+1])
0087 xrealloc((char *)part->boundary,
0088 part->boundary_alloc * (PART_MAX_BOUNDARY_LEN+1));
0089 part->boundary_length = (int *)
0090 xrealloc((char *)part->boundary_length,
0091 part->boundary_alloc * sizeof(int));
0092 }
0093
0094 strncpy(part->boundary[part->boundary_num], boundary,
0095 PART_MAX_BOUNDARY_LEN);
0096 part->boundary[part->boundary_num][PART_MAX_BOUNDARY_LEN] = '\0';
0097 part->boundary_length[part->boundary_num] =
0098 strlen(part->boundary[part->boundary_num]);
0099 part->boundary_num++;
0100 if (part->boundary_seen+1 == part->boundary_num) {
0101 part->boundary_seen++;
0102 }
0103 }
0104
0105
0106
0107
0108
0109
0110
0111 int
0112 part_fill(struct part *part)
0113 {
0114
0115 part->cnt++;
0116
0117
0118 if (part->boundary_seen < part->boundary_num) return EOF;
0119
0120
0121 if (part->cnt == 0) {
0122 part->ptr = part->buf;
0123 part->cnt = fread(part->buf, 1, part->buf_alloc, part->infile);
0124 if (part->cnt == 0) {
0125 part->boundary_seen = 0;
0126 return EOF;
0127 }
0128 }
0129
0130
0131 if (part->ptr[0] == '\n' && pendingboundary(part)) {
0132 return EOF;
0133 }
0134
0135 part->cnt--;
0136 return *part->ptr++;
0137 }
0138
0139
0140
0141
0142
0143
0144
0145
0146 char *
0147 part_gets(char *s, int n, struct part *part)
0148 {
0149 int c;
0150 char *p = s;
0151
0152 if (n == 0) return 0;
0153 n--;
0154 while (n-- && (c = part_getc(part)) != EOF) {
0155 *p++ = c;
0156 if (c == '\n') break;
0157 }
0158 if (p == s) return 0;
0159 *p++ = '\0';
0160 return s;
0161 }
0162
0163
0164
0165
0166
0167
e768bd1221 Jean*0168 void part_ungets(char *s, struct part *part)
ec6cf3b09d Ed H*0169 {
0170 int len = strlen(s);
0171 int i;
0172
0173
0174 if (part->cnt + len + 1 > part->buf_alloc) {
0175 i = part->ptr - part->buf;
0176 part->buf_alloc = part->cnt + len + 1;
0177 part->buf = (unsigned char *)
0178 xrealloc((char *)part->buf, part->buf_alloc);
0179 part->ptr = part->buf + i;
0180 }
0181
0182
0183 if (len + 1 > part->ptr - part->buf) {
0184 for (i = part->cnt-1; i >= 0; i--) {
0185 part->buf[len+1+i] = part->ptr[i];
0186 }
0187 part->ptr = part->buf + len + 1;
0188 }
0189
0190
0191 part->ptr -= len;
0192 part->cnt += len;
0193 for (i = 0; i < len; i++) {
0194 part->ptr[i] = s[i];
0195 }
0196 }
0197
0198
0199
0200
0201
0202
0203 int
0204 part_readboundary(struct part *part)
0205 {
0206 int c;
0207 int sawfinal = 0;
0208
0209 if (part->boundary_seen < part->boundary_num-1) {
0210
0211
0212
0213 part->boundary_num--;
0214 return 1;
0215 }
0216
0217
0218 if (part->cnt == 0) return 1;
0219
0220
0221 part->ptr += part->boundary_length[part->boundary_seen] + 3;
0222 part->cnt -= part->boundary_length[part->boundary_seen] + 3;
0223 part->boundary_seen = part->boundary_num;
0224
0225
0226 c = part_getc(part);
0227 if (c == '-') {
0228 c = part_getc(part);
0229 if (c == '-') {
0230 sawfinal = 1;
0231 part->boundary_num--;
0232 }
0233 }
0234
0235
0236 while (c != '\n' && c != EOF) {
0237 c = part_getc(part);
0238 }
0239
0240 return sawfinal;
0241 }
0242
0243
0244
0245
0246
0247
0248 static int
0249 pendingboundary(struct part *part)
0250 {
0251 int bufleft;
0252 int i;
0253
0254
0255 if (part->cnt < 3 ||
0256 (part->cnt < PART_MAX_BOUNDARY_LEN+3 && part->ptr[1] == '-' &&
0257 part->ptr[2] == '-')) {
0258
0259 bufleft = part->buf_alloc - part->cnt - (part->ptr - part->buf);
0260
0261
0262 if (part->ptr!=part->buf && bufleft + part->cnt < PART_MAX_BOUNDARY_LEN+3) {
0263 for (i = 0; i < part->cnt; i++) {
0264 part->buf[i] = part->ptr[i];
0265 }
0266 part->ptr = part->buf;
0267 bufleft = part->buf_alloc - part->cnt;
0268 }
0269
0270
0271 part->cnt += fread(part->ptr+part->cnt, 1, bufleft, part->infile);
0272 }
0273
0274
0275 if (part->cnt < 3 || part->ptr[1] != '-' || part->ptr[2] != '-') {
0276 return 0;
0277 }
0278
0279 for (i = 0; i < part->boundary_num; i++) {
0280 if (part->cnt - 3 >= part->boundary_length[i] &&
0281 !strncmp((char *)part->ptr+3, part->boundary[i],
0282 part->boundary_length[i])) {
0283 break;
0284 }
0285 }
0286
0287 if (i == part->boundary_num) return 0;
0288
0289
0290 part->boundary_seen = i;
0291 return 1;
0292 }