str.c revision 6a9f9a5101b665fd2ef80c9e048a5eace78e01ef
45312f52ff3a3d4c137447be4c7556500c2f8bf2Timo Sirainen/* Copyright (c) 2002-2003 Timo Sirainen */
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#include "lib.h"
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#include "buffer.h"
08d6658a4e2ec8104cd1307f6baa75fdb07a24f8Mark Washenberger#include "printf-upper-bound.h"
d5abbb932a0a598f002da39a8b3326643b1b5efcTimo Sirainen#include "str.h"
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#include <stdio.h>
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainenstring_t *str_new(pool_t pool, size_t initial_size)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen{
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen return buffer_create_dynamic(pool, initial_size);
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen}
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainenstring_t *t_str_new(size_t initial_size)
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen{
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen return str_new(pool_datastack_create(), initial_size);
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen}
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen
49e513d090753ccbf95560b2f3a21f081a5b6c51Timo Sirainenvoid str_free(string_t *str)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen{
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen buffer_free(str);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen}
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainenstatic void str_add_nul(string_t *str)
473080c7c0d25ddfdf77e7dfa0ba8f73c6c669d5Timo Sirainen{
473080c7c0d25ddfdf77e7dfa0ba8f73c6c669d5Timo Sirainen size_t len;
473080c7c0d25ddfdf77e7dfa0ba8f73c6c669d5Timo Sirainen
473080c7c0d25ddfdf77e7dfa0ba8f73c6c669d5Timo Sirainen len = str_len(str);
473080c7c0d25ddfdf77e7dfa0ba8f73c6c669d5Timo Sirainen buffer_write(str, len, "", 1);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
356303df200c991580bd24041996a070ad08c05eTimo Sirainen /* remove the \0 - we don't want to keep it */
473080c7c0d25ddfdf77e7dfa0ba8f73c6c669d5Timo Sirainen buffer_set_used_size(str, len);
49e513d090753ccbf95560b2f3a21f081a5b6c51Timo Sirainen}
49e513d090753ccbf95560b2f3a21f081a5b6c51Timo Sirainen
473080c7c0d25ddfdf77e7dfa0ba8f73c6c669d5Timo Sirainenchar *str_free_without_data(string_t *str)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen{
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen str_add_nul(str);
d9b73f3eb3d5f608dfd438cee89ce2b27547173fTimo Sirainen return buffer_free_without_data(str);
00bde9ae9eab9e720462bf6ec9a4dd85e88c3bbfTimo Sirainen}
00bde9ae9eab9e720462bf6ec9a4dd85e88c3bbfTimo Sirainen
00bde9ae9eab9e720462bf6ec9a4dd85e88c3bbfTimo Sirainenconst char *str_c(string_t *str)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen{
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen str_add_nul(str);
49e513d090753ccbf95560b2f3a21f081a5b6c51Timo Sirainen return buffer_get_data(str, NULL);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen}
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainenconst unsigned char *str_data(const string_t *str)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen{
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen return buffer_get_data(str, NULL);
d5abbb932a0a598f002da39a8b3326643b1b5efcTimo Sirainen}
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainenchar *str_c_modifyable(string_t *str)
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen{
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen str_add_nul(str);
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen return buffer_get_modifyable_data(str, NULL);
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen}
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainensize_t str_len(const string_t *str)
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen{
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen return buffer_get_used_size(str);
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen}
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainenvoid str_append(string_t *str, const char *cstr)
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen{
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen buffer_append(str, cstr, strlen(cstr));
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen}
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainenvoid str_append_n(string_t *str, const void *cstr, size_t max_len)
3b94ff5951db4d4eddb7a80ed4e3f61207202635Timo Sirainen{
66d2db642fe24d555d113ba463e446b038d476efTimo Sirainen size_t len;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen len = 0;
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen while (len < max_len && ((const char *)cstr)[len] != '\0')
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen len++;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen buffer_append(str, cstr, len);
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen}
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainenvoid str_append_c(string_t *str, unsigned char chr)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen{
49e513d090753ccbf95560b2f3a21f081a5b6c51Timo Sirainen buffer_append_c(str, chr);
22535a9e685e29214082878e37a267157044618eTimo Sirainen}
6fabfb7bbfd88d0c1de66981e52850f26067623bTimo Sirainen
22535a9e685e29214082878e37a267157044618eTimo Sirainenvoid str_append_str(string_t *dest, const string_t *src)
22535a9e685e29214082878e37a267157044618eTimo Sirainen{
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen const char *cstr;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen size_t len;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen cstr = buffer_get_data(src, &len);
356303df200c991580bd24041996a070ad08c05eTimo Sirainen buffer_append(dest, cstr, len);
49e513d090753ccbf95560b2f3a21f081a5b6c51Timo Sirainen}
6fabfb7bbfd88d0c1de66981e52850f26067623bTimo Sirainen
22535a9e685e29214082878e37a267157044618eTimo Sirainenvoid str_printfa(string_t *str, const char *fmt, ...)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen{
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen va_list args;
e15f1d736c225c7ce6f3d08a37c1b2ae66b57c50Timo Sirainen
e15f1d736c225c7ce6f3d08a37c1b2ae66b57c50Timo Sirainen va_start(args, fmt);
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen str_vprintfa(str, fmt, args);
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen va_end(args);
e15f1d736c225c7ce6f3d08a37c1b2ae66b57c50Timo Sirainen}
e15f1d736c225c7ce6f3d08a37c1b2ae66b57c50Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainenvoid str_vprintfa(string_t *str, const char *fmt, va_list args)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen{
356303df200c991580bd24041996a070ad08c05eTimo Sirainen char *buf;
49e513d090753ccbf95560b2f3a21f081a5b6c51Timo Sirainen int ret;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen va_list args2;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen size_t len, append_len;
356303df200c991580bd24041996a070ad08c05eTimo Sirainen
49e513d090753ccbf95560b2f3a21f081a5b6c51Timo Sirainen VA_COPY(args2, args);
49e513d090753ccbf95560b2f3a21f081a5b6c51Timo Sirainen
6fabfb7bbfd88d0c1de66981e52850f26067623bTimo Sirainen len = buffer_get_used_size(str);
22535a9e685e29214082878e37a267157044618eTimo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen append_len = printf_string_upper_bound(&fmt, args);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen buf = buffer_append_space_unsafe(str, append_len);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#ifdef HAVE_VSNPRINTF
22535a9e685e29214082878e37a267157044618eTimo Sirainen ret = vsnprintf(buf, append_len, fmt, args2);
6fabfb7bbfd88d0c1de66981e52850f26067623bTimo Sirainen i_assert(ret >= 0 && (size_t)ret <= append_len);
22535a9e685e29214082878e37a267157044618eTimo Sirainen#else
22535a9e685e29214082878e37a267157044618eTimo Sirainen ret = vsprintf(buf, fmt, args2);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen i_assert(ret >= 0);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#endif
6fabfb7bbfd88d0c1de66981e52850f26067623bTimo Sirainen
6fabfb7bbfd88d0c1de66981e52850f26067623bTimo Sirainen len += ret;
6fabfb7bbfd88d0c1de66981e52850f26067623bTimo Sirainen
6fabfb7bbfd88d0c1de66981e52850f26067623bTimo Sirainen buffer_set_used_size(str, len);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen}
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainenvoid str_insert(string_t *str, size_t pos, const char *cstr)
6fabfb7bbfd88d0c1de66981e52850f26067623bTimo Sirainen{
6fabfb7bbfd88d0c1de66981e52850f26067623bTimo Sirainen buffer_insert(str, pos, cstr, strlen(cstr));
6fabfb7bbfd88d0c1de66981e52850f26067623bTimo Sirainen}
6fabfb7bbfd88d0c1de66981e52850f26067623bTimo Sirainen
6fabfb7bbfd88d0c1de66981e52850f26067623bTimo Sirainenvoid str_delete(string_t *str, size_t pos, size_t len)
6fabfb7bbfd88d0c1de66981e52850f26067623bTimo Sirainen{
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen buffer_delete(str, pos, len);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen}
c5383a0ed56a188a7d5efaaa4c6f8243af432d65Timo Sirainen
c5383a0ed56a188a7d5efaaa4c6f8243af432d65Timo Sirainenvoid str_truncate(string_t *str, size_t len)
c5383a0ed56a188a7d5efaaa4c6f8243af432d65Timo Sirainen{
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen buffer_set_used_size(str, len);
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen}
806cb455553b71934314da98f1b4a694a3aa152eTimo Sirainen