OK, here is all code that related to my problem. It's a part of my Morse code converter.
//qstring_load - just a utility function for text file loading into QString
{
return s;
in.setCodec ("UTF-8");
s = in.readAll();
return s;
}
//hash_load_keyval - loads a key/value pairs table into the hash:
QHash<QString, QString> hash_load_keyval
(const QString &fname
){
QHash<QString, QString> result;
if (! file_exists (fname))
return result;
int c = l.size();
for (int i = 0; i < c; i++)
{
if (sl.size() == 2)
result.insert (sl[0], sl[1]);
}
return result;
}
//morse_from_lang - converts QString s into the Morse code using the key/value file that embeded into the resource. The QString lang is a locale id, for example "en" or "ru".
Please note that all data from the file loads OK.
{
QHash<QString, QString> h = hash_load_keyval (":/text-data/morse-" + lang);
int c = s.size();
for (int i = 0; i < c; i++)
{
QString t
= h.
value (QString (s
[i
]));
//here can be an empty value if key is non-Latin, and I don't know why if (! t.isNull() || ! t.isEmpty())
result.append (t + " ");
}
return result;
}
//qstring_load - just a utility function for text file loading into QString
QString qstring_load (const QString &fileName)
{
QFile file (fileName);
QString s;
if (! file.open(QFile::ReadOnly | QFile::Text))
return s;
QTextStream in(&file);
in.setCodec ("UTF-8");
s = in.readAll();
return s;
}
//hash_load_keyval - loads a key/value pairs table into the hash:
QHash<QString, QString> hash_load_keyval (const QString &fname)
{
QHash<QString, QString> result;
if (! file_exists (fname))
return result;
QStringList l = qstring_load (fname).split ("\n");
int c = l.size();
for (int i = 0; i < c; i++)
{
QStringList sl = l[i].split("=");
if (sl.size() == 2)
result.insert (sl[0], sl[1]);
}
return result;
}
//morse_from_lang - converts QString s into the Morse code using the key/value file that embeded into the resource. The QString lang is a locale id, for example "en" or "ru".
Please note that all data from the file loads OK.
QString morse_from_lang (const QString &s, const QString &lang)
{
QHash<QString, QString> h = hash_load_keyval (":/text-data/morse-" + lang);
QString result;
int c = s.size();
for (int i = 0; i < c; i++)
{
QString t = h.value (QString (s[i])); //here can be an empty value if key is non-Latin, and I don't know why
if (! t.isNull() || ! t.isEmpty())
result.append (t + " ");
}
return result;
}
To copy to clipboard, switch view to plain text mode
Bookmarks