示例#1
0
int main(int argc, char *argv[]) {
    char option = '\0';
    char *filepath = NULL;
    unsigned int length = 0;
    int *array = NULL;
    int *original_array = NULL;
	struct sorting_stats ops;
    /* parse the filepath given in command line arguments */
    filepath = parse_filepath(argc, argv);

    /* parse the array given in the input file */
    array = array_from_file(filepath, &length);

    /* save a copy of array used to make some checks later */
    original_array = array_duplicate(array, length);

    /* print a simple menu and do the actual sorting */
    do {
        option = print_menu();
        switch (option) {
        case INSERTION_SORT:
            ops = insertion_sort(array, length);
            break;
        case SELECTION_SORT:
            ops = selection_sort(array, length);
            break;
        case QUICK_SORT:
            ops = quick_sort(array, length);
            break;
        case EXIT:
            printf("Exiting.\n");
            return (EXIT_SUCCESS);
        default:
            printf("\n\"%c\" is invalid. Please choose a valid option."
                   "\n\n", option);
        }
    } while (!is_valid_option(option));

    /* show the ordered array in the screen */
    array_dump(array, length);
    
    // show the comparation of the algorithm
    printf("Comparisons: %d \n",(int)ops.comps);
    printf("Swaps: %d \n",(int)ops.swaps);
    
    /* check if it is sorted */
    assert(array_is_sorted(array, length));

    /* check if it is a permutation of original */
    assert(array_is_permutation_of(array, original_array, length));

    /* destroy array */
    array_destroy(array);
    array_destroy(original_array);

    return (EXIT_SUCCESS);
}
示例#2
0
/**
 * Accept the current filter settings.
 */
static void filter_accept_rule(void)
{
	if (filter_last_selected)
	{
		/* Store the DestFolder */
		free(filter_last_selected->dest_folder); /* Safe to call with NULL */
		filter_last_selected->dest_folder = mystrdup((char*)xget(filter_move_text,MUIA_Text_Contents));
		filter_last_selected->use_dest_folder = xget(filter_move_check,MUIA_Selected);

		/* Store the ARexxfile */
		free(filter_last_selected->arexx_file);
		filter_last_selected->arexx_file = mystrdup((char*)xget(filter_arexx_string,MUIA_String_Contents));
		filter_last_selected->use_arexx_file = xget(filter_arexx_check,MUIA_Selected);

		/* Store the Soundfile */
		free(filter_last_selected->sound_file);
		filter_last_selected->sound_file = mystrdup((char*)xget(filter_sound_string,MUIA_String_Contents));
		filter_last_selected->use_sound_file = xget(filter_sound_check,MUIA_Selected);

		/* Store the flags */
		filter_last_selected->flags = 0;

		if (xget(filter_request_check, MUIA_Selected)) filter_last_selected->flags |= FILTER_FLAG_REQUEST;
		if (xget(filter_new_check, MUIA_Selected)) filter_last_selected->flags |= FILTER_FLAG_NEW;
		if (xget(filter_sent_check, MUIA_Selected)) filter_last_selected->flags |= FILTER_FLAG_SENT;
		if (xget(filter_remote_check, MUIA_Selected)) filter_last_selected->flags |= FILTER_FLAG_REMOTE;

		/* Go though all objects of the rule_rule_group and build a new
       rule list from it */
		{
		  struct List *child_list = (struct List*)xget(filter_rule_group,MUIA_Group_ChildList);
		  Object *cstate = (Object *)child_list->lh_Head;
		  Object *child;

		  while ((child = (Object*)NextObject(&cstate)))
		  {
		  	struct filter_rule *fr = (struct filter_rule*)xget(child,MUIA_UserData);
		  	if (fr)
		  	{
		  		struct filter_rule *new_fr = (struct filter_rule*)xget(child,MUIA_FilterRule_Data);

					/* free all strings of the rule */
					switch (fr->type)
					{
						case	RULE_FROM_MATCH:
						    	array_free(fr->u.from.from);
						    	array_free(fr->u.from.from_pat);
						    	break;
						case	RULE_RCPT_MATCH:
						    	array_free(fr->u.rcpt.rcpt);
						    	array_free(fr->u.rcpt.rcpt_pat);
						    	break;
						case	RULE_SUBJECT_MATCH:
						    	array_free(fr->u.subject.subject);
						    	array_free(fr->u.subject.subject_pat);
						    	break;
						case	RULE_HEADER_MATCH:
						    	if (fr->u.header.name) free(fr->u.header.name);
						    	if (fr->u.header.name_pat) free(fr->u.header.name_pat);
						    	array_free(fr->u.header.contents);
						    	array_free(fr->u.header.contents_pat);
						    	break;
						case	RULE_BODY_MATCH:
						    	if (fr->u.body.body) free(fr->u.body.body);
						    	filter_deinit_rule(&fr->u.body.body_parsed);
						    	break;
					}

					/* clear all the memory. the node it self should not be cleared, so it stays
					   on the same position */
					memset(((char*)fr)+sizeof(struct node),0,sizeof(struct filter_rule)-sizeof(struct node));

					/* now copy over the new settings */
					fr->type = new_fr->type;
					fr->flags = new_fr->flags;
					switch (new_fr->type)
					{
						case	RULE_FROM_MATCH:
									fr->u.from.from = array_duplicate(new_fr->u.from.from);
									break;
						case	RULE_RCPT_MATCH:
									fr->u.rcpt.rcpt = array_duplicate(new_fr->u.rcpt.rcpt);
									break;
						case	RULE_SUBJECT_MATCH:
									fr->u.subject.subject = array_duplicate(new_fr->u.subject.subject);
									break;
						case	RULE_HEADER_MATCH:
									fr->u.header.name = mystrdup(new_fr->u.header.name);
									fr->u.header.contents = array_duplicate(new_fr->u.header.contents);
									break;
						case	RULE_STATUS_MATCH:
									fr->u.status.status = new_fr->u.status.status;
									break;
					}
		  	}
		  }
		}
		filter_parse_filter_rules(filter_last_selected);
	}
}