Tiny Object Oriented Dictionary in C

Easy dynamic "variable variables" for C with "string" as variable name.

Here is a tiny object-oriented C library for dynamic strings. It works like PHP's variable variables ($$var) and Python dictionaries to create lists of strings with names that are also strings.

It supports the following methods:

dictionary_add()
dictionary_print()
dictionary_destroy()

Simple to use:

dictionary_add("a word","some definition");
dictionary_print("a word");//prints "some definition"
dictionary_destroy();//please destroy after use.

Advanced usage: It is like adding string labels to malloc():

char *cat=dictionary("lolcat");/* kind of like malloc(99) */
char *dog=dictionary("loldog");/* except now we can find it by text label */
if(cat)strcpy(cat,"I can has text!");
if(dog)strcpy(dictionary("loldog"),"Can I has text?");
puts(dictionary("lolcat"));//prints "I can has text!"
puts(dictionary("loldog"));//prints "Can I has text?"
puts(cat);//prints "I can has text!"
dictionary("destroy()");//please destroy after use.
Advantages:

Tiny, easy to use, fully self-contained module; no global structs, bloated library calls or linking steps; just paste it into your program. No init() method. It knows when it has been invoked for the first time and uses almost no memory when not in use. No modify() method. Just add() the same word again to modify it. Easily adaptable. It can serve up other objects, not just strings.

Disadvantages:

Strings are limited to 99 (adjustable) bytes. Searching and linear and appending traverses the whole list. Only 3 methods. No iterate() or delete() methods, although you can use add() again to set the value to "no entry". Empty storage strings are created in memory for every word not found in the dictionary, so it is not suitable for a lot of random queries. For that you would be better off using linked lists, something like GTK (GLib) g_list object or perhaps a database.

If you don't like the tabs or compact formatting of the source code, run the "indent" program on it or use your text editor's automatic indent feature. I would be curious to know what projects this is included in, so if you find it useful, please drop me a line.

Changelog:

Updated: Now uses strncpy for buffer overflow prevention.
Although out of memory errors should never happen, it now handles them... :)

henry

Known bugs:

Bugs? I didn't say it was perfect or robust, only that it was tiny! Patches welcome. Feedback.

Download:

The library may be downloaded from http://thenerdshow.com/c/dictionary.zip

License: The tiny dictionary library is released under the GPL. No warranties!

links
Services
Custom Programming by Henry Kroll III

Dynamic web content:

Your_Name.ANCH.ORG - Anchorage Net Community Hosting, Non-Commercial Hosting, CMS
Your_Name.EQ2.US - No Equal 2 Us! - No Limits Hosting - Custom MySQL, PHP Content Management
3-D stock charts live, in the browser (requires vrml plugin)
3-D graphing calculator in the browser (requires vrml plugin)
1,000,000 digit calculator in the browser
Stock chart comparison in the browser
On-line maze generator

3-D graphics plug-ins for Moray 3.5

Geodesic Dome Design
Triangle Mesh Import
C Programs and Libraries

dictionary.c: dynamic "variable variables" for C with "string" as variable name
acbuff2.sh
: a decal-like macro plug-in for games like Asheron's Call™ on Linux™!
gcf: Find the greatest common factor among a bunch of numbers.

[   ] gcf.exe 14-Oct-2009 15:49 24K

Installable Windows programs:

SafeTasks antivirus / spyware removal
FoznerEdit notepad replacement
Computer-generated music:

mixculture.org

Hosted Sites

Founding Members

Dry Cleaning by Mayfield's Quality Cleaners
Technical Services provided by CompTune.com
Programming by TheNerdShow.com
Graphic Design by Business Card Express
DNS and Server Hardware by eq2.us
BriarLarkson.com Author, Historical Romance Novels
Alaska Snow Removal
Pye' Wackets (Coming Soon!)
HankKroll.com
GuardDogBooks.com

Free Members

North Wind Traders
Kenai / Seldovia Museum
Church of Scientology
New Alliance for the future of CIRI

See some artwork by my sister, Rachel Kroll

Support free software development. Annual membership:

Windows is a registered trademark of The Microsoft Corporation

Page content subject to change.
Please review our Terms of Service

Fedora LogoValid HTML 4.01 Transitional