[dovecot-cvs] dovecot/src/lib str.c,NONE,1.1 str.h,NONE,1.1 Makefile.am,1.23,1.24 lib.h,1.11,1.12 strfuncs.h,1.10,1.11 temp-string.c,1.8,NONE temp-string.h,1.4,NONE

cras at procontrol.fi cras at procontrol.fi
Sun Dec 22 00:03:00 EET 2002


Update of /home/cvs/dovecot/src/lib
In directory danu:/tmp/cvs-serv15300/lib

Modified Files:
	Makefile.am lib.h strfuncs.h 
Added Files:
	str.c str.h 
Removed Files:
	temp-string.c temp-string.h 
Log Message:
Replaced TempString with a String which can use any memory pool and uses
Buffer internally.



--- NEW FILE: str.c ---
/*
    Copyright (c) 2002 Timo Sirainen

    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files (the
    "Software"), to deal in the Software without restriction, including
    without limitation the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the Software, and to
    permit persons to whom the Software is furnished to do so, subject to
    the following conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/* @UNSAFE: whole file */

#include "lib.h"
#include "buffer.h"
#include "str.h"

#include <stdio.h>

String *str_new(Pool pool, size_t initial_size)
{
	return buffer_create_dynamic(pool, initial_size, (size_t)-1);
}

String *t_str_new(size_t initial_size)
{
	return str_new(data_stack_pool, initial_size);
}

static int str_add_nul(String *str)
{
	size_t len;

	len = str_len(str);
	if (buffer_write(str, len, "", 1) != 1) {
		/* no space - doesn't happen with our dynamically growing
		   strings though, but make sure it's \0 terminated. */
		if (len == 0)
			return FALSE;

		len--;
		if (buffer_write(str, len, "", 1) != 1)
			i_panic("BUG in str_c()");
	}

	/* remove the \0 - we don't want to keep it */
	buffer_set_used_size(str, len);
	return TRUE;
}

const char *str_c(String *str)
{
	if (!str_add_nul(str))
		return "";

	return buffer_get_data(str, NULL);
}

char *str_c_modifyable(String *str)
{
	if (!str_add_nul(str))
		return NULL;

	return buffer_get_modifyable_data(str, NULL);
}

size_t str_len(const String *str)
{
	return buffer_get_used_size(str);
}

void str_append(String *str, const char *cstr)
{
	buffer_append(str, cstr, strlen(cstr));
}

void str_append_n(String *str, const char *cstr, size_t max_len)
{
	size_t len;

	len = 0;
	while (len < max_len && cstr[len] != '\0')
		len++;

	buffer_append(str, cstr, len);
}

void str_append_c(String *str, char chr)
{
	buffer_append_c(str, chr);
}

void str_append_str(String *dest, const String *src)
{
	const char *cstr;
	size_t len;

	cstr = buffer_get_data(src, &len);
	buffer_append(dest, cstr, len);
}

void str_printfa(String *str, const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	str_vprintfa(str, fmt, args);
	va_end(args);
}

void str_vprintfa(String *str, const char *fmt, va_list args)
{
	char *buf;
	size_t len;

	len = buffer_get_used_size(str);

	buf = buffer_append_space(str, printf_string_upper_bound(fmt, args));
	len += vsprintf(buf, fmt, args);

	buffer_set_used_size(str, len);
}

void str_delete(String *str, size_t pos, size_t len)
{
	buffer_delete(str, pos, len);
}

void str_truncate(String *str, size_t len)
{
	buffer_set_used_size(str, len);
}

--- NEW FILE: str.h ---
#ifndef __STR_H
#define __STR_H

String *str_new(Pool pool, size_t initial_size);
String *t_str_new(size_t initial_size);

const char *str_c(String *str);
char *str_c_modifyable(String *str);
size_t str_len(const String *str);

/* Append string/character */
void str_append(String *str, const char *cstr);
void str_append_n(String *str, const char *cstr, size_t max_len);
void str_append_c(String *str, char chr);
void str_append_str(String *dest, const String *src);

/* Append printf()-like data */
void str_printfa(String *str, const char *fmt, ...)
	__attr_format__(2, 3);
void str_vprintfa(String *str, const char *fmt, va_list args);

/* Delete/truncate */
void str_delete(String *str, size_t pos, size_t len);
void str_truncate(String *str, size_t len);

#endif

Index: Makefile.am
===================================================================
RCS file: /home/cvs/dovecot/src/lib/Makefile.am,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -d -r1.23 -r1.24
--- Makefile.am	21 Dec 2002 13:08:49 -0000	1.23
+++ Makefile.am	21 Dec 2002 22:02:58 -0000	1.24
@@ -44,7 +44,7 @@
 	safe-mkdir.c \
 	sendfile-util.c \
 	strfuncs.c \
-	temp-string.c \
+	str.c \
 	unlink-directory.c \
 	unlink-lockfiles.c \
 	utc-offset.c \
@@ -90,7 +90,7 @@
 	safe-mkdir.h \
 	sendfile-util.h \
 	strfuncs.h \
-	temp-string.h \
+	str.h \
 	unlink-directory.h \
 	unlink-lockfiles.h \
 	utc-offset.h \

Index: lib.h
===================================================================
RCS file: /home/cvs/dovecot/src/lib/lib.h,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- lib.h	8 Dec 2002 05:23:07 -0000	1.11
+++ lib.h	21 Dec 2002 22:02:58 -0000	1.12
@@ -25,7 +25,7 @@
 typedef struct _IStream IStream;
 typedef struct _OStream OStream;
 typedef struct _Buffer Buffer;
-typedef struct _TempString TempString;
+typedef struct _Buffer String;
 
 #include "compat.h"
 #include "macros.h"

Index: strfuncs.h
===================================================================
RCS file: /home/cvs/dovecot/src/lib/strfuncs.h,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- strfuncs.h	19 Dec 2002 01:02:35 -0000	1.10
+++ strfuncs.h	21 Dec 2002 22:02:58 -0000	1.11
@@ -7,7 +7,7 @@
 #define MAX_INT_STRLEN ((sizeof(uintmax_t) * CHAR_BIT + 2) / 3 + 1)
 
 size_t printf_string_upper_bound(const char *format, va_list args);
-const char *printf_string_fix_format(const char *fmt);
+const char *printf_string_fix_format(const char *fmt) __attr_format_arg__(1);
 
 /* Returns -1 if dest wasn't large enough, 0 if not. */
 int i_snprintf(char *dest, size_t max_chars, const char *format, ...)

--- temp-string.c DELETED ---

--- temp-string.h DELETED ---




More information about the dovecot-cvs mailing list