debian: added giffgaff chatscripts
[barry.git] / src / record-internal.h
blob9ef521cca06687f8a82b0ca8f34b69f54b8121be
1 ///
2 /// \file record-internal.h
3 /// Support functions, types, and templates for the
4 /// general record parsing classes in r_*.h files.
5 /// This header is NOT installed for applications to
6 /// use, so it is safe to put library-specific things
7 /// in here.
8 ///
11 Copyright (C) 2005-2013, Net Direct Inc. (http://www.netdirect.ca/)
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 See the GNU General Public License in the COPYING file at the
23 root directory of this project for more details.
26 #ifndef __BARRY_RECORD_INTERNAL_H__
27 #define __BARRY_RECORD_INTERNAL_H__
29 #include <string>
30 #include "protostructs.h"
31 #include "error.h"
32 #include "endian.h"
33 #include "record.h"
35 // forward declarations
36 namespace Barry {
37 class Data;
38 class IConverter;
41 namespace Barry {
43 template <class RecordT>
44 const unsigned char* ParseCommonFields(RecordT &rec,
45 const void *begin,
46 const void *end,
47 const IConverter *ic = 0)
49 const unsigned char *b = (const unsigned char*) begin;
50 const unsigned char *e = (const unsigned char*) end;
52 while( (b + COMMON_FIELD_HEADER_SIZE) < e )
53 b = rec.ParseField(b, e, ic);
54 return b;
57 // Does not exist, so error will be caught by linker instead of at runtime.
58 void ConvertHtoB_Not_Implemented();
60 // Use templates here to guarantee types are converted in the strictest manner.
61 template <class SizeT>
62 inline SizeT ConvertHtoB(SizeT s)
64 ConvertHtoB_Not_Implemented();
65 // throw Error("Not implemented.");
68 // specializations for specific sizes
69 template <> inline uint8_t ConvertHtoB<uint8_t>(uint8_t s) { return s; }
70 template <> inline uint16_t ConvertHtoB<uint16_t>(uint16_t s) { return htobs(s); }
71 template <> inline uint32_t ConvertHtoB<uint32_t>(uint32_t s) { return htobl(s); }
72 template <> inline uint64_t ConvertHtoB<uint64_t>(uint64_t s) { return htobll(s); }
75 template <class RecordT>
76 struct FieldLink
78 int type;
79 const char *name;
80 const char *ldif;
81 const char *objectClass;
82 std::string RecordT::* strMember; // FIXME - find a more general
83 EmailAddressList RecordT::* addrMember; // way to do this...
84 Barry::TimeT RecordT::* timeMember;
85 PostalAddress RecordT::* postMember;
86 std::string PostalAddress::* postField;
87 bool iconvNeeded;
90 void BuildField1900(Data &data, size_t &size, uint8_t type, time_t t);
91 inline void BuildField1900(Data &data, size_t &size, uint8_t type, const Barry::TimeT &t)
93 BuildField1900(data, size, type, t.Time);
95 void BuildField(Data &data, size_t &size, uint8_t type, char c);
96 void BuildField(Data &data, size_t &size, uint8_t type, uint8_t c);
97 void BuildField(Data &data, size_t &size, uint8_t type, uint16_t value);
98 void BuildField(Data &data, size_t &size, uint8_t type, uint32_t value);
99 void BuildField(Data &data, size_t &size, uint8_t type, uint64_t value);
100 void BuildField(Data &data, size_t &size, uint8_t type, const std::string &str);
101 void BuildField(Data &data, size_t &size, uint8_t type, const void *buf, size_t bufsize);
102 void BuildField(Data &data, size_t &size, const Barry::UnknownField &field);
103 void BuildField(Data &data, size_t &size, uint8_t type, const Barry::Protocol::GroupLink &link);
104 std::string ParseFieldString(const Barry::Protocol::CommonField *field);
105 std::string ParseFieldString(const void *data, uint16_t maxlen);
108 // Functions to help build JDWP command packets
109 void AddJDWByte(Barry::Data &data, size_t &size, const uint8_t value);
110 void AddJDWInt(Barry::Data &data, size_t &size, const uint32_t value);
111 void AddJDWChar(Barry::Data &data, size_t &size, const void *buf, size_t bufsize);
112 void AddJDWString(Barry::Data &data, size_t &size, const std::string &str);
114 } // namespace Barry
116 #endif