source: tags/ms_r17q1/CORE/arb_strbuf.cxx

Last change on this file was 15176, checked in by westram, 8 years ago
File size: 3.9 KB
Line 
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#include "arb_string.h"
13
14void 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
30void 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
38static GBS_strstruct last_used;
39
40GBS_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 -> allocate smaller block
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
70char *GBS_strclose(GBS_strstruct *strstr) {
71    // returns a char* copy of the memory file
72    char *str = ARB_strndup(strstr->get_data(), strstr->get_position());
73    GBS_strforget(strstr);
74    return str;
75}
76
77void GBS_strforget(GBS_strstruct *strstr) {
78    size_t last_bsize = last_used.get_buffer_size();
79    size_t curr_bsize = strstr->get_buffer_size();
80
81    if (last_bsize < curr_bsize) { // last_used is smaller -> keep this
82        last_used.reassign_mem(*strstr);
83    }
84    delete strstr;
85}
86
87char *GBS_mempntr(GBS_strstruct *strstr) {
88    // returns the memory file (with write access)
89    return (char*)strstr->get_data(); 
90}
91
92long GBS_memoffset(GBS_strstruct *strstr) {
93    // returns the offset into the memory file
94    return strstr->get_position();
95}
96
97void GBS_str_cut_tail(GBS_strstruct *strstr, size_t byte_count) {
98    // Removes byte_count characters at the tail of a memfile
99    strstr->cut_tail(byte_count);
100}
101
102void GBS_strncat(GBS_strstruct *strstr, const char *ptr, size_t len) {
103    /* append some bytes string to strstruct
104     * (caution : copies zero byte and mem behind if used with wrong len!)
105     */
106    strstr->ncat(ptr, len);
107}
108
109void GBS_strcat(GBS_strstruct *strstr, const char *ptr) {
110    // append string to strstruct
111    strstr->cat(ptr);
112}
113
114void GBS_strnprintf(GBS_strstruct *strstr, long maxlen, const char *templat, ...) {
115    va_list parg;
116    va_start(parg, templat);
117    strstr->vnprintf(maxlen+2, templat, parg);
118}
119
120void GBS_chrcat(GBS_strstruct *strstr, char ch) {
121    strstr->put(ch);
122}
123
124void GBS_chrncat(GBS_strstruct *strstr, char ch, size_t n) {
125    strstr->nput(ch, n);
126}
127
128void GBS_intcat(GBS_strstruct *strstr, long val) {
129    char buffer[100];
130    long len = sprintf(buffer, "%li", val);
131    GBS_strncat(strstr, buffer, len);
132}
133
134void GBS_floatcat(GBS_strstruct *strstr, double val) {
135    char buffer[100];
136    long len = sprintf(buffer, "%f", val);
137    GBS_strncat(strstr, buffer, len);
138}
139
Note: See TracBrowser for help on using the repository browser.