File indexing completed on 2024-04-28 07:29:21

0001 /*
0002     This file is part of Kiten, a KDE Japanese Reference Tool
0003     SPDX-FileCopyrightText: 2001 Jason Katz-Brown <jason@katzbrown.com>
0004     SPDX-FileCopyrightText: 2008 Joseph Kerian <jkerian@gmail.com>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "linearedictfile.h"
0010 
0011 #include <QDebug>
0012 #include <QFile>
0013 #include <QStringDecoder>
0014 
0015 using namespace Qt::StringLiterals;
0016 
0017 LinearEdictFile::LinearEdictFile()
0018     : m_properlyLoaded(false)
0019 {
0020 }
0021 
0022 /**
0023  * Get everything that looks remotely like a given search string
0024  */
0025 QVector<QString> LinearEdictFile::findMatches(const QString &searchString) const
0026 {
0027     QVector<QString> matches;
0028     for (const QString &it : m_edict) {
0029         if (it.contains(searchString)) {
0030             matches.append(it);
0031         }
0032     }
0033 
0034     return matches;
0035 }
0036 
0037 bool LinearEdictFile::loadFile(const QString &filename)
0038 {
0039     qDebug() << "Loading edict from " << filename;
0040 
0041     // if already loaded
0042     if (!m_edict.isEmpty()) {
0043         return true;
0044     }
0045 
0046     QFile file(filename);
0047     if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
0048         return false;
0049     }
0050 
0051     QStringDecoder decoder("EUC-JP");
0052     const QString decoded = decoder(file.readAll());
0053 
0054     QTextStream fileStream(decoded.toUtf8());
0055 
0056     QString lastLine;
0057     while (!fileStream.atEnd()) {
0058         lastLine = fileStream.readLine();
0059         if (lastLine[0] != '#'_L1)
0060             m_edict << lastLine;
0061     }
0062 
0063     file.close();
0064     m_properlyLoaded = true;
0065 
0066     return true;
0067 }
0068 
0069 bool LinearEdictFile::valid() const
0070 {
0071     return m_properlyLoaded;
0072 }