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 | |
---|
63 | TEST_EXPECT_EQUAL(buf.get_position(), 14); |
---|
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 | |
---|