vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
longstring.h
Go to the documentation of this file.
1 #ifndef LONGSTRING_H_
2 #define LONGSTRING_H_
3 
4 #include <string>
5 #include "utils.h"
6 
7 namespace AP_LSystem {
8 static unsigned int ZERO = 0;
9 
15 {
16 private:
17  char * m_String;
18  unsigned int m_Length;
19  unsigned int m_Allocated;
20  unsigned int m_Increment;
21 
23 
27  inline Parameter::Type getType(double) { return Parameter::LS_DOUBLE; }
28  inline Parameter::Type getType(double *) { return Parameter::LS_DOUBLE; }
29  inline Parameter::Type getType(unsigned int) { return Parameter::LS_UINT; }
30  inline Parameter::Type getType(unsigned char) { return Parameter::LS_UBYTE; }
31  inline Parameter::Type getType(int) { return Parameter::LS_INT; }
32  inline Parameter::Type getType(int *) { return Parameter::LS_INT; }
34 
38  void resize();
39 
44  void append( std::string str );
45 
50  void append( Parameter::Type type );
51 public:
56  LongString( unsigned int length = 1048576); // 2^20 bytes - 1 MB
57 
61  LongString( const LongString& c );
62 
66  LongString& operator=( const LongString & c );
67 
71  ~LongString( );
72 
77  void convertFromString( std::string * str, unsigned int & = ZERO, const char = '\0' );
78 
84  template< class T >
85  void append( T par )
86  {
87  if(m_Allocated < m_Length + sizeof( T ) + 2)
88  {
89  resize( );
90  }
91  append( getType(par) );
92  memcpy( m_String + m_Length, &par, sizeof( T ) );
93  m_Length += sizeof( T );
94  append( getType(par) );
95  }
96 
101  void append( const char ch );
102 
103 
109  void append( const char * data, int length);
110 
115  void append( LongString * ls);
116 
125  template< class T >
126  bool getParameters( unsigned int & pos, T * pParams, int & paramsCnt )
127  {
128  if(pos >= m_Length)
129  return false;
130 
131  char * pPos = m_String + pos + 1;
132  // while pointer pPos points correctly to m_String buffer
133  // and
134  // the parameter in buffer has the same type as parameter array pParams
135  while(((pPos - m_String) < static_cast<int>(m_Length) ) && (*pPos == getType(pParams)))
136  {
137  // copy parameter to parameter array
138  memcpy(pParams + paramsCnt, ++pPos, sizeof(T));
139  // increment parameter counter
140  paramsCnt++;
141  // move pointer behind the parameter in m_String buffer
142  pPos += sizeof(T)+1;
143  }
144  pos = pPos - m_String - 1;
145  return true;
146  }
147 
152  char * getData( );
153 
154 
162  char * getData( unsigned int & pos, unsigned int & length, char delimiter);
163 
169  char * getSymbol( unsigned int & pos );
170 
180  int matchRight( char ch, int pos, const std::string * ignore = NULL, const std::string * consider = NULL );
181 
191  int matchLeft( char ch, int pos, const std::string * ignore = NULL, const std::string * consider = NULL );
192 
199  char peekSymbol( int & pos, bool direction );
200 
206  int findMatchingRightBracket( int pos );
207 
213  int findMatchingLeftBracket( int pos );
214 
220  std::string toString( );
221 
225  inline char& operator[](unsigned int i) const
226  {
227  return m_String[i];
228  }
229 
234  inline unsigned int length() const
235  {
236  return m_Length;
237  }
238 };
239 }
240 #endif // LONGSTRING_H