PhoenixProtocolBuffer  1.0.1
Set of tools to decode offset from protocol buffer
Loading...
Searching...
No Matches
phoenix_field.cpp File Reference
#include "dico_replace_var.h"
#include "phoenix_varint.h"
#include "phoenix_field.h"
+ Include dependency graph for phoenix_field.cpp:

Go to the source code of this file.

Functions

FieldConfig phoenix_createField (FieldType::FieldType type, size_t id, const PString &name, bool isArray)
 Create a FieldConfig.
 
void phoenix_field (size_t &fieldId, size_t &fieldType, char *&iter)
 Reads a Field header.
 
void phoenix_fieldPrint (char *&iter, size_t fieldType, const char *message)
 Print the current field.
 
void phoenix_fieldPrintArray (char *&iter, const char *message)
 Print the current ByteArray.
 
void phoenix_fieldSkip (char *&iter, size_t fieldType)
 Skip the current field.
 
bool phoenix_parseFieldConfig (FieldConfig &field, const DicoValue &dico)
 Load a fieldConfig with a DicoValue (parsed yml file)
 
bool phoenix_parseFieldConfig (FieldConfig &field, const DicoValue &dico, const PString &fieldConfigKey)
 Load a fieldConfig with a DicoValue (parsed yml file)
 
bool phoenix_parseFieldConfig (FieldConfig &field, const PPath &fileName, const PString &fieldConfigKey)
 Parse a yml file and search for a field_config key.
 
void phoenix_printFieldMessage (const char *message, size_t nbByte)
 Print the Fields in a message.
 
void phoenix_printFieldMessage (const char *message, size_t nbByte, size_t offset)
 Print the Fields in a message.
 

Function Documentation

◆ phoenix_createField()

FieldConfig phoenix_createField ( FieldType::FieldType type,
size_t id,
const PString & name,
bool isArray )

Create a FieldConfig.

Parameters
type: type of the field
id: id of the field
name: name of the field
isArray: true if the field is an array
Returns
corresponding FieldConfig

Definition at line 89 of file phoenix_field.cpp.

89 {
90 FieldConfig field;
91 field.setType(type);
92 field.setId(id);
93 field.setName(name);
94 field.setIsArray(isArray);
95 return field;
96}
Configuration to be used to create a field from a yml file.
void setType(const FieldType::FieldType &type)
Sets the type of the FieldConfig.
void setId(size_t id)
Sets the id of the FieldConfig.
void setName(const PString &name)
Sets the name of the FieldConfig.
void setIsArray(bool isArray)
Sets the isArray of the FieldConfig.

References FieldConfig::setId(), FieldConfig::setIsArray(), FieldConfig::setName(), and FieldConfig::setType().

Referenced by phoenix_parseFieldConfig().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ phoenix_field()

void phoenix_field ( size_t & fieldId,
size_t & fieldType,
char *& iter )

Reads a Field header.

Parameters
[out]fieldId: id of the current field
[out]fieldType: type of the current field
[out]iter: iterator ofer the message to be read

Definition at line 16 of file phoenix_field.cpp.

16 {
17 char value = *iter;
18 fieldType = value & 7; //Get 3 first bits
19 fieldId = value >> 3; //Shifted of 3 bits on the left
20 iter += 1lu;
21}

Referenced by FieldParser::loadComputeOffset(), and phoenix_printFieldMessage().

+ Here is the caller graph for this function:

◆ phoenix_fieldPrint()

void phoenix_fieldPrint ( char *& iter,
size_t fieldType,
const char * message )

Print the current field.

Parameters
[out]iter: iterator of the current message to be read
fieldType: type of the field to be skipped
message: message to be used

Definition at line 62 of file phoenix_field.cpp.

62 {
63 switch(fieldType){
64 case 0lu: //This is an enum
65 std::cout << "Enum = " << phoenix_readVarInt(iter) << std::endl;
66 break;
67 case 1lu: //This is a uint64_t
68 std::cout << "uint64_t = " << phoenix_readValue<uint64_t>(iter) << std::endl;
69 break;
70 case 2lu: //This is a binary array, which starts by a varint
71 phoenix_fieldPrintArray(iter, message);
72 break;
73 case 5lu: //This is a uint64_t
74 std::cout << "uint32_t = " << phoenix_readValue<uint32_t>(iter) << std::endl;
75 break;
76 default:
77 std::cout << "Unknown Type" << std::endl;
78 break;
79 }
80}
void phoenix_fieldPrintArray(char *&iter, const char *message)
Print the current ByteArray.
T phoenix_readValue(char *&iter)
Read a value.
size_t phoenix_readVarInt(char *&iter)
Reads and print a varint from protocol buffer.

