record-internal.h

Go to the documentation of this file.
00001 ///
00002 /// \file       record-internal.h
00003 ///             Support functions, types, and templates for the
00004 ///             general record parsing classes in r_*.h files.
00005 ///             This header is NOT installed for applications to
00006 ///             use, so it is safe to put library-specific things
00007 ///             in here.
00008 ///
00009 
00010 /*
00011     Copyright (C) 2005-2012, Net Direct Inc. (http://www.netdirect.ca/)
00012 
00013     This program is free software; you can redistribute it and/or modify
00014     it under the terms of the GNU General Public License as published by
00015     the Free Software Foundation; either version 2 of the License, or
00016     (at your option) any later version.
00017 
00018     This program is distributed in the hope that it will be useful,
00019     but WITHOUT ANY WARRANTY; without even the implied warranty of
00020     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00021 
00022     See the GNU General Public License in the COPYING file at the
00023     root directory of this project for more details.
00024 */
00025 
00026 #ifndef __BARRY_RECORD_INTERNAL_H__
00027 #define __BARRY_RECORD_INTERNAL_H__
00028 
00029 #include <string>
00030 #include "protostructs.h"
00031 #include "error.h"
00032 #include "endian.h"
00033 #include "record.h"
00034 
00035 // forward declarations
00036 namespace Barry {
00037         class Data;
00038         class IConverter;
00039 }
00040 
00041 namespace Barry {
00042 
00043 template <class RecordT>
00044 const unsigned char*  ParseCommonFields(RecordT &rec,
00045                                         const void *begin,
00046                                         const void *end,
00047                                         const IConverter *ic = 0)
00048 {
00049         const unsigned char *b = (const unsigned char*) begin;
00050         const unsigned char *e = (const unsigned char*) end;
00051 
00052         while( (b + COMMON_FIELD_HEADER_SIZE) < e )
00053                 b = rec.ParseField(b, e, ic);
00054         return b;
00055 }
00056 
00057 // Does not exist, so error will be caught by linker instead of at runtime.
00058 void ConvertHtoB_Not_Implemented();
00059 
00060 // Use templates here to guarantee types are converted in the strictest manner.
00061 template <class SizeT>
00062 inline SizeT ConvertHtoB(SizeT s)
00063 {
00064         ConvertHtoB_Not_Implemented();
00065 //      throw Error("Not implemented.");
00066 }
00067 
00068 // specializations for specific sizes
00069 template <> inline uint8_t ConvertHtoB<uint8_t>(uint8_t s)    { return s; }
00070 template <> inline uint16_t ConvertHtoB<uint16_t>(uint16_t s) { return htobs(s); }
00071 template <> inline uint32_t ConvertHtoB<uint32_t>(uint32_t s) { return htobl(s); }
00072 template <> inline uint64_t ConvertHtoB<uint64_t>(uint64_t s) { return htobll(s); }
00073 
00074 
00075 template <class RecordT>
00076 struct FieldLink
00077 {
00078         int type;
00079         const char *name;
00080         const char *ldif;
00081         const char *objectClass;
00082         std::string RecordT::* strMember;       // FIXME - find a more general
00083         EmailAddressList RecordT::* addrMember; // way to do this...
00084         Barry::TimeT RecordT::* timeMember;
00085         PostalAddress RecordT::* postMember;
00086         std::string PostalAddress::* postField;
00087         bool iconvNeeded;
00088 };
00089 
00090 void BuildField1900(Data &data, size_t &size, uint8_t type, time_t t);
00091 inline void BuildField1900(Data &data, size_t &size, uint8_t type, const Barry::TimeT &t)
00092 {
00093         BuildField1900(data, size, type, t.Time);
00094 }
00095 void BuildField(Data &data, size_t &size, uint8_t type, char c);
00096 void BuildField(Data &data, size_t &size, uint8_t type, uint8_t c);
00097 void BuildField(Data &data, size_t &size, uint8_t type, uint16_t value);
00098 void BuildField(Data &data, size_t &size, uint8_t type, uint32_t value);
00099 void BuildField(Data &data, size_t &size, uint8_t type, uint64_t value);
00100 void BuildField(Data &data, size_t &size, uint8_t type, const std::string &str);
00101 void BuildField(Data &data, size_t &size, uint8_t type, const void *buf, size_t bufsize);
00102 void BuildField(Data &data, size_t &size, const Barry::UnknownField &field);
00103 void BuildField(Data &data, size_t &size, uint8_t type, const Barry::Protocol::GroupLink &link);
00104 std::string ParseFieldString(const Barry::Protocol::CommonField *field);
00105 std::string ParseFieldString(const void *data, uint16_t maxlen);
00106 
00107 
00108 // Functions to help build JDWP command packets
00109 void AddJDWByte(Barry::Data &data, size_t &size, const uint8_t value);
00110 void AddJDWInt(Barry::Data &data, size_t &size, const uint32_t value);
00111 void AddJDWChar(Barry::Data &data, size_t &size, const void *buf, size_t bufsize);
00112 void AddJDWString(Barry::Data &data, size_t &size, const std::string &str);
00113 
00114 } // namespace Barry
00115 
00116 #endif
00117