Difference between revisions of "DevTools:Code Examples"
David Haslam (talk | contribs) m (changed section levels) |
Underlined (talk | contribs) m (→C++) |
||
Line 45: | Line 45: | ||
<pre> | <pre> | ||
QStringList bibles = getModules("Biblical Texts"); | QStringList bibles = getModules("Biblical Texts"); | ||
+ | </pre> | ||
+ | |||
+ | ==== Get Biblical Book Names Example(s) ==== | ||
+ | Obtaining a list of the canonical books of the Bible doesn't necessarily require any SWORD classes (though in the following example we'll be using [http://www.crosswire.org/~ghellings/svnclassdocs/classSWConfig.html SWConfig]). A default SWORD installation installs the file <tt>abbr.conf</tt> at <tt>/usr/share/sword/locales.d</tt> on a Unix-like system (on Windows it may be located at <tt>C:\Program Files\CrossWire\The SWORD Project\locales.d</tt>). The syntax of the file is the same as any INI file. Therefore we just need to parse the file for the canonical books; so technically any configuration file parser that can parse INI files will do. The section we want to obtain from <tt>abbr.conf</tt> is called <tt>Text</tt>. (Again, the example uses a Qt4 class, [http://doc.trolltech.com/4.5/qstringlist.html QStringList].) | ||
+ | |||
+ | ===== Code ===== | ||
+ | <pre> | ||
+ | #include <QStringList> | ||
+ | #include <swconfig.h> | ||
+ | |||
+ | QStringList getBiblicalBooks() | ||
+ | { | ||
+ | QStringList books; | ||
+ | SWConfig config("/usr/share/sword/locales.d/abbr.conf"); | ||
+ | ConfigEntMap::iterator it; | ||
+ | for (it = config["Text"].begin(); it != config["Text"].end(); it++) | ||
+ | { | ||
+ | books << (*it).first.c_str(); | ||
+ | } | ||
+ | |||
+ | return books; | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | After obtaining all the Bible book names, you can obtain their abbreviations with [http://www.crosswire.org/~ghellings/svnclassdocs/classSWConfig.html SWConfig], again: | ||
+ | <pre> | ||
+ | SWConfig config("/usr/share/sword/locales.d/abbr.conf"); | ||
+ | const char *GenAbbr = config["Text"]["Genesis"]; | ||
</pre> | </pre> | ||
Revision as of 03:29, 3 August 2009
This page was started to provoke developers who know how the SWORD API works to post simple, everyday code examples that anyone can use. By "simple", I mean something that can work within a static function if possible.
Contents
Examples
C++
Get Modules Example(s)
The following example is for obtaining modules that are installed in the user's SWORD repository. This example also makes slight use of the Qt4 GUI library. The following function takes one parameter, the type of the modules you want to retrieve. Unless stated otherwise, there are up to 4 different types to choose from:
- "Biblical Texts"
- "Commentaries"
- "Lexicons / Dictionaries"
- "Generic Books"
Code
#include <QStringList> #include <swmgr.h> #include <swmodule.h> QStringList getModules(const char* modType) { QStringList modules; SWMgr library; ModMap::iterator it; for (it = library.Modules.begin(); it != library.Modules.end(); it++) { SWModule *module = (*it).second; if (!strcmp(module->Type(), modType)) modules << module->Name(); } return modules; }
Example Usage
QStringList bibles = getModules("Biblical Texts");
Get Biblical Book Names Example(s)
Obtaining a list of the canonical books of the Bible doesn't necessarily require any SWORD classes (though in the following example we'll be using SWConfig). A default SWORD installation installs the file abbr.conf at /usr/share/sword/locales.d on a Unix-like system (on Windows it may be located at C:\Program Files\CrossWire\The SWORD Project\locales.d). The syntax of the file is the same as any INI file. Therefore we just need to parse the file for the canonical books; so technically any configuration file parser that can parse INI files will do. The section we want to obtain from abbr.conf is called Text. (Again, the example uses a Qt4 class, QStringList.)
Code
#include <QStringList> #include <swconfig.h> QStringList getBiblicalBooks() { QStringList books; SWConfig config("/usr/share/sword/locales.d/abbr.conf"); ConfigEntMap::iterator it; for (it = config["Text"].begin(); it != config["Text"].end(); it++) { books << (*it).first.c_str(); } return books; }
After obtaining all the Bible book names, you can obtain their abbreviations with SWConfig, again:
SWConfig config("/usr/share/sword/locales.d/abbr.conf"); const char *GenAbbr = config["Text"]["Genesis"];