Crossfire Server, Trunk  1.75.0
stringbuffer.cpp
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 
22 static void stringbuffer_ensure(StringBuffer *sb, size_t len);
23 
25  return new std::string;
26 }
27 
29  delete sb;
30 }
31 
33  char* dat = strdup(sb->c_str());
34  delete sb;
35  return dat;
36 }
37 
39  sstring result = add_string(sb->c_str());
40  delete sb;
41  return result;
42 }
43 
44 void stringbuffer_append_string(StringBuffer *sb, const char *str) {
45  *sb += str;
46 }
47 
48 void stringbuffer_append_char(StringBuffer *sb, const char c) {
49  *sb += c;
50 }
51 
53  if (x < 0) {
54  stringbuffer_append_char(sb, '-');
55  if (x == INT64_MIN) {
56  x = INT64_MAX;
57  } else {
58  x = -x;
59  }
60  }
61 
62  const int MAX_DIGITS = 20; // handle at most 20 digits, since an int64_t has at most 19 digits
63  char buf[MAX_DIGITS];
64  int i;
65  for (i = 0; i < MAX_DIGITS; i++) {
66  buf[i] = x % 10 + '0';
67  if ((x /= 10) == 0) {
68  i++;
69  break;
70  }
71  }
72 
73  // copy into sb in reverse
74  for (int j = i - 1; j >= 0; j--) {
76  }
77 }
78 
79 void stringbuffer_append_printf(StringBuffer *sb, const char *format, ...) {
80  va_list arg;
81  va_start(arg, format);
82  char buf[HUGE_BUF];
83  vsnprintf(buf, sizeof(buf), format, arg);
84  va_end(arg);
86 }
87 
89  *sb += *sb2;
90 }
91 
95 static void stringbuffer_ensure(StringBuffer *sb, size_t len) {
96  sb->reserve(len);
97 }
98 
99 void stringbuffer_append_multiline_block(StringBuffer *sb, const char *start, const char *content, const char *end) {
100  if (!content || *content == '\0') {
101  return;
102  }
103  *sb += start;
104  *sb += "\n";
105  *sb += content;
106 
107  // Add newline if len(content) is non-zero and missing a trailing newline
108  if ((*content != '\0') && (sb->back() != '\n')) {
109  *sb += "\n";
110  }
111 
112  if (end == NULL) {
113  *sb += "end_";
114  *sb += start;
115  } else {
116  *sb += "end";
117  }
118  *sb += "\n";
119 }
120 
122  return sb->size();
123 }
124 
126  while (isspace(sb->back())) {
127  sb->pop_back();
128  }
129 }
global.h
stringbuffer_length
size_t stringbuffer_length(StringBuffer *sb)
Return the current length of the buffer.
Definition: stringbuffer.cpp:121
stringbuffer_append_printf
void stringbuffer_append_printf(StringBuffer *sb, const char *format,...)
Append a formatted string to a string buffer instance.
Definition: stringbuffer.cpp:79
c
static event_registration c
Definition: citylife.cpp:424
stringbuffer_new
StringBuffer * stringbuffer_new(void)
Create a new string buffer.
Definition: stringbuffer.cpp:24
stringbuffer_append_char
void stringbuffer_append_char(StringBuffer *sb, const char c)
Append a character to a string buffer instance.
Definition: stringbuffer.cpp:48
buf
StringBuffer * buf
Definition: readable.cpp:1564
HUGE_BUF
#define HUGE_BUF
Used for messages - some can be quite long.
Definition: define.h:37
stringbuffer_append_int64
void stringbuffer_append_int64(StringBuffer *sb, int64_t x)
Append a signed integer to a string buffer instance.
Definition: stringbuffer.cpp:52
stringbuffer.h
stringbuffer_finish
char * stringbuffer_finish(StringBuffer *sb)
Deallocate the string buffer instance and return the string.
Definition: stringbuffer.cpp:32
stringbuffer_finish_shared
sstring stringbuffer_finish_shared(StringBuffer *sb)
Deallocate the string buffer instance and return the string as a shared string.
Definition: stringbuffer.cpp:38
add_string
sstring add_string(const char *str)
Share a string.
Definition: shstr.cpp:137
StringBuffer
std::string StringBuffer
The string buffer state.
Definition: stringbuffer.h:33
stringbuffer_ensure
static void stringbuffer_ensure(StringBuffer *sb, size_t len)
Ensure sb can hold at least len more characters, growing the sb if not.
Definition: stringbuffer.cpp:95
stringbuffer_append_string
void stringbuffer_append_string(StringBuffer *sb, const char *str)
Append a string to a string buffer instance.
Definition: stringbuffer.cpp:44
stringbuffer_append_multiline_block
void stringbuffer_append_multiline_block(StringBuffer *sb, const char *start, const char *content, const char *end)
Append the specified content in a multiline block, starting with "start" and ending with "end".
Definition: stringbuffer.cpp:99
stringbuffer_trim_whitespace
void stringbuffer_trim_whitespace(StringBuffer *sb)
Trim trailing whitespace from a stringbuffer.
Definition: stringbuffer.cpp:125
stringbuffer_delete
void stringbuffer_delete(StringBuffer *sb)
Totally delete a string buffer.
Definition: stringbuffer.cpp:28
sstring
const typedef char * sstring
Definition: sstring.h:2
stringbuffer_append_stringbuffer
void stringbuffer_append_stringbuffer(StringBuffer *sb, const StringBuffer *sb2)
Append the contents of a string buffer instance to another string buffer instance.
Definition: stringbuffer.cpp:88
libproto.h