0

I have ordinrary text file with settings, which is generated by java application. Inside this file, there is a key db.url, which has value db.URL=jdbc\:mysql\://192.168.0.101\:3306/dbuser. I parse this file with QSettings class in QSettings::Native mode, everything is ok, but this db.URL gets messed up if I read it via value() method. Whatever I do (if I transform it into QString or QUrl), I get same result: jdbcmysql//192.,168.0.1013306/user. Why this key gets messed up?? I am using Qt 5.4 on Kubuntu 14.10 with kernel Linux desktop001 3.16.0-30-generic #40-Ubuntu SMP Mon Jan 12 22:06:37 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux. Here is a simple method that wrongly reads value of key:

QString UePOSSesttings::ueReadDbUrl() const
{
//    QVariant dbUrl=this->value(UeDefaults::UeDbKeys::KEY_DB_URL);
    return this->value(UeDefaults::UeDbKeys::KEY_DB_URL).toString();
}

and constants:

#ifndef UEDEFAULTS
#define UEDEFAULTS

#include <QString>

namespace UeDefaults
{
    namespace UeDbKeys
    {
        static const QString KEY_DB_DRIVER="db.driver";
        static const QString KEY_DB_PASSWORD="db.password";
        static const QString KEY_DB_URL="db.URL";
        static const QString KEY_DB_DRIVER_LIB="db.driverlib";
        static const QString KEY_DB_ENGINE="db.engine";
        static const QString KEY_DB_USER="db.user";
    }
}

#endif // UEDEFAULTS
2
  • Which OS are you on? Why do you escape ':'? Commented Feb 24, 2015 at 12:51
  • @vahancho, I've upgraded my question and I do NOT know, why string is escaped since I am NOT author of java app neither I have source of it. Commented Feb 24, 2015 at 12:59

2 Answers 2

2

QSettings clears up the string from unsupported escape sequences, in this case \:. Remove \ slashes before reading the value or don't use QSettings for parsing unsupported file formats.

Perhaps not the most optimal solution but you could processes the settings file to escape all \: before reading it with QSettings.

QFile oldSettings("settings.txt");
oldSettings.open(QIODevice::ReadOnly);
QString data = QString::fromAscii(oldSettings.readAll().constData());
oldSettings.close();

data.replace("\\:", "\\\\:");

QFile newSettings("/tmp/settings.txt");
newSettings.open(QIODevice::WriteOnly);
newSettings.write(data.toAscii(), data.size());
newSettings.close();
Sign up to request clarification or add additional context in comments.

8 Comments

is it possible to use custom Codec or reimplement read/write functions of QSettings?
Yes, implement your own QTextCodec.
If you reimplement them you'll need to reimplement for all calls to value(), setValue(). Probably easier to have write your own function to do something w/ what QSettings thinks are escape sequences and pass that value to value(), setValue(). I do it all the time when I want to save values of something unsupported: QColor, for example.
@svlasov you wrong. Custom QTextCodec will not help to fix escape sequences. Because escaping is done inside QSettings and called before QTextCodec calls. You may research sources of QSettings to be sure in it. I spent a lot of time with same issue and my solution was to write own .ini parser.
@SaZ, you are right. I checked the source, codec->fromUnicode() is called after escaping the string.
|
0

I did it:

void UePOSSesttings::ueParseData(const QString& filename)
{
    QFile settingsFile(filename);
    QString data;

    settingsFile.open(QIODevice::ReadOnly);
    data=QString::fromLatin1(settingsFile.readAll().constData());
    data.replace("\\:",
                 ":");
    this->ueSetParsedData(data);

    qDebug() << this->ueParsedData();

    settingsFile.close();
}

and now I get this url:

db.URL=jdbc:mysql://192.168.0.101:3306/dbuser

which is ok!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.