Blame view

morfeusz/deserializationUtils.hpp 1.49 KB
Michał Lenart authored
1
2
3
4
5
6
7
8
9
10
11
/* 
 * File:   deserializationUtils.hpp
 * Author: lennyn
 *
 * Created on April 4, 2014, 6:07 PM
 */

#ifndef DESERIALIZATIONUTILS_HPP
#define	DESERIALIZATIONUTILS_HPP

#include "endianness.hpp"
Michał Lenart authored
12
#include <iostream>
Michał Lenart authored
13
#include <vector>
Michał Lenart authored
14
Michał Lenart authored
15
16
17
18
inline unsigned char readInt8(const unsigned char*& currPtr) {
    return *currPtr++;
}
Michał Lenart authored
19
inline uint16_t readInt16(const unsigned char*& currPtr) {
Michał Lenart authored
20
    uint16_t res = htons(*reinterpret_cast<const uint16_t*> (currPtr));
Michał Lenart authored
21
22
23
24
    currPtr += 2;
    return res;
}
Michał Lenart authored
25
26
27
28
29
30
inline unsigned int readInt24(const unsigned char*& currPtr) {
    unsigned int res = currPtr[0] << 16 | currPtr[1] << 8 | currPtr[2];
    currPtr += 3;
    return res;
}
Michał Lenart authored
31
32
33
34
35
36
inline uint32_t readInt32(const unsigned char*& currPtr) {
    uint32_t res = htonl(*reinterpret_cast<const uint32_t*> (currPtr));
    currPtr += 4;
    return res;
}
Michał Lenart authored
37
38
39
40
41
inline uint32_t readInt32Const(const unsigned char* const currPtr) {
    uint32_t res = htonl(*reinterpret_cast<const uint32_t*> (currPtr));
    return res;
}
Michał Lenart authored
42
inline std::string readString(const unsigned char*& currPtr) {
Michał Lenart authored
43
    std::string res((const char*) currPtr);
Michał Lenart authored
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
    currPtr += res.length();
    currPtr++;
    return res;
}

inline void readTags(const unsigned char*& currPtr, std::vector<std::string>& tags) {
    tags.clear();
    tags.resize(65536);
    uint16_t tagsNum = readInt16(currPtr);
    for (unsigned int i = 0; i < tagsNum; i++) {
        unsigned int tagNum = readInt16(currPtr);
        tags[tagNum] = readString(currPtr);
    }
}

#endif	/* DESERIALIZATIONUTILS_HPP */