References phoenix_fieldPrintArray(), phoenix_readValue(), and phoenix_readVarInt().

Referenced by phoenix_printFieldMessage().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ phoenix_fieldPrintArray()

void phoenix_fieldPrintArray ( char *& iter,
const char * message )

Print the current ByteArray.

Parameters
[out]iter: iterator of the current message to be read
message: message to be used

Definition at line 50 of file phoenix_field.cpp.

50 {
51 size_t nbByte = phoenix_readVarInt(iter);
52 size_t dataOffset = iter - (char*)message;
53 std::cout << "ByteArray/SubMessage(data_offset = " << dataOffset << ", size = " << nbByte << ")" << std::endl;
54 iter += nbByte;
55}

References phoenix_readVarInt().

Referenced by phoenix_fieldPrint().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ phoenix_fieldSkip()

void phoenix_fieldSkip ( char *& iter,
size_t fieldType )

Skip the current field.

Parameters
[out]iter: iterator of the current message to be read
fieldType: type of the field to be skipped

Definition at line 27 of file phoenix_field.cpp.

27 {
28 switch(fieldType){
29 case 0lu: //This is an enum
31 break;
32 case 1lu: //This is a uint64_t
33 iter += sizeof(uint64_t);
34 break;
35 case 2lu: //This is a binary array, which starts by a varint
36 iter += phoenix_readVarInt(iter);
37 break;
38 case 5lu: //This is a uint64_t
39 iter += sizeof(uint32_t);
40 break;
41 default:
42 break;
43 }
44}
void phoenix_skipVarInt(char *&iter)
Skips a varint.

References phoenix_readVarInt(), and phoenix_skipVarInt().

Referenced by FieldParser::loadComputeOffset().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ phoenix_parseFieldConfig() [1/3]

bool phoenix_parseFieldConfig ( FieldConfig & field,
const DicoValue & dico )

Load a fieldConfig with a DicoValue (parsed yml file)

Parameters
[out]field: fieldConfig to be initialised
dico: DicoValue which contains the FieldConfig definition
Returns
true on success, false otherwise

Definition at line 140 of file phoenix_field.cpp.

140 {
141 const MapDicoValue & mapChildren = dico.getMapChild();
142 for(MapDicoValue::const_iterator it(mapChildren.begin()); it != mapChildren.end(); ++it){
143 PString name(it->first);
144 size_t id(phoenix_load_value_from_config<size_t>(it->second, "id", 0lu));
145 PString typeStr(phoenix_get_string(it->second, "type", "uint64"));
146 bool isArray(phoenix_load_value_from_config<bool>(it->second, "is_array", false));
148 if(type != FieldType::NONE){
149 FieldConfig subField = phoenix_createField(type, id, name, isArray);
150 if(type == FieldType::SUBMESSAGE){ //We check only the submessage definition with the type is FieldType::SUBMESSAGE
151 if(!phoenix_parseFieldConfig(subField, it->second, "sub_message")){
152 std::cerr << "phoenix_parseFieldConfig : cannot parse 'sub_message' of Field '"<<name<<"'" << std::endl;
153 return false;
154 }
155 }
156 field.getVecChildren()[name] = subField;
157 }else{
158 std::cerr << "phoenix_parseFieldConfig : type '"<<typeStr<<"' does not exist for Field '"<<name<<"'. Please use one of :" << std::endl;
159 std::cerr << "\t- enum" << std::endl;
160 std::cerr << "\t- submessage" << std::endl;
161 std::cerr << "\t- uint64" << std::endl;
162 std::cerr << "\t- uint32" << std::endl;
163 std::cerr << "\t- uint16" << std::endl;
164 std::cerr << "\t- uint8" << std::endl;
165 std::cerr << "\t- int64" << std::endl;
166 std::cerr << "\t- int32" << std::endl;
167 std::cerr << "\t- int16" << std::endl;
168 std::cerr << "\t- int8" << std::endl;
169 return false;
170 }
171 }
172 return true;
173}
FieldType::FieldType phoenix_fieldTypeFromStr(const std::string &name)
Convert a string into a FieldType.
Definition FieldType.cpp:78
const std::map< PString, FieldConfig > & getVecChildren() const
Gets the vecChildren of the FieldConfig.
FieldType
Type of the Field.
Definition FieldType.h:14
FieldConfig phoenix_createField(FieldType::FieldType type, size_t id, const PString &name, bool isArray)
Create a FieldConfig.
bool phoenix_parseFieldConfig(FieldConfig &field, const PPath &fileName, const PString &fieldConfigKey)
Parse a yml file and search for a field_config key.

