Win32 to Linux: Replacement for GetPrivateProfileString

Win32 to Linux: Replacement for GetPrivateProfileString

Post by Lester Memmot » Thu, 13 May 1999 04:00:00



In Win32 there is a set of functions that allow an application to store
configuration information.  They are:
GetPrivateProfileString
GetPrivateProfileInt
WritePrivateProfileString
WritePrivateProfileInt

I'm just making the transition to Linux and am looking for functions that
perform the same functionality.  I've seen .conf files in a lot of places
and figure that there must be a standard set of functions to access and
write data to these files.

Any ideas?

Thanks,

------------------  Posted via SearchLinux  ------------------
                  http://www.searchlinux.com

 
 
 

Win32 to Linux: Replacement for GetPrivateProfileString

Post by Lew Pitch » Thu, 13 May 1999 04:00:00



Quote:

>In Win32 there is a set of functions that allow an application to store
>configuration information.  They are:
>GetPrivateProfileString

char string[BIG_ENOUGH];
FILE *fp;

fscanf(fp,"keyword=%s\n",string);     /* assumes fp is fopen()ed */

Quote:>GetPrivateProfileInt

int number;

fscanf(fp,"keyword=%d\n",&number);        /* assumes fp is fopen()ed */

Quote:>WritePrivateProfileString

fprintf(fp,"keyword=%s\n",string);

Quote:>WritePrivateProfileInt

fprintf(fp,"keyword=%d\n",number);

Of course, the standard file I/O error checking should be performed
at each step (fopen/fcreat, fscanf/fprintf, fclose).

>I'm just making the transition to Linux and am looking for functions that
>perform the same functionality.  I've seen .conf files in a lot of places
>and figure that there must be a standard set of functions to access and
>write data to these files.

>Any ideas?

>Thanks,

>------------------  Posted via SearchLinux  ------------------
>                  http://www.searchlinux.com

Lew Pitcher
System Consultant, Integration Solutions Architecture
Toronto Dominion Bank


