Updated version recognizing 10g:
#include <QFile>
#include <QFileInfo>
#include <QHostInfo>
#include <QSettings>
#include <QStringList>
{
QString home
(settings.
value("ORACLE_HOME").
toString());
// before 10g if (home.isEmpty()) {
// try 10g
if (key.startsWith("KEY_")) {
home = settings.value(key+"/ORACLE_HOME").toString();
if (!home.isEmpty()) break;
} // if
} // if
if (home.isEmpty()) return names; // hopeless
if (!home.replace("\\", "/").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());
while (!file.atEnd()) {
if (line.trimmed().startsWith("#")) continue;
str += line;
} // while
file.close();
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();
} else
if (token == "(") {
paren++;
} else
if (token == ")") {
if (paren > 0) paren--;
} 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()); // before 10g
if (home.isEmpty()) {
// try 10g
QStringList keys(settings.childGroups());
foreach (QString key, keys)
if (key.startsWith("KEY_")) {
home = settings.value(key+"/ORACLE_HOME").toString();
if (!home.isEmpty()) break;
} // if
} // if
if (home.isEmpty()) return names; // hopeless
if (!home.replace("\\", "/").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
To copy to clipboard, switch view to plain text mode
Bookmarks