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 "suggest/core/policy/dictionary_header_structure_policy.h" 24 #include "suggest/policyimpl/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 bool writeDictionaryVersion(BufferWithExtendableBuffer *const buffer, 50 const FormatUtils::FORMAT_VERSION version, int *const writingPos); 51 52 static bool writeDictionaryFlags(BufferWithExtendableBuffer *const buffer, 53 const DictionaryFlags flags, int *const writingPos); 54 55 static bool writeDictionaryHeaderSize(BufferWithExtendableBuffer *const buffer, 56 const int size, int *const writingPos); 57 58 static bool writeHeaderAttributes(BufferWithExtendableBuffer *const buffer, 59 const DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes, 60 int *const writingPos); 61 62 /** 63 * Methods for header attributes. 64 */ 65 static void setCodePointVectorAttribute( 66 DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes, 67 const char *const key, const std::vector<int> value); 68 69 static void setBoolAttribute( 70 DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes, 71 const char *const key, const bool value); 72 73 static void setIntAttribute( 74 DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes, 75 const char *const key, const int value); 76 77 static const std::vector<int> readCodePointVectorAttributeValue( 78 const DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes, 79 const char *const key); 80 81 static bool readBoolAttributeValue( 82 const DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes, 83 const char *const key, const bool defaultValue); 84 85 static int readIntAttributeValue( 86 const DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes, 87 const char *const key, const int defaultValue); 88 89 static void insertCharactersIntoVector(const char *const characters, 90 DictionaryHeaderStructurePolicy::AttributeMap::key_type *const key); 91 92 private: 93 DISALLOW_IMPLICIT_CONSTRUCTORS(HeaderReadWriteUtils); 94 95 static const int LARGEST_INT_DIGIT_COUNT; 96 static const int MAX_ATTRIBUTE_KEY_LENGTH; 97 static const int MAX_ATTRIBUTE_VALUE_LENGTH; 98 99 static const int HEADER_MAGIC_NUMBER_SIZE; 100 static const int HEADER_DICTIONARY_VERSION_SIZE; 101 static const int HEADER_FLAG_SIZE; 102 static const int HEADER_SIZE_FIELD_SIZE; 103 104 // Value for the "flags" field. It's unused at the moment. 105 static const DictionaryFlags NO_FLAGS; 106 107 static void setIntAttributeInner( 108 DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes, 109 const DictionaryHeaderStructurePolicy::AttributeMap::key_type *const key, 110 const int value); 111 112 static int readIntAttributeValueInner( 113 const DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes, 114 const DictionaryHeaderStructurePolicy::AttributeMap::key_type *const key, 115 const int defaultValue); 116 }; 117 } 118 #endif /* LATINIME_HEADER_READ_WRITE_UTILS_H */ 119