aboutsummaryrefslogtreecommitdiffstats
path: root/native/jni/src/dictionary/header/header_read_write_utils.h
blob: f67d614df7cea69fc724955322ef7ba38a214b92 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * Copyright (C) 2013, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef LATINIME_HEADER_READ_WRITE_UTILS_H
#define LATINIME_HEADER_READ_WRITE_UTILS_H

#include <cstdint>

#include "defines.h"
#include "dictionary/interface/dictionary_header_structure_policy.h"
#include "dictionary/utils/format_utils.h"

namespace latinime {

class BufferWithExtendableBuffer;

class HeaderReadWriteUtils {
 public:
    typedef uint16_t DictionaryFlags;

    static int getHeaderSize(const uint8_t *const dictBuf);

    static DictionaryFlags getFlags(const uint8_t *const dictBuf);

    static AK_FORCE_INLINE int getHeaderOptionsPosition() {
        return HEADER_MAGIC_NUMBER_SIZE + HEADER_DICTIONARY_VERSION_SIZE + HEADER_FLAG_SIZE
                + HEADER_SIZE_FIELD_SIZE;
    }

    static DictionaryFlags createAndGetDictionaryFlagsUsingAttributeMap(
            const DictionaryHeaderStructurePolicy::AttributeMap *const attributeMap);

    static void fetchAllHeaderAttributes(const uint8_t *const dictBuf,
            DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes);

    static const int *readCodePointTable(
            DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes);

    static bool writeDictionaryVersion(BufferWithExtendableBuffer *const buffer,
            const FormatUtils::FORMAT_VERSION version, int *const writingPos);

    static bool writeDictionaryFlags(BufferWithExtendableBuffer *const buffer,
            const DictionaryFlags flags, int *const writingPos);

    static bool writeDictionaryHeaderSize(BufferWithExtendableBuffer *const buffer,
            const int size, int *const writingPos);

    static bool writeHeaderAttributes(BufferWithExtendableBuffer *const buffer,
            const DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes,
            int *const writingPos);

    /**
     * Methods for header attributes.
     */
    static void setCodePointVectorAttribute(
            DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes,
            const char *const key, const std::vector<int> &value);

    static void setBoolAttribute(
            DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes,
            const char *const key, const bool value);

    static void setIntAttribute(
            DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes,
            const char *const key, const int value);

    static const std::vector<int> readCodePointVectorAttributeValue(
            const DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes,
            const char *const key);

    static bool readBoolAttributeValue(
            const DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes,
            const char *const key, const bool defaultValue);

    static int readIntAttributeValue(
            const DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes,
            const char *const key, const int defaultValue);

    static void insertCharactersIntoVector(const char *const characters,
            DictionaryHeaderStructurePolicy::AttributeMap::key_type *const key);

 private:
    DISALLOW_IMPLICIT_CONSTRUCTORS(HeaderReadWriteUtils);

    static const int LARGEST_INT_DIGIT_COUNT;
    static const int MAX_ATTRIBUTE_KEY_LENGTH;
    static const int MAX_ATTRIBUTE_VALUE_LENGTH;

    static const int HEADER_MAGIC_NUMBER_SIZE;
    static const int HEADER_DICTIONARY_VERSION_SIZE;
    static const int HEADER_FLAG_SIZE;
    static const int HEADER_SIZE_FIELD_SIZE;

    static const char *const CODE_POINT_TABLE_KEY;

    // Value for the "flags" field. It's unused at the moment.
    static const DictionaryFlags NO_FLAGS;

    static void setIntAttributeInner(
            DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes,
            const DictionaryHeaderStructurePolicy::AttributeMap::key_type *const key,
            const int value);

    static int readIntAttributeValueInner(
            const DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes,
            const DictionaryHeaderStructurePolicy::AttributeMap::key_type *const key,
            const int defaultValue);
};
}
#endif /* LATINIME_HEADER_READ_WRITE_UTILS_H */