<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2448.0">
<TITLE>RE: [LCP]global variable alternative</TITLE>
</HEAD>
<BODY>
<P><FONT SIZE=2>> Many of these options are global variables since I reference </FONT>
<BR><FONT SIZE=2>> them often </FONT>
<BR><FONT SIZE=2>> within deep lying functions.</FONT>
</P>
<P><FONT SIZE=2>I was about to say "One way is to write a small class, eg Config",</FONT>
<BR><FONT SIZE=2>but I just noticed we're on the C list ;)</FONT>
</P>
<P><FONT SIZE=2>How about using strings, but hashing them to get an identifier</FONT>
<BR><FONT SIZE=2>that you use for more speed. This identifier could be an index</FONT>
<BR><FONT SIZE=2>to the table of all your options, thus yielding fast access.</FONT>
</P>
<P><FONT SIZE=2>If in function foo you need to access option bar a lot, you can do:</FONT>
</P>
<P><FONT SIZE=2>void foo()</FONT>
<BR><FONT SIZE=2>{</FONT>
<BR><FONT SIZE=2> static option_id bar=options_hash("bar"); // note the static</FONT>
<BR><FONT SIZE=2> /* use bar a lot */</FONT>
<BR><FONT SIZE=2>}</FONT>
</P>
<P><FONT SIZE=2>options_hash would then keep a hash table of all your option</FONT>
<BR><FONT SIZE=2>strings, create one when an unknown option is requested, and</FONT>
<BR><FONT SIZE=2>return the exising entry (eg an index is a table where you</FONT>
<BR><FONT SIZE=2>allocate new option data at the back, hence getting a linear</FONT>
<BR><FONT SIZE=2>table indexed by all the indices you send back) when the string</FONT>
<BR><FONT SIZE=2>is there already (from a previous call to options_hash with</FONT>
<BR><FONT SIZE=2>this same string).</FONT>
<BR><FONT SIZE=2>The static is very important there, as the point is to call the</FONT>
<BR><FONT SIZE=2>hash function very few times (even if a hash lookup is supposedly</FONT>
<BR><FONT SIZE=2>O(1)). The hashing will be done only at the first invocation.</FONT>
</P>
<P><FONT SIZE=2>You would of course need other routines like:</FONT>
</P>
<P><FONT SIZE=2>option_t *options_get(option_id);</FONT>
<BR><FONT SIZE=2>void options_clear();</FONT>
</P>
<P><FONT SIZE=2>and possibly others:</FONT>
</P>
<P><FONT SIZE=2>int options_save(const char *filename);</FONT>
<BR><FONT SIZE=2>int options_load(const char *(filename);</FONT>
</P>
<P><FONT SIZE=2>-- </FONT>
<BR><FONT SIZE=2>Vincent Penquerc'h </FONT>
</P>
</BODY>
</HTML>