coin-Bcp
BCP_string.hpp
Go to the documentation of this file.
1 // Copyright (C) 2000, International Business Machines
2 // Corporation and others. All Rights Reserved.
3 #ifndef _BCP_STRING_H
4 #define _BCP_STRING_H
5 
6 // This file is fully docified.
7 
8 #include <cstring>
9 
13 class BCP_string {
14 public:
15  /* Return the length of the string. */
16  int length() const { return _len; }
17  /* Return a pointer to the data stored in the string. I.e., return a C
18  style string. */
19  const char * c_str() const { return _data; }
20 private:
21  /* the length of the string */
22  int _len;
23  /* the data in the string */
24  char * _data;
25 public:
26  /* The default constructor creates an empty sting. */
27  BCP_string() : _len(0), _data(0) {}
28  /* Create a <code>BCP_string</code> from a C style string. */
29  BCP_string(const char * str) {
30  _len = std::strlen(str);
31  _data = new char[_len+1];
32  std::memcpy(_data, str, _len);
33  _data[_len] = 0;
34  }
35  /* Make a copy of the argument string. */
36  BCP_string(const BCP_string& str) {
37  _len = str.length();
38  _data = new char[_len+1];
39  std::memcpy(_data, str.c_str(), _len);
40  _data[_len] = 0;
41  }
42  /* Delete the data members. */
44  delete[] _data;
45  }
46  /* This methods replaces the current <code>BCP_string</code> with one
47  create from the first <code>len</code> bytes in <code>source</code>. */
48  BCP_string& assign(const char * source, const int len) {
49  delete[] _data;
50  _len = len;
51  _data = new char[_len+1];
52  std::memcpy(_data, source, _len);
53  _data[_len] = 0;
54  return *this;
55  }
56  /* replace the current <code>BCP_string</code> with a copy of the argument
57  */
59  return assign(str.c_str(), str.length());
60  }
61  /* replace the current <code>BCP_string</code> with a copy of the argument
62  C style string. */
63  BCP_string& operator= (const char * str) {
64  return assign(str, std::strlen(str));
65  }
66 
67 };
68 
70 inline bool operator==(const BCP_string& s0, const char* s1) {
71  if (s0.c_str() == 0)
72  return s1 == 0;
73  return s1 == 0 ? false : (std::strcmp(s0.c_str(), s1) == 0);
74 }
75 
77 inline bool
78 operator==(const BCP_string& s0, const BCP_string& s1) {
79  return s0 == s1.c_str();
80 }
81 
83 inline bool
84 operator==(const char* s0, const BCP_string& s1) {
85  return s1 == s0;
86 }
87 
88 #endif
BCP_string(const BCP_string &str)
Definition: BCP_string.hpp:36
const char * c_str() const
Definition: BCP_string.hpp:19
bool operator==(const BCP_obj_change &ch0, const BCP_obj_change &ch1)
This class is a very simple impelementation of a constant length string.
Definition: BCP_string.hpp:13
BCP_string(const char *str)
Definition: BCP_string.hpp:29
BCP_string & assign(const char *source, const int len)
Definition: BCP_string.hpp:48
BCP_string & operator=(const BCP_string &str)
Definition: BCP_string.hpp:58
int length() const
Definition: BCP_string.hpp:16
char * _data
Definition: BCP_string.hpp:24