Example #1
0
int sky_importer_process_event(sky_importer *importer, bstring source,
                               jsmntok_t *tokens, uint32_t *index)
{
    int rc;
    check(importer != NULL, "Importer required");
    check(source != NULL, "Source required");
    check(tokens != NULL, "Tokens required");
    check(index != NULL, "Token index required");

    jsmntok_t *event_token = &tokens[*index];
    (*index)++;

    // Open table if it hasn't been already.
    if(!importer->table->opened) {
        check(sky_table_open(importer->table) == 0, "Unable to open table");
    }

    // Create the event object.
    sky_event *event = sky_event_create(0, 0, 0); check_mem(event);
        
    // Process over child tokens.
    int32_t i;
    for(i=0; i<(event_token->size/2); i++) {
        jsmntok_t *token = &tokens[*index];
        (*index)++;
        
        if(sky_importer_tokstr_equal(source, token, "timestamp")) {
            bstring timestamp = sky_importer_token_parse_bstring(source, &tokens[(*index)++]);
            rc = sky_timestamp_parse(timestamp, &event->timestamp);
            check(rc == 0, "Unable to parse timestamp");
            bdestroy(timestamp);
        }
        else if(sky_importer_tokstr_equal(source, token, "objectId")) {
            event->object_id = (sky_object_id_t)sky_importer_token_parse_int(source, &tokens[(*index)++]);
        }
        else if(sky_importer_tokstr_equal(source, token, "action")) {
            sky_action *action = NULL;
            bstring action_name = sky_importer_token_parse_bstring(source, &tokens[(*index)++]);
            rc = sky_action_file_find_action_by_name(importer->table->action_file, action_name, &action);
            check(rc == 0, "Unable to find action: %s", bdata(action_name));
            event->action_id = action->id;
        }
        else if(sky_importer_tokstr_equal(source, token, "data")) {
            rc = sky_importer_process_event_data(importer, event, source, tokens, index);
            check(rc == 0, "Unable to import event data");
        }
        else {
            sentinel("Invalid token at char %d", tokens[*index].start);
        }
    }
    
    // Add event.
    rc = sky_table_add_event(importer->table, event);
    check(rc == 0, "Unable to add event");
    
    return 0;

error:
    return -1;
}
Example #2
0
// Adds an action to an action file.
//
// action_file - The action file to add the action to.
// action      - The action to add to the action file.
//
// Returns 0 if successful, otherwise returns -1.
int sky_action_file_add_action(sky_action_file *action_file,
                               sky_action *action)
{
    assert(action_file != NULL);
    assert(action != NULL);
    check(action->id == 0, "Action ID must be zero");
    check(action->action_file == NULL, "Action ID must be zero");
    
    // Make sure an action with that name doesn't already exist.
    sky_action *_action;
    int rc = sky_action_file_find_action_by_name(action_file, action->name, &_action);
    check(rc == 0 && _action == NULL, "Action already exists with the same name");
    
    // Link to action file.
    action->action_file = action_file;

    // Increment action identifier.
    if(action_file->action_count == 0) {
        action->id = 1;
    }
    else {
        action->id = action_file->actions[action_file->action_count-1]->id + 1;
    }
    
    // Append action to list.
    action_file->action_count++;
    action_file->actions = realloc(action_file->actions, sizeof(sky_action*) * action_file->action_count);
    check_mem(action_file->actions);
    action_file->actions[action_file->action_count-1] = action;
    
    return 0;

error:
    return -1;
}