~ubuntu-branches/ubuntu/quantal/kdepimlibs/quantal-proposed

« back to all changes in this revision

Viewing changes to kcal/libical/src/test/testmime.c

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2006-09-06 22:45:39 UTC
  • Revision ID: james.westby@ubuntu.com-20060906224539-fiq8t03qdbqu7z3i
Tags: upstream-3.80.1
ImportĀ upstreamĀ versionĀ 3.80.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C -*-
 
2
  ======================================================================
 
3
  FILE: 
 
4
  CREATOR: eric 25 June 2000
 
5
  
 
6
  $Id: testmime.c 537971 2006-05-06 11:24:08Z kainhofe $
 
7
  $Locker:  $
 
8
    
 
9
 (C) COPYRIGHT 2000, Eric Busboom <eric@softwarestudio.org>, http://www.softwarestudio.org
 
10
 
 
11
 The contents of this file are subject to the Mozilla Public License
 
12
 Version 1.0 (the "License"); you may not use this file except in
 
13
 compliance with the License. You may obtain a copy of the License at
 
14
 http://www.mozilla.org/MPL/
 
15
 
 
16
 Software distributed under the License is distributed on an "AS IS"
 
17
 basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 
18
 the License for the specific language governing rights and
 
19
 limitations under the License.
 
20
 
 
21
 
 
22
 This program is free software; you can redistribute it and/or modify
 
23
 it under the terms of either: 
 
24
 
 
25
    The LGPL as published by the Free Software Foundation, version
 
26
    2.1, available at: http://www.fsf.org/copyleft/lesser.html
 
27
 
 
28
  Or:
 
29
 
 
30
    The Mozilla Public License Version 1.0. You may obtain a copy of
 
31
    the License at http://www.mozilla.org/MPL/
 
32
 
 
33
  The Initial Developer of the Original Code is Eric Busboom
 
34
 
 
35
 (C) COPYRIGHT 2000, Eric Busboom, http://www.softwarestudio.org
 
36
 ======================================================================*/
 
37
 
 
38
#include "ical.h"
 
39
 
 
40
#include <stdlib.h> /* For rand */
 
41
#include <string.h> /* for strrchr, strdup*/
 
42
#include <unistd.h>   /* for getopt */
 
43
 
 
44
/*int sspm_parse_mime(struct sspm_part *parts, 
 
45
               size_t max_parts,
 
46
               struct sspm_action_map *actions,
 
47
               char* (*get_string)(char *s, size_t size, void* data),
 
48
               void *get_string_data,
 
49
               struct sspm_header *first_header
 
50
    );
 
51
*/
 
52
 
 
53
 
 
54
 
 
55
char* major_type_string[] = {
 
56
    "TEXT",
 
57
    "IMAGE",
 
58
    "AUDIO",
 
59
    "VIDEO",
 
60
    "APPLICATION",
 
61
    "MULTIPART",
 
62
    "MESSAGE",
 
63
    "UNKNOWN",
 
64
    "NO"
 
65
};
 
66
 
 
67
char* minor_type_string[] = {
 
68
    "ANY",
 
69
    "PLAIN",
 
70
    "RFC822",
 
71
    "DIGEST",
 
72
    "CALENDAR",
 
73
    "MIXED",
 
74
    "RELATED",
 
75
    "ALTERNATIVE",
 
76
    "PARALLEL",
 
77
    "UNKNOWN",
 
78
    "NO"
 
79
};
 
80
 
 
81
 
 
82
char* read_stream(char *s, size_t size, void *d)
 
83
{
 
84
    char *c = fgets(s,size, (FILE*)d);
 
85
 
 
86
    return c;
 
87
 
 
88
}
 
