COVISE Core
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
tokenbuffer.h
Go to the documentation of this file.
1 /* This file is part of COVISE.
2 
3  You can use it under the terms of the GNU Lesser General Public License
4  version 2.1 or later, see lgpl-2.1.txt.
5 
6  * License: LGPL 2+ */
7 
8 #ifndef TokenBuffer_H
9 #define TokenBuffer_H
10 
11 #include <string.h>
12 #include <stdio.h>
13 #include <iostream>
14 
15 #include <util/coExport.h>
16 #include <util/byteswap.h>
17 
18 #ifdef _WIN64
19 #define __WORDSIZE 64
20 #endif
21 
22 /*
23  $Log: $
24  * Revision 1.4 1994/03/23 18:07:03 zrf30125
25  * Modifications for multiple Shared Memory segments have been finished
26  * (not yet for Cray)
27  *
28  * Revision 1.3 93/10/11 09:22:19 zrhk0125
29  * new types DM_CONTACT_DM and APP_CONTACT_DM included
30  *
31  * Revision 1.2 93/10/08 19:18:06 zrhk0125
32  * data type sizes introduced
33 * some fixed type sizes with sizeof calls replaced
34  *
35  * Revision 1.1 93/09/25 20:47:03 zrhk0125
36  * Initial revision
37  *
38  */
39 
40 /***********************************************************************\
41  ** **
42  ** Message classes Version: 1.1 **
43  ** **
44  ** **
45  ** Description : The basic message structure as well as ways to **
46  ** initialize messages easily are provided. **
47  ** Subclasses for special types of messages **
48  ** can be introduced. **
49  ** **
50  ** Classes : Message, ShmMessage **
51  ** **
52  ** Copyright (C) 1993 by University of Stuttgart **
53  ** Computer Center (RUS) **
54  ** Allmandring 30 **
55  ** 7000 Stuttgart 80 **
56  ** HOSTID **
57  ** **
58  ** Author : A. Wierse (RUS) **
59  ** **
60  ** History : **
61  ** 15.04.93 Ver 1.0 **
62  ** 15.04.93 Ver 1.1 new Messages and type added **
63  ** sender and send_type added **
64  ** **
65  ** **
66 \***********************************************************************/
67 
68 namespace covise
69 {
70 
71 class Message;
72 
73 class NETEXPORT TokenBuffer // class for tokens
74 {
75 private:
76  int buflen; // number of allocated bytes
77  int length; // number of used bytes
78  char *data; // pointer to the tokens
79  char *currdata; // pointer to the tokens
81 
82  void incbuf(int size = 100);
83 
84 public:
85  TokenBuffer(bool nbo = false)
86  {
87  buflen = length = 0;
88  data = currdata = NULL;
89  networkByteOrder = nbo;
90  }
91  TokenBuffer(int al, bool nbo = false)
92  {
93  buflen = al;
94  length = 0;
95  data = currdata = new char[al];
96  networkByteOrder = nbo;
97  }
98  virtual ~TokenBuffer();
99  void delete_data();
100  TokenBuffer(Message *msg, bool nbo = false);
101  TokenBuffer(const char *dat, int len, bool nbo = false);
102 
103  const char *getBinary(int n)
104  {
105  const char *c = currdata;
106  currdata += n;
107  return c;
108  }
109 
110  void addBinary(const char *buf, int n)
111  {
112  if (buflen < length + n + 1)
113  incbuf(n + 40);
114  memcpy(currdata, buf, n);
115  currdata += n;
116  length += n;
117  }
118  const char *allocBinary(int n)
119  {
120  if (buflen < length + n + 1)
121  incbuf(n + 40);
122  const char *buf = currdata;
123  currdata += n;
124  length += n;
125  return buf;
126  }
127 
128  TokenBuffer &operator<<(const uint64_t i);
129 #ifndef WIN32 // it does not work on win32 as size_t == int
130 //TokenBuffer& operator << (const size_t s){return (*this<<(uint64_t)s);}
131 #endif
132  TokenBuffer &operator<<(const uint32_t i);
133  TokenBuffer &operator<<(const int i)
134  {
135  return (*this << (uint32_t)i);
136  }
138  TokenBuffer &operator<<(const char c)
139  {
140  if (buflen < length + 2)
141  incbuf();
142  *currdata = c;
143  currdata++;
144  length++;
145  return (*this);
146  }
147  TokenBuffer &operator<<(const float f);
148  TokenBuffer &operator<<(const double f);
149  TokenBuffer &operator<<(const char *c)
150  {
151  int l = int(strlen(c) + 1);
152  if (buflen < length + l + 1)
153  incbuf(l * 10);
154  strcpy(currdata, c);
155  currdata += l;
156  length += l;
157  return (*this);
158  }
161  {
162  if (buflen < length + t.get_length() + 1)
163  incbuf(t.get_length() * 4);
164  memcpy(currdata, t.get_data(), t.get_length());
165  currdata += t.get_length();
166  length += t.get_length();
167  return (*this);
168  }
169  TokenBuffer &operator>>(uint64_t &i);
170 #ifndef WIN32 // it does not work on win32 as size_t == int
171 //TokenBuffer& operator >> (size_t &s){uint64_t i; *this>>i; s=i; return *this; }
172 #endif
173  TokenBuffer &operator>>(uint32_t &i);
175  {
176  return (*this >> *((uint32_t *)&i));
177  }
179  {
180  c = *(char *)currdata;
181  currdata++;
182  return (*this);
183  }
184  TokenBuffer &operator>>(unsigned char &c)
185  {
186  c = *(unsigned char *)currdata;
187  currdata++;
188  return (*this);
189  }
190  TokenBuffer &operator>>(float &f);
191  TokenBuffer &operator>>(double &f);
192  TokenBuffer &operator>>(std::string &s);
194  {
195  char *end = data + length - 1;
196  c = currdata;
197  while (*currdata)
198  {
199  currdata++;
200  if (currdata > end)
201  {
202  std::cerr << "string not terminated within range" << std::endl;
203  *end = '\0';
204  return (*this);
205  }
206  }
207  currdata++;
208  return (*this);
209  }
210 
211  uint32_t get_int_token();
213  {
214  char ret = *(char *)currdata;
215  currdata++;
216  return (ret);
217  };
218  float get_float_token();
220  {
221  char *ret = currdata;
222  while (*currdata)
223  currdata++;
224  currdata++;
225  return (ret);
226  };
228  {
229  return (length);
230  };
231  const char *get_data()
232  {
233  return (data);
234  };
235  void reset()
236  {
237  currdata = data;
238  length = 0;
239  if (data)
240  data[0] = 0;
241  };
242 };
243 }
244 #endif
GLenum GLsizei len
Definition: khronos-glext.h:7440
char get_char_token()
Definition: tokenbuffer.h:212
const char * allocBinary(int n)
Definition: tokenbuffer.h:118
TokenBuffer(bool nbo=false)
Definition: tokenbuffer.h:85
TokenBuffer & operator>>(unsigned char &c)
Definition: tokenbuffer.h:184
const char * getBinary(int n)
Definition: tokenbuffer.h:103
GLdouble n
Definition: khronos-glext.h:8447
char * get_charp_token()
Definition: tokenbuffer.h:219
TokenBuffer(int al, bool nbo=false)
Definition: tokenbuffer.h:91
char * data
Definition: tokenbuffer.h:78
int buflen
Definition: tokenbuffer.h:76
GLdouble s
Definition: khronos-glext.h:6441
#define NULL
Definition: covise_list.h:22
Definition: message.h:111
GLsizei const GLchar *const * string
Definition: khronos-glext.h:6750
GLuint GLuint end
Definition: khronos-glext.h:6343
GLsizeiptr size
Definition: khronos-glext.h:6610
int length
Definition: tokenbuffer.h:77
TokenBuffer & operator>>(char *&c)
Definition: tokenbuffer.h:193
void reset()
Definition: tokenbuffer.h:235
TokenBuffer & operator>>(char &c)
Definition: tokenbuffer.h:178
Definition: tokenbuffer.h:73
const GLubyte * c
Definition: khronos-glext.h:9864
char * currdata
Definition: tokenbuffer.h:79
TokenBuffer & operator<<(const char c)
Definition: tokenbuffer.h:138
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: khronos-glext.h:8469
TokenBuffer & operator<<(const char *c)
Definition: tokenbuffer.h:149
bool networkByteOrder
Definition: tokenbuffer.h:80
#define NETEXPORT
Definition: coExport.h:343
TokenBuffer & operator>>(int &i)
Definition: tokenbuffer.h:174
const char * get_data()
Definition: tokenbuffer.h:231
int get_length()
Definition: tokenbuffer.h:227
TokenBuffer & operator<<(const int i)
Definition: tokenbuffer.h:133
GLdouble GLdouble t
Definition: khronos-glext.h:6449
TokenBuffer & operator<<(TokenBuffer t)
Definition: tokenbuffer.h:160
GLfloat f
Definition: khronos-glext.h:8258
void addBinary(const char *buf, int n)
Definition: tokenbuffer.h:110
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: khronos-glext.h:6354
GLenum GLuint GLenum GLsizei length
Definition: khronos-glext.h:6279
QTextStream & operator<<(QTextStream &out, const coConfigEntryStringList list)
Definition: coConfigEntryString.cpp:166