3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
10 #include <ksimpleconfig.h>
14 #include "typetable.h"
17 // the TypeTables of all known libraries
18 static QList
<TypeTable
> typeTables
;
19 bool typeTablesInited
= false;
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");
45 for (QValueListConstIterator
<QString
> p
= files
.begin(); p
!= files
.end(); ++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('\\');
94 slash
>= 0 ? fileName
.mid(slash
+1, fileName
.length()) : fileName
;
95 int dot
= m_displayName
.findRev('.');
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();
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
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
))
124 cf
.readListEntry(typesEntry
, typeNames
, ',');
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()) {
135 // look up the alias type and insert it
136 TypeInfo
* info
= m_typeDict
[alias
];
138 TRACE(QString().sprintf("<%s>: alias %s not found",
139 it
.operator char*(), alias
.data()));
141 m_aliasDict
.insert(alias
, info
);
142 TRACE(QString().sprintf("<%s>: alias <%s>",
143 it
.operator char*(), alias
.data()));
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'",
163 // Expr1, Expr2, etc...
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
;
177 m_typeDict
.insert(type
, info
);
178 TRACE(QString().sprintf("<%s>: %d exprs", type
,
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();
196 if (strcmp(feature
, f
) == 0)
198 f
= m_enabledBuiltins
.next();
203 TypeInfo::TypeInfo(const QString
& displayString
)
205 // decompose the input into the parts
209 while (i
< typeInfoMaxExpr
&&
210 (idx
= displayString
.find('%', startIdx
)) >= 0)
212 m_displayString
[i
] = displayString
.mid(startIdx
, idx
-startIdx
);
218 * Remaining string; note that there's one more display string than
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()
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
];
266 result
= m_aliasDict
[type
];
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
286 QList
<TypeTable
> allTables
= typeTables
; /* shallow copy! */
287 allTables
.setAutoDelete(false); /* important! */
289 for (; it
&& allTables
.count() > 0; ++it
)
291 // look up the library
293 for (TypeTable
* t
= allTables
.first(); t
!= 0; t
= allTables
.next())
295 if (t
->matchFileName(it
))
297 TRACE("adding types for " + QString(it
));
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)