f



Reading a tcl file in C

I have a tcl configure file (a list of variables with values and
comments) that I wish to read into an array in C (or better yet,
Objective-C). Does anyone have any suggestions of how to do this, or
have any public-domain or GPL code that does this?

0
3/13/2007 9:37:03 AM
comp.lang.tcl 23428 articles. 2 followers. Post Follow

4 Replies
806 Views

Similar Articles

[PageSpeed] 3

randall.h.wood@gmail.com wrote:
> I have a tcl configure file (a list of variables with values and
> comments) that I wish to read into an array in C (or better yet,
> Objective-C). Does anyone have any suggestions of how to do this, or
> have any public-domain or GPL code that does this?

Can you give us a short example of what this file will look like?

-- 
+--------------------------------+---------------------------------------+
| Gerald W. Lester                                                       |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
0
Gerald.Lester (2014)
3/13/2007 1:02:12 PM
On Mar 13, 9:02 am, "Gerald W. Lester" <Gerald.Les...@cox.net> wrote:
> randall.h.w...@gmail.com wrote:
> > I have a tcl configure file (a list of variables with values and
> > comments) that I wish to read into an array in C (or better yet,
> > Objective-C). Does anyone have any suggestions of how to do this, or
> > have any public-domain or GPL code that does this?
>
> Can you give us a short example of what this file will look like?
>
I apologize for the delayed response.
EXAMPLE FOLLOWS:
# Set the directory in which to install ports
prefix                  /opt/local

# Where to store DarwinPorts working data
portdbpath              /opt/local/var/db/dports

# Type of storage to use for the port registry information, "flat" or
"sqlite"
# NOTE: sqlite not yet supported.
portdbformat            flat

# Type of installation to do for ports, "direct" or "image".  See
ports.conf(5)
and online documentation.
portinstalltype         image

0
3/25/2007 1:24:11 AM
On Mar 25, 3:24 am, "randall.h.w...@gmail.com"
<randall.h.w...@gmail.com> wrote:
> > > I have a tcl configure file (a list of variables with values and
> > > comments) that I wish to read into an array in C (or better yet,
>
> # Set the directory in which to install ports
> prefix                  /opt/local

You have to realize that "being a Tcl file" (Tcl code) is a property
of a *much* greater variety of formats than the one you're showing.
>From here, two options:

 - either you've shown the whole complexity; in this case, you can
restate your problem as

           ignore everything after #
           ignore blank lines
           parse the rest with sscanf(line,"%s %s",field,value)

 and completely forget about Tcl. (Unless you encounter spaces in the
value part -- in this case you'll have to emulate "" and {}; but then,
beware of backslashes if you're inside ""...)

 - or, creative admins will have written these config files with the
whole power of Tcl in mind. A realistic example which *I* would write
is a foreach loop on a list of directories (like a PATH search) so
that the config file works on machines with different setups.

 In this case, bad news, you won't rewrite a Tcl interpreter in a few
lines of C or objC. Instead, either "embed" (i.e. use Tcl as a
library, allocate an interpreter, and feed it), or just write in Tcl a
converter that will simply [source] your config files, and write back
to stdout in the simplified format above. From your example:

     proc prefix x {puts [list prefix $x]}
     proc portdbpath x {puts [list portdbpath $x]}
     proc portdbformat x {puts [list portdbformat $x]}
     proc portinstalltype x {puts [list portinstalltype $x]}
     # and so one for all meaningful keywords

HTH,

-Alex

0
3/25/2007 9:03:26 AM
Alexandre Ferrieux wrote:
> In this case, bad news, you won't rewrite a Tcl interpreter in a few
> lines of C or objC. Instead, either "embed" (i.e. use Tcl as a
> library, allocate an interpreter, and feed it), or just write in Tcl a
> converter that will simply [source] your config files, and write back
> to stdout in the simplified format above. From your example:
> 
>      proc prefix x {puts [list prefix $x]}
>      proc portdbpath x {puts [list portdbpath $x]}
>      proc portdbformat x {puts [list portdbformat $x]}
>      proc portinstalltype x {puts [list portinstalltype $x]}
>      # and so one for all meaningful keywords

Note that getting access to a Tcl interpreter in a few lines of C is
very easy indeed. After all, Tcl is really just a C library.

   Tcl_Interp *interp; /* helpful variable for later */

   Tcl_FindExecutable(argv[0]); /* MUST be first to init Tcl lib! */
   interp = Tcl_CreateInterp();
   /* Maybe define a command to trap back into your code here? */
   if (Tcl_EvalFile(interp, "/the/app/dir/setup.tcl") != TCL_OK) {
      fprintf(stderr, "error in setting up: %s\nTrace follows:\n%s\n",
              Tcl_GetStringResult(interp),
              Tcl_GetVar(interp, "::errorInfo", 0));
      exit(1);
   }

   if (Tcl_EvalFile(interp, "app.cfg") != TCL_OK) {
      fprintf(stderr, "error in config file: %s\nTrace follows:\n%s\n",
              Tcl_GetStringResult(interp),
              Tcl_GetVar(interp, "::errorInfo", 0));
      exit(1);
   }

   Tcl_DeleteInterp(interp);

The only other thing to note is that setup.tcl is where you put all
those procedure declarations that make your little config parsing
language. But that's about all there is to it, though maybe you'll use
better handling of errors than just printing the error message and
exiting. :-)

The other thing to note is that if you're embedding in Java, you might
find either TclBlend or Jacl interesting.

Donal.
0
3/25/2007 1:51:45 PM
Reply: