Example #1
0
int main(int argc, char* argv[]) {

    FILE *f;
    int c;
    extern char *optarg;
    extern int optind, optopt;
    int errflg=0;
    char* program_name;

    struct options{
	    int normal;
	    int stress;
	    int base64;
	    int qp;
	    int sleep;
	    int count;
	    char* input_file;
    } opt;

    memset(&opt,0,sizeof(opt));
    
    program_name = (char*)strrchr((char*)argv[0],'/');
    program_name++;

    while ((c = getopt(argc, argv, "nsbqi:S:c:")) != -1) {
	switch (c) {
	    case 'i': { /* Input comes from named file */
		opt.input_file = strdup(optarg);
		break;
	    }
	    case 'n':{ /* Normal */

		if(opt.stress+opt.base64+opt.qp != 0){
		    fprintf(stderr,
			    "%s: Use only one of  n,s,b and q\n",
			    program_name);
		}
		opt.normal = 1;
		break;
	    }
	    case 's':{ /* Stress-test*/
		if(opt.base64+opt.normal+opt.qp != 0){
		    fprintf(stderr,
			    "%s: Use only one of  n,s,b and q\n",
			    program_name);
		}
		opt.stress = 1;
		break;
	    }
	    case 'b':{ /* test base64 decoding*/
		if(opt.stress+opt.normal+opt.qp != 0){
		    fprintf(stderr,
			    "%s: Use only one of  n,s,b and q\n",
			    program_name);
		}
		opt.base64 = 1;
		break;
	    }
	    case 'q':{ /* test quoted-printable decoding*/
		if(opt.stress+opt.base64+opt.normal != 0){
		    fprintf(stderr,
			    "%s: Use only one of  n,s,b and q\n",
			    program_name);
		}
		opt.qp = 1;
		break;
	    }
	    case 'S':{ /* sleep at end of run */
		opt.sleep = atoi(optarg);
		break;
	    }

	    case 'c':{ /* number of iterations of stress test */
		opt.count = atoi(optarg);
		break;
	    }
	    
	    case ':': {/* Option given without an operand */
		fprintf(stderr,
			"%s: Option -%c requires an operand\n", 
			program_name,optopt);
		errflg++;
		break;
	    }
	    case '?': {
		errflg++;
	    }
	}
    }
    
    if (errflg >0){
	fprintf(stderr,"Usage: %s [-n|-s|-b|-q] [-i input_file]\n",
		program_name);
	exit(1);
    }
    
    if(opt.stress+opt.base64+opt.normal+opt.qp == 0){
	fprintf(stderr,
		"%s: Must have one of n,s,b or q\n",
		program_name);
    }
    
    if(opt.input_file){
	f = fopen(opt.input_file,"r");
	if (f == 0){
	    fprintf(stderr,"Could not open input file \"%s\"\n",
		    opt.input_file);
	    exit(1);
	}
    } else {
	f = stdin;
    }
	
 	

    if(opt.normal == 1){
	icalcomponent *c;

	c = icalmime_parse(read_stream,f);
	
	printf("%s\n",icalcomponent_as_ical_string(c));

	icalcomponent_free(c);

    }  else if (opt.stress==1 ){
	/* Read file in by lines, then randomize the lines into a
           string buffer */

	char *array[1024];
	char temp[1024];
       	char *buf;
	int i,last;
	int size = 0;
	int non_rand;
	int rand_lines;
	int r;
	int j;
	icalcomponent *c;
	struct slg_data {
		char* pos;
		char* str;
	} d;
    
	for(i=0; !feof(f); i++){
	    fgets(temp,1024,f);
	    array[i] = strdup(temp);
	    size += strlen(temp);
	}
	last = i;
 
	buf = malloc(size*2);
	assert(buf != 0);
	

	for(j=0; j<opt.count; j++){

	    srand(j);
	    memset(buf,0,size*2);
	    /* First insert some non-randomized lines */
	    non_rand = ((float)rand()/(float)RAND_MAX) * last;
	    for(i=0;i<non_rand;i++){
		strcat(buf,array[i]);
	    }
	    
	    /* Then, insert some lines at random */
	    
	    rand_lines = last - non_rand;
	    
	    for(i=0;i<rand_lines;i++){
		srand(i);
		r = ((float)rand()/(float)RAND_MAX) * rand_lines;
		strcat(buf,array[r+non_rand]);
		
	    }

	    d.pos = 0;
	    d.str = buf;

	    c = icalmime_parse(icalparser_string_line_generator,&d);

	    printf("%s\n",icalcomponent_as_ical_string(c));

	    icalcomponent_free(c);
	    
	}

	free(buf);

	for(i=0; i<last; i++){
	    free(array[i]);
	}

    } else if(opt.qp == 1){	
	char str[4096];
	char conv[4096];
	
	memset(str,0,4096);
	
	while(!feof(f) && fgets(str,4096,f)!=0){
	    size_t size;
	    
	    size = strlen(str);
	    memset(conv,0,4096);
	    decode_quoted_printable(conv,str,&size);
	    
	    conv[size] = '\0';
	    printf("%s",conv);
	    memset(str,0,4096);
	    
	}
    } else if (opt.base64 == 1) {
	char str[4096];
	char conv[4096];
	
	memset(str,0,4096);
	
	while(!feof(f) && fgets(str,4096,f)!=0){
	    size_t size;
	    
	    size = strlen(str);
	    memset(conv,0,4096);
	    decode_base64(conv,str,&size);
	    
	    conv[size] = '\0';
	    printf("%s",conv);
	    memset(str,0,4096);
	    
	}
    }

    if (opt.sleep != 0){
#ifdef WIN32
        _sleep(opt.sleep*1000);
#else
	sleep(opt.sleep);
#endif
    }

    if(	opt.input_file != 0){
	free(opt.input_file);
    }

    icalmemory_free_ring();

    return 0;

}
Example #2
0
int main(int argc, char *argv[])
{
    icalset *cin;
    struct icaltimetype next;
    icalcomponent *itr;
    icalproperty *desc, *dtstart, *rrule;
    struct icalrecurrencetype recur;
    icalrecur_iterator* ritr;
    time_t tt;
    char* file;

    icalerror_set_error_state(ICAL_PARSE_ERROR, ICAL_ERROR_NONFATAL);

#ifndef WIN32
    signal(SIGALRM,sig_alrm);
#endif

    if (argc <= 1) {
        file = "../../test-data/recur.txt";
    } else if (argc == 2) {
        file = argv[1];
    } else {
        fprintf(stderr,"usage: recur [input file]\n");
        exit(1);
    }

#ifndef WIN32
    alarm(300); /* to get file lock */
#endif
    cin = icalfileset_new(file);
#ifndef WIN32
    alarm(0);
#endif

    if(cin == 0) {
        fprintf(stderr,"recur: can't open file %s\n",file);
        exit(1);
    }


    for (itr = icalfileset_get_first_component(cin);
            itr != 0;
            itr = icalfileset_get_next_component(cin)) {

        struct icaltimetype start = icaltime_from_timet(1,0);
        struct icaltimetype end = icaltime_today();



        desc = icalcomponent_get_first_property(itr,ICAL_DESCRIPTION_PROPERTY);
        dtstart = icalcomponent_get_first_property(itr,ICAL_DTSTART_PROPERTY);
        rrule = icalcomponent_get_first_property(itr,ICAL_RRULE_PROPERTY);

        if (desc == 0 || dtstart == 0 || rrule == 0) {
            printf("\n******** Error in input component ********\n");
            printf("The following component is malformed:\n %s\n",
                   icalcomponent_as_ical_string(itr));
            continue;
        }

        printf("\n\n#### %s\n",icalproperty_get_description(desc));
        printf("#### %s\n",icalvalue_as_ical_string(icalproperty_get_value(rrule)));
        recur = icalproperty_get_rrule(rrule);
        start = icalproperty_get_dtstart(dtstart);

        ritr = icalrecur_iterator_new(recur,start);

        tt = icaltime_as_timet(start);

        printf("#### %s\n",ctime(&tt ));

        icalrecur_iterator_free(ritr);

        for(ritr = icalrecur_iterator_new(recur,start),
                next = icalrecur_iterator_next(ritr);
                !icaltime_is_null_time(next);
                next = icalrecur_iterator_next(ritr)) {

            tt = icaltime_as_timet(next);

            printf("  %s",ctime(&tt ));

        }
        icalrecur_iterator_free(ritr);

        icalcomponent_foreach_recurrence(itr, start, end,
                                         recur_callback, NULL);



    }

    icalset_free(cin);

    icaltimezone_free_builtin_timezones();

    icalmemory_free_ring();

    free_zone_directory();

    return 0;
}