StringBuffer.h

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2004 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037  */
00038 
00039 #ifndef _OASYS_STRING_BUFFER_H_
00040 #define _OASYS_STRING_BUFFER_H_
00041 
00042 #include <algorithm>
00043 
00044 #include "../debug/Log.h"       // for PRINTFLIKE macro
00045 #include "ScratchBuffer.h"
00046 
00047 namespace oasys {
00048 
00049 class ExpandableBuffer;
00050 
00059 class StringBuffer {
00060 public:
00065     StringBuffer(size_t initsz = 256, const char* initstr = 0);
00066 
00068     StringBuffer(const char* fmt, ...) PRINTFLIKE(2, 3);
00069 
00071     StringBuffer(ExpandableBuffer* buffer, bool own_buf);
00073         
00074     ~StringBuffer();
00075 
00079     ExpandableBuffer* expandable_buf() { return buf_; }
00080 
00084     const char* data() const { return buf_->raw_buf(); }
00085 
00089     char* data() { return buf_->raw_buf(); }
00090 
00095     size_t length() const { return buf_->len(); }
00096 
00100     const char* c_str() const;
00101 
00109     size_t append(const char* str, size_t len = 0);
00110 
00117     size_t append(const std::string& str)
00118     {
00119         return append(str.data(), str.length());
00120     }
00121 
00128     size_t append(char c);
00129 
00136     size_t appendf(const char* fmt, ...) PRINTFLIKE(2, 3);
00137 
00145     size_t vappendf(const char* fmt, va_list ap);
00146 
00152     size_t append_int(u_int32_t val, int base);
00153 
00157     void trim(size_t cnt)
00158     {
00159         ASSERT(buf_->len() >= cnt);
00160         buf_->set_len(buf_->len() - cnt);
00161     }
00162 
00166     void set_length(size_t len)
00167     {
00168         ASSERT(buf_->buf_len() >= len);
00169         buf_->set_len(len);
00170     }
00171 
00172 private:
00173     mutable ExpandableBuffer* buf_;
00174     bool    own_buf_;
00175 };
00176 
00185 template<size_t _sz>
00186 class StaticStringBuffer : public StringBuffer {
00187 public:
00191     StaticStringBuffer()
00192         : StringBuffer(new ScratchBuffer<char*, _sz>(), true) {}
00193     
00199     StaticStringBuffer(const char* fmt, ...) PRINTFLIKE(2, 3);
00200 };
00201 
00202 template <size_t _sz>
00203 StaticStringBuffer<_sz>::StaticStringBuffer(const char* fmt, ...)
00204     : StringBuffer(new ScratchBuffer<char*, _sz>(), true)
00205 {
00206     if (fmt != 0) 
00207     {
00208         va_list ap;
00209         va_start(ap, fmt);
00210         vappendf(fmt, ap);
00211         va_end(ap);
00212     }
00213 }
00214 
00215 } // namespace oasys
00216 
00217 #endif /* _OASYS_STRING_BUFFER_H_ */

Generated on Fri Dec 22 14:48:00 2006 for DTN Reference Implementation by  doxygen 1.5.1