Example #1
0
/* Delete the given section of src string and put the rest in str, pointers can
    be same, negative numbers will wrap from the end */
void str_delete(str_t *str, const str_t *src, int pos, int count)
{
    str_t ans = str_init(NULL, 0);

    if (pos <= 0)
        pos += (int)src->len;

    if ((pos >= 0) && (pos + count <= (int)src->len) && (count >= 0) &&
        str_resize(&ans, src->len - count + 1))
    {
        memcpy(ans.c_str, src->c_str, pos);
        memcpy(ans.c_str + pos, src->c_str + pos + count,
            src->len - pos - count);
        ans.len = src->len - count;
        ans.c_str[ans.len] = '\0';
    }

    str_free(str);
    str->c_str = ans.c_str;
    str->len = ans.len;
    str->size = ans.size;
#ifdef STR_CACHE_HASH
	str->hash = 0;
#endif
}
Example #2
0
File: string.c Project: vegard/jato
static int str_vprintf(struct string *str, unsigned long offset,
		       const char *fmt, va_list args)
{
	unsigned long size;
	va_list tmp_args;
	int nr, err = 0;

  retry:
	size = str_remaining(str);
	va_copy(tmp_args, args);
	nr = vsnprintf(str->value + offset, size, fmt, tmp_args);
	va_end(tmp_args);

	if ((unsigned long)nr >= size) {
		int err;
	
		err = str_resize(str, str->capacity * 2);
		if (err)
			goto out;
	
		str->length = offset;
		goto retry;
	}
	str->length += nr;

  out:
	return err;
}
Example #3
0
/* Take the substring of src and fill up str, pointers can be same, negative
    numbers will wrap from the end */
void str_substr(str_t *str, const str_t *src, int start, int end)
{
    str_t ans = str_init(NULL, 0);

    if (start < 0)
        start += (int)src->len;
    if (end <= 0)
        end += (int)src->len;

    if ((start >= 0) && (end >= 0) && (start <= (int)src->len) &&
        (end <= (int)src->len) && (end >= start) &&
        str_resize(&ans, end-start+1))
    {
        memcpy(ans.c_str, src->c_str + start, end-start);
        ans.c_str[end-start] = '\0';
        ans.len = end-start;
    }

    str_free(str);
    str->c_str = ans.c_str;
    str->len = ans.len;
    str->size = ans.size;
#ifdef STR_CACHE_HASH
	str->hash = 0;
#endif
}
Example #4
0
/* Turn the src into all upper case and return in str, pointers can be same */
void str_upper(str_t *str, const str_t *src)
{
    size_t i;
    if (str_resize(str, src->len + 1))
        for (i=0; i<=src->len; i++)
            str->c_str[i] = toupper(src->c_str[i]);
#ifdef STR_CACHE_HASH
	str->hash = 0;
#endif
}
Example #5
0
/* Fill up the string from another string */
void str_copy(str_t *str, const str_t *src)
{
    if (!str_resize(str, src->len + 1))
        return;

    memcpy(str->c_str, src->c_str, src->len + 1);
    str->len = src->len;
#ifdef STR_CACHE_HASH
	str->hash = src->hash;
#endif
}
Example #6
0
/* Fill up the string from a given character */
void str_char(str_t *str, const char ch)
{
    if (!str_resize(str, 2))
        return;

    str->c_str[0] = ch;
    str->c_str[1] = '\0';
    str->len = 1;
#ifdef STR_CACHE_HASH
	str->hash = 0;
#endif
}
Example #7
0
/* Append a single character to str (eg, for string building) */
void str_append_char(str_t *str, char c)
{
    if (str_resize(str, str->len + 1))
    {
		str->c_str[str->len] = c;
		++str->len;
		str->c_str[str->len] = '\0';

#ifdef STR_CACHE_HASH
	str->hash = 0;
#endif
    }
}
Example #8
0
/* Reset the string to empty */
void str_clr(str_t *str)
{
    if (str->c_str)
        X_FREE(str->c_str);
    str->c_str = NULL;
    str->len = str->size = 0;
#ifdef STR_CACHE_HASH
	str->hash = 0;
#endif

    if (str_resize(str, 1))
        str->c_str[0] = 0;
}
Example #9
0
static char *
stringDecode(
	const char *src, 
	MODPCodecType codec) {
	size_t srclen;
	char *dest, *fdest, *result;
	int d = -1;
	
	if(src == NULL)
		return NULL;
	
	int (*decode_func)(char*, const char*,int);
	size_t (*decode_len_func)(size_t);
	
	switch (codec) {
		case MODPCodecBase64:
			decode_func = modp_b64_decode;
			decode_len_func = b64_decode_len;
		break;
		case MODPCodecBase64W:
			decode_func = modp_b64w_decode;
			decode_len_func = b64w_decode_len;
		break;
		case MODPCodecBase16:
			decode_func = modp_b16_decode;
			decode_len_func = b16_decode_len;
		break;
		case MODPCodecBase2:
			decode_func = modp_b2_decode;
			decode_len_func = b2_decode_len;
		break;
		default:
			return NULL;
	}
	
	result = NULL;
	srclen = strlen(src);
	if (srclen) {
		if (NULL != (dest = malloc(decode_len_func(srclen) +1))) {
			if ( 0 < (d = decode_func(dest, src, srclen))) {
				if (NULL != (fdest = str_resize(dest, d))) {
					result = fdest;
				}
			}
			free(dest);
		}
	}
	
	return result;
}
Example #10
0
/* Fill up the string from a char* string of given size, 0 will autocalculate */
void str_fill(str_t *str, const char *buf, size_t len)
{
    if (len == 0)
        len = strlen(buf);

    if (!str_resize(str, len+1))
        return;

    memcpy(str->c_str, buf, len);
    str->c_str[len] = '\0';
    str->len = len;
#ifdef STR_CACHE_HASH
	str->hash = 0;
#endif
}
Example #11
0
File: string.c Project: vegard/jato
struct string *alloc_str(void)
{
	int err;
	struct string *str = malloc(sizeof *str);

	if (!str)
		return NULL;

	memset(str, 0, sizeof *str);

	err = str_resize(str, INITIAL_CAPACITY);
	if (err) {
		free(str);
		return NULL;
	}
	memset(str->value, 0, str->capacity);

	return str;
}
Example #12
0
/** Makes sure that there is space for lengthNeeded characters.
 *
 * @param str the string
 * @param lengthNeeded the length of the string to append
 */
static void str_fit(Str *str, size_t lengthNeeded)
{
	size_t spaceLeft;

	if (str->error)
		return;

	// if the string does not fit, allocate a multiple of step to make it fit
	spaceLeft = (u16) (str->buf_size - str_len(str) - 1);	// 1 for the ending 0
	if (spaceLeft < lengthNeeded)
	{
		if (str->step == 0)
		{
			str_free(str);
			return;
		}

		str_resize(str, (u16) ( str->buf_size + ((lengthNeeded - spaceLeft) / str->step + 1) * str->step));
	}
}
Example #13
0
/* Initialize a string */
str_t str_init(str_t *str, int initial_size)
{
    static str_t s;

    if (!str)
        str = &s;

    str->c_str = NULL;
    str->len = 0;
    str->size = 0;
	str->is_owner = 1;
#ifdef STR_CACHE_HASH
	str->hash = 0;
#endif

	if (initial_size)
	{
		if (str_resize(str, initial_size))
			str->c_str[0] = '\0';
	}

    return *str;
}