Subj : Re: store collections of strings in tdb or gdbm To : comp.programming From : jburgy Date : Sat Oct 08 2005 08:01 am Lasse Kliemann wrote: > Greetings, > Hi there, > my data is organized as follows: > > - There are several records. > - Each record consists of a fixed number of strings. [snip] > My idea so far for each record: > > - Get the decimal representation of all the lengths of the strings in > the record. > - Write these into dptr, as null-terminated strings. > - Then write all the strings into dptr. > > It is obvious how to get the strings back from this encoding. > > > My questions now are: > > - How elegant is my approach? It sounds just plain wrong. I know nothing about tdb so feel free to diss my comment but the way you would normally do this in a relational database is like this record_id | string_id | string_text ----------------------------------- 0 | 0 | 'foo' 0 | 1 | 'bar' 0 | 2 | 'baz' 1 | 3 | 'qux' 1 | 4 | 'quux' 1 | 5 | 'quuux' Hopefully you get the picture. If tdb is as trivial as the struct you showed leads me to believe, you may need two tables to achieve this: one stores the strings (with one string per record, no worries, these records have nothing to do with yours), the other the correlation between string_id and record_id. Something like char *string_text[] = { "foo", "bar", "baz", "qux", "quux", "quuux", .... }; int record_id = { 0, 0, 0, 1, 1, 1, .... }; You catch my drift? > - Is the conversion into the decimal representation necessary, or can I > write the lengths of the strings as unsinged ints into dptr? Would > this be portable? If you insist on going with your idea: showing unsigned ints into a char* is not portable, google endianness. > - Hasn't this been done before and exists in form of a library? I don't believe so, people usually use a variation of what I'm describing Good luck and let me now how it goes, Jan .