Libmacro  0.2
Libmacro is an extensible macro and hotkey library.
string.h
1 /* Libmacro - A multi-platform, extendable macro and hotkey C library
2  Copyright (C) 2013 Jonathan Pelletier, New Paradigm Software
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #ifndef __cplusplus
20  #pragma message "C++ support is required for extras module"
21  #include "mcr/err.h"
22 #endif
23 
24 #ifndef MCR_EXTRAS_UTIL_STRING_H_
25 #define MCR_EXTRAS_UTIL_STRING_H_
26 
27 #include "mcr/extras/base_cpp.h"
28 
29 #include <string>
30 
31 namespace mcr
32 {
33 // \todo std::string
35 class MCR_API String
36 {
37 public:
38  String(const char *val = nullptr, size_t length = (size_t)~0)
39  : _buffer(nullptr), _length(0)
40  {
41  setString(val, length);
42  }
43  String(const std::string &val)
44  : _buffer(nullptr), _length(0)
45  {
46  setString(val.c_str(), val.size());
47  }
48  String(const String&copytron)
49  : _buffer(nullptr), _length(0)
50  {
51  setString(copytron._buffer, copytron._length);
52  }
53  virtual ~String()
54  {
55  clear();
56  }
57  inline String &operator =(const String &copytron)
58  {
59  if (&copytron != this)
60  setString(copytron._buffer, copytron._length);
61  return *this;
62  }
63  inline String &operator =(const char *val)
64  {
65  setString(val);
66  return *this;
67  }
69  inline operator bool() const
70  {
71  return _buffer && _length && _buffer[0] != '\0';
72  }
74  inline bool operator !() const
75  {
76  return empty();
77  }
78 
81  inline const char *operator *() const
82  {
83  return empty() ? "" : string();
84  }
85 
86  inline char &operator[](size_t index)
87  {
88  return _buffer[index];
89  }
90  inline const char &operator[](size_t index) const
91  {
92  return _buffer[index];
93  }
94 
95  inline char *string()
96  {
97  return _buffer;
98  }
99  inline const char *string() const
100  {
101  return _buffer;
102  }
103  void setString(const char *val = nullptr, size_t length = (size_t)~0);
104  inline void terminate()
105  {
106  if (_buffer)
107  _buffer[_length] = '\0';
108  }
109 
110  inline bool empty() const
111  {
112  return !_buffer || !_length || _buffer[0] == '\0';
113  }
114 
115  inline size_t length() const
116  {
117  return _length;
118  }
119  inline size_t size() const
120  {
121  return _length + 1;
122  }
123 
124  inline void clear()
125  {
126  if (_buffer) {
127  delete[] _buffer;
128  _buffer = nullptr;
129  }
130  _length = 0;
131  }
137  void resize(size_t val);
142  void setLength(size_t val);
143 private:
144  char *_buffer;
145  size_t _length;
146 };
147 }
148 
149 #endif
Raise a compiler error. Usage: #include "mcr/err.h"
Libmacro, by Jonathan Pelletier, New Paradigm Software. Alpha version.
Definition: classes.h:31