ScummVM API documentation
px_string.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * Additional copyright for this file:
8  * Copyright (C) 1999-2000 Revolution Software Ltd.
9  * This code is based on source code created by Revolution Software,
10  * used with permission.
11  *
12  * This program is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  *
25  */
26 
27 #ifndef ICB_PX_STRING_H
28 #define ICB_PX_STRING_H
29 
30 #include "engines/icb/common/px_rcutypes.h"
31 
32 namespace ICB {
33 
34 class pxString {
35 protected:
36  char *s; // The actual string
37 
38 public:
39  pxString(); // Empty creator
40  pxString(const char *); // Copy constructor
41  pxString(const pxString &); // Copy constructor
42  ~pxString(); // Destructor
43 
44  operator const char *() const {
45  return (s); // Return a pointer to the string
46  }
47  const char *operator=(const char *); // Assign a value
48  void operator=(const pxString &); // Assign a value
49  const char *operator+=(const char *); // Add a string
50  char &operator[](uint32 n) {
51  return (s[n]); // Get a character (no reason not to allow it to change)
52  }
53  char &operator[](int32 n) {
54  return (s[n]); // Get a character (no reason not to allow it to change)
55  }
56  bool IsNull() const {
57  return ((bool)(s == NULL)); // Check for a null value
58  }
59  bool IsEmpty() const; // Check for an empty string
60  uint32 GetLen() const; // Returns the length of the string.
61  void ToUpper(); // Make contents of string uppercase
62  void ToLower(); // Make contents of string lowercase
63  void ConvertPath(); // Converts a path to native format
64  const char *c_str() { return s; }
65  const pxString &Format(const char *, ...); // Use variable arguments to set the string
66 
67  const pxString operator+(const char *) const;
68 
69  inline pxString Substr(uint32 nStart, uint32 nLen) const; // Return a part of this string
70  void Substr(pxString &rsStr, uint32 nStart, uint32 nLen) const; // A faster substring.
71 
72  void SetString(const char *data, uint32 len); // Get the first len characters from another string
73 
74  uint32 StrChr(char cToFind, uint32 nStartPos = 0) const; // Find position of a character in a string [PS 17/08/98]
75 
76  // char * comparisons
77  bool operator==(const char *string) const; // Do a character by character comparison
78  bool operator!=(const char *string) const; // Do a character by character uncomparison
79 };
80 
81 inline pxString::pxString(const pxString &str) {
82  if (str.s) {
83  // There is some data to copy
84  uint32 len = strlen((char *)str.s) + 1;
85  s = new char[len];
86  memcpy((unsigned char *)s, (unsigned char *)str.s, len);
87  } else
88  // No data for this string
89  s = NULL;
90 }
91 
92 inline pxString::~pxString() {
93  // Destructor
94  if (s)
95  delete[] s;
96 }
97 
98 const char *pxVString(MSVC_PRINTF const char *format, ...) GCC_PRINTF(1, 2);
99 
101  char *m_buffer; // The buffer itself
102  uint32 m_bufLen; // The buffer length
103 
104 public:
105  // explicit
106  pxFlexiCharBuffer(uint32 len = 40);
108 
109  char &operator[](uint32); // Allow array access
110  void CheckSize(uint32); // Make sure we have enough room
111  char *GetBuffer() {
112  return (m_buffer); // Make it a little more difficult to pass the pointer
113  }
114 
115  // Pointer access was originally const char *, but for use as a buffer for reading in from
116  // files this needs to be changeable, and needs a void * operator
117  operator char *() {
118  return (m_buffer); // Treat as a char *
119  }
120  operator void *() {
121  return (m_buffer); // Treat as a void *
122  }
123 
124  void StrCpy(uint32 offset, const char *text); // Copy a string to the buffer
125  void StrnCpy(uint32 offset, const char *text, uint32 len); // Copy a number of characters to the buffer
126 
127  // Prevent copy or assignment
128 private:
130  void operator=(const pxFlexiCharBuffer &) {}
131 };
132 
134  char *m_data;
135 
136 public:
137  pxFixedCharBuffer(uint32 len);
138  ~pxFixedCharBuffer() { delete[] m_data; }
139 
140  operator void *() { return (m_data); }
141  operator char *() { return (m_data); }
142 
143  // Prevent copy or assignment
144 private:
146  void operator=(const pxFixedCharBuffer &) {}
147 };
148 
149 inline pxString::pxString() {
150  // Empty creator
151  s = NULL;
152 }
153 
154 inline pxString::pxString(const char *str) {
155  // Copy constructor
156  if (str) {
157  uint32 len = strlen(str) + 1;
158  s = new char[len];
159  memcpy(s, str, len);
160  } else
161  s = NULL;
162 }
163 
164 inline bool pxString::IsEmpty() const {
165  // Check for an empty string
166  if ((s == NULL) || (*s == 0))
167  return (true);
168 
169  return (false);
170 }
171 
172 inline uint32 pxString::GetLen() const {
173  if ((s == NULL))
174  return (0);
175 
176  return (strlen(s));
177 }
178 
179 inline pxString pxString::Substr(uint32 nStart, uint32 nNum) const {
180  pxString rsRetVal;
181 
182  Substr(rsRetVal, nStart, nNum);
183 
184  return rsRetVal;
185 }
186 
187 inline bool pxString::operator!=(const char *string) const {
188  // Do a character by character uncomparison
189  // Simply return the opposit of the == function
190  return ((bool)!((*this) == string));
191 }
192 
193 } // End of namespace ICB
194 
195 #endif // #ifndef _PX_pxString_H
Definition: px_string.h:34
Definition: px_string.h:100
Definition: actor.h:32
int FORCEINLINE GCC_PRINTF(2, 0) int vsprintf_s(T(&dst)[N]
Definition: px_string.h:133