seneca
25th February 2006, 20:02
For oracle login dialogs a list of tnsnames would be handy, however I couldn't find anything in the Qt SQL driver class. I therefore created the function below and post it here in public domain. Please note that this is designed for windows only, linux and mac zealots are of cause welcome to help creating a multiplatform version ;)
#include <QFile>
#include <QFileInfo>
#include <QHostInfo>
#include <QSettings>
#include <QStringList>
QStringList tnsnames()
{
QStringList names;
QSettings settings("HKEY_LOCAL_MACHINE\\Software\\ORACLE", QSettings::NativeFormat);
QString home(settings.value("ORACLE_HOME").toString().replace("\\", "/"));
if (!home.endsWith("/")) home += "/";
QFileInfo info(home+"network/admin/tnsnames.ora");
if (!info.exists()) {
info.setFile(home+"net80/admin/tnsnames.ora");
if (!info.exists()) return names;
} // if
QFile file(info.absoluteFilePath());
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return names;
QString str;
while (!file.atEnd()) {
QString line(file.readLine());
if (line.trimmed().startsWith("#")) continue;
str += line;
} // while
file.close();
QRegExp rx("(\\w+|=|\\(|\\))");
QString word;
int pos = 0;
int paren = 0;
while ((pos = rx.indexIn(str, pos)) != -1) {
QString token(rx.capturedTexts()[0]);
if (token == "=") {
if (paren==0 && !word.isEmpty()) names << word.toLower();
word = QString::null;
} else
if (token == "(") {
paren++;
word = QString::null;
} else
if (token == ")") {
if (paren > 0) paren--;
word = QString::null;
} else {
if (paren == 0) word = token;
} // if
pos += rx.matchedLength();
} // while
names.sort();
return names;
} // tnsnames
#include <QFile>
#include <QFileInfo>
#include <QHostInfo>
#include <QSettings>
#include <QStringList>
QStringList tnsnames()
{
QStringList names;
QSettings settings("HKEY_LOCAL_MACHINE\\Software\\ORACLE", QSettings::NativeFormat);
QString home(settings.value("ORACLE_HOME").toString().replace("\\", "/"));
if (!home.endsWith("/")) home += "/";
QFileInfo info(home+"network/admin/tnsnames.ora");
if (!info.exists()) {
info.setFile(home+"net80/admin/tnsnames.ora");
if (!info.exists()) return names;
} // if
QFile file(info.absoluteFilePath());
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return names;
QString str;
while (!file.atEnd()) {
QString line(file.readLine());
if (line.trimmed().startsWith("#")) continue;
str += line;
} // while
file.close();
QRegExp rx("(\\w+|=|\\(|\\))");
QString word;
int pos = 0;
int paren = 0;
while ((pos = rx.indexIn(str, pos)) != -1) {
QString token(rx.capturedTexts()[0]);
if (token == "=") {
if (paren==0 && !word.isEmpty()) names << word.toLower();
word = QString::null;
} else
if (token == "(") {
paren++;
word = QString::null;
} else
if (token == ")") {
if (paren > 0) paren--;
word = QString::null;
} else {
if (paren == 0) word = token;
} // if
pos += rx.matchedLength();
} // while
names.sort();
return names;
} // tnsnames