aboutsummaryrefslogtreecommitdiffstats
path: root/native/jni/src/dictionary/utils/dict_file_writing_utils.cpp
blob: 033a758ba45e353e9df7b3d14712ebeca6fa6f1a (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Copyright (C) 2013, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "dictionary/utils/dict_file_writing_utils.h"

#include <cstdio>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "dictionary/header/header_policy.h"
#include "dictionary/structure/backward/v402/ver4_dict_buffers.h"
#include "dictionary/structure/pt_common/dynamic_pt_writing_utils.h"
#include "dictionary/structure/v4/ver4_dict_buffers.h"
#include "dictionary/utils/buffer_with_extendable_buffer.h"
#include "dictionary/utils/entry_counters.h"
#include "dictionary/utils/file_utils.h"
#include "dictionary/utils/format_utils.h"
#include "utils/time_keeper.h"

namespace latinime {

const char *const DictFileWritingUtils::TEMP_FILE_SUFFIX_FOR_WRITING_DICT_FILE = ".tmp";
// Enough size to describe buffer size.
const int DictFileWritingUtils::SIZE_OF_BUFFER_SIZE_FIELD = 4;

/* static */ bool DictFileWritingUtils::createEmptyDictFile(const char *const filePath,
        const int dictVersion, const std::vector<int> localeAsCodePointVector,
        const DictionaryHeaderStructurePolicy::AttributeMap *const attributeMap) {
    TimeKeeper::setCurrentTime();
    const FormatUtils::FORMAT_VERSION formatVersion = FormatUtils::getFormatVersion(dictVersion);
    switch (formatVersion) {
        case FormatUtils::VERSION_402:
            return createEmptyV4DictFile<backward::v402::Ver4DictConstants,
                    backward::v402::Ver4DictBuffers,
                    backward::v402::Ver4DictBuffers::Ver4DictBuffersPtr>(
                            filePath, localeAsCodePointVector, attributeMap, formatVersion);
        case FormatUtils::VERSION_4_ONLY_FOR_TESTING:
        case FormatUtils::VERSION_403:
            return createEmptyV4DictFile<Ver4DictConstants, Ver4DictBuffers,
                    Ver4DictBuffers::Ver4DictBuffersPtr>(
                            filePath, localeAsCodePointVector, attributeMap, formatVersion);
        default:
            AKLOGE("Cannot create dictionary %s because format version %d is not supported.",
                    filePath, dictVersion);
            return false;
    }
}

template<class DictConstants, class DictBuffers, class DictBuffersPtr>
/* static */ bool DictFileWritingUtils::createEmptyV4DictFile(const char *const dirPath,
        const std::vector<int> localeAsCodePointVector,
        const DictionaryHeaderStructurePolicy::AttributeMap *const attributeMap,
        const FormatUtils::FORMAT_VERSION formatVersion) {
    HeaderPolicy headerPolicy(formatVersion, localeAsCodePointVector, attributeMap);
    DictBuffersPtr dictBuffers = DictBuffers::createVer4DictBuffers(&headerPolicy,
            DictConstants::MAX_DICT_EXTENDED_REGION_SIZE);
    headerPolicy.fillInAndWriteHeaderToBuffer(true /* updatesLastDecayedTime */,
            EntryCounts(), 0 /* extendedRegionSize */, dictBuffers->getWritableHeaderBuffer());
    if (!DynamicPtWritingUtils::writeEmptyDictionary(
            dictBuffers->getWritableTrieBuffer(), 0 /* rootPos */)) {
        AKLOGE("Empty ver4 dictionary structure cannot be created on memory.");
        return false;
    }
    return dictBuffers->flush(dirPath);
}

/* static */ bool DictFileWritingUtils::flushBufferToFileWithSuffix(const char *const basePath,
        const char *const suffix, const BufferWithExtendableBuffer *const buffer) {
    const int filePathBufSize = FileUtils::getFilePathWithSuffixBufSize(basePath, suffix);
    char filePath[filePathBufSize];
    FileUtils::getFilePathWithSuffix(basePath, suffix, filePathBufSize, filePath);
    return flushBufferToFile(filePath, buffer);
}

/* static */ bool DictFileWritingUtils::writeBufferToFileTail(FILE *const file,
        const BufferWithExtendableBuffer *const buffer) {
    uint8_t bufferSize[SIZE_OF_BUFFER_SIZE_FIELD];
    int writingPos = 0;
    ByteArrayUtils::writeUintAndAdvancePosition(bufferSize, buffer->getTailPosition(),
            SIZE_OF_BUFFER_SIZE_FIELD, &writingPos);
    if (fwrite(bufferSize, SIZE_OF_BUFFER_SIZE_FIELD, 1 /* count */, file) < 1) {
        return false;
    }
    return writeBufferToFile(file, buffer);
}

/* static */ bool DictFileWritingUtils::flushBufferToFile(const char *const filePath,
        const BufferWithExtendableBuffer *const buffer) {
    const int fd = open(filePath, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
    if (fd == -1) {
        AKLOGE("File %s cannot be opened. errno: %d", filePath, errno);
        ASSERT(false);
        return false;
    }
    FILE *const file = fdopen(fd, "wb");
    if (!file) {
        AKLOGE("fdopen failed for the file %s. errno: %d", filePath, errno);
        ASSERT(false);
        return false;
    }
    if (!writeBufferToFile(file, buffer)) {
        fclose(file);
        remove(filePath);
        AKLOGE("Buffer cannot be written to the file %s. size: %d", filePath,
                buffer->getTailPosition());
        ASSERT(false);
        return false;
    }
    fclose(file);
    return true;
}

// Returns whether the writing was succeeded or not.
/* static */ bool DictFileWritingUtils::writeBufferToFile(FILE *const file,
        const BufferWithExtendableBuffer *const buffer) {
    const int originalBufSize = buffer->getOriginalBufferSize();
    if (originalBufSize > 0 && fwrite(buffer->getBuffer(false /* usesAdditionalBuffer */),
            originalBufSize, 1, file) < 1) {
        return false;
    }
    const int additionalBufSize = buffer->getUsedAdditionalBufferSize();
    if (additionalBufSize > 0 && fwrite(buffer->getBuffer(true /* usesAdditionalBuffer */),
            additionalBufSize, 1, file) < 1) {
        return false;
    }
    return true;
}

} // namespace latinime