89
 
 
90
 
 
91
int main(int argc, char* argv[]) {
 
92
 
 
93
    FILE *f;
 
94
    int c;
 
95
    extern char *optarg;
 
96
    extern int optind, optopt;
 
97
    int errflg=0;
 
98
    char* program_name;
 
99
 
 
100
    struct options{
 
101
            int normal;
 
102
            int stress;
 
103
            int base64;
 
104
            int qp;
 
105
            int sleep;
 
106
            int count;
 
107
            char* input_file;
 
108
    } opt;
 
109
 
 
110
    memset(&opt,0,sizeof(opt));
 
111
    
 
112
    program_name = (char*)strrchr((char*)argv[0],'/');
 
113
    program_name++;
 
114
 
 
115
    while ((c = getopt(argc, argv, "nsbqi:S:c:")) != -1) {
 
116
        switch (c) {
 
117
            case 'i': { /* Input comes from named file */
 
118
                opt.input_file = strdup(optarg);
 
119
                break;
 
120
            }
 
121
            case 'n':{ /* Normal */
 
122
 
 
123
                if(opt.stress+opt.base64+opt.qp != 0){
 
124
                    fprintf(stderr,
 
125
                            "%s: Use only one of  n,s,b and q\n",
 
126
                            program_name);
 
127
                }
 
128
                opt.normal = 1;
 
129
                break;
 
130
            }
 
131
            case 's':{ /* Stress-test*/
 
132
                if(opt.base64+opt.normal+opt.qp != 0){
 
133
                    fprintf(stderr,
 
134
                            "%s: Use only one of  n,s,b and q\n",
 
135
                            program_name);
 
136
                }
 
137
                opt.stress = 1;
 
138
                break;
 
139
            }
 
140
            case 'b':{ /* test base64 decoding*/
 
141
                if(opt.stress+opt.normal+opt.qp != 0){
 
142
                    fprintf(stderr,
 
143
                            "%s: Use only one of  n,s,b and q\n",
 
144
                            program_name);
 
145
                }
 
146
                opt.base64 = 1;
 
147
                break;
 
148
            }
 
149
            case 'q':{ /* test quoted-printable decoding*/
 
150
                if(opt.stress+opt.base64+opt.normal != 0){
 
151
                    fprintf(stderr,
 
152
                            "%s: Use only one of  n,s,b and q\n",
 
153
                            program_name);
 
154
                }
 
155
                opt.qp = 1;
 
156
                break;
 
157
            }
 
158
            case 'S':{ /* sleep at end of run */
 
159
                opt.sleep = atoi(optarg);
 
160
                break;
 
161
            }
 
162
 
 
163
            case 'c':{ /* number of iterations of stress test */
 
164
                opt.count = atoi(optarg);
 
165
                break;
 
166
            }
 
167
            
 
168
            case ':': {/* Option given without an operand */
 
169
                fprintf(stderr,
 
170
                        "%s: Option -%c requires an operand\n", 
 
171
                        program_name,optopt);
 
172
                errflg++;
 
173
                break;
 
174
            }
 
175
            case '?': {
 
176
                errflg++;
 
177
            }
 
178
        }
 
179
    }
 
180
    
 
181
    if (errflg >0){
 
182
        fprintf(stderr,"Usage: %s [-n|-s|-b|-q] [-i input_file]\n",
 
183
                program_name);
 
184
        exit(1);
 
185
    }
 
186
    
 
187
    if(opt.stress+opt.base64+opt.normal+opt.qp == 0){
 
188
        fprintf(stderr,
 
189
                "%s: Must have one of n,s,b or q\n",
 
190
                program_name);
 
191
    }
 
192
    
 
193
    if(opt.input_file){
 
194
        f = fopen(opt.input_file,"r");
 
195
        if (f == 0){
 
196
            fprintf(stderr,"Could not open input file \"%s\"\n",
 
197
                    opt.input_file);
 
198
            exit(1);
 
199
        }
 
200
    } else {
 
201
        f = stdin;
 
202
    }
 
203
        
 
204
        
 
205
 
 
206
    if(opt.normal == 1){
 
207
        icalcomponent *c;
 
208
 
 
209
        c = icalmime_parse(read_stream,f);
 
210
        
 
211
        printf("%s\n",icalcomponent_as_ical_string(c));
 
212
 
 
213
        icalcomponent_free(c);
 
214
 
 
215
    }  else if (opt.stress==1 ){
 
216
        /* Read file in by lines, then randomize the lines into a
 
217
           string buffer */
 
218
 
 
219
        char *array[1024];
 
220
        char temp[1024];
 
221
        char *buf;
 
222
        int i,last;
 
223
        int size;
 
224
        int non_rand;
 
225
        int rand_lines;
 
226
        int r;
 
227
        int j;
 
228
        icalcomponent *c;
 
229
        struct slg_data {
 
230
                char* pos;
 
231
                char* str;
 
232
        } d;
 
233
    
 
234
        for(i=0; !feof(f); i++){
 
235
            fgets(temp,1024,f);
 
236
            array[i] = strdup(temp);
 
237
            size += strlen(temp);
 
238
        }
 
239
        last = i;
 
240
 
 
241
        buf = malloc(size*2);
 
242
        assert(buf != 0);
 
243
        
 
244
 
 
245
        for(j=0; j<opt.count; j++){
 
246
 
 
247
            srand(j);
 
248
            memset(buf,0,size*2);
 
249
            /* First insert some non-randomized lines */
 
250
            non_rand = ((float)rand()/(float)RAND_MAX) * last;
 
251
            for(i=0;i<non_rand;i++){
 
252
                strcat(buf,array[i]);
 
253
            }
 
254
            
 
255
            /* Then, insert some lines at random */
 
256
            
 
257
            rand_lines = last - non_rand;
 
258
            
 
259
            for(i=0;i<rand_lines;i++){
 
260
                srand(i);
 
261
                r = ((float)rand()/(float)RAND_MAX) * rand_lines;
 
262
                strcat(buf,array[r+non_rand]);
 
263
                
 
264
            }
 
265
 
 
266
            d.pos = 0;
 
267
            d.str = buf;
 
268
 
 
269
            c = icalmime_parse(icalparser_string_line_generator,&d);
 
270
 
 
271
            printf("%s\n",icalcomponent_as_ical_string(c));
 
272
 
 
273
            icalcomponent_free(c);
 
274
            
 
275
        }
 
276
 
 
277
        free(buf);
 
278
 
 
279
        for(i=0; i<last; i++){
 
280
            free(array[i]);
 
281
        }
 
282
 
 
283
    } else if(opt.qp == 1){     
 
284
        char str[4096];
 
285
        char conv[4096];
 
286
        
 
287
        memset(str,0,4096);
 
288
        
 
289
        while(!feof(f) && fgets(str,4096,f)!=0){
 
290
            size_t size;
 
291
            
 
292
            size = strlen(str);
 
293
            memset(conv,0,4096);
 
294
            decode_quoted_printable(conv,str,&size);
 
295
            
 
296
            conv[size] = '\0';
 
297
            printf("%s",conv);
 
298
            memset(str,0,4096);
 
299
            
 
300
        }
 
301
    } else if (opt.base64 == 1) {
 
302
        char str[4096];
 
303
        char conv[4096];
 
304
        
 
305
        memset(str,0,4096);
 
306
        
 
307
        while(!feof(f) && fgets(str,4096,f)!=0){
 
308
            size_t size;
 
309
            
 
310
            size = strlen(str);
 
311
            memset(conv,0,4096);
 
312
            decode_base64(conv,str,&size);
 
313
            
 
314
            conv[size] = '\0';
 
315
            printf("%s",conv);
 
316
            memset(str,0,4096);
 
317
            
 
318
        }
 
319
    }
 
320
 
 
321
    if (opt.sleep != 0){
 
322
        sleep(opt.sleep);
 
323
    }
 
324
 
 
325
    if( opt.input_file != 0){
 
326
        free(opt.input_file);
 
327
    }
 
328
 
 
329
    icalmemory_free_ring();
 
330
 
 
331
    return 0;
 
332
 
 
333
}
 
334
 
 
335
 
 
336
 
 
337
 
 
338
 
 
339
        
 
340
        
 
341