[comp.sys.atari.st.tech] How to write config info into mail .PRG or .ACC file?

grahamt@syma.sussex.ac.uk (Graham Thomas) (08/21/90)

Could someone give me some tips on how to save (& of course load)
configuration information to the program that uses it, while running
that same program?  I've seen lots of programs that do this - e.g.
Charles Johnson's Little Green Selector & Arcshell, the Universal Item
Selector, etc., and I'd like to do this myself.

I've written a program (in Laser C) which sends various settings to my
printer.  It runs either as a .PRG or as an .ACC (I used Samuel
Streeper's init file), and it uses a dialog box as its user interface. 
At present, the program sets the defaults I like (e.g.  elite, draft,
skip 4 lines at perfs, A4 page size) at startup time.  I'm aware that my
preferences aren't everybody's, and I'd like to let other people set
their own defaults. 

My options are either to read from / write to a separate .CFG or .INF
file, which I could do but would rather not (I've incorporated my
resource file info into the main program file and I'd like to keep
everything in a single place if possible), or save the info directly to
the program itself.  How is this done?

Thanks in advance.

Graham

fischer-michael@cs.yale.edu (Michael Fischer) (08/22/90)

In article <3300@syma.sussex.ac.uk> grahamt@syma.sussex.ac.uk (Graham Thomas) writes:
>
>My options are either to read from / write to a separate .CFG or .INF
>file, which I could do but would rather not (I've incorporated my
>resource file info into the main program file and I'd like to keep
>everything in a single place if possible), or save the info directly to
>the program itself.  How is this done?

It's easy enough to rewrite bytes in the middle of the .prg file using
the Gemdos functions Fseek and Fwrite.  Your only problems are to find
the file (not so easy) and to figure out which bytes to change.

I don't know of a reliable way to find the name and location of the
.prg file which is currently running.  It will often be in the default
directory on the default device (depending on how the program was
invoked), but it doesn't have to be.  The Pexec that invokes your
program probably leaves this information behind in some undocumented
memory locations somewhere, but you can't count on that not changing
in the future. 

The second part is easier.  Declare all of your configuration
variables at the top of your program as static, and initialize them to
appropriate default values.  This should cause the compiler to place
them at the start of the data segment of your program.  Information in
the header of a .prg file tells the lengths of the text and data
segments.  Read that information and calculate the position in the
.prg file of the start of the data segment.  Seek to that position and
rewrite your variables.  To be on the safe side, put a magic number as
the first thing in the data segment and check that it is there before
you start writing.  This may save you from clobbering the wrong file.
Thus, your program might start as follows:
	static char magic[8] = "myprogxx";
	static int width = 85;
	...

Hope this helps.
-- 
==================================================
| Michael Fischer <fischer-michael@cs.yale.edu>  |
==================================================

roeder@robin.cs.uni-sb.de (Edgar &) (08/22/90)

In article <25854@cs.yale.edu> fischer-michael@cs.yale.edu (Michael Fischer) writes:

   In article <3300@syma.sussex.ac.uk> grahamt@syma.sussex.ac.uk (Graham Thomas) writes:
   >
   >My options are either to read from / write to a separate .CFG or .INF
   >file, which I could do but would rather not (I've incorporated my
   >resource file info into the main program file and I'd like to keep
   >everything in a single place if possible), or save the info directly to
   >the program itself.  How is this done?

   I don't know of a reliable way to find the name and location of the
   .prg file which is currently running.  It will often be in the default
   directory on the default device (depending on how the program was
   invoked), but it doesn't have to be.  The Pexec that invokes your
   program probably leaves this information behind in some undocumented
   memory locations somewhere, but you can't count on that not changing
   in the future. 

If the program is launched by the desktop, you can find out it's name
and path by calling shel_read. This function expects two parameters:
the first array of characters is filled with the path of the current
program, the second array of chars will contain the arguments supplied
by the TTP-box or the "Install application"-feature.
If your program is launched by some shell, you could look at argv[0]
supplied by one of the extended argument passing methods (eg. xArg or
ARGV environment vars). You could also search for your programs name
(obtained from argv[0] or your default name) in the directories
specified in the PATH environment variable.
As a last resort, you could prompt the user for the location of the
program.

   Hope this helps.

	- Edgar

grahamt@syma.sussex.ac.uk (Graham Thomas) (08/23/90)

Thanks to everyone who replied to my query.  If I get any mail that
raises points not mentioned in followups, I'll post them.  Just one
clarification: I've no idea how the word 'mail' got into the subject
line of the original message.  It's a typo.  I must have been dreaming.
Sorry about that, if it caused any confusion.

Graham
-- 
Graham Thomas, SPRU, Mantell Building, U of Sussex, Brighton, BN1 9RF, UK
 JANET: grahamt@uk.ac.sussex.syma   BITNET: grahamt%syma.sussex.ac.uk@UKACRL
 INTERNET: grahamt%syma.sussex.ac.uk@nsfnet-relay.ac.uk
 UUCP: grahamt%syma.sussex@ukc.uucp  PHONE: +44 273 686758  FAX: [..] 685865