Esempio n. 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;
}
Esempio n. 2
0
int sky_importer_process_property(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 *property_token = &tokens[*index];
    (*index)++;
    
    // Create the property object.
    sky_property *property = sky_property_create(); check_mem(property);
    
    // Process over child tokens.
    int32_t i;
    for(i=0; i<(property_token->size/2); i++) {
        jsmntok_t *token = &tokens[*index];
        (*index)++;
        
        if(sky_importer_tokstr_equal(source, token, "type")) {
            bstring type = sky_importer_token_parse_bstring(source, &tokens[*index]);
            property->type = biseqcstr(type, "action") == 1 ? SKY_PROPERTY_TYPE_ACTION : SKY_PROPERTY_TYPE_OBJECT;
            bdestroy(type);
        }
        else if(sky_importer_tokstr_equal(source, token, "dataType")) {
            bstring data_type_str = sky_importer_token_parse_bstring(source, &tokens[*index]);
            property->data_type = sky_data_type_to_enum(data_type_str);
            bdestroy(data_type_str);
        }
        else if(sky_importer_tokstr_equal(source, token, "name")) {
            property->name = sky_importer_token_parse_bstring(source, &tokens[*index]);
        }
        else {
            sentinel("Invalid token at char %d", tokens[*index].start);
        }
        
        (*index)++;
    }
    
    // Add property.
    if(!importer->table->opened) {
        check(sky_table_open(importer->table) == 0, "Unable to open table");
    }
    rc = sky_property_file_add_property(importer->table->property_file, property);
    check(rc == 0, "Unable to add property: %s", bdata(property->name));
    
    return 0;

error:
    return -1;
}
Esempio n. 3
0
int sky_importer_process_table(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 *table_token = &tokens[*index];
    (*index)++;
    
    // Initialize import table.
    importer->table = sky_table_create(); check_mem(importer->table);
    importer->table->path = bstrcpy(importer->path);
    
    // Process over child tokens.
    int32_t i;
    for(i=0; i<(table_token->size/2); i++) {
        jsmntok_t *token = &tokens[*index];
        (*index)++;
        
        if(sky_importer_tokstr_equal(source, token, "blockSize")) {
            importer->table->default_block_size = (uint32_t)sky_importer_token_parse_int(source, &tokens[(*index)++]);
        }
        else if(sky_importer_tokstr_equal(source, token, "actions")) {
            rc = sky_importer_process_actions(importer, source, tokens, index);
            check(rc == 0, "Unable to process actions import");
        }
        else if(sky_importer_tokstr_equal(source, token, "properties")) {
            rc = sky_importer_process_properties(importer, source, tokens, index);
            check(rc == 0, "Unable to process properties import");
        }
        else if(sky_importer_tokstr_equal(source, token, "events")) {
            rc = sky_importer_process_events(importer, source, tokens, index);
            check(rc == 0, "Unable to process events import");
        }
        else {
            sentinel("Invalid token at char %d", tokens[*index].start);
        }
    }
    
    sky_table_close(importer->table);

    return 0;

error:
    return -1;
}
Esempio n. 4
0
// Processes the JSON tokens.
//
// importer - The importer.
// source   - The JSON source text.
// tokens   - The tokens.
//
// Returns 0 if successful, otherwise returns -1.
int sky_importer_process(sky_importer *importer, bstring source,
                         jsmntok_t *tokens)
{
    int rc;
    assert(importer != NULL);
    assert(source != NULL);
    assert(tokens != NULL);

    // Setup index to track current token and root token.
    uint32_t index = 1;
    jsmntok_t *root_token = &tokens[0];
    
    // Process over child tokens.
    int32_t i;
    for(i=0; i<root_token->size-1; i++) {
        jsmntok_t *token = &tokens[index];
        index++;
        
        if(sky_importer_tokstr_equal(source, token, "table")) {
            rc = sky_importer_process_table(importer, source, tokens, &index);
            check(rc == 0, "Unable to process table import");
        }
    }
    
    return 0;

error:
    return -1;
}
Esempio n. 5
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;
}