Exemplo n.º 1
0
int main(int argc, char *argv[])
{
    FILE *input, *log;
    int min_word_len = 0;
    bag_t *index;
    clock_t ticks;

    /* First, check that there is a first command line argument and
     * that it is the name of a file that can be opened for reading. */
    if (argc <= 1 || ! (input = fopen(argv[1], "r"))) {
        fprintf(stderr,
                "ERROR: missing or incorrect argument!\n"
                "USAGE: %s <filename> [minimum_word_length]\n"
                "  . <filename> is the name of a text file (required)\n"
                "  . [minimum_word_length] is a positive integer (optional)\n",
                argv[0]);
        exit(EXIT_FAILURE);
    }
    /* If we get here, the file has been opened for reading. */


    /* Next, check if there is a second command line argument to specify
     * a minimum word length. */
    if (argc < 3 || (min_word_len = (int) strtol(argv[2], NULL, 10)) <= 0)
        min_word_len = MIN_WORD_LEN;
    /* If we get here, the minimum word length has a positive value. */

    //creat or append to a runtime log file
    log = fopen("runtime_log.txt", "a");
    fprintf(log, "For %s and word %d characters and larger:\n", argv[1], min_word_len);

    /* Next, generate the index, close the input file (because we're done with
     * it at this point), and print timing data. */
    ticks = clock();
    index = generate_index(input, min_word_len);
    ticks = clock() - ticks;
    fclose(input);
    fprintf(log, "Elapsed time for generating the index: %gms\n",
                    1000.0 * ticks / CLOCKS_PER_SEC);
    /* Timing data is printed on stderr so we can isolate it from the rest of
     * the output below, if desired. */

    /* Finally, print the index on stdout and clean up: free the memory
     * allocated for each index entry, then the memory for the index itself. */
    if (index) {

        // timing how long it takes to print the index
        ticks = clock();
        bag_traverse(index, entry_print);
        ticks = clock() - ticks;
        fprintf(log, "Elapsed time for printing the index: %gms\n",
                        1000.0 * ticks / CLOCKS_PER_SEC);

        // timing how long it takes to destroy the index
        ticks = clock();
        bag_traverse(index, entry_destroy);
        bag_destroy(index);
        ticks = clock() - ticks;
        fprintf(log, "Elapsed time for destroy the index: %gms\n\n",
                        1000.0 * ticks / CLOCKS_PER_SEC);
    }

    fclose(log);

    return EXIT_SUCCESS;
}
Exemplo n.º 2
0
/** Generate reStructuredText documentation for all ingestion definitions.
 * \ingroup harp_documentation
 * \param path Path to directory in which the documentation files will be written.
 * \return
 *   \arg \c 0, Success.
 *   \arg \c -1, Error occurred (check #harp_errno).
 */
LIBHARP_API int harp_doc_export_ingestion_definitions(const char *path)
{
    harp_ingestion_module_register *module_register;
    char *filename;
    int i;
    int j;

    if (path == NULL)
    {
        harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "path is NULL (%s:%u)", __FILE__, __LINE__);
        return -1;
    }

    if (harp_ingestion_init() != 0)
    {
        return -1;
    }

    module_register = harp_ingestion_get_module_register();
    assert(module_register != NULL);

    filename = (char *)malloc(scprintf("%s/index.rst", path) + 1);
    if (filename == NULL)
    {
        harp_set_error(HARP_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate % lu bytes) (%s:%u)",
                       scprintf("%s/index.rst", path) + 1, __FILE__, __LINE__);
        return -1;
    }

    sprintf(filename, "%s/index.rst", path);
    if (generate_index(filename, module_register->num_ingestion_modules, module_register->ingestion_module) != 0)
    {
        free(filename);
        return -1;
    }
    else
    {
        free(filename);
    }

    for (i = 0; i < module_register->num_ingestion_modules; i++)
    {
        harp_ingestion_module *ingestion_module = module_register->ingestion_module[i];

        for (j = 0; j < ingestion_module->num_product_definitions; j++)
        {
            harp_product_definition *product_definition = ingestion_module->product_definition[j];

            filename = (char *)malloc(scprintf("%s/%s.rst", path, product_definition->name) + 1);
            if (filename == NULL)
            {
                harp_set_error(HARP_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate % lu bytes) (%s:%u)",
                               scprintf("%s/%s.rst", path, product_definition->name) + 1, __FILE__, __LINE__);
                return -1;
            }

            sprintf(filename, "%s/%s.rst", path, product_definition->name);
            if (generate_product_definition(filename, product_definition) != 0)
            {
                free(filename);
                return -1;
            }
            else
            {
                free(filename);
            }
        }
    }

    return 0;
}