Tiny C library for a limited amount of dynamic strings. I wrote this quick and dirty solution because somebody said dynamic associative arrays were impossible in C... Well, C can be just like any other language. If you need more functionality, feel free to extend it, write your own linked lists or check out GTK's glib GList object.
Examples:
if(dictionary("moo"))
strcpy(dictionary("moo"),"Something about cows.");
puts(dictionary("moo"));//prints Something about cows.
dictionary("destroy()");
Limitations:
0. *Not thread-safe. Static state information.
1. CERT buffer overflow vulnerability. The strcpy() function is not for untrusted input! Size of strings in this demo are limited to 99 characters (see CHARS in source code). This can be averted by enforcing use of stncpy or by using the dictionary_add method.
2. 100 bytes of memory (99 + the terminating NULL) are set aside for each and every word it does not find, so you can add definitions to them. This makes it very friendly and easy to use, but it is a very bad idea to use it in situations where you are looking up a lot of words that are not in the dictionary! This "feature" is easy to correct, but I left it in there to give you something to do.
3. Uses linear searching which is fast on small data sets, but for a very large dictionary you might want to use something else, the aforementioned GList or a database.
4. You are responsible for freeing the memory used by the dictionary upon program termination. You can do that using the _destroy method:
dictionary_destroy()
Or by passing "destroy()" as a string to the dictionary object directly, vis:
dictionary("destroy()");
5. (updated) Like malloc, this library now returns NULL when out of memory. You may desire to check for this.
6. Slightly obfuscated code for compactness. If you don't like the formatting, run the free "indent" program (available for linux and windows) on it or use your text editor's automatic indent feature.
7. Uses int for size when size_t would be the proper type.
The tiny dictionary library and demonstration program may be downloaded from my web site:
http://www.thenerdshow.com/c/dictiona...
The free editor used in this demonstration:
http://www.scintilla.org/SciTE.html
This video contains parts or visuals of a free software program. You may use it freely according to its particular license:
This work is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This work is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
This video is licensed under the Creative Commons Attribution 2.5 Generic license. In short: you are free to share and make derivatives of this work under the conditions that you appropriately attribute it. Any of the above conditions can be waived if you get permission from the copyright holder.