File: linearedictfile.cpp

package info (click to toggle)
kiten 4%3A25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 34,156 kB
  • sloc: cpp: 6,079; xml: 239; makefile: 8; sh: 2
file content (72 lines) | stat: -rw-r--r-- 1,548 bytes parent folder | download
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
/*
    This file is part of Kiten, a KDE Japanese Reference Tool
    SPDX-FileCopyrightText: 2001 Jason Katz-Brown <jason@katzbrown.com>
    SPDX-FileCopyrightText: 2008 Joseph Kerian <jkerian@gmail.com>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "linearedictfile.h"

#include <QDebug>
#include <QFile>
#include <QStringDecoder>

using namespace Qt::StringLiterals;

LinearEdictFile::LinearEdictFile()
    : m_properlyLoaded(false)
{
}

/**
 * Get everything that looks remotely like a given search string
 */
QList<QString> LinearEdictFile::findMatches(const QString &searchString) const
{
    QList<QString> matches;
    for (const QString &it : m_edict) {
        if (it.contains(searchString)) {
            matches.append(it);
        }
    }

    return matches;
}

bool LinearEdictFile::loadFile(const QString &filename)
{
    qDebug() << "Loading edict from " << filename;

    // if already loaded
    if (!m_edict.isEmpty()) {
        return true;
    }

    QFile file(filename);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        return false;
    }

    QStringDecoder decoder("EUC-JP");
    const QString decoded = decoder(file.readAll());

    QTextStream fileStream(decoded.toUtf8());

    QString lastLine;
    while (!fileStream.atEnd()) {
        lastLine = fileStream.readLine();
        if (lastLine[0] != '#'_L1)
            m_edict << lastLine;
    }

    file.close();
    m_properlyLoaded = true;

    return true;
}

bool LinearEdictFile::valid() const
{
    return m_properlyLoaded;
}