Exemple #1
0
PJ_GridCatalog *pj_gc_readcatalog( projCtx ctx, const char *catalog_name )
{
    PAFile fid;
    PJ_GridCatalog *catalog;
    int entry_max;
    char line[302];
    
    fid = pj_open_lib( ctx, (char *) catalog_name, "r" );
    if (fid == NULL) 
        return NULL;

    /* discard title line */
    pj_ctx_fgets(ctx, line, sizeof(line)-1, fid);

    catalog = (PJ_GridCatalog *) calloc(1,sizeof(PJ_GridCatalog));
    if( !catalog )
        return NULL;
    
    catalog->catalog_name = strdup(catalog_name);
    
    entry_max = 10;
    catalog->entries = (PJ_GridCatalogEntry *) 
        malloc(entry_max * sizeof(PJ_GridCatalogEntry));
    
    while( pj_gc_readentry( ctx, fid, 
                            catalog->entries+catalog->entry_count) == 0)
    {
        catalog->entry_count++;
        
        if( catalog->entry_count == entry_max ) 
        {
            entry_max = entry_max * 2;
            catalog->entries = (PJ_GridCatalogEntry *) 
                realloc(catalog->entries, 
                        entry_max * sizeof(PJ_GridCatalogEntry));
            if (catalog->entries == NULL )
                return NULL;
        }
    }

    pj_gc_sortcatalog( ctx, catalog );

    return catalog;
}
Exemple #2
0
static int pj_gc_read_csv_line( projCtx ctx, PAFile fid, 
                                char **tokens, int max_tokens ) 
{
    char line[302];
   
    while( pj_ctx_fgets(ctx, line, sizeof(line)-1, fid) != NULL )
    {
        char *next = line;
        int token_count = 0;
        
        while( isspace(*next) ) 
            next++;
        
        /* skip blank and comment lines */
        if( next[0] == '#' || next[0] == '\0' )
            continue;
        
        while( token_count < max_tokens && *next != '\0' ) 
        {
            const char *start = next;
            
            while( *next != '\0' && *next != ',' ) 
                next++;
            
            if( *next == ',' )
            {
                *next = '\0';
                next++;
            }
            
            tokens[token_count++] = strdup(start);
        }

        return token_count;
    }
    
    return 0; 
}
Exemple #3
0
static char *get_init_string (PJ_CONTEXT *ctx, char *name) {
/***************************************************************************************
    Read a section of an init file. Return its contents as a plain character string.
    It is the duty of the caller to free the memory allocated for the string.
***************************************************************************************/
#define MAX_LINE_LENGTH 1000
    size_t current_buffer_size = 5 * (MAX_LINE_LENGTH + 1);
    char *fname, *section, *key;
    char *buffer = 0;
    char *line = 0;
    PAFile fid;
    size_t n;


    line = pj_malloc (MAX_LINE_LENGTH + 1);
    if (0==line)
        return 0;

    fname = pj_malloc (MAX_PATH_FILENAME+ID_TAG_MAX+3);
    if (0==fname) {
        pj_dealloc (line);
        return 0;
    }

    /* Support "init=file:section", "+init=file:section", and "file:section" format */
    key = strstr (name, "init=");
    if (0==key)
        key = name;
    else
        key += 5;
    if (MAX_PATH_FILENAME + ID_TAG_MAX + 2 < strlen (key)) {
        pj_dealloc (fname);
        pj_dealloc (line);
        return 0;
    }
    memmove (fname, key, strlen (key) + 1);

    /* Locate the name of the section we search for */
    section = strrchr(fname, ':');
    if (0==section) {
        proj_context_errno_set (ctx, PJD_ERR_NO_COLON_IN_INIT_STRING);
        pj_dealloc (fname);
        pj_dealloc (line);
        return 0;
    }
    *section = 0;
    section++;
    n = strlen (section);
    pj_log (ctx, PJ_LOG_TRACE,
            "get_init_string: searching for section [%s] in init file [%s]",
            section, fname);

    fid = pj_open_lib (ctx, fname, "rt");
    if (0==fid) {
        pj_dealloc (fname);
        pj_dealloc (line);
        proj_context_errno_set (ctx, PJD_ERR_NO_OPTION_IN_INIT_FILE);
        return 0;
    }

    /* Search for section in init file */
    for (;;) {

        /* End of file? */
        if (0==pj_ctx_fgets (ctx, line, MAX_LINE_LENGTH, fid)) {
            pj_dealloc (buffer);
            pj_dealloc (fname);
            pj_dealloc (line);
            pj_ctx_fclose (ctx, fid);
            proj_context_errno_set (ctx, PJD_ERR_NO_OPTION_IN_INIT_FILE);
            return 0;
        }

        /* At start of right section? */
        pj_chomp (line);
        if ('<'!=line[0])
            continue;
        if (strlen (line) < n + 2)
            continue;
        if (line[n + 1] != '>')
            continue;
        if (0==strncmp (line + 1, section, n))
            break;
    }

    /* We're at the first line of the right section - copy line to buffer */
    buffer = pj_malloc (current_buffer_size);
    if (0==buffer) {
        pj_dealloc (fname);
        pj_dealloc (line);
        pj_ctx_fclose (ctx, fid);
        return 0;
    }

    /* Skip the "<section>" indicator, and copy the rest of the line over */
    strcpy (buffer, line + strlen (section) + 2);

    /* Copy the remaining lines of the section to buffer */
    for (;;) {
        char *end_i_cator;
        size_t next_length, buffer_length;

        /* Did the section end somewhere in the most recently read line? */
        end_i_cator = strchr (buffer, '<');
        if (end_i_cator) {
            *end_i_cator = 0;
            break;
        }

        /* End of file? - done! */
        if (0==pj_ctx_fgets (ctx, line, MAX_LINE_LENGTH, fid))
            break;

        /* Otherwise, handle the line. It MAY be the start of the next section, */
        /* but that will be handled at the start of next trip through the loop  */
        buffer_length = strlen (buffer);
        pj_chomp (line);   /* Remove '#' style comments */
        next_length = strlen (line) + buffer_length + 2;
        if (next_length > current_buffer_size) {
            char *b = pj_malloc (2 * current_buffer_size);
            if (0==b) {
                pj_dealloc (buffer);
                buffer = 0;
                break;
            }
            strcpy (b, buffer);
            current_buffer_size *= 2;
            pj_dealloc (buffer);
            buffer = b;
        }
        buffer[buffer_length] = ' ';
        strcpy (buffer + buffer_length + 1, line);
    }

    pj_ctx_fclose (ctx, fid);
    pj_dealloc (fname);
    pj_dealloc (line);
    if (0==buffer)
        return 0;
    pj_shrink (buffer);
    pj_log (ctx, PJ_LOG_TRACE, "key=%s, value: [%s]", key, buffer);
    return buffer;
}