Home | History | Annotate | Download | only in header
      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 <map>
     21 #include <stdint.h>
     22 #include <vector>
     23 
     24 #include "defines.h"
     25 #include "suggest/policyimpl/dictionary/utils/format_utils.h"
     26 
     27 namespace latinime {
     28 
     29 class BufferWithExtendableBuffer;
     30 
     31 class HeaderReadWriteUtils {
     32  public:
     33     typedef uint16_t DictionaryFlags;
     34     typedef std::map<std::vector<int>, std::vector<int> > AttributeMap;
     35 
     36     static int getHeaderSize(const uint8_t *const dictBuf);
     37 
     38     static DictionaryFlags getFlags(const uint8_t *const dictBuf);
     39 
     40     static AK_FORCE_INLINE bool supportsDynamicUpdate(const DictionaryFlags flags) {
     41         return (flags & SUPPORTS_DYNAMIC_UPDATE_FLAG) != 0;
     42     }
     43 
     44     static AK_FORCE_INLINE bool requiresGermanUmlautProcessing(const DictionaryFlags flags) {
     45         return (flags & GERMAN_UMLAUT_PROCESSING_FLAG) != 0;
     46     }
     47 
     48     static AK_FORCE_INLINE bool requiresFrenchLigatureProcessing(const DictionaryFlags flags) {
     49         return (flags & FRENCH_LIGATURE_PROCESSING_FLAG) != 0;
     50     }
     51 
     52     static AK_FORCE_INLINE int getHeaderOptionsPosition() {
     53         return HEADER_MAGIC_NUMBER_SIZE + HEADER_DICTIONARY_VERSION_SIZE + HEADER_FLAG_SIZE
     54                 + HEADER_SIZE_FIELD_SIZE;
     55     }
     56 
     57     static DictionaryFlags createAndGetDictionaryFlagsUsingAttributeMap(
     58             const HeaderReadWriteUtils::AttributeMap *const attributeMap);
     59 
     60     static void fetchAllHeaderAttributes(const uint8_t *const dictBuf,
     61             AttributeMap *const headerAttributes);
     62 
     63     static bool writeDictionaryVersion(BufferWithExtendableBuffer *const buffer,
     64             const FormatUtils::FORMAT_VERSION version, int *const writingPos);
     65 
     66     static bool writeDictionaryFlags(BufferWithExtendableBuffer *const buffer,
     67             const DictionaryFlags flags, int *const writingPos);
     68 
     69     static bool writeDictionaryHeaderSize(BufferWithExtendableBuffer *const buffer,
     70             const int size, int *const writingPos);
     71 
     72     static bool writeHeaderAttributes(BufferWithExtendableBuffer *const buffer,
     73             const AttributeMap *const headerAttributes, int *const writingPos);
     74 
     75     /**
     76      * Methods for header attributes.
     77      */
     78     static void setBoolAttribute(AttributeMap *const headerAttributes,
     79             const char *const key, const bool value);
     80 
     81     static void setIntAttribute(AttributeMap *const headerAttributes,
     82             const char *const key, const int value);
     83 
     84     static bool readBoolAttributeValue(const AttributeMap *const headerAttributes,
     85             const char *const key, const bool defaultValue);
     86 
     87     static int readIntAttributeValue(const AttributeMap *const headerAttributes,
     88             const char *const key, const int defaultValue);
     89 
     90     static void insertCharactersIntoVector(const char *const characters,
     91             AttributeMap::key_type *const key);
     92 
     93  private:
     94     DISALLOW_IMPLICIT_CONSTRUCTORS(HeaderReadWriteUtils);
     95 
     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     static const DictionaryFlags NO_FLAGS;
    105     // Flags for special processing
    106     // Those *must* match the flags in makedict (FormatSpec#*_PROCESSING_FLAGS) or
    107     // something very bad (like, the apocalypse) will happen. Please update both at the same time.
    108     static const DictionaryFlags GERMAN_UMLAUT_PROCESSING_FLAG;
    109     static const DictionaryFlags SUPPORTS_DYNAMIC_UPDATE_FLAG;
    110     static const DictionaryFlags FRENCH_LIGATURE_PROCESSING_FLAG;
    111 
    112     static const char *const SUPPORTS_DYNAMIC_UPDATE_KEY;
    113     static const char *const REQUIRES_GERMAN_UMLAUT_PROCESSING_KEY;
    114     static const char *const REQUIRES_FRENCH_LIGATURE_PROCESSING_KEY;
    115 
    116     static void setIntAttributeInner(AttributeMap *const headerAttributes,
    117             const AttributeMap::key_type *const key, const int value);
    118 
    119     static int readIntAttributeValueInner(const AttributeMap *const headerAttributes,
    120             const AttributeMap::key_type *const key, const int defaultValue);
    121 };
    122 }
    123 #endif /* LATINIME_HEADER_READ_WRITE_UTILS_H */
    124