Debian Codegraph @

src/bin/dwarfgen/strtabdata.h

raw
1 #ifndef STRTABDATA_H
2 #define STRTABDATA_H
3 /*
4 Copyright (C) 2010-2013 David Anderson. All rights reserved.
5
6 Redistribution and use in source and binary forms,
7 with or without modification, are permitted provided
8 that the following conditions are met:
9 * Redistributions of source code must retain the above
10 copyright notice, this list of conditions and the following
11 disclaimer.
12 * Redistributions in binary form must reproduce the
13 above copyright notice, this list of conditions and the following
14 disclaimer in the documentation and/or other materials
15 provided with the distribution.
16 * Neither the name of the example nor the
17 names of its contributors may be used to endorse or
18 promote products derived from this software without
19 specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY David Anderson ''AS IS''
22 AND ANY
23 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
26 SHALL David Anderson BE LIABLE FOR ANY DIRECT, INDIRECT,
27 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
31 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34 OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 // strtabdata.h
38 // Creates a string table in a way consistent with
39 // elf string tables. The zero index is a null byte always.
40
41 class strtabdata {
42 public:
43 strtabdata(): data_(new char[1000]),
44 datalen_(1000), nexttouse_(0) { data_[0] = 0;
45 nexttouse_ = 1;};
46 ~strtabdata() { delete[]data_; };
47 unsigned addString(const std::string & newstr) {
48 // The 1 is for the terminating null byte.
49 unsigned nsz = newstr.size() +1;
50 unsigned needed = nexttouse_ + nsz;
51 if (needed >= datalen_) {
52 unsigned baseincr = nsz;
53 unsigned altincr = datalen_*2;
54 if (altincr> baseincr) {
55 baseincr = altincr;
56 }
57 unsigned newsize = datalen_ + baseincr;
58 char *newdata = new char [newsize];
59 memcpy(newdata, data_, nexttouse_);
60 delete [] data_;
61 data_ = newdata;
62 datalen_ = newsize;
63 }
64 memcpy(data_ + nexttouse_, newstr.c_str(),nsz);
65 unsigned newstrindex = nexttouse_;
66 nexttouse_ += nsz;
67 return newstrindex;
68 };
69 std::string asString(unsigned offset) {
70 return std::string(data_+ offset);
71 }
72 void *exposedata() {return (void *)data_;};
73 unsigned exposelen() const {return nexttouse_;};
74 private:
75 char * data_;
76
77 // datalen_ is the size in bytes pointed to by data_ .
78 unsigned datalen_;
79
80 // nexttouse_ is the index of the next (unused) byte in
81 // data_ , so it is also the amount of space in data_ that
82 // is in use.
83 unsigned nexttouse_;
84 };
85 #endif /* STRTABDATA_H */