Changeset 583 for trunk/src2


Ignore:
Timestamp:
Feb 22, 2007, 1:45:50 AM (18 years ago)
Author:
dkearney
Message:

updated RpBuffer? object, cleaning up much of the code.
added putData() calls to RpLibrary?, still need a getData call.
added new function to the scew extra functions to directly add a char* buffer to the xml
updated makefiles as needed

Location:
trunk/src2/core
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src2/core/Makefile.in

    r576 r583  
    1313
    1414CXX             = @CXX@
    15 # CXX             = g++
    1615
    1716CFLAGS=-g -Wall
     
    3837
    3938prefix = @prefix@
    40 # prefix = /apps/rappture/20070216
    4139exec_prefix = ${prefix}
    4240libdir = ${exec_prefix}/lib
     
    8078        ln -s $@ $(SHAREDLIBM)
    8179
    82 #test$(EXE): test.o $(LIBS)
    83 #       $(CXX) $(CFLAGS) -o $@ test.o $(LDFLAGS)
    84 
    8580test(EXE): test.o Ptr.o Outcome.o Lookup.o
    8681        $(CXX) $(CFLAGS) -o $@ test.o Ptr.o Outcome.o Lookup.o
     
    9085        $(CXX) $(CFLAGS) -o $@ RpBuffer_test.o $(LDFLAGS) -L ${libdir} -lz -lb64
    9186
    92 install: $(LIBS)
     87install: $(LIBS) $(SHAREDLIBV)
    9388        -@if [ ! -d $(exec_prefix) ]; then mkdir -p $(exec_prefix); fi
    9489        -@if [ ! -d $(includedir)  ]; then mkdir -p $(includedir); fi
     
    10499        chmod 644 $(includedir)/RpBuffer.h
    105100        cp $(LIBS) $(libdir)
     101        cp $(SHAREDLIBV) $(libdir)
    106102        cd $(libdir); chmod 755 $(LIBS)
    107103        -@(cd $(libdir); $(RANLIB) librappture2.a || true) >/dev/null 2>&1
  • trunk/src2/core/RpBuffer.cc

    r575 r583  
    9393SimpleBuffer::SimpleBuffer(const SimpleBuffer& b)
    9494{
    95     _buf = NULL;
    96     _pos =0;
    97     _size = b._size;
    98     _spaceAvl = b._spaceAvl;
    99     _shallow = false;
    100     _fileState = true;
    101 
    102     _buf = new char[b._spaceAvl];
    103     memcpy((void*) _buf, (void*) b._buf, (size_t) b._spaceAvl);
     95    bufferInit();
     96    append(b.bytes(),b.size());
    10497}
    10598
     
    114107    bufferFree();
    115108    bufferInit();
    116 
    117     _buf = new char[b._spaceAvl];
    118     memcpy((void*) _buf, (void*) b._buf, (size_t) b._spaceAvl);
    119 
    120     _pos = b._pos;
    121     _size = b._size;
    122     _spaceAvl = b._spaceAvl;
    123 
     109    append(b.bytes(),b.size());
     110    return *this;
     111}
     112
     113
     114/**
     115 * Operator +
     116 * @param SimpleBuffer object to add
     117 * @param SimpleBuffer object to add
     118 */
     119SimpleBuffer
     120SimpleBuffer::operator+(const SimpleBuffer& b) const
     121{
     122    SimpleBuffer newBuffer(*this);
     123    newBuffer.operator+=(b);
     124    return newBuffer;
     125}
     126
     127
     128/**
     129 * Operator +=
     130 * @param SimpleBuffer object to add
     131 */
     132SimpleBuffer&
     133SimpleBuffer::operator+=(const SimpleBuffer& b)
     134{
     135    append(b.bytes(),b.size());
    124136    return *this;
    125137}
     
    185197    char *newBuffer = NULL;
    186198
     199    // User specified NULL buffer to append
     200    if (bytes == NULL) {
     201        return 0;
     202    }
     203
    187204    if (nbytes == -1) {
    188205        // user signaled null terminated string
     
    195212    }
    196213
     214    // Empty internal buffer, make sure its properly initialized.
    197215    if (_buf == NULL) {
    198         _size = 0;
    199         _spaceAvl = 0;
     216        bufferInit();
    200217    }
    201218
     
    243260 */
    244261int
    245 SimpleBuffer::read(char* bytes, int nbytes)
     262SimpleBuffer::read(const char* bytes, int nbytes)
    246263{
    247264    int bytesRead = 0;
    248265
     266    // SimpleBuffer is empty.
    249267    if (_buf == NULL) {
    250268        return 0;
    251269    }
    252270
    253     // make sure we dont read off the end of our buffer
     271    // User specified NULL buffer.
     272    if (bytes == NULL) {
     273        return 0;
     274    }
     275
     276    // make sure we don't read off the end of our buffer
    254277    if ( (_pos + nbytes) >= _size) {
    255278        bytesRead = (_size - _pos);
     
    264287
    265288    if (bytesRead > 0) {
    266         memcpy((void*) bytes, (void*) _buf, (size_t) bytesRead);
     289        memcpy((void*) bytes, (void*) (_buf+_pos), (size_t) bytesRead);
    267290    }
    268291
     
    270293
    271294    return bytesRead;
     295}
     296
     297
     298/**
     299 * Set buffer position indicator to spot within the buffer
     300 * @param Offset from whence location in buffer.
     301 * @param Place from where offset is added or subtracted.
     302 * @return 0 on success, anything else is failure
     303 */
     304int
     305SimpleBuffer::seek(int offset, int whence)
     306{
     307    int retVal = 0;
     308
     309    if (_buf == NULL) {
     310        return -1 ;
     311    }
     312
     313    if (whence == SEEK_SET) {
     314        if (offset < 0) {
     315            /* dont go off the beginning of data */
     316            _pos = 0;
     317        }
     318        else if (offset >= _size) {
     319            /* dont go off the end of data */
     320            _pos = _size - 1;
     321        }
     322        else {
     323            _pos = _pos + offset;
     324        }
     325    }
     326    else if (whence == SEEK_CUR) {
     327        if ( (_pos + offset) < 0) {
     328            /* dont go off the beginning of data */
     329            _pos = 0;
     330        }
     331        else if ((_pos + offset) >= _size) {
     332            /* dont go off the end of data */
     333            _pos = _size - 1;
     334        }
     335        else {
     336            _pos = _pos + offset;
     337        }
     338    }
     339    else if (whence == SEEK_END) {
     340        if (offset <= (-1*_size)) {
     341            /* dont go off the beginning of data */
     342            _pos = 0;
     343        }
     344        else if (offset >= 0) {
     345            /* dont go off the end of data */
     346            _pos = _size - 1;
     347        }
     348        else {
     349            _pos = (_size - 1) + offset;
     350        }
     351    }
     352    else {
     353        retVal = -1;
     354    }
     355
     356    return retVal;
     357}
     358
     359
     360/**
     361 * Tell caller the offset of the position indicator from the start of buffer
     362 * @return Number of bytes the position indicator is from start of buffer
     363 */
     364int
     365SimpleBuffer::tell()
     366{
     367   return _pos;
     368}
     369
     370
     371/**
     372 * Read data from the buffer into a memory location provided by caller
     373 */
     374SimpleBuffer&
     375SimpleBuffer::rewind()
     376{
     377    _pos = 0;
     378    return *this;
    272379}
    273380
     
    305412{
    306413    return (_pos >= _size);
    307 }
    308 
    309 
    310 /**
    311  * Use this SimpleBuffer as a temporary pointer to another SimpleBuffer
    312  * This SimpleBuffer does not own the data that it points to, thus
    313  * when clear() or operator=() is called, the data is not deleted,
    314  * but the _buf value is changed. This could lead to a memory leak if
    315  * used improperly.
    316  * @param Pointer location memory to temporarily own.
    317  * @param number of bytes used in the buffer.
    318  * @param number of total bytes in the allocated buffer.
    319  * @return True or false boolean value.
    320  */
    321 SimpleBuffer&
    322 SimpleBuffer::shallowCopy(char* bytes, int nBytes, int spaceAvl)
    323 {
    324     bufferFree();
    325 
    326     _buf = bytes;
    327     _pos = 0;
    328     _size = nBytes;
    329     _spaceAvl = spaceAvl;
    330     _shallow = true;
    331     _fileState = true;
    332 
    333     return *this;
    334414}
    335415
     
    345425SimpleBuffer::move(SimpleBuffer& b)
    346426{
    347     b.bufferFree();
    348 
    349     b._buf = _buf;
    350     b._pos = 0;
    351     b._size = _size;
    352     b._spaceAvl = _spaceAvl;
    353     b._shallow = _shallow;
    354 
    355     bufferInit();
     427    bufferFree();
     428
     429    _buf = b._buf;
     430    _pos = b._pos;
     431    _size = b._size;
     432    _spaceAvl = b._spaceAvl;
     433    _fileState = b._fileState;
     434
     435    b.bufferInit();
    356436
    357437    return *this;
     
    371451    _size = 0;
    372452    _spaceAvl = 0;
    373     _shallow = false;
    374453    _fileState = true;
    375454}
     
    383462SimpleBuffer::bufferFree()
    384463{
    385     if (_shallow == false) {
    386         if (_buf != NULL) {
    387             delete [] _buf;
    388             _buf = NULL;
    389         }
     464    if (_buf != NULL) {
     465        delete [] _buf;
     466        _buf = NULL;
    390467    }
    391468    bufferInit();
     
    422499 */
    423500Buffer::Buffer(const Buffer& b)
    424   : _level(b._level),
     501  : SimpleBuffer(b),
     502    _level(b._level),
    425503    _compressionType(b._compressionType),
    426504    _windowBits(b._windowBits)
    427 {
    428     bufferInit();
    429 
    430     _buf = new char[b._spaceAvl];
    431     memcpy((void*) _buf, (void*) b._buf, (size_t) b._spaceAvl);
    432     _size = b._size;
    433     _spaceAvl = b._spaceAvl;
    434 }
     505{}
    435506
    436507/**
     
    441512Buffer::operator=(const Buffer& b)
    442513{
    443     bufferFree();
    444     bufferInit();
    445 
    446     _buf = new char[b._spaceAvl];
    447     memcpy((void*) _buf, (void*) b._buf, (size_t) b._spaceAvl);
    448     _pos = b._pos;
    449     _size = b._size;
    450     _spaceAvl = b._spaceAvl;
     514    SimpleBuffer::operator=(b);
    451515
    452516    _level = b._level;
    453517    _compressionType = b._compressionType;
    454518    _windowBits = b._windowBits;
    455     _fileState = b._fileState;
    456519
    457520    return *this;
     
    459522
    460523
     524Buffer
     525Buffer::operator+(const Buffer& b) const
     526{
     527    Buffer newBuffer(*this);
     528    newBuffer.operator+=(b);
     529    return newBuffer;
     530}
     531
     532
    461533Buffer&
    462 Buffer::operator=(const SimpleBuffer& b)
    463 {
    464     bufferFree();
    465     bufferInit();
    466 
    467     _buf = new char[b.size()];
    468     memcpy((void*) _buf, (void*) b.bytes(), (size_t) b.size());
    469     _pos = 0;
    470     _size = b.size();
    471     _spaceAvl = b.size();
    472 
    473     _level = 6;
    474     _compressionType = RPCOMPRESS_GZIP;
    475     _windowBits = 15;
    476     _fileState = true;
    477 
     534Buffer::operator+=(const Buffer& b)
     535{
     536    SimpleBuffer::operator+=(b);
    478537    return *this;
    479538}
     
    538597    }
    539598
    540     outFile.write(_buf,_size);
     599    outFile.write(bytes(),size());
    541600    outFile.close();
    542601
     
    555614    err.addContext("Rappture::Buffer::encode()");
    556615
    557     bin.shallowCopy(_buf, _size, _spaceAvl);
     616    rewind();
    558617
    559618    if (compress) {
    560         do_compress(err,bin,bout);
     619        do_compress(err,*this,bout);
    561620    }
    562621
    563622    if (base64) {
    564623        if (compress) {
    565             bout.move(bin);
    566         }
    567         do_base64_enc(err,bin,bout);
     624            bin.move(bout);
     625            do_base64_enc(err,bin,bout);
     626        }
     627        else {
     628            do_base64_enc(err,*this,bout);
     629        }
    568630    }
    569631
    570632    if (!err) {
    571633        // write the encoded data to the internal buffer
    572         bufferFree();
    573         operator=(bout);
     634        move(bout);
    574635    }
    575636
     
    585646    SimpleBuffer bout;
    586647
    587     bin.shallowCopy(_buf, _size, _spaceAvl);
     648    rewind();
    588649
    589650    if (base64) {
    590         do_base64_dec(err,bin,bout);
     651        do_base64_dec(err,*this,bout);
    591652    }
    592653
    593654    if (decompress) {
    594655        if (base64) {
    595             bout.move(bin);
    596         }
    597         do_decompress(err,bin,bout);
     656            bin.move(bout);
     657            do_decompress(err,bin,bout);
     658        }
     659        else {
     660            do_decompress(err,*this,bout);
     661        }
    598662    }
    599663
    600664    if (!err) {
    601665        // write the decoded data to the internal buffer
    602         bufferFree();
    603         operator=(bout);
     666        move(bout);
    604667    }
    605668
     
    609672
    610673void
    611 Buffer::do_compress(Outcome& status, SimpleBuffer& bin, SimpleBuffer& bout)
    612 {
    613     int ret, flush;
    614     unsigned have;
     674Buffer::do_compress(    Outcome& status,
     675                        SimpleBuffer& bin,
     676                        SimpleBuffer& bout  )
     677{
     678    int ret=0, flush=0;
     679    unsigned have=0;
    615680    z_stream strm;
    616681
     
    638703    do {
    639704        strm.avail_in = bin.read(in, CHUNK);
    640         if (bad() == true) {
     705        if (bin.bad() == true) {
    641706            (void)deflateEnd(&strm);
    642707            // return Z_ERRNO;
     
    646711        }
    647712        flush = bin.eof() ? Z_FINISH : Z_NO_FLUSH;
    648         strm.next_in = (unsigned char*) in;
     713        strm.next_in = (Bytef*) in;
    649714        /* run deflate() on input until output buffer not full, finish
    650715           compression if all of source has been read in */
    651716        do {
    652717            strm.avail_out = CHUNK;
    653             strm.next_out = (unsigned char*) out;
     718            strm.next_out = (Bytef*) out;
    654719            ret = deflate(&strm, flush);    /* no bad return value */
    655720            assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
     
    681746
    682747void
    683 Buffer::do_decompress(Outcome& status, SimpleBuffer& bin, SimpleBuffer& bout)
     748Buffer::do_decompress(  Outcome& status,
     749                        SimpleBuffer& bin,
     750                        SimpleBuffer& bout  )
    684751{
    685752    int ret;
     
    709776    do {
    710777        strm.avail_in = bin.read(in, CHUNK);
    711         if (bad() == true) {
     778        if (bin.bad() == true) {
    712779            (void)inflateEnd(&strm);
    713780            // return Z_ERRNO;
     
    761828void
    762829Buffer::do_base64_enc(  Outcome& status,
    763                         SimpleBuffer& bin,
     830                        const SimpleBuffer& bin,
    764831                        SimpleBuffer& bout )
    765832{
     
    782849void
    783850Buffer::do_base64_dec(  Outcome& status,
    784                         SimpleBuffer& bin,
     851                        const SimpleBuffer& bin,
    785852                        SimpleBuffer& bout )
    786853{
  • trunk/src2/core/RpBuffer.h

    r575 r583  
    8686    SimpleBuffer(const SimpleBuffer& b);
    8787    SimpleBuffer& operator=(const SimpleBuffer& b);
     88    SimpleBuffer  operator+(const SimpleBuffer& b) const;
     89    SimpleBuffer& operator+=(const SimpleBuffer& b);
    8890    virtual ~SimpleBuffer();
    8991
     
    9395    SimpleBuffer& clear();
    9496    int append(const char* bytes, int nbytes=-1);
    95     int read(char* bytes, int nbytes);
     97    int read(const char* bytes, int nbytes);
     98    int seek(int offset, int whence);
     99    int tell();
     100    SimpleBuffer& rewind();
    96101
    97102    bool good() const;
     
    99104    bool eof() const;
    100105
    101     SimpleBuffer& shallowCopy(char* bytes, int nBytes, int spaceAvl);
    102106    SimpleBuffer& move(SimpleBuffer& b);
    103107
    104108protected:
     109
     110    void bufferInit();
     111    void bufferFree();
     112
     113private:
     114
    105115    /// Pointer to the memory that holds our buffer's data
    106116    char* _buf;
     
    115125    int _spaceAvl;
    116126
    117     /// Shallow copy flag.
    118     bool _shallow;
    119 
    120127    /// State of the last file like operation.
    121128    bool _fileState;
    122 
    123     void bufferInit();
    124     void bufferFree();
    125129};
    126130
     
    137141    Buffer(const char* bytes, int nbytes=-1);
    138142    Buffer(const Buffer& buffer);
    139     Buffer& operator=(const Buffer& buffer);
     143    Buffer& operator=(const Buffer& b);
     144    Buffer  operator+(const Buffer& b) const;
     145    Buffer& operator+=(const Buffer& b);
    140146    virtual ~Buffer();
    141147
     
    158164    enum { CHUNK = 4096 };
    159165
    160     Buffer& operator=(const SimpleBuffer& buffer);
    161     void do_compress(Outcome& status,SimpleBuffer& bin,SimpleBuffer& bout);
    162     void do_decompress(Outcome& status,SimpleBuffer& bin,SimpleBuffer& bout);
    163     void do_base64_enc(Outcome& status,SimpleBuffer& bin,SimpleBuffer& bout);
    164     void do_base64_dec(Outcome& status,SimpleBuffer& bin,SimpleBuffer& bout);
     166    void do_compress(   Outcome& status,
     167                        SimpleBuffer& bin,
     168                        SimpleBuffer& bout  );
     169    void do_decompress( Outcome& status,
     170                        SimpleBuffer& bin,
     171                        SimpleBuffer& bout  );
     172    void do_base64_enc( Outcome& status,
     173                        const SimpleBuffer& bin,
     174                        SimpleBuffer& bout  );
     175    void do_base64_dec( Outcome& status,
     176                        const SimpleBuffer& bin,
     177                        SimpleBuffer& bout  );
    165178};
    166179
  • trunk/src2/core/RpBuffer_test.cc

    r575 r583  
    449449        return (tests == passed);
    450450    }
     451
     452    passed++;
     453    /* =========================================================== */
     454
     455    return (tests == passed);
     456}
     457
     458int testFile()
     459{
     460    int passed = 0;
     461    int tests = 0;
     462
     463
     464    /* =========================================================== */
     465    tests++;
     466    const char* inFile = "out.dx";
     467    const char* outFile = "out.dx.gz";
     468    Rappture::Buffer buffer1;
     469    buffer1.load(inFile);
     470
     471    // compress, dont encode
     472    buffer1.encode(true,false);
     473    std::remove(outFile);
     474    buffer1.dump(outFile);
     475    buffer1.decode(true,false);
     476
     477    /*
     478    if (size1before != 26) {
     479        std::cout << "Error testClear1" << std::endl;
     480        std::cout << "incorrect buffer size" << std::endl;
     481        return (tests == passed);
     482    }
     483    if (size1after != 0) {
     484        std::cout << "Error testClear1" << std::endl;
     485        std::cout << "clear failed buffer size" << std::endl;
     486        return (tests == passed);
     487    }
     488    */
    451489
    452490    passed++;
     
    468506    passed += testLoad();
    469507    passed += testClear();
    470 
    471     if (passed != 8) {
     508    passed += testFile();
     509
     510    if (passed != 9) {
    472511        printf("failed: %d\n", passed);
    473512    }
Note: See TracChangeset for help on using the changeset viewer.