MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
mbstrbuf.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libmbutil/mbstrbuf.cc,v 1.10 2017/01/12 14:44:05 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 #include "mbconfig.h" /* This goes first in every *.c,*.cc file */
33 
34 #include <cstdio>
35 #include <cstring>
36 #include "mbstrbuf.h"
37 //#include <stdio.h>
38 
39 std::ostream&
40 mbstrbuf::stats(std::ostream& out)
41 {
42  return out << "len=" << len << "; cursor=" << cursor << std::endl;
43 }
44 
45 
46 void
47 mbstrbuf::make_room(unsigned newlen)
48 {
49  newlen = std::max(cursor + 2*newlen, 2*len);
50  char *ptr = new char[newlen];
51  memcpy(ptr, buf, cursor);
52  delete[] buf;
53  buf = ptr;
54  len = newlen;
55 }
56 
57 void
58 mbstrbuf::return_cursor(unsigned newcursor)
59 {
60  cursor = newcursor;
61 }
62 
63 
64 void
65 mbstrbuf::print_str(const char *str)
66 {
67  int slen, buflen;
68 
69 retry:;
70  buflen = len - cursor;
71  slen = strlen(str);
72  if (slen >= buflen) {
73  make_room(slen);
74  goto retry;
75  }
76 
77  memcpy(&buf[cursor], str, slen);
78  cursor += slen;
79  buf[cursor] = '\0';
80 }
81 
82 void
83 mbstrbuf::print_double(const char *fmt, double d)
84 {
85  int dlen, buflen;
86 
87 retry:;
88  buflen = len - cursor;
89  dlen = snprintf(&buf[cursor], buflen, fmt, d);
90  if (dlen >= buflen) {
91  make_room(dlen);
92  goto retry;
93  }
94 
95  cursor += dlen;
96 }
97 
98 const char *
99 mbstrbuf::get_buf(void) const
100 {
101  return buf;
102 }
103 
104 unsigned
105 mbstrbuf::get_len(void) const
106 {
107  return len;
108 }
109 
110 std::ostream&
111 operator << (std::ostream& out, const mbstrbuf& buf)
112 {
113  return out << buf.buf;
114 }
115 
116 #ifdef MAIN
117 
118 int
119 main(void)
120 {
121  mbstrbuf buf(10);
122  buf.stats(std::cout) << std::endl;
123 
124  buf.print_double("%e", 10.2);
125 
126  std::cout << buf << std::endl;
127  buf.stats(std::cout) << std::endl;
128 
129  buf.print_str(",");
130  buf.print_double("%e", 12345678.9);
131 
132  std::cout << buf << std::endl;
133  buf.stats(std::cout) << std::endl;
134 
135  buf.print_str(",");
136  buf.print_double("%e", 12345678.9);
137 
138  std::cout << buf << std::endl;
139  buf.stats(std::cout) << std::endl;
140 
141  buf.print_str(",");
142  buf.print_double("%e", 12345678.9);
143 
144  std::cout << buf << std::endl;
145  buf.stats(std::cout) << std::endl;
146 
147  return 0;
148 }
149 
150 #endif // MAIN
void print_double(const char *fmt, double d)
Definition: mbstrbuf.cc:83
void print_str(const char *str)
Definition: mbstrbuf.cc:65
unsigned cursor
Definition: mbstrbuf.h:42
int main(int argc, char *argv[])
Definition: dgeequtest.cc:74
unsigned get_len(void) const
Definition: mbstrbuf.cc:105
void make_room(unsigned newlen)
Definition: mbstrbuf.cc:47
unsigned len
Definition: mbstrbuf.h:41
const char * get_buf(void) const
Definition: mbstrbuf.cc:99
std::ostream & operator<<(std::ostream &out, const mbstrbuf &buf)
Definition: mbstrbuf.cc:111
char * buf
Definition: mbstrbuf.h:43
void return_cursor(unsigned newcursor=0)
Definition: mbstrbuf.cc:58
static doublereal buf[BUFSIZE]
Definition: discctrl.cc:333
std::ostream & stats(std::ostream &out)
Definition: mbstrbuf.cc:40