Esempio n. 1
0
/**
 * prelude_string_get_string_released:
 * @string: Pointer to a #prelude_string_t object.
 * @outptr: Pointer to an address where to store the released string.
 *
 * Get @string content, and release it so that further operation on
 * @string won't modify the returned content.
 *
 * Returns: 0 on success, or a negative value if an error occured.
 */
int prelude_string_get_string_released(prelude_string_t *string, char **outptr)
{
        prelude_return_val_if_fail(string, prelude_error(PRELUDE_ERROR_ASSERTION));
        *outptr = NULL;

        if ( ! string->index )
                return 0;

        if ( ! (string->flags & PRELUDE_STRING_OWN_DATA) ) {
                *outptr = strdup(string->data.robuf);
                return (*outptr) ? 0 : prelude_error_from_errno(errno);
        }

        if ( string->index + 1 <= string->index )
                return prelude_error(PRELUDE_ERROR_INVAL_LENGTH);

        *outptr = _prelude_realloc(string->data.rwbuf, string->index + 1);
        if ( ! *outptr )
                return prelude_error_from_errno(errno);

        string->size = 0;
        string->index = 0;
        string->data.rwbuf = NULL;

        return 0;
}
Esempio n. 2
0
static int op_delete_line(config_t *cfg, unsigned int start, unsigned int end)
{
        unsigned int i, j;

        if ( ! cfg->elements )
                return 0;

        if ( start >= end || end > cfg->elements )
                return -1;

        for ( i = start; i < end; i++ ) {
                free(cfg->content[i]);
                cfg->content[i] = NULL;
        }

        for ( i = end, j = start; i < cfg->elements; i++ )
                cfg->content[j++] = cfg->content[i];

        cfg->elements -= end - start;

        cfg->content = _prelude_realloc(cfg->content, cfg->elements * sizeof(char **));
        if ( ! cfg->content )
                return prelude_error_from_errno(errno);

        return 0;
}
Esempio n. 3
0
static int allocate_more_chunk_if_needed(prelude_string_t *s, size_t needed_len)
{
        int ret;
        char *ptr;
        size_t len;

        if ( ! needed_len )
                len = BUFFER_CHUNK_SIZE;
        else
                len = MAX(needed_len - (s->size - s->index), s->size * BUFFER_GROWTH_FACTOR);

        if ( s->size + len < s->size )
                return prelude_error(PRELUDE_ERROR_INVAL_LENGTH);

        if ( s->flags & PRELUDE_STRING_OWN_DATA ) {
                ptr = _prelude_realloc(s->data.rwbuf, s->size + len);
                if ( ! ptr )
                        return prelude_error_from_errno(errno);

                s->size += len;
                s->data.rwbuf = ptr;
        }

        else {
                ret = make_string_own(s, s->size + len);
                if ( ret < 0 )
                        return ret;
        }

        return 0;
}
Esempio n. 4
0
/*
 * Appends a line to an array of line.
 * Takes the address of the array and the line to append as arguments.
 *
 * list must be NULL the first time this function is called,
 * in order to initialize indexing variable.
 */
static int op_append_line(config_t *cfg, char *line)
{
        if ( ! line )
                return 0;

        if ( cfg->elements + 1 < cfg->elements )
                return -1;

        cfg->elements++;

        cfg->content = _prelude_realloc(cfg->content, cfg->elements * sizeof(char **));
        if ( ! cfg->content )
                return prelude_error_from_errno(errno);

        cfg->content[cfg->elements - 1] = line;

        return 0;
}
Esempio n. 5
0
static int op_insert_line(config_t *cfg, char *line, unsigned int lins)
{
        unsigned int i;

        if ( lins > cfg->elements || ! line )
                return -1;

        if ( cfg->elements + 1 < cfg->elements )
                return -1;

        cfg->content = _prelude_realloc(cfg->content, ++cfg->elements * sizeof(char **));
        if ( ! cfg->content )
                return prelude_error_from_errno(errno);

        for ( i = cfg->elements - 2; i >= lins; i-- ) {
                cfg->content[i + 1] = cfg->content[i];
                if ( i == 0 )
                        break;
        }

        cfg->content[lins] = line;

        return 0;
}