Crossfire Server, Trunk  R20513
stringbuffer.c
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5  * Copyright (c) 1992 Frank Tore Johansen
6  *
7  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8  * welcome to redistribute it under certain conditions. For details, please
9  * see COPYING and LICENSE.
10  *
11  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
12  */
13 
14 #include <stdarg.h>
15 #include <stdlib.h>
16 #include <string.h>
17 
18 #include "global.h"
19 #include "libproto.h"
20 #include "stringbuffer.h"
21 
25 struct StringBuffer {
30  char *buf;
31 
37  size_t pos;
38 
42  size_t size;
43 };
44 
45 
54 static void stringbuffer_ensure(StringBuffer *sb, size_t len);
55 
56 
58  StringBuffer *sb;
59 
60  sb = malloc(sizeof(*sb));
61  if (sb == NULL) {
63  }
64 
65  sb->size = 256;
66  sb->buf = malloc(sb->size);
67  sb->pos = 0;
68  return sb;
69 }
70 
72  free(sb->buf);
73  free(sb);
74 }
75 
77  char *result;
78 
79  sb->buf[sb->pos] = '\0';
80  result = sb->buf;
81  free(sb);
82  return result;
83 }
84 
86  char *str;
87  sstring result;
88 
89  str = stringbuffer_finish(sb);
90  result = add_string(str);
91  free(str);
92  return result;
93 }
94 
95 void stringbuffer_append_string(StringBuffer *sb, const char *str) {
96  size_t len;
97 
98  len = strlen(str);
99  stringbuffer_ensure(sb, len+1);
100  memcpy(sb->buf+sb->pos, str, len);
101  sb->pos += len;
102 }
103 
104 void stringbuffer_append_printf(StringBuffer *sb, const char *format, ...) {
105  size_t size;
106 
107  size = 100; /* arbitrary guess */
108  for (;;) {
109  int n;
110  va_list arg;
111 
112  stringbuffer_ensure(sb, size);
113 
114  va_start(arg, format);
115  n = vsnprintf(sb->buf+sb->pos, size, format, arg);
116  va_end(arg);
117 
118  if (n > -1 && (size_t)n < size) {
119  sb->pos += (size_t)n;
120  break;
121  }
122 
123  if (n > -1) {
124  size = n+1; /* precisely what is needed */
125  } else {
126  size *= 2; /* twice the old size */
127  }
128  }
129 }
130 
132  stringbuffer_ensure(sb, sb2->pos+1);
133  memcpy(sb->buf+sb->pos, sb2->buf, sb2->pos);
134  sb->pos += sb2->pos;
135 }
136 
137 static void stringbuffer_ensure(StringBuffer *sb, size_t len) {
138  char *tmp;
139  size_t new_size;
140 
141  if (sb->pos+len <= sb->size) {
142  return;
143  }
144 
145  new_size = sb->pos+len+256;
146  tmp = realloc(sb->buf, new_size);
147  if (tmp == NULL) {
149  }
150  sb->buf = tmp;
151  sb->size = new_size;
152 }
153 
155  return sb->pos;
156 }
void stringbuffer_append_stringbuffer(StringBuffer *sb, const StringBuffer *sb2)
Append the contents of a string buffer instance to another string buffer instance.
Definition: stringbuffer.c:131
sstring stringbuffer_finish_shared(StringBuffer *sb)
Deallocate the string buffer instance and return the string as a shared string.
Definition: stringbuffer.c:85
void fatal(enum fatal_error err)
fatal() is meant to be called whenever a fatal signal is intercepted.
Definition: utils.c:596
StringBuffer * stringbuffer_new(void)
Create a new string buffer.
Definition: stringbuffer.c:57
Global type definitions and header inclusions.
void stringbuffer_append_string(StringBuffer *sb, const char *str)
Append a string to a string buffer instance.
Definition: stringbuffer.c:95
char * buf
The string buffer.
Definition: stringbuffer.c:30
size_t stringbuffer_length(StringBuffer *sb)
Return the current length of the buffer.
Definition: stringbuffer.c:154
static void stringbuffer_ensure(StringBuffer *sb, size_t len)
Make sure that at least len bytes are available in the passed string buffer.
Definition: stringbuffer.c:137
Implements a general string buffer: it builds a string by concatenating.
size_t size
The allocation size of buf.
Definition: stringbuffer.c:42
const char * sstring
Strings that should be manipulated through add_string() and free_string().
Definition: global.h:40
#define vsnprintf
Definition: win32.h:61
void stringbuffer_delete(StringBuffer *sb)
Totally delete a string buffer.
Definition: stringbuffer.c:71
sstring add_string(const char *str)
This will add &#39;str&#39; to the hash table.
Definition: shstr.c:124
void stringbuffer_append_printf(StringBuffer *sb, const char *format,...)
Append a formatted string to a string buffer instance.
Definition: stringbuffer.c:104
A buffer that will be expanded as content is added to it.
Definition: stringbuffer.c:25
char * stringbuffer_finish(StringBuffer *sb)
Deallocate the string buffer instance and return the string.
Definition: stringbuffer.c:76
size_t pos
The current length of buf.
Definition: stringbuffer.c:37