00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00046 #ifndef OPENRJ_INCL_OPENRJ_CPP_HPP_FUNCTIONS
00047 #define OPENRJ_INCL_OPENRJ_CPP_HPP_FUNCTIONS
00048
00049
00050
00051
00052
00053 #ifndef OPENRJ_DOCUMENTATION_SKIP_SECTION
00054 # define OPENRJ_VER_OPENRJ_CPP_HPP_FUNCTIONS_MAJOR 1
00055 # define OPENRJ_VER_OPENRJ_CPP_HPP_FUNCTIONS_MINOR 1
00056 # define OPENRJ_VER_OPENRJ_CPP_HPP_FUNCTIONS_REVISION 3
00057 # define OPENRJ_VER_OPENRJ_CPP_HPP_FUNCTIONS_EDIT 8
00058 #endif
00059
00060
00061
00062
00063
00064 #include <openrj/cpp/openrj.hpp>
00065 #include <openrj/cpp/field.hpp>
00066 #include <openrj/cpp/record.hpp>
00067 #include <openrj/cpp/databasebase.hpp>
00068
00069 #include <stlsoft/memory/auto_buffer.hpp>
00070 #include <stlsoft/shims/access/string.hpp>
00071 #include <stdio.h>
00072
00073
00074
00075
00076
00077 namespace openrj
00078 {
00079 namespace cpp
00080 {
00081
00082
00083
00084
00085
00086 #ifndef OPENRJ_DOCUMENTATION_SKIP_SECTION
00087 namespace helper
00088 {
00089 inline Record throw_for_missing_Record(char const *reason, char const *recordName)
00090 {
00091 try
00092 {
00093 const size_t cchReason = ::strlen(reason);
00094 const size_t cchRecordName = ::strlen(recordName);
00095 stlsoft::auto_buffer<char> message(cchReason + 2 + cchRecordName + 1);
00096
00097 if(message.empty())
00098 {
00099 goto plain_throw;
00100 }
00101 else
00102 {
00103 ::sprintf(&message[0], "%*s: %*s", (int)cchReason, reason, (int)cchRecordName, recordName);
00104
00105 throw ::std::out_of_range(message.data());
00106 }
00107 }
00108 catch(std::out_of_range &)
00109 {
00110 throw;
00111 }
00112 catch(...)
00113 {
00114 plain_throw:
00115 throw ::std::out_of_range(reason);
00116 }
00117
00118 return Record();
00119 }
00120
00121 }
00122 #endif
00123
00124
00125
00126
00127
00135 inline String Lookup(char const *fieldName, Record const &r0, char const *defaultValue)
00136 {
00137 return r0.HasField(fieldName) ? r0[fieldName] : defaultValue;
00138 }
00139
00149 inline String Lookup(char const *fieldName, Record const &r0, Record const &r1)
00150 {
00151 return r0.HasField(fieldName) ? r0[fieldName] : r1[fieldName];
00152 }
00153
00162 inline String Lookup(char const *fieldName, Record const &r0, Record const &r1, char const *defaultValue)
00163 {
00164 return r0.HasField(fieldName) ? r0[fieldName] : r1.HasField(fieldName) ? r1[fieldName] : defaultValue;
00165 }
00166
00167
00168 inline Record FindFirstRecordWithFieldName(DatabaseBase const &db, char const *name)
00169 {
00170 { for(size_t i = 0; i < db.GetNumRecords(); ++i)
00171 {
00172 if(db[i].HasField(name))
00173 {
00174 return db[i];
00175 }
00176 }}
00177
00178 return helper::throw_for_missing_Record("Matching record not found", name);
00179 }
00180
00181 template <typename S>
00182 inline Record FindFirstRecordWithFieldName(DatabaseBase const &db, S const &name)
00183 {
00184 return FindFirstRecordWithFieldName(db, ::stlsoft::c_str_ptr(name));
00185 }
00186
00187 #if 0
00188 inline
00189
00190 inline Record find_first_record_with_field(DatabaseBase const &db, Field const &f)
00191 {
00192 { for(size_t i = 0; i < db.GetNumRecords(); ++i)
00193 {
00194 if(db[i].HasFieldWithValue(name, value))
00195 {
00196 return db[i];
00197 }
00198 }
00199
00200 throw ::std::out_of_range("Matching record not found");
00201
00202 return Record();
00203 }
00204
00205
00206 inline Record find_first_record_match(DatabaseBase const &db, Field const &f)
00207 {
00208 }
00209
00210 template <typename S>
00211 inline Record find_first_record_match(DatabaseBase const &db, S const &name)
00212 {
00213 }
00214
00215 inline Record find_first_record_match(DatabaseBase const &db, Field const &f)
00216 {
00217 }
00218
00219 #endif
00220
00221
00222
00223
00224
00225 }
00226 }
00227
00228
00229
00230 #endif
00231
00232