1 /* 2 * Copyright (C) 2013, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef LATINIME_HEADER_READ_WRITE_UTILS_H 18 #define LATINIME_HEADER_READ_WRITE_UTILS_H 19 20 #include <cstdint> 21 22 #include "defines.h" 23 #include "dictionary/interface/dictionary_header_structure_policy.h" 24 #include "dictionary/utils/format_utils.h" 25 26 namespace latinime { 27 28 class BufferWithExtendableBuffer; 29 30 class HeaderReadWriteUtils { 31 public: 32 typedef uint16_t DictionaryFlags; 33 34 static int getHeaderSize(const uint8_t *const dictBuf); 35 36 static DictionaryFlags getFlags(const uint8_t *const dictBuf); 37 38 static AK_FORCE_INLINE int getHeaderOptionsPosition() { 39 return HEADER_MAGIC_NUMBER_SIZE + HEADER_DICTIONARY_VERSION_SIZE + HEADER_FLAG_SIZE 40 + HEADER_SIZE_FIELD_SIZE; 41 } 42 43 static DictionaryFlags createAndGetDictionaryFlagsUsingAttributeMap( 44 const DictionaryHeaderStructurePolicy::AttributeMap *const attributeMap); 45 46 static void fetchAllHeaderAttributes(const uint8_t *const dictBuf, 47 DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes); 48 49 static const int *readCodePointTable( 50 DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes); 51 52 static bool writeDictionaryVersion(BufferWithExtendableBuffer *const buffer, 53 const FormatUtils::FORMAT_VERSION version, int *const writingPos); 54 55 static bool writeDictionaryFlags(BufferWithExtendableBuffer *const buffer, 56 const DictionaryFlags flags, int *const writingPos); 57 58 static bool writeDictionaryHeaderSize(BufferWithExtendableBuffer *const buffer, 59 const int size, int *const writingPos); 60 61 static bool writeHeaderAttributes(BufferWithExtendableBuffer *const buffer, 62 const DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes, 63 int *const writingPos); 64 65 /** 66 * Methods for header attributes. 67 */ 68 static void setCodePointVectorAttribute( 69 DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes, 70 const char *const key, const std::vector<int> &value); 71 72 static void setBoolAttribute( 73 DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes, 74 const char *const key, const bool value); 75 76 static void setIntAttribute( 77 DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes, 78 const char *const key, const int value); 79 80 static const std::vector<int> readCodePointVectorAttributeValue( 81 const DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes, 82 const char *const key); 83 84 static bool readBoolAttributeValue( 85 const DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes, 86 const char *const key, const bool defaultValue); 87 88 static int readIntAttributeValue( 89 const DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes, 90 const char *const key, const int defaultValue); 91 92 static void insertCharactersIntoVector(const char *const characters, 93 DictionaryHeaderStructurePolicy::AttributeMap::key_type *const key); 94 95 private: 96 DISALLOW_IMPLICIT_CONSTRUCTORS(HeaderReadWriteUtils); 97 98 static const int LARGEST_INT_DIGIT_COUNT; 99 static const int MAX_ATTRIBUTE_KEY_LENGTH; 100 static const int MAX_ATTRIBUTE_VALUE_LENGTH; 101 102 static const int HEADER_MAGIC_NUMBER_SIZE; 103 static const int HEADER_DICTIONARY_VERSION_SIZE; 104 static const int HEADER_FLAG_SIZE; 105 static const int HEADER_SIZE_FIELD_SIZE; 106 107 static const char *const CODE_POINT_TABLE_KEY; 108 109 // Value for the "flags" field. It's unused at the moment. 110 static const DictionaryFlags NO_FLAGS; 111 112 static void setIntAttributeInner( 113 DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes, 114 const DictionaryHeaderStructurePolicy::AttributeMap::key_type *const key, 115 const int value); 116 117 static int readIntAttributeValueInner( 118 const DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes, 119 const DictionaryHeaderStructurePolicy::AttributeMap::key_type *const key, 120 const int defaultValue); 121 }; 122 } 123 #endif /* LATINIME_HEADER_READ_WRITE_UTILS_H */ 124