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.
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... :)
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!
Dynamic web content:
Your_Name.ANCH.ORG - Anchorage Net Community Hosting, Non-Commercial Hosting, CMSYour_Name.EQ2.US - No Equal 2 Us! - No Limits Hosting - Custom MySQL, PHP Content Management BetaBrite™ Graphical Guestbook: Display your message on our sign. 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 browserOn-line maze generator
3-D graphics plug-ins for Moray 3.5
Geodesic Dome Design Triangle Mesh Import
dictionary.c: dynamic "variable variables" for C with "string" as variable nameacbuff2.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
dictionary.c: dynamic "variable variables" for C with "string" as variable nameacbuff2.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
mixculture.org
Founding Members
Dry Cleaning by Mayfield's Quality CleanersTechnical Services provided by CompTune.comProgramming by TheNerdShow.comGraphic Design by Business Card ExpressDNS and Server Hardware by eq2.usBriarLarkson.com Author, Historical Romance NovelsAlaska Snow RemovalPye' Wackets (Coming Soon!)HankKroll.comGuardDogBooks.com
Free Members
North Wind TradersKenai / Seldovia MuseumChurch of ScientologyNew Alliance for the future of CIRI
See some artwork by my sister, Rachel Kroll
Windows is a registered trademark of The Microsoft Corporation
Page content subject to change. Please review our Terms of Service