1 // Copyright 2010-2018, Google Inc. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above 11 // copyright notice, this list of conditions and the following disclaimer 12 // in the documentation and/or other materials provided with the 13 // distribution. 14 // * Neither the name of Google Inc. nor the names of its 15 // contributors may be used to endorse or promote products derived from 16 // this software without specific prior written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 30 // UserDicUtil provides various utility functions related to the user 31 // dictionary. 32 33 #ifndef MOZC_DICTIONARY_USER_DICTIONARY_UTIL_H_ 34 #define MOZC_DICTIONARY_USER_DICTIONARY_UTIL_H_ 35 36 #include <string> 37 #include <vector> 38 #include "base/port.h" 39 #include "protocol/user_dictionary_storage.pb.h" 40 41 namespace mozc { 42 43 class UserPOSInterface; 44 45 // TODO(hidehiko): Move this class into user_dictionary namespace. 46 class UserDictionaryUtil { 47 public: 48 // Following methods return limits of dictionary/entry size. 49 static size_t max_dictionary_size(); 50 static size_t max_entry_size(); 51 52 // Returns true if all characters in the given string is a legitimate 53 // character for reading. 54 static bool IsValidReading(const string &reading); 55 56 // Performs varirous kinds of character normalization such as 57 // katakana-> hiragana and full-width ascii -> half width 58 // ascii. Identity of reading of a word should be defined by the 59 // output of this function. 60 static void NormalizeReading(const string &input, string *output); 61 62 // Returns true if all fields of the given data is properly set and 63 // have a legitimate value. It checks for an empty string, an 64 // invalid character and so on. If the function returns false, we 65 // shouldn't accept the data being passed into the dictionary. 66 // TODO(hidehikoo): Replace this method by the following ValidateEntry. 67 static bool IsValidEntry( 68 const UserPOSInterface &user_pos, 69 const user_dictionary::UserDictionary::Entry &entry); 70 71 // Returns the error status of the validity for the given entry. 72 // The validation process is as follows: 73 // - Checks the reading 74 // - if it isn't empty 75 // - if it doesn't exceed the max length 76 // - if it doesn't contain invalid character 77 // - Checks the word 78 // - if it isn't empty 79 // - if it doesn't exceed the max length 80 // - if it doesn't contain invalid character 81 // - Checks the comment 82 // - if it isn't exceed the max length 83 // - if it doesn't contain invalid character 84 // - Checks if a valid pos type is set. 85 static user_dictionary::UserDictionaryCommandStatus::Status ValidateEntry( 86 const user_dictionary::UserDictionary::Entry &entry); 87 88 // Sanitizes a dictionary entry so that it's acceptable to the 89 // class. A user of the class may want this function to make sure an 90 // error want happen before calling AddEntry() and other 91 // methods. Return true if the entry is changed. 92 static bool SanitizeEntry(user_dictionary::UserDictionary::Entry *entry); 93 94 // Helper function for SanitizeEntry 95 // "max_size" is the maximum allowed size of str. If str size exceeds 96 // "max_size", remaining part is truncated by this function. 97 static bool Sanitize(string *str, size_t max_size); 98 99 // Returns the error status of the validity for the given dictionary name. 100 static user_dictionary::UserDictionaryCommandStatus::Status 101 ValidateDictionaryName(const user_dictionary::UserDictionaryStorage &storage, 102 const string &dictionary_name); 103 104 // Returns true if the given storage hits the limit for the number of 105 // dictionaries. 106 static bool IsStorageFull( 107 const user_dictionary::UserDictionaryStorage &storage); 108 109 // Returns true if the given dictionary hits the limit for the number of 110 // entries. 111 static bool IsDictionaryFull( 112 const user_dictionary::UserDictionary &dictionary); 113 114 // Returns UserDictionary with the given id, or NULL if not found. 115 static const user_dictionary::UserDictionary *GetUserDictionaryById( 116 const user_dictionary::UserDictionaryStorage &storage, 117 uint64 dictionary_id); 118 static user_dictionary::UserDictionary *GetMutableUserDictionaryById( 119 user_dictionary::UserDictionaryStorage *storage, 120 uint64 dictionary_id); 121 122 // Returns the index of the dictionary with the given dictionary_id 123 // in the storage, or -1 if not found. 124 static int GetUserDictionaryIndexById( 125 const user_dictionary::UserDictionaryStorage &storage, 126 uint64 dictionary_id); 127 128 // Returns the file name of UserDictionary. 129 static string GetUserDictionaryFileName(); 130 131 // Returns the string representation of PosType, or NULL if the given 132 // pos is invalid. 133 // For historicall reason, the pos was represented in Japanese characters. 134 static const char* GetStringPosType( 135 user_dictionary::UserDictionary::PosType pos_type); 136 137 // Returns the string representation of PosType, or NULL if the given 138 // pos is invalid. 139 static user_dictionary::UserDictionary::PosType ToPosType( 140 const char *string_pos_type); 141 142 // Generates a new dictionary id, i.e. id which is not in the storage. 143 static uint64 CreateNewDictionaryId( 144 const user_dictionary::UserDictionaryStorage &storage); 145 146 // Creates dictionary with the given name. 147 static user_dictionary::UserDictionaryCommandStatus::Status CreateDictionary( 148 user_dictionary::UserDictionaryStorage *storage, 149 const string &dictionary_name, 150 uint64 *new_dictionary_id); 151 152 // Deletes dictionary specified by the given dictionary_id. 153 // If the deleted_dictionary is not NULL, the pointer to the 154 // delete dictionary is stored into it. In other words, 155 // caller has responsibility to actual deletion of the instance. 156 // Returns true if succeeded, otherwise false. 157 static bool DeleteDictionary( 158 user_dictionary::UserDictionaryStorage *storage, 159 uint64 dictionary_id, 160 int *original_index, 161 user_dictionary::UserDictionary **deleted_dictionary); 162 163 private: 164 DISALLOW_IMPLICIT_CONSTRUCTORS(UserDictionaryUtil); 165 }; 166 } // namespace mozc 167 168 #endif // MOZC_DICTIONARY_USER_DICTIONARY_UTIL_H_ 169