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 | |
---|
14 | void GBS_strstruct::vnprintf(size_t maxlen, const char *templat, va_list& parg) { |
---|
15 | ensure_mem(maxlen+1); |
---|
16 | |
---|
17 | char *buffer = data+pos; |
---|
18 | int printed; |
---|
19 | |
---|
20 | #ifdef LINUX |
---|
21 | printed = vsnprintf(buffer, maxlen+1, templat, parg); |
---|
22 | #else |
---|
23 | printed = vsprintf(buffer, templat, parg); |
---|
24 | #endif |
---|
25 | |
---|
26 | assert_or_exit(printed >= 0 && (size_t)printed <= maxlen); |
---|
27 | inc_pos(printed); |
---|
28 | } |
---|
29 | |
---|
30 | void GBS_strstruct::nprintf(size_t maxlen, const char *templat, ...) { |
---|
31 | va_list parg; |
---|
32 | va_start(parg, templat); |
---|
33 | vnprintf(maxlen, templat, parg); |
---|
34 | } |
---|
35 | |
---|
36 | // old interface |
---|
37 | |
---|
38 | static GBS_strstruct last_used; |
---|
39 | |
---|
40 | GBS_strstruct *GBS_stropen(long init_size) { |
---|
41 | /*! create a new memory file |
---|
42 | * @param init_size estimated used size |
---|
43 | */ |
---|
44 | |
---|
45 | GBS_strstruct *strstr = new GBS_strstruct; |
---|
46 | |
---|
47 | arb_assert(init_size>0); |
---|
48 | |
---|
49 | if (last_used.get_buffer_size() >= (size_t)init_size) { |
---|
50 | strstr->reassign_mem(last_used); |
---|
51 | |
---|
52 | static short oversized_counter = 0; |
---|
53 | |
---|
54 | if ((size_t)init_size*10 < strstr->get_buffer_size()) oversized_counter++; |
---|
55 | else oversized_counter = 0; |
---|
56 | |
---|
57 | if (oversized_counter>10) { // was oversized more than 10 times -> realloc |
---|
58 | size_t dummy; |
---|
59 | free(strstr->release_mem(dummy)); |
---|
60 | strstr->alloc_mem(init_size); |
---|
61 | } |
---|
62 | } |
---|
63 | else { |
---|
64 | strstr->alloc_mem(init_size); |
---|
65 | } |
---|
66 | |
---|
67 | return strstr; |
---|
68 | } |
---|
69 | |
---|
70 | char *GBS_strclose(GBS_strstruct *strstr) { |
---|
71 | // returns a char* copy of the memory file |
---|
72 | |
---|
73 | size_t length = strstr->get_position(); |
---|
74 | char *str = (char*)malloc(length+1); |
---|
75 | |
---|
76 | memcpy(str, strstr->get_data(), length+1); // copy with 0 |
---|
77 | GBS_strforget(strstr); |
---|
78 | |
---|
79 | return str; |
---|
80 | } |
---|
81 | |
---|
82 | void GBS_strforget(GBS_strstruct *strstr) { |
---|
83 | size_t last_bsize = last_used.get_buffer_size(); |
---|
84 | size_t curr_bsize = strstr->get_buffer_size(); |
---|
85 | |
---|
86 | if (last_bsize < curr_bsize) { // last_used is smaller -> keep this |
---|
87 | last_used.reassign_mem(*strstr); |
---|
88 | } |
---|
89 | delete strstr; |
---|
90 | } |
---|
91 | |
---|
92 | char *GBS_mempntr(GBS_strstruct *strstr) { |
---|
93 | // returns the memory file (with write access) |
---|
94 | return (char*)strstr->get_data(); |
---|
95 | } |
---|
96 | |
---|
97 | long GBS_memoffset(GBS_strstruct *strstr) { |
---|
98 | // returns the offset into the memory file |
---|
99 | return strstr->get_position(); |
---|
100 | } |
---|
101 | |
---|
102 | void GBS_str_cut_tail(GBS_strstruct *strstr, size_t byte_count) { |
---|
103 | // Removes byte_count characters at the tail of a memfile |
---|
104 | strstr->cut_tail(byte_count); |
---|
105 | } |
---|
106 | |
---|
107 | void GBS_strncat(GBS_strstruct *strstr, const char *ptr, size_t len) { |
---|
108 | /* append some bytes string to strstruct |
---|
109 | * (caution : copies zero byte and mem behind if used with wrong len!) |
---|
110 | */ |
---|
111 | strstr->ncat(ptr, len); |
---|
112 | } |
---|
113 | |
---|
114 | void GBS_strcat(GBS_strstruct *strstr, const char *ptr) { |
---|
115 | // append string to strstruct |
---|
116 | strstr->cat(ptr); |
---|
117 | } |
---|
118 | |
---|
119 | void GBS_strnprintf(GBS_strstruct *strstr, long maxlen, const char *templat, ...) { |
---|
120 | va_list parg; |
---|
121 | va_start(parg, templat); |
---|
122 | strstr->vnprintf(maxlen+2, templat, parg); |
---|
123 | } |
---|
124 | |
---|
125 | void GBS_chrcat(GBS_strstruct *strstr, char ch) { |
---|
126 | strstr->put(ch); |
---|
127 | } |
---|
128 | |
---|
129 | void GBS_chrncat(GBS_strstruct *strstr, char ch, size_t n) { |
---|
130 | strstr->nput(ch, n); |
---|
131 | } |
---|
132 | |
---|
133 | void GBS_intcat(GBS_strstruct *strstr, long val) { |
---|
134 | char buffer[100]; |
---|
135 | long len = sprintf(buffer, "%li", val); |
---|
136 | GBS_strncat(strstr, buffer, len); |
---|
137 | } |
---|
138 | |
---|
139 | void GBS_floatcat(GBS_strstruct *strstr, double val) { |
---|
140 | char buffer[100]; |
---|
141 | long len = sprintf(buffer, "%f", val); |
---|
142 | GBS_strncat(strstr, buffer, len); |
---|
143 | } |
---|
144 | |
---|