ARB
arb_strbuf.cxx
Go to the documentation of this file.
1 // ============================================================= //
2 // //
3 // File : arb_strbuf.cxx //
4 // Purpose : "unlimited" output buffer //
5 // //
6 // Institute of Microbiology (Technical University Munich) //
7 // http://www.arb-home.de/ //
8 // //
9 // ============================================================= //
10 
11 #include "arb_strbuf.h"
12 
13 void GBS_strstruct::vnprintf(size_t maxlen, const char *templat, va_list& parg) {
14  ensure_mem(maxlen+1);
15 
16  char *buffer = data+pos;
17  int printed;
18 
19 #ifdef LINUX
20  printed = vsnprintf(buffer, maxlen+1, templat, parg);
21 #else
22  printed = vsprintf(buffer, templat, parg);
23 #endif
24 
25  assert_or_exit(printed >= 0 && (size_t)printed <= maxlen);
26  inc_pos(printed);
27 }
28 
29 void GBS_strstruct::nprintf(size_t maxlen, const char *templat, ...) {
30  va_list parg;
31  va_start(parg, templat);
32  vnprintf(maxlen, templat, parg);
33 }
34 
35 // --------------------------------------------------------------------------------
36 
37 #ifdef UNIT_TESTS
38 #ifndef TEST_UNIT_H
39 #include <test_unit.h>
40 #endif
41 
42 #define EXPECT_CONTENT(expected) TEST_EXPECT_EQUAL(buf.get_data(), expected)
43 #define EXPECT_CONTENT__BROKEN(expected,got) TEST_EXPECT_EQUAL__BROKEN(buf.get_data(), expected, got)
44 
45 void TEST_GBS_strstruct() {
46  { GBS_strstruct buf; buf.put ('c'); EXPECT_CONTENT("c"); }
47  { GBS_strstruct buf; buf.cat ("abc"); EXPECT_CONTENT("abc"); }
48  { GBS_strstruct buf; buf.nput('x', 0); EXPECT_CONTENT(""); }
49 
50  { GBS_strstruct buf; buf.ncat("abc", 0); EXPECT_CONTENT(""); }
51  { GBS_strstruct buf; buf.cat (""); EXPECT_CONTENT(""); }
52  { GBS_strstruct buf; EXPECT_CONTENT(""); }
53 
54  // test multiple consecutive write commands:
55  {
56  GBS_strstruct buf(1000); EXPECT_CONTENT("");
57 
58  buf.nput('b', 3); EXPECT_CONTENT("bbb");
59  buf.putlong(17); EXPECT_CONTENT("bbb17");
60  buf.put('_'); EXPECT_CONTENT("bbb17_");
61  buf.putfloat(3.5); EXPECT_CONTENT("bbb17_3.500000");
62 
64 
65  buf.cut_tail(13); EXPECT_CONTENT("b");
66  buf.cat("utter"); EXPECT_CONTENT("butter");
67  buf.ncat("flying", 3); EXPECT_CONTENT("butterfly");
68 
69  buf.cut(4, 2); EXPECT_CONTENT("buttfly");
70 
71  buf.cat("er"); EXPECT_CONTENT("buttflyer");
72 
73  buf.cut(99, 99); EXPECT_CONTENT("buttflyer");
74  buf.cut(0, 0); EXPECT_CONTENT("buttflyer");
75  buf.cut(4, 0); EXPECT_CONTENT("buttflyer");
76  buf.cut(0, 4); EXPECT_CONTENT("flyer");
77  buf.cut(3, 2); EXPECT_CONTENT("fly");
78  buf.cut(3, 99); EXPECT_CONTENT("fly");
79  buf.cut(4, 99); EXPECT_CONTENT("fly");
80  buf.cut(2, 1); EXPECT_CONTENT("fl");
81  buf.cut(1, 99); EXPECT_CONTENT("f");
82  }
83 
84  // test reallocation works:
85  {
86  GBS_strstruct buf(10);
87  size_t oldbufsize = buf.get_buffer_size();
88  buf.nput('x', 20); // trigger reallocation of buffer
89 
90  TEST_EXPECT_DIFFERENT(oldbufsize, buf.get_buffer_size()); // did we reallocate?
91  EXPECT_CONTENT("xxxxxxxxxxxxxxxxxxxx");
92  }
93 }
94 
95 #endif // UNIT_TESTS
96 
97 // --------------------------------------------------------------------------------
98 
void cut_tail(size_t byte_count)
Definition: arb_strbuf.h:145
void cut(const size_t at, const size_t byte_count)
Definition: arb_strbuf.h:150
void vnprintf(size_t maxlen, const char *templat, va_list &parg) __ATTR__VFORMAT_MEMBER(2)
Definition: arb_strbuf.cxx:13
void nput(char c, size_t count)
Definition: arb_strbuf.h:180
void cat(const char *from)
Definition: arb_strbuf.h:199
char buffer[MESSAGE_BUFFERSIZE]
Definition: seq_search.cxx:34
void putlong(long l)
Definition: arb_strbuf.h:240
size_t get_buffer_size() const
Definition: arb_strbuf.h:108
void putfloat(float f)
Definition: arb_strbuf.h:241
void ncat(const char *from, size_t count)
Definition: arb_strbuf.h:189
#define assert_or_exit(cond)
Definition: arb_assert.h:263
void nprintf(size_t maxlen, const char *templat,...) __ATTR__FORMAT_MEMBER(2)
Definition: arb_strbuf.cxx:29
va_start(argPtr, format)
#define TEST_EXPECT_DIFFERENT(expr, want)
Definition: test_unit.h:1301
#define TEST_EXPECT_EQUAL(expr, want)
Definition: test_unit.h:1294
size_t get_position() const
Definition: arb_strbuf.h:112
void put(char c)
Definition: arb_strbuf.h:174