Created
January 28, 2016 19:58
-
-
Save jucrouzet/0148237d9c3ce79aa147 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Serialize a value to BSON. | |
* | |
* \param document Document to increment. | |
* \param index Value index. | |
* | |
* \return Serialized value. | |
*/ | |
bsonSerializedValue _serializedValue(bsonDocument* document, uint index) { | |
bsonSerializedValue value; | |
bsonSerializedValue subValue; | |
bsonInt32 i32; | |
bsonInt64 i64; | |
char byteValue; | |
bool boolValue; | |
switch(document->fieldTypes[index]) { | |
case BSON_TYPE_DOUBLE : | |
case BSON_TYPE_INT64 : | |
value.length = sizeof(bsonInt64); | |
value.binaryValue = calloc(1, value.length); | |
if (!value.binaryValue) { | |
error("Could not allocate memory"); | |
} | |
memcpy(value.binaryValue, document->fields[index], value.length); | |
break; | |
case BSON_TYPE_STRING : | |
//(int32) <byte length> <string> \x00 | |
i32 = strlen(document->fields[index]) + 1; | |
value.length = 4 + i32; | |
value.binaryValue = calloc(value.length, sizeof(char)); | |
if (!value.binaryValue) { | |
error("Could not allocate memory"); | |
} | |
i32 = htole32(i32); | |
memcpy( | |
value.binaryValue, | |
&i32, | |
sizeof(bsonInt32) | |
); | |
memcpy( | |
value.binaryValue + sizeof(bsonInt32), | |
document->fields[index], | |
strlen(document->fields[index]) | |
); | |
break; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment