Exemple #1
0
int test_sky_action_file_save() {
    int rc;
    struct tagbstring path = bsStatic("tmp/actions");
    
    // Initialize action file.
    sky_action_file *action_file = sky_action_file_create();
    sky_action_file_set_path(action_file, &path);
    
    // Action 1
    sky_action *action1 = sky_action_create();
    action1->name = bfromcstr("foo");
    rc = sky_action_file_add_action(action_file, action1);
    mu_assert_int_equals(rc, 0);
    mu_assert_int_equals(action_file->action_count, 1);

    // Action 2
    sky_action *action2 = sky_action_create();
    action2->name = bfromcstr("this_is_a_really_long_action_name_woohoo");
    rc = sky_action_file_add_action(action_file, action2);
    mu_assert_int_equals(rc, 0);
    mu_assert_int_equals(action_file->action_count, 2);

    // Save
    rc = sky_action_file_save(action_file);
    mu_assert_int_equals(rc, 0);
    mu_assert_file("tmp/actions", "tests/fixtures/action_files/0");

    sky_action_file_free(action_file);
    return 0;
}
Exemple #2
0
int sky_importer_process_action(sky_importer *importer, bstring source,
                                jsmntok_t *tokens, uint32_t *index)
{
    int rc;
    assert(importer != NULL);
    assert(source != NULL);
    assert(tokens != NULL);
    assert(index != NULL);

    jsmntok_t *action_token = &tokens[*index];
    (*index)++;
    
    // Create the action object.
    sky_action *action = sky_action_create(); check_mem(action);
    
    // Process over child tokens.
    int32_t i;
    for(i=0; i<(action_token->size/2); i++) {
        jsmntok_t *token = &tokens[*index];
        (*index)++;
        
        if(sky_importer_tokstr_equal(source, token, "name")) {
            action->name = sky_importer_token_parse_bstring(source, &tokens[*index]);
        }
        else {
            sentinel("Invalid token at char %d", tokens[*index].start);
        }
        (*index)++;
    }
    
    // Add action.
    if(!importer->table->opened) {
        check(sky_table_open(importer->table) == 0, "Unable to open table");
    }
    rc = sky_action_file_add_action(importer->table->action_file, action);
    check(rc == 0, "Unable to add action: %s", bdata(action->name));
    
    return 0;

error:
    return -1;
}
Exemple #3
0
// Loads actions from file.
//
// action_file - The action file to load.
//
// Returns 0 if successful, otherwise returns -1.
int sky_action_file_load(sky_action_file *action_file)
{
    int rc;
    size_t sz;
    uint32_t count = 0;
    FILE *file = NULL;
    sky_action **actions = NULL;
    assert(action_file != NULL);
    check(action_file->path != NULL, "Action file path required");

    // Unload any actions currently in memory.
    rc = sky_action_file_unload(action_file);
    check(rc == 0, "Unable to unload action file");

    // Read in actions file if it exists.
    if(sky_file_exists(action_file->path)) {
        file = fopen(bdata(action_file->path), "r");
        check(file, "Failed to open action file: %s",  bdata(action_file->path));

        // Read action count.
        count = minipack_fread_array(file, &sz);
        check(sz != 0, "Unable to read actions array at byte: %ld", ftell(file));

        // Allocate actions.
        if(count > 0) {
            actions = malloc(sizeof(sky_action*) * count);
            check_mem(actions);
        }

        // Read actions.
        uint32_t i;
        for(i=0; i<count; i++) {
            sky_action *action = sky_action_create();
            check_mem(action);

            // Read action id.
            action->id = (sky_action_id_t)minipack_fread_uint(file, &sz);
            check(sz != 0, "Unable to read action identifier at byte: %ld", ftell(file));

            // Read action name.
            rc = sky_minipack_fread_bstring(file, &action->name);
            check(rc == 0, "Unable to read action name at byte: %ld", ftell(file));

            // Append to array.
            action->action_file = action_file;
            actions[i] = action;
        }

        // Close the file.
        fclose(file);
    }

    // Store action list on action file.
    action_file->actions = actions;
    action_file->action_count = count;

    return 0;

error:
    if(file) fclose(file);
    if(actions) free(actions);
    return -1;
}