~ubuntu-branches/ubuntu/raring/ceph/raring

« back to all changes in this revision

Viewing changes to src/include/cstring.h

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-06-08 15:54:37 UTC
  • mfrom: (1.1.8) (0.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20120608155437-gy3j9k6wzv7w4gn9
Tags: 0.44.1-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - d/control: Switch from libcryptopp to libnss as libcryptopp
    is not seeded.
  - d/control,d/rules: Move from python-support to dh_python2.
  - d/patches/manpage_updates*.patch: cherry picked upstream manpage
    updates warning about lack of encryption, per MIR review.
  - d/rules,d/control: Drop radosgw since libfcgi is not in main and
    the code may not be suitable for LTS.
  - d/rules,d/control: Drop tcmalloc since google perftools is not
    in main yet.
  - d/rules,d/control: Drop ceph-fuse entirely per MIR review
    recommendation.
* d/patches/fix-radosgw-tests.patch: Cherry picked patch from upstream
  VCS to fixup tests to conditionally use radosgw if enabled.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#ifndef CEPH_CSTRING_H
2
 
#define CEPH_CSTRING_H
3
 
 
4
 
/*
5
 
 * cstring - a simple string class
6
 
 *
7
 
 * the key difference between this and std::string is that the string
8
 
 * is stored with a null terminator, providing an efficient c_str()
9
 
 * method.
10
 
 */
11
 
 
12
 
#include "buffer.h"
13
 
#include "encoding.h"
14
 
 
15
 
class cstring {
16
 
 private:
17
 
  int _len;
18
 
  char *_data;
19
 
  
20
 
 public:
21
 
  cstring() : _len(0), _data(0) {}
22
 
  cstring(int l, const char *d=0) : _len(l) {
23
 
    _data = new char[_len + 1];
24
 
    if (d)
25
 
      memcpy(_data, d, l);
26
 
    _data[l] = 0;
27
 
  }
28
 
  cstring(const char *s) { 
29
 
    if (s) {
30
 
      _len = strlen(s);
31
 
      _data = new char[_len + 1];
32
 
      memcpy(_data, s, _len);
33
 
      _data[_len] = 0;
34
 
    } else {
35
 
      _len = 0;
36
 
      _data = 0;
37
 
    }
38
 
  }
39
 
  cstring(const string &s) {
40
 
    _len = s.length();
41
 
    _data = new char[_len + 1];
42
 
    memcpy(_data, s.data(), _len);
43
 
    _data[_len] = 0;
44
 
  }
45
 
  cstring(const cstring &s) {
46
 
    _len = s.length();
47
 
    _data = new char[_len + 1];
48
 
    memcpy(_data, s.data(), _len);
49
 
    _data[_len] = 0;
50
 
  }
51
 
  ~cstring() {
52
 
    if (_data) delete[] _data;
53
 
  }
54
 
 
55
 
  // accessors
56
 
  int length() const { return _len; }
57
 
  bool empty() const { return _len == 0; }
58
 
  char *c_str() const { return _data; }
59
 
  char *data() const { return _data; }
60
 
 
61
 
  //const char *operator() const { return _data; }
62
 
 
63
 
  // modifiers
64
 
  const cstring& operator=(const char *s) {
65
 
    if (_data) delete[] _data;
66
 
    _len = strlen(s);
67
 
    _data = new char[_len + 1];
68
 
    memcpy(_data, s, _len);
69
 
    _data[_len] = 0;
70
 
    return *this;
71
 
  }
72
 
  const cstring& operator=(const string &s) {
73
 
    if (_data) delete[] _data;
74
 
    _len = s.length();
75
 
    _data = new char[_len + 1];
76
 
    memcpy(_data, s.data(), _len);
77
 
    _data[_len] = 0;
78
 
    return *this;
79
 
  }
80
 
  const cstring& operator=(const cstring &ns) {
81
 
    if (_data) delete[] _data;
82
 
    _len = ns.length();
83
 
    _data = new char[_len + 1];
84
 
    memcpy(_data, ns.data(), _len);
85
 
    _data[_len] = 0;
86
 
    return *this;
87
 
  }
88
 
  char &operator[](int n) {
89
 
    assert(n <= _len);
90
 
    return _data[n];
91
 
  }
92
 
  void swap(cstring &other) {
93
 
    int tlen = _len;
94
 
    char *tdata = _data;
95
 
    _len = other._len;
96
 
    _data = other._data;
97
 
    other._len = tlen;
98
 
    other._data = tdata;
99
 
  }
100
 
  void clear() {
101
 
    _len = 0;
102
 
    delete _data;
103
 
    _data = 0;
104
 
  }
105
 
 
106
 
  // encoding
107
 
  void encode(bufferlist &bl) const {
108
 
    __u32 l = _len;
109
 
    ::encode(l, bl);
110
 
    bl.append(_data, _len);
111
 
  }
112
 
  void decode(bufferlist::iterator &bl) {
113
 
    __u32 l;
114
 
    ::decode(l, bl);
115
 
    decode_nohead(l, bl);
116
 
  }
117
 
  void decode_nohead(int l, bufferlist::iterator& bl) {
118
 
    if (_data) delete[] _data;
119
 
    _len = l;
120
 
    _data = new char[_len + 1];
121
 
    bl.copy(_len, _data);
122
 
    _data[_len] = 0;
123
 
  }
124
 
};
125
 
WRITE_CLASS_ENCODER(cstring)
126
 
 
127
 
inline void encode_nohead(const cstring& s, bufferlist& bl)
128
 
{
129
 
  bl.append(s.data(), s.length());
130
 
}
131
 
inline void decode_nohead(int len, cstring& s, bufferlist::iterator& p)
132
 
{
133
 
  s.decode_nohead(len, p);
134
 
}
135
 
 
136
 
 
137
 
#endif