References FieldConfig::getVecChildren(), FieldType::NONE, phoenix_createField(), phoenix_fieldTypeFromStr(), phoenix_parseFieldConfig(), and FieldType::SUBMESSAGE.

+ Here is the call graph for this function:

◆ phoenix_parseFieldConfig() [2/3]

bool phoenix_parseFieldConfig ( FieldConfig & field,
const DicoValue & dico,
const PString & fieldConfigKey )

Load a fieldConfig with a DicoValue (parsed yml file)

Parameters
[out]field: fieldConfig to be initialised
dico: DicoValue which contains the FieldConfig definition
fieldConfigKey: name of the key to be searched in the yml file to initialise the FieldConfig
Returns
true on success, false otherwise

Definition at line 125 of file phoenix_field.cpp.

125 {
126 const DicoValue * ymlField = dico.getMap(fieldConfigKey);
127 if(ymlField != NULL){
128 return phoenix_parseFieldConfig(field, *ymlField);
129 }else{
130 std::cerr << "phoenix_parseFieldConfig : FieldConfig definition '"<<fieldConfigKey<<"' does not exist in yml file" << std::endl;
131 return false;
132 }
133}

References phoenix_parseFieldConfig().

+ Here is the call graph for this function:

◆ phoenix_parseFieldConfig() [3/3]

bool phoenix_parseFieldConfig ( FieldConfig & field,
const PPath & fileName,
const PString & fieldConfigKey )

Parse a yml file and search for a field_config key.

Parameters
[out]field: fieldConfig to be initialised
fileName: name of the yml file to be loaded
fieldConfigKey: name of the key to be searched in the yml file to initialise the FieldConfig
Returns
true on success, false otherwise

Definition at line 104 of file phoenix_field.cpp.

104 {
105 DicoValue dico;
106 if(!parser_yml(dico, fileName)){
107 std::cerr << "phoenix_parseFieldConfig : cannot open yml file '"<<fileName<<"'" << std::endl;
108 return false;
109 }
110 dico_replace_var(dico);
111 if(!phoenix_parseFieldConfig(field, dico, fieldConfigKey)){
112 std::cerr << "phoenix_parseFieldConfig : cannot parse yml file '"<<fileName<<"'" << std::endl;
113 return false;
114 }else{
115 return true;
116 }
117}

References phoenix_parseFieldConfig().

Referenced by phoenix_parseFieldConfig(), phoenix_parseFieldConfig(), and phoenix_parseFieldConfig().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ phoenix_printFieldMessage() [1/2]

void phoenix_printFieldMessage ( const char * message,
size_t nbByte )

Print the Fields in a message.

Parameters
message: message to be used
nbByte: number of bytes of the message

Definition at line 198 of file phoenix_field.cpp.

198 {
199 phoenix_printFieldMessage(message, nbByte, 0lu);
200}
void phoenix_printFieldMessage(const char *message, size_t nbByte, size_t offset)
Print the Fields in a message.

References phoenix_printFieldMessage().

+ Here is the call graph for this function:

◆ phoenix_printFieldMessage() [2/2]

void phoenix_printFieldMessage ( const char * message,
size_t nbByte,
size_t offset )

Print the Fields in a message.

Parameters
message: message to be used
nbByte: number of bytes of the message
offset: offset to start reading the message

Definition at line 180 of file phoenix_field.cpp.

180 {
181 char* iter = (char*)message + offset;
182 size_t messageSize = nbByte - offset;
183 std::cout << "Field{" << std::endl;
184 while(iter < message + messageSize){
185 size_t fieldId(0lu), fieldType(0lu);
186 phoenix_field(fieldId, fieldType, iter);
187 size_t fieldOffset = iter - (char*)message;
188 std::cout << "\t- Field " << fieldId << " : field_offset = " << fieldOffset << ", ";
189 phoenix_fieldPrint(iter, fieldType, message);
190 }
191 std::cout << "}" << std::endl;
192}
void phoenix_field(size_t &fieldId, size_t &fieldType, char *&iter)
Reads a Field header.
void phoenix_fieldPrint(char *&iter, size_t fieldType, const char *message)
Print the current field.

References phoenix_field(), and phoenix_fieldPrint().

Referenced by phoenix_printFieldMessage(), and printProtocolBufferMessage().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: