Beispiel #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);
}
Beispiel #2
0
struct page *
parse_page(FILE *f, char *file_path)
{
    char c;
    int parsed_header = 0;
    struct ut_str buffer;

    /* markdown vars */

    struct page *p = malloc(sizeof(struct page));

    parse_filepath(file_path, p);

    p->inherits = NULL;
    p->attr_top = NULL;
    str_init(&buffer);

    while ((c = fgetc(f)) != EOF) {
        if (!parsed_header && '-' == c && flook_ahead(f, "--", 2)) {
            parse_header(f, p);
            parsed_header = 1;
        }
        else {
            str_append(&buffer, c);
        }
    } 
    while (c != EOF);

    p->code = malloc(sizeof(char) * buffer.size + 1);
    memset(p->code, '\0', buffer.size + 1);

    if (MARKDOWN == p->page_type) {
        Document *doc = mkd_string(buffer.s, buffer.size + 1, 0);
        if (NULL != doc && mkd_compile(doc, 0) ) {
            char *html = NULL;
            int szdoc = mkd_document(doc, &html);
            strncpy(p->code, html, szdoc);
            mkd_cleanup(doc);

        }
    } else {
        strncpy(p->code, buffer.s, buffer.size);
    }
    str_free(&buffer);


    return p;
}