(Opinions expressed are my own, not my employer's.)

 
 
 

Win32 to Linux: Replacement for GetPrivateProfileString

Post by Tom Emerso » Thu, 13 May 1999 04:00:00




>>In Win32 there is a set of functions that allow an application to store

>>configuration information.  They are:

>>GetPrivateProfileString
>char string[BIG_ENOUGH];
>FILE *fp;

>fscanf(fp,"keyword=%s\n",string); /* assumes fp is fopen()ed */

[ditto for an int]

using fscanf, while seemingly kludgey, is probably as effective as anything
else -- but what happens if "keyword=" ends up being the "data" part of some
unrelated string?  [probably should modify the scan string to "\nkeyworkd="
to ensure that you're matching "keyword=" at the beginning of a line]

Quote:>>WritePrivateProfileString
>fprintf(fp,"keyword=%s\n",string);

[ditto again for int's]

Now you're really asking for it -- just where are you writing?  the safest
of course would be to open with append access, otherwise you'll overwrite
all the other "keyword=" parameters, but that doesn't take into account
UPDATING a value contained in the file... [sure, you'll have written it, but
when you go to re-access it using the above "get" routine,  you'll "get" the
original value since that will be the first one "fscanf'd"]

Another important aspect of the [private]profile variables is that the "key"
is actually a two-part key -- there is a "section" and an explicit "keyword"
that, taken together, determine the actual key to look up.  This allows, for
instance, to store multiple instances of similar data in the same file.
i.e., if the program you're working with is a text editor that can have
multiple edit windows open and you want to remember the size and position of
each window, you're more likely to want to use "keys" of X & Y under a
"subject" (section) of "window1" instead of keys like "Window_1_X",
"Window_1_Y", "Window_2_X", and so on.

Quote:>Of course, the standard file I/O error checking should be performed
>at each step (fopen/fcreat, fscanf/fprintf, fclose).

>>I'm just making the transition to Linux and am looking for functions that

>>perform the same functionality.  I've seen .conf files in a lot of places

>>and figure that there must be a standard set of functions to access and

>>write data to these files.

>>Any ideas?

 
 
 

Win32 to Linux: Replacement for GetPrivateProfileString

Post by Christopher Brow » Fri, 14 May 1999 04:00:00




>>In Win32 there is a set of functions that allow an application to store

>>configuration information.  They are:

>>GetPrivateProfileString
>char string[BIG_ENOUGH];
>FILE *fp;

>fscanf(fp,"keyword=%s\n",string); /* assumes fp is fopen()ed */

>>GetPrivateProfileInt
>int number;

>fscanf(fp,"keyword=%d\n",&number);    /* assumes fp is fopen()ed */

>>WritePrivateProfileString
>fprintf(fp,"keyword=%s\n",string);

>>WritePrivateProfileInt
>fprintf(fp,"keyword=%d\n",number);

>Of course, the standard file I/O error checking should be performed
>at each step (fopen/fcreat, fscanf/fprintf, fclose).

>>I'm just making the transition to Linux and am looking for functions that
>>perform the same functionality.  I've seen .conf files in a lot of places
>>and figure that there must be a standard set of functions to access and
>>write data to these files.

Somewhat contrary to the above suggestion, I'll suggest a couple of
URLs to packages that provide somewhat more "intelligent" handling of
configuration information.

There is not a single "standard" or "definitive" approach or library,
and there is unlikely to be complete equanimity on the "right"
approach, which is quite fair enough because there are different kinds
of configuration information for which different behaviours are
appropriate.

<ftp://ftp.windowmaker.org/pub/libs> The libPropList library provides
an abstraction to read/write NeXTstep-style default databases.

"The libPropList library, hereafter referred to as PL, uses an opaque
data type to represent a tree structure made of strings, data blocks,
arrays and dictionaries (key-value pair lists). This structure can be
manipulated, written out to and read in from a file, and synchronized
with the contents of a file. The purpose of PL is to closely mimick
the behaviour of the property lists used in GNUstep and OPENSTEP
(there formed with the NSString, NSData, NSArray and NSDictionary
classes) and to be compatible with it. PL enables programs that use
configuration or preference files to make these compatible with
GNUstep or OPENSTEP's user defaults handling mechanism, without
needing to use Objective-C or GNUstep or OPENSTEP themselves."

<ULINK URL="http://www.thestuff.net/libcfg/?theme=lcars"> LibCFG
</ULINK>

<ULINK URL="http://24.1.97.22/gmd/opStore/">opStore - Linux "Registry"
Project </ULINK>

The GNOME and KDE "desktop environments" both have APIs to provide
somewhat-smart handling of configuration information.  

None of these schemes are "definitively preferable," but I would
suggest considering using one of these schemes as they can provide
such advantages as:

- Already having debugged locking issues.
- Already being network-aware.
- Already providing debugged parsers.
- Providing enough structure/metadata as to allow tools to be
  constructed to help manage the configuration data.  ("Linuxconf or
  COAS may already know how to treat it as part of the data they
  manage.")

There are ample valid indications that building up monstrous fragile
binary databases is "really dumb."  The fragility of the Windows
Registry is a testament to that.

That does *not* provide a testament to the notion that "using some
common facilities to manage configuration data" is correspondingly a
really dumb idea.

Take a look at libProplist; that seems to be one of the more
well-thought-through configuration libraries...
--
"Lumping configuration data, security data, kernel tuning parameters,
etc. into one monstrous fragile binary data structure is really dumb."
- David F. Skoll

 
 
 

Win32 to Linux: Replacement for GetPrivateProfileString

Post by Lew Pitch » Fri, 14 May 1999 04:00:00


Short answer...

There is no *standard* way of processing configuration files
(in Linux in particular, and Unix in general) that is analogous
to the XXXPrivateProfileXXX functions in Windows.

The only portable method (available on all unixes) is to use standard
file I/O (fscanf(), fprintf()) to perform the retrieval/updates.

Long answer.....

Some third-party applications provide tools to perform these tasks.
These tools are not guaranteed to be installed or even available
on the platform of choice.

My examples were, by choice and expediency, sparse, and neglected
several areas of I/O concern. They were not meant to illustrate
the entire processing necessary to perform the equivalent functions,
but were just pointers to the larger task.

Final answer....

To the MS Windows programmer: Unix is an entirely different OS!
Do some reading; familiarize yourself with the common APIs, and
stop thinking in the MS Windows box.

Lew Pitcher
System Consultant, Integration Solutions Architecture
Toronto Dominion Bank


(Opinions expressed are my own, not my employer's.)