I'm wondering if Tcl can act as a configuration language for another
application written in C?
I have an application which needs some user-specified data
(parameters, arrays, etc.). What I can do is to configure this
parameters in the source codes and recompile them everytime I change
one of the parameters.
So I'm thinking of using Tcl as a configuration language, writing
every parameters in a script, and let my application READ the data
from the script.
I know ns-2 (Network Simulator 2) is implemented in this way. But it
depends on Otcl and TclCL (which I'm not familiar with).
I wonder if the standard Tcl C library provides this kind of
interface? Or anyone could instruct me to some tutorials or books
about it?
Thanks in advance!
That's what it was designed for in the beginning :^)
>
> I have an application which needs some user-specified data
> (parameters, arrays, etc.). What I can do is to configure this
> parameters in the source codes and recompile them everytime I change
> one of the parameters.
>
> So I'm thinking of using Tcl as a configuration language, writing
> every parameters in a script, and let my application READ the data
> from the script.
>
> I know ns-2 (Network Simulator 2) is implemented in this way. But it
> depends on Otcl and TclCL (which I'm not familiar with).
>
> I wonder if the standard Tcl C library provides this kind of
> interface?
Sure. With a call to
Tcl_LinkVar(interp, varName, addr, type)
you can link global C variables to Tcl ones, let your users write in a
script
set foo 42
set bar whatever
...
and retrieve those setting from your C/C++ program.
The Tcl C API is great for more than this, you could use it for
managing most of your application. It is very common to write a
library in C and then connect up the C API, including the data
structures, to Tcl scripting. Tcl C API can provide many advanced
features that usually have to be figured out in any application, one
of which is configuration.
Maybe the primary source for doing this would be the Tcl source code.
Every Tcl command is implemented in C, including list and array
creation. The only difference in your case is that you pass the data
in via a script and then use is mostly in C.