Blame dwarfgen/strtabdata.h

Packit cdaae3
#ifndef STRTABDATA_H
Packit cdaae3
#define STRTABDATA_H
Packit cdaae3
/*
Packit cdaae3
  Copyright (C) 2010-2013 David Anderson.  All rights reserved.
Packit cdaae3
Packit cdaae3
  Redistribution and use in source and binary forms, with or without
Packit cdaae3
  modification, are permitted provided that the following conditions are met:
Packit cdaae3
  * Redistributions of source code must retain the above copyright
Packit cdaae3
    notice, this list of conditions and the following disclaimer.
Packit cdaae3
  * Redistributions in binary form must reproduce the above copyright
Packit cdaae3
    notice, this list of conditions and the following disclaimer in the
Packit cdaae3
    documentation and/or other materials provided with the distribution.
Packit cdaae3
  * Neither the name of the example nor the
Packit cdaae3
    names of its contributors may be used to endorse or promote products
Packit cdaae3
    derived from this software without specific prior written permission.
Packit cdaae3
Packit cdaae3
  THIS SOFTWARE IS PROVIDED BY David Anderson ''AS IS'' AND ANY
Packit cdaae3
  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Packit cdaae3
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Packit cdaae3
  DISCLAIMED. IN NO EVENT SHALL David Anderson BE LIABLE FOR ANY
Packit cdaae3
  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Packit cdaae3
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Packit cdaae3
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
Packit cdaae3
  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit cdaae3
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Packit cdaae3
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit cdaae3
Packit cdaae3
*/
Packit cdaae3
Packit cdaae3
Packit cdaae3
// strtabdata.h
Packit cdaae3
// Creates a string table in a way consistent with
Packit cdaae3
// elf string tables. The zero index is a null byte always.
Packit cdaae3
Packit cdaae3
class strtabdata {
Packit cdaae3
public:
Packit cdaae3
    strtabdata(): data_(new char[1000]),
Packit cdaae3
        datalen_(1000), nexttouse_(0) { data_[0] = 0; nexttouse_ = 1;};
Packit cdaae3
    ~strtabdata()  { delete[]data_; };
Packit cdaae3
    unsigned addString(const std::string & newstr) {
Packit cdaae3
        // The 1 is for the terminating null byte.
Packit cdaae3
        unsigned nsz = newstr.size() +1;
Packit cdaae3
        unsigned needed = nexttouse_ + nsz;
Packit cdaae3
        if (needed >= datalen_) {
Packit cdaae3
            unsigned baseincr = nsz;
Packit cdaae3
            unsigned altincr = datalen_*2;
Packit cdaae3
            if(altincr> baseincr) {
Packit cdaae3
                baseincr = altincr;
Packit cdaae3
            }
Packit cdaae3
            unsigned newsize = datalen_ + baseincr;
Packit cdaae3
            char *newdata = new char [newsize];
Packit cdaae3
            memcpy(newdata, data_, nexttouse_);
Packit cdaae3
            delete [] data_;
Packit cdaae3
            data_ = newdata;
Packit cdaae3
            datalen_ = newsize;
Packit cdaae3
        }
Packit cdaae3
        memcpy(data_ + nexttouse_, newstr.c_str(),nsz);
Packit cdaae3
        unsigned newstrindex = nexttouse_;
Packit cdaae3
        nexttouse_ += nsz;
Packit cdaae3
        return newstrindex;
Packit cdaae3
    };
Packit cdaae3
    void *exposedata() {return (void *)data_;};
Packit cdaae3
    unsigned exposelen() const {return nexttouse_;};
Packit cdaae3
private:
Packit cdaae3
    char *   data_;
Packit cdaae3
Packit cdaae3
    // datalen_ is the size in bytes pointed to by data_ .
Packit cdaae3
    unsigned datalen_;
Packit cdaae3
Packit cdaae3
    // nexttouse_ is the index of the next (unused) byte in
Packit cdaae3
    // data_ , so it is also the amount of space in data_ that
Packit cdaae3
    // is in use.
Packit cdaae3
    unsigned nexttouse_;
Packit cdaae3
};
Packit cdaae3
#endif /* STRTABDATA_H */