source: tags/ms_r16q2/CORE/arb_strbuf.cxx

Last change on this file was 8035, checked in by westram, 13 years ago

merge from dev [7940] [7941] [7962]

  • do not export error from GB_check_key
  • added some NAN guards
  • internally increase maxlen by 1 in GBS_strstruct::vnprintf (fixes zero-bytes into buffer)
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
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 -> 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
70char *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
82void 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
92char *GBS_mempntr(GBS_strstruct *strstr) {
93    // returns the memory file (with write access)
94    return (char*)strstr->get_data(); 
95}
96
97long GBS_memoffset(GBS_strstruct *strstr) {
98    // returns the offset into the memory file
99    return strstr->get_position();
100}
101
102void 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
107void 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
114void GBS_strcat(GBS_strstruct *strstr, const char *ptr) {
115    // append string to strstruct
116    strstr->cat(ptr);
117}
118
119void 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
125void GBS_chrcat(GBS_strstruct *strstr, char ch) {
126    strstr->put(ch);
127}
128
129void GBS_chrncat(GBS_strstruct *strstr, char ch, size_t n) {
130    strstr->nput(ch, n);
131}
132
133void 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
139void 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
Note: See TracBrowser for help on using the repository browser.