vhdlstring.h
Go to the documentation of this file.
1 #ifndef VHDLSTRING_H
2 #define VHDLSTRING_H
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 
8 
9 /** @brief Minimal string class with std::string like behaviour that fulfills the JavaCC
10  * string requirements.
11  */
13 {
14  public:
16  {
17  init();
18  }
20  {
21  m_str = (char*)malloc(other.m_len+1);
22  memcpy(m_str,other.m_str,other.m_len);
23  m_len = other.m_len;
24  m_str[m_len]=0;
25  }
27  {
28  if (this!=&other)
29  {
30  free(m_str);
31  m_str = (char*)malloc(other.m_len+1);
32  memcpy(m_str,other.m_str,other.m_len);
33  m_len = other.m_len;
34  m_str[m_len]=0;
35  }
36  return *this;
37  }
38  VhdlString(const char *s)
39  {
40  m_len = strlen(s);
41  m_str=(char*)malloc(m_len+1);
42  memcpy(m_str,s,m_len+1);
43  }
44  VhdlString(const char *s,int size)
45  {
46  m_str = (char*)malloc(size+1);
47  memcpy(m_str,s,size);
48  m_str[size]=0;
49  m_len=size;
50  }
52  {
53  free(m_str);
54  }
55  VhdlString& append(const char *s,int size)
56  {
57  int oldlen = m_len;
58  m_len+=size+1;
59  if (m_len)
60  {
61  m_str = (char*)realloc(m_str,m_len);
62  memcpy(m_str+oldlen,s,m_len-oldlen-1);
63  m_str[m_len-1]=0;
64  }
65  return *this;
66  }
67  VhdlString& append(const char *s)
68  {
69  return append(s,strlen(s));
70  }
72  {
73  return append(other.m_str,other.m_len);
74  }
75  VhdlString substr(int pos=0,int len=-1)
76  {
77  return VhdlString(m_str?m_str+pos:0,len==-1?m_len-pos:m_len);
78  }
79  int copy(char *s,int len,int pos=0) const
80  {
81  if (len==0) return 0;
82  if (pos>=m_len) { s[0]=0; return 0; }
83  int r=m_len<pos+len ? m_len-pos : len;
84  memcpy(s,m_str+pos,r);
85  return r;
86  }
87  const char *c_str() const { return m_str; }
88  const char *data() const { return m_str; }
89  int size() const { return m_len; }
90  int length() const { return m_len; }
91  char & operator[](int i) { return m_str[i]; }
92  const char &operator[](int i) const { return m_str[i]; }
93  void clear() { free(m_str); init(); }
94  VhdlString &operator+=(char c) { char s[2]; s[0]=c; s[1]=0; return append(s); }
95  VhdlString &operator+=(const char *s) { return append(s); }
96 
97  private:
98  void init() { m_str=(char*)calloc(1,1); m_len=0; }
99  char *m_str;
100  int m_len;
101 };
102 
103 #endif
VhdlString & operator+=(const char *s)
Definition: vhdlstring.h:95
VhdlString & append(const VhdlString &other)
Definition: vhdlstring.h:71
int copy(char *s, int len, int pos=0) const
Definition: vhdlstring.h:79
int length() const
Definition: vhdlstring.h:90
char * m_str
Definition: vhdlstring.h:99
VhdlString & operator=(const VhdlString &other)
Definition: vhdlstring.h:26
VhdlString & append(const char *s, int size)
Definition: vhdlstring.h:55
const char * data() const
Definition: vhdlstring.h:88
void clear()
Definition: vhdlstring.h:93
VhdlString(const char *s)
Definition: vhdlstring.h:38
VhdlString(const VhdlString &other)
Definition: vhdlstring.h:19
int size() const
Definition: vhdlstring.h:89
VhdlString & append(const char *s)
Definition: vhdlstring.h:67
const char * c_str() const
Definition: vhdlstring.h:87
VhdlString & operator+=(char c)
Definition: vhdlstring.h:94
VhdlString(const char *s, int size)
Definition: vhdlstring.h:44
Minimal string class with std::string like behaviour that fulfills the JavaCC string requirements...
Definition: vhdlstring.h:12
const char & operator[](int i) const
Definition: vhdlstring.h:92
void init()
Definition: vhdlstring.h:98
static QCString * s
Definition: config.cpp:1042
char & operator[](int i)
Definition: vhdlstring.h:91
VhdlString substr(int pos=0, int len=-1)
Definition: vhdlstring.h:75