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

openrj/cpp/field.hpp

Go to the documentation of this file.
00001 /* /////////////////////////////////////////////////////////////////////////////
00002  * File:    openrj/cpp/field.hpp
00003  *
00004  * Purpose: Field class, in the C++ mapping of the Open-RJ library
00005  *
00006  * Created: 18th June 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_CPP_H_FIELD
00047 #define OPENRJ_INCL_OPENRJ_CPP_H_FIELD
00048 
00049 /* /////////////////////////////////////////////////////////////////////////////
00050  * Version information
00051  */
00052 
00053 #ifndef OPENRJ_DOCUMENTATION_SKIP_SECTION
00054 # define OPENRJ_VER_OPENRJ_CPP_H_FIELD_MAJOR    1
00055 # define OPENRJ_VER_OPENRJ_CPP_H_FIELD_MINOR    4
00056 # define OPENRJ_VER_OPENRJ_CPP_H_FIELD_REVISION 3
00057 # define OPENRJ_VER_OPENRJ_CPP_H_FIELD_EDIT     16
00058 #endif /* !OPENRJ_DOCUMENTATION_SKIP_SECTION */
00059 
00060 /* /////////////////////////////////////////////////////////////////////////////
00061  * Includes
00062  */
00063 
00064 #include <openrj/cpp/openrj.hpp>
00065 
00066 #include <stlsoft/string/shim_string.hpp>
00067 #include <stlsoft/shims/access/string.hpp>
00068 
00069 #include <string.h>
00070 
00071 /* /////////////////////////////////////////////////////////////////////////////
00072  * Namespace
00073  */
00074 
00075 namespace openrj
00076 {
00077 namespace cpp
00078 {
00079 
00080 /* /////////////////////////////////////////////////////////////////////////////
00081  * Classes
00082  */
00083 
00085 class Field
00086 {
00089 public:
00090     typedef Field   class_type;
00092 
00095 public:
00096     Field(ORJField const *field)
00097         : m_field(field)
00098     {}
00099 
00101     Field()
00102         : m_field(NULL)
00103     {}
00105     Field(class_type const &rhs)
00106         : m_field(rhs.m_field)
00107     {}
00108 
00110     class_type &operator =(class_type const &rhs)
00111     {
00112         m_field = rhs.m_field;
00113 
00114         return *this;
00115     }
00117 
00120 public:
00122     char const *GetName() const
00123     {
00124         ORJString const *name;
00125 
00126         ORJ_Field_GetNameA(m_field, &name);
00127 
00128         return name->ptr;
00129     }
00131     char const *GetValue() const
00132     {
00133         ORJString const *value;
00134 
00135         ORJ_Field_GetValueA(m_field, &value);
00136 
00137         return value->ptr;
00138     }
00140 
00143 public:
00144     bool IsEqual(class_type const &rhs) const
00145     {
00146         return 0 == strcmp(GetName(), rhs.GetName()) && 0 == strcmp(GetValue(), rhs.GetValue());
00147     }
00149 
00152 public:
00154     ORJField const  *GetField() const
00155     {
00156         return m_field;
00157     }
00160     ORJRecord const *GetRecord() const
00161     {
00162         openrj_assert(NULL != m_field);
00163 
00164         return ORJ_Field_GetRecordA(m_field);
00165     }
00167 
00168 // Members
00169 private:
00170     ORJField const *m_field;
00171 };
00172 
00173 /* /////////////////////////////////////////////////////////////////////////////
00174  * Operators
00175  */
00176 
00178 inline bool operator ==(Field const &lhs, Field const &rhs)
00179 {
00180     return lhs.IsEqual(rhs);
00181 }
00182 
00184 inline bool operator !=(Field const &lhs, Field const &rhs)
00185 {
00186     return !lhs.IsEqual(rhs);
00187 }
00188 
00189 /* /////////////////////////////////////////////////////////////////////////////
00190  * Shims
00191  */
00192 
00196 inline ::stlsoft::basic_shim_string<char, true> c_str_ptr_null(Field const &f)
00197 {
00198     ORJField const                              *pf =   f.GetField();
00199     size_t                                      cch =   pf->name.len + 1 + pf->value.len;
00200     ::stlsoft::basic_shim_string<char, true>    ss(cch);
00201 
00202     if(cch < ss.size())
00203     {
00204         char    *s  =   ss.data();
00205 
00206         strncpy(s, pf->name.ptr, pf->name.len);
00207         s += pf->name.len;
00208         *s++ = '=';
00209         strncpy(s, pf->value.ptr, pf->value.len);
00210     }
00211 
00212     return ss;
00213 }
00214 
00218 inline ::stlsoft::basic_shim_string<char> c_str_ptr(Field const &f)
00219 {
00220     ORJField const                      *pf =   f.GetField();
00221     size_t                              cch =   pf->name.len + 1 + pf->value.len;
00222     ::stlsoft::basic_shim_string<char>  ss(cch);
00223 
00224     if(cch < ss.size())
00225     {
00226         char    *s  =   ss.data();
00227 
00228         strncpy(s, pf->name.ptr, pf->name.len);
00229         s += pf->name.len;
00230         *s++ = '=';
00231         strncpy(s, pf->value.ptr, pf->value.len);
00232     }
00233 
00234     return ss;
00235 }
00236 
00240 inline ::stlsoft::basic_shim_string<char> c_str_data(Field const &f)
00241 {
00242     return c_str_ptr(f);
00243 }
00244 
00248 inline size_t c_str_len(Field const &f)
00249 {
00250     ORJField const  *pf =   f.GetField();
00251 
00252     return pf->name.len + 1 + pf->value.len;
00253 }
00254 
00258 template <class S>
00259 inline S &operator <<(S &s, Field const &field)
00260 {
00261     s << field.GetName() << "=" << field.GetValue();
00262 
00263     return s;
00264 }
00265 
00266 /* /////////////////////////////////////////////////////////////////////////////
00267  * Namespace
00268  */
00269 
00270 } // namespace cpp
00271 } // namespace openrj
00272 
00273 namespace stlsoft
00274 {
00275     using ::openrj::cpp::c_str_ptr_null;
00276     using ::openrj::cpp::c_str_ptr;
00277     using ::openrj::cpp::c_str_data;
00278     using ::openrj::cpp::c_str_len;
00279 }
00280 
00281 /* ////////////////////////////////////////////////////////////////////////// */
00282 
00283 #endif /* !OPENRJ_INCL_OPENRJ_CPP_H_FIELD */
00284 
00285 /* ////////////////////////////////////////////////////////////////////////// */

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