Attach to Process dialog: Disallow OK if no process is selected.
[kdbg.git] / kdbg / typetable.cpp
blob10067b96b9baf6f915dcbfbdde2a337e5d0b6bfb
1 // $Id$
3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
6 #include <qdir.h>
7 #include <qptrlist.h>
8 #include <kglobal.h>
9 #include <kstddirs.h>
10 #include <ksimpleconfig.h>
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14 #include "typetable.h"
15 #include "mydebug.h"
17 // the TypeTables of all known libraries
18 static QList<TypeTable> typeTables;
19 bool typeTablesInited = false;
22 // the unknown type
23 TypeInfo TypeInfo::m_unknownType("");
26 void TypeTable::initTypeLibraries()
28 if (!typeTablesInited) {
29 TypeTable::loadTypeTables();
33 void TypeTable::loadTypeTables()
35 typeTablesInited = true;
37 const QStringList files = KGlobal::dirs()->findAllResources("types", "*.kdbgtt");
39 if (files.isEmpty()) {
40 TRACE("no type tables found");
41 return;
44 QString fileName;
45 for (QValueListConstIterator<QString> p = files.begin(); p != files.end(); ++p) {
46 fileName = *p;
47 TypeTable* newTable = new TypeTable;
48 newTable->loadFromFile(fileName);
49 typeTables.append(newTable);
54 TypeTable::TypeTable() :
55 m_printQStringDataCmd(0)
57 m_typeDict.setAutoDelete(true);
58 // aliasDict keeps only pointers to items into typeDict
59 m_aliasDict.setAutoDelete(false);
62 TypeTable::~TypeTable()
64 delete[] m_printQStringDataCmd;
68 static const char TypeTableGroup[] = "Type Table";
69 static const char LibDisplayName[] = "LibDisplayName";
70 static const char ShlibRE[] = "ShlibRE";
71 static const char EnableBuiltin[] = "EnableBuiltin";
72 static const char PrintQStringCmd[] = "PrintQStringCmd";
73 static const char TypesEntryFmt[] = "Types%d";
74 static const char DisplayEntry[] = "Display";
75 static const char AliasEntry[] = "Alias";
76 static const char ExprEntryFmt[] = "Expr%d";
77 static const char FunctionGuardEntryFmt[] = "FunctionGuard%d";
80 void TypeTable::loadFromFile(const QString& fileName)
82 TRACE("reading file " + fileName);
83 KSimpleConfig cf(fileName, true); /* read-only */
86 * Read library name and properties.
88 cf.setGroup(TypeTableGroup);
89 m_displayName = cf.readEntry(LibDisplayName);
90 if (m_displayName.isEmpty()) {
91 // use file name instead
92 int slash = fileName.findRev('\\');
93 m_displayName =
94 slash >= 0 ? fileName.mid(slash+1, fileName.length()) : fileName;
95 int dot = m_displayName.findRev('.');
96 if (dot > 0) {
97 m_displayName.truncate(dot);
101 m_shlibNameRE = QRegExp(cf.readEntry(ShlibRE));
102 cf.readListEntry(EnableBuiltin, m_enabledBuiltins);
104 QString printQString = cf.readEntry(PrintQStringCmd);
105 const char* ascii = printQString.ascii();
106 if (ascii == 0)
107 ascii = "";
108 m_printQStringDataCmd = new char[strlen(ascii)+1];
109 strcpy(m_printQStringDataCmd, ascii);
112 * Get the types. We search for entries of kind Types1, Types2, etc.
113 * because a single entry Types could get rather long for large
114 * libraries.
116 QStrList typeNames;
117 QString typesEntry;
118 for (int i = 1; ; i++) {
119 // next bunch of types
120 cf.setGroup(TypeTableGroup);
121 typesEntry.sprintf(TypesEntryFmt, i);
122 if (!cf.hasKey(typesEntry))
123 break;
124 cf.readListEntry(typesEntry, typeNames, ',');
126 // now read them
127 QString alias;
128 for (QListIterator<char> it(typeNames); it != 0; ++it) {
129 cf.setGroup(it.current());
130 // check if this is an alias
131 alias = cf.readEntry(AliasEntry);
132 if (alias.isEmpty()) {
133 readType(cf, it);
134 } else {
135 // look up the alias type and insert it
136 TypeInfo* info = m_typeDict[alias];
137 if (info == 0) {
138 TRACE(QString().sprintf("<%s>: alias %s not found",
139 it.operator char*(), alias.data()));
140 } else {
141 m_aliasDict.insert(alias, info);
142 TRACE(QString().sprintf("<%s>: alias <%s>",
143 it.operator char*(), alias.data()));
147 } // for all Types%d
150 void TypeTable::readType(KConfigBase& cf, const char* type)
152 // the display string
153 QString expr = cf.readEntry(DisplayEntry);
155 TypeInfo* info = new TypeInfo(expr);
156 if (info->m_numExprs == 0) {
157 TRACE(QString().sprintf("bogus type %s: no %% in Display: '%s'",
158 type, expr.data()));
159 delete info;
160 return;
163 // Expr1, Expr2, etc...
164 QString exprEntry;
165 QString funcGuardEntry;
166 for (int j = 0; j < info->m_numExprs; j++) {
167 exprEntry.sprintf(ExprEntryFmt, j+1);
168 expr = cf.readEntry(exprEntry);
169 info->m_exprStrings[j] = expr;
171 funcGuardEntry.sprintf(FunctionGuardEntryFmt, j+1);
172 expr = cf.readEntry(funcGuardEntry);
173 info->m_guardStrings[j] = expr;
176 // add the new type
177 m_typeDict.insert(type, info);
178 TRACE(QString().sprintf("<%s>: %d exprs", type,
179 info->m_numExprs));
182 void TypeTable::copyTypes(QDict<TypeInfo>& dict)
184 for (QDictIterator<TypeInfo> it = m_typeDict; it != 0; ++it) {
185 dict.insert(it.currentKey(), it);
187 for (QDictIterator<TypeInfo> it = m_aliasDict; it != 0; ++it) {
188 dict.insert(it.currentKey(), it);
192 bool TypeTable::isEnabledBuiltin(const char* feature)
194 char* f = m_enabledBuiltins.first();
195 while (f) {
196 if (strcmp(feature, f) == 0)
197 return true;
198 f = m_enabledBuiltins.next();
200 return false;
203 TypeInfo::TypeInfo(const QString& displayString)
205 // decompose the input into the parts
206 int i = 0;
207 int startIdx = 0;
208 int idx;
209 while (i < typeInfoMaxExpr &&
210 (idx = displayString.find('%', startIdx)) >= 0)
212 m_displayString[i] = displayString.mid(startIdx, idx-startIdx);
213 startIdx = idx+1;
214 i++;
216 m_numExprs = i;
218 * Remaining string; note that there's one more display string than
219 * sub-expressions.
221 m_displayString[i] = displayString.right(displayString.length()-startIdx);
224 TypeInfo::~TypeInfo()
229 ProgramTypeTable::ProgramTypeTable() :
230 m_parseQt2QStrings(false),
231 m_QCharIsShort(false),
232 m_printQStringDataCmd(0)
234 m_types.setAutoDelete(false); /* paranoia */
235 m_aliasDict.setAutoDelete(false); /* paranoia */
238 ProgramTypeTable::~ProgramTypeTable()
242 void ProgramTypeTable::clear()
244 m_types.clear();
247 void ProgramTypeTable::loadTypeTable(TypeTable* table)
249 table->copyTypes(m_types);
250 // check whether to enable builtin QString support
251 if (!m_parseQt2QStrings) {
252 m_parseQt2QStrings = table->isEnabledBuiltin("QString::Data");
254 if (!m_QCharIsShort) {
255 m_QCharIsShort = table->isEnabledBuiltin("QCharIsShort");
257 if (!m_printQStringDataCmd && *table->printQStringDataCmd()) {
258 m_printQStringDataCmd = table->printQStringDataCmd();
262 TypeInfo* ProgramTypeTable::lookup(const char* type)
264 TypeInfo* result = m_types[type];
265 if (result == 0) {
266 result = m_aliasDict[type];
268 return result;
271 void ProgramTypeTable::registerAlias(const QString& name, TypeInfo* type)
273 ASSERT(lookup(name) == 0 || lookup(name) == type);
274 m_aliasDict.insert(name, type);
277 void ProgramTypeTable::loadLibTypes(const QStrList& libs)
279 QStrListIterator it = libs;
282 * We use a copy of the list of known libraries, from which we delete
283 * those libs that we already have added. This way we avoid to load a
284 * library twice.
286 QList<TypeTable> allTables = typeTables; /* shallow copy! */
287 allTables.setAutoDelete(false); /* important! */
289 for (; it && allTables.count() > 0; ++it)
291 // look up the library
292 repeatLookup:;
293 for (TypeTable* t = allTables.first(); t != 0; t = allTables.next())
295 if (t->matchFileName(it))
297 TRACE("adding types for " + QString(it));
298 loadTypeTable(t);
299 // remove the table
300 allTables.remove();
302 * continue the search (due to remove's unpredictable
303 * behavior of setting the current item we simply go
304 * through the whole list again)
306 goto repeatLookup;