Open-RJ C/C++ User's Journal Synesis Software STLSoft - ... Robust, Lightweight, Cross-platform, Template Software ...

openrj/stl/field.hpp

Go to the documentation of this file.
00001 /* /////////////////////////////////////////////////////////////////////////////
00002  * File:    openrj/stl/field.hpp
00003  *
00004  * Purpose: field class, in the STL mapping of the Open-RJ library.
00005  *
00006  * Created: 28th September 2004
00007  * Updated: 14th July 2006
00008  *
00009  * Home:    http://openrj.org/
00010  *
00011  * Copyright (c) 2004-2006, Matthew Wilson and Synesis Software
00012  * All rights reserved.
00013  *
00014  * Redistribution and use in source and binary forms, with or without
00015  * modification, are permitted provided that the following conditions are met:
00016  *
00017  * - Redistributions of source code must retain the above copyright notice, this
00018  *   list of conditions and the following disclaimer.
00019  * - Redistributions in binary form must reproduce the above copyright notice,
00020  *   this list of conditions and the following disclaimer in the documentation
00021  *   and/or other materials provided with the distribution.
00022  * - Neither the names of Matthew Wilson and Synesis Software nor the names of
00023  *   any 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 "AS IS"
00027  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00029  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00030  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00031  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00032  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00033  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00034  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00035  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00036  * POSSIBILITY OF SUCH DAMAGE.
00037  *
00038  * ////////////////////////////////////////////////////////////////////////// */
00039 
00040 
00046 #ifndef OPENRJ_INCL_OPENRJ_STL_HPP_FIELD
00047 #define OPENRJ_INCL_OPENRJ_STL_HPP_FIELD
00048 
00049 /* /////////////////////////////////////////////////////////////////////////////
00050  * Version information
00051  */
00052 
00053 #ifndef OPENRJ_DOCUMENTATION_SKIP_SECTION
00054 # define OPENRJ_VER_OPENRJ_STL_HPP_FIELD_MAJOR      1
00055 # define OPENRJ_VER_OPENRJ_STL_HPP_FIELD_MINOR      4
00056 # define OPENRJ_VER_OPENRJ_STL_HPP_FIELD_REVISION   4
00057 # define OPENRJ_VER_OPENRJ_STL_HPP_FIELD_EDIT       18
00058 #endif /* !OPENRJ_DOCUMENTATION_SKIP_SECTION */
00059 
00060 /* /////////////////////////////////////////////////////////////////////////////
00061  * Includes
00062  */
00063 
00064 #include <openrj/stl/openrj.hpp>
00065 
00066 #include <stlsoft/string/shim_string.hpp>
00067 #include <stlsoft/shims/access/string.hpp>
00068 
00069 #include <stdexcept>
00070 
00071 /* /////////////////////////////////////////////////////////////////////////////
00072  * Compiler warnings
00073  */
00074 
00075 #ifdef __BORLANDC__
00076 # pragma warn -8027
00077 #endif /* __BORLANDC__ */
00078 
00079 /* /////////////////////////////////////////////////////////////////////////////
00080  * Namespace
00081  */
00082 
00083 namespace openrj
00084 {
00085 
00086 namespace stl
00087 {
00088 
00089 /* /////////////////////////////////////////////////////////////////////////////
00090  * Classes
00091  */
00092 
00094 class field
00095 {
00098 public:
00099     typedef field       class_type;
00101 
00104 public:
00106     explicit field(ORJField const *field);
00108     field();
00110     field(field const &rhs);
00112     field &operator =(field const &rhs);
00114 
00117 public:
00119     string_t name() const;
00121     string_t value() const;
00123 
00126 public:
00128     ORJField const  *get_field() const;
00131     ORJRecord const *get_record() const;
00133 
00134 // Members
00135 private:
00136     ORJField const *m_field;
00137 };
00138 
00139 /* /////////////////////////////////////////////////////////////////////////////
00140  * Shims
00141  */
00142 
00146 inline stlsoft::basic_shim_string<char, true> c_str_ptr_null(field const &f)
00147 {
00148     ORJField const  *pf =   f.get_field();
00149     string_t        s;
00150 
00151     s.reserve(pf->name.len + 1 + pf->value.len);
00152 
00153     s.append(pf->name.ptr, pf->name.len);
00154     s.append(1, '=');
00155     s.append(pf->value.ptr, pf->value.len);
00156 
00157     return stlsoft::basic_shim_string<char, true>(s.c_str(), s.length());
00158 }
00159 
00163 inline stlsoft::basic_shim_string<char> c_str_ptr(field const &f)
00164 {
00165     ORJField const  *pf =   f.get_field();
00166     string_t        s;
00167 
00168     s.reserve(pf->name.len + 1 + pf->value.len);
00169 
00170     s.append(pf->name.ptr, pf->name.len);
00171     s.append(1, '=');
00172     s.append(pf->value.ptr, pf->value.len);
00173 
00174     return stlsoft::basic_shim_string<char>(s.c_str(), s.length());
00175 }
00176 
00180 inline size_t c_str_len(field const &f)
00181 {
00182     ORJField const  *pf =   f.get_field();
00183 
00184     return pf->name.len + 1 + pf->value.len;
00185 }
00186 
00190 inline ::stlsoft::basic_shim_string<char> c_str_data(field const &f)
00191 {
00192     return c_str_ptr(f);
00193 }
00194 
00198 template <class S>
00199 inline S &operator <<(S &s, field const &field)
00200 {
00201     s << field.name() << "=" << field.value();
00202 
00203     return s;
00204 }
00205 
00206 /* /////////////////////////////////////////////////////////////////////////////
00207  * Implementation
00208  */
00209 
00210 inline field::field(ORJField const *field)
00211     : m_field(field)
00212 {
00213     openrj_assert(NULL != field);
00214     openrj_assert(0 == field->mbz0);
00215     openrj_assert(static_cast<ptrdiff_t>(field->name.len) >= 0);
00216     openrj_assert(static_cast<ptrdiff_t>(field->value.len) >= 0);
00217 }
00218 
00219 inline field::field()
00220     : m_field(NULL)
00221 {}
00222 
00223 inline field::field(field const &rhs)
00224     : m_field(rhs.m_field)
00225 {
00226     openrj_assert(NULL == m_field || 0 == rhs.m_field->mbz0);
00227     openrj_assert(NULL == m_field || static_cast<ptrdiff_t>(rhs.m_field->name.len) >= 0);
00228     openrj_assert(NULL == m_field || static_cast<ptrdiff_t>(rhs.m_field->value.len) >= 0);
00229 }
00230 
00231 inline field &field::operator =(field const &rhs)
00232 {
00233     m_field = rhs.m_field;
00234 
00235     return *this;
00236 }
00237 
00238 inline string_t field::name() const
00239 {
00240     if(NULL == m_field)
00241     {
00242         return string_t();
00243     }
00244 
00245     openrj_assert(NULL != m_field);
00246     openrj_assert(0 == m_field->mbz0);
00247     openrj_assert(static_cast<ptrdiff_t>(m_field->name.len) >= 0);
00248     openrj_assert(static_cast<ptrdiff_t>(m_field->value.len) >= 0);
00249 
00250     return string_t(m_field->name.ptr, m_field->name.len);
00251 }
00252 
00253 inline string_t field::value() const
00254 {
00255     if(NULL == m_field)
00256     {
00257         return string_t();
00258     }
00259 
00260     openrj_assert(NULL != m_field);
00261     openrj_assert(0 == m_field->mbz0);
00262     openrj_assert(static_cast<ptrdiff_t>(m_field->name.len) >= 0);
00263     openrj_assert(static_cast<ptrdiff_t>(m_field->value.len) >= 0);
00264 
00265     return string_t(m_field->value.ptr, m_field->value.len);
00266 }
00267 
00268 inline ORJField const *field::get_field() const
00269 {
00270     return m_field;
00271 }
00272 
00273 inline ORJRecord const *field::get_record() const
00274 {
00275     openrj_assert(NULL != m_field);
00276 
00277     return ORJ_Field_GetRecordA(m_field);
00278 }
00279 
00280 /* /////////////////////////////////////////////////////////////////////////////
00281  * Namespace
00282  */
00283 
00284 } // namespace stl
00285 } // namespace openrj
00286 
00287 namespace stlsoft
00288 {
00289     using ::openrj::stl::c_str_ptr_null;
00290     using ::openrj::stl::c_str_ptr;
00291     using ::openrj::stl::c_str_data;
00292     using ::openrj::stl::c_str_len;
00293 }
00294 
00295 /* ////////////////////////////////////////////////////////////////////////// */
00296 
00297 #endif /* !OPENRJ_INCL_OPENRJ_STL_HPP_FIELD */
00298 
00299 /* ////////////////////////////////////////////////////////////////////////// */

Open-RJ Library documentation © Synesis Software Pty Ltd, 2004-2005