enumerated static string table

T

Tim

I'm looking to implement a function of the form:
const char *get_string(enum string_id id);
so I can move all my static strings into a separate library that will
be easily editted for localization. The strings will be of widely
varying length so I don't want to put them in a fixed dimension array.
I think the code below does roughly what I want. Is there a more
direct way to get this same result? Or, a way to macro-ize it to
isolate the string data for a translator, and have only 1 list of
(enum name,string) to maintain.

const char s1[] = "string1";
const char s2[] = "string2";
enum string_id {
id1 = 0,
id2
}
const char *sa[2];

void init()
{
sa[id1] = s1;
sa[id2] = s2;
}

const char *get_string(enum string_id id)
{
return sa[id];
}
 
K

Keith Thompson

I'm looking to implement a function of the form:
const char *get_string(enum string_id id);
so I can move all my static strings into a separate library that will
be easily editted for localization. The strings will be of widely
varying length so I don't want to put them in a fixed dimension array.
I think the code below does roughly what I want. Is there a more
direct way to get this same result? Or, a way to macro-ize it to
isolate the string data for a translator, and have only 1 list of
(enum name,string) to maintain.

const char s1[] = "string1";
const char s2[] = "string2";
enum string_id {
id1 = 0,
id2
}
const char *sa[2];

void init()
{
sa[id1] = s1;
sa[id2] = s2;
}

const char *get_string(enum string_id id)
{
return sa[id];
}

I was briefly thrown off by the missing semicolon on the declaration
of "enum string_id". If you're going to post code samples, please
cut-and-paste them from something that actually compiles.

Here's a small program that avoids declaring an object for each string:
========================================
#include <stdio.h>

enum string_id {
id1 = 0, /* the "= 0" is superfluous */
id2
};

const char *sa[] = {
"string1", /* id1 */
"string2" /* id2 */
};

const char *get_string(enum string_id id)
{
return sa[id];
}

int main(void)
{
printf("get_string(id1) = \"%s\"\n", get_string(id1));
printf("get_string(id2) = \"%s\"\n", get_string(id2));
return 0;
}
========================================

The output is:
get_string(id1) = "string1"
get_string(id2) = "string2"

One problem is that the list of identifiers in the the declaration of
enum string_id and in the sa array have to be kept in sync manually.
You might consider generating the C source for the declarations from
an input file; the input file might look like this:

id1 string1
id2 string2

<OT>I'd probably use Perl for something like this</OT>, but you can
certainly do it in C. You can probably generate the declarations
automatically when you build your application; <OT>the Unix "make"
tool is good at things like this</OT>.
 
P

Peter Nilsson

Tim said:
I'm looking to implement a function of the form:
const char *get_string(enum string_id id);
so I can move all my static strings into a separate library that will
be easily editted for localization. The strings will be of widely
varying length so I don't want to put them in a fixed dimension array.
I think the code below does roughly what I want. Is there a more
direct way to get this same result? Or, a way to macro-ize it to
isolate the string data for a translator, and have only 1 list of
(enum name,string) to maintain.

http://groups.google.com/[email protected]
 
T

Tim

A little more searching here and I found exactly what I was looking
for in a 1996 post by James Pinkham, "Converting Code to Data".
Learned 2 things:

char *sa[] = "string 1","string 2";
gets the strings loaded into memory with sa to index them. And,
can use a macro with changing definition to associate the enum and the
string.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,366
Latest member
IanCulpepp

Latest Threads

Top