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_SINGLE_DICT_CONTENT_H
18 #define LATINIME_SINGLE_DICT_CONTENT_H
19 
20 #include <cstdio>
21 
22 #include "defines.h"
23 #include "dictionary/structure/v4/ver4_dict_constants.h"
24 #include "dictionary/utils/buffer_with_extendable_buffer.h"
25 #include "dictionary/utils/dict_file_writing_utils.h"
26 #include "utils/byte_array_view.h"
27 
28 namespace latinime {
29 
30 class SingleDictContent {
31  public:
SingleDictContent(const ReadWriteByteArrayView buffer)32     SingleDictContent(const ReadWriteByteArrayView buffer)
33             : mExpandableContentBuffer(buffer,
34                     BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE) {}
35 
SingleDictContent()36     SingleDictContent()
37             : mExpandableContentBuffer(Ver4DictConstants::MAX_DICTIONARY_SIZE) {}
38 
~SingleDictContent()39     virtual ~SingleDictContent() {}
40 
isNearSizeLimit()41     bool isNearSizeLimit() const {
42         return mExpandableContentBuffer.isNearSizeLimit();
43     }
44 
45  protected:
getWritableBuffer()46     BufferWithExtendableBuffer *getWritableBuffer() {
47         return &mExpandableContentBuffer;
48     }
49 
getBuffer()50     const BufferWithExtendableBuffer *getBuffer() const {
51         return &mExpandableContentBuffer;
52     }
53 
flush(FILE * const file)54     bool flush(FILE *const file) const {
55         return DictFileWritingUtils::writeBufferToFileTail(file, &mExpandableContentBuffer);
56     }
57 
58  private:
59     DISALLOW_COPY_AND_ASSIGN(SingleDictContent);
60 
61     BufferWithExtendableBuffer mExpandableContentBuffer;
62 };
63 } // namespace latinime
64 #endif /* LATINIME_SINGLE_DICT_CONTENT_H */
65