PxConfigWrite

PxConfigWriteBool, PxConfigWriteChar, PxConfigWriteInt, PxConfigWriteLong, PxConfigWriteShort, PxConfigWriteString, — write values to configuration files.

Syntax

PxConfigWriteBool (section, entry, format, value)
PxConfigWriteChar (section, entry, format, value)
PxConfigWriteInt (section, entry, format, value)
PxConfigWriteShort (section, entry, format, value)
PxConfigWriteLong (section, entry, format, value)
PxConfigWriteString (section, entry, format, value)

		

Arguments

section

The section name, as a string.

entry

The entry name, as a string.

format

The format of the entry. Valid options are listed in the table below.

value

The value of the entry.

Returns

-1 if the value was written, or 0 for error.

Description

These functions write an entry value in the specified section of the configuration file. If nil is passed for section, the current section is used. Existing entry values are overwritten if that entry is being written. New entries and their values are added to the end of their respective sections, and new sections are created if they don't exist.

To use these functions, the file must first be opened with a call to PxConfigOpen. To create an empty section, use PxConfigForceEmptySection.

Table 3. PxConfigWriteBool Format Options

Format OptionsValueWritten Output
PXCONFIG_FMT_BOOL_ON0 or non-0OFF or ON
PXCONFIG_FMT_BOOL_YES0 or non-0NO or YES
PXCONFIG_FMT_BOOL_TRUE0 or non-0FALSE or TRUE

Table 4. PxConfigWriteChar Format Options

Format OptionsWritten Output
PXCONFIG_FMT_CHAR_CHARA single character (letter or digit).
PXCONFIG_FMT_CHAR_HEXA 2-digit hex number.

Table 5. PxConfigWriteInt, -WriteLong, -WriteShort Format Options

Format OptionsWritten Output
PXCONFIG_FMT_INT_DECIMALA decimal number.
PXCONFIG_FMT_INT_HEXA hex number.

Table 6. PxConfigWriteString Format Options

Format OptionsWritten Output
PXCONFIG_FMT_STRINGA string.

Example

This example, ex_PxConfigWrite.g, is included in the product distribution.

#!/usr/cogent/bin/phgamma

system("echo \n");
system("echo Copying config file to new file to run tests.");
system("cp configtest.txt configtest2.txt");
system("echo \n");
system("echo --------- Start example ----------");

op = PxConfigOpen("configtest2.txt", PXCONFIG_WRITE);
princ("\nOpened? (0 = no, -1 = yes): ", op,"\n\n");

a = PxConfigWriteBool("Section Five", "LineOne", PXCONFIG_FMT_BOOL_TRUE, 2);
b = PxConfigWriteChar("Section Four", "LineTwo", PXCONFIG_FMT_CHAR_CHAR, 'j');
c = PxConfigWriteChar("Section Five", "LineThree", PXCONFIG_FMT_CHAR_HEX, 0x6a);
d = PxConfigWriteInt("Section Four", "LineFour", PXCONFIG_FMT_INT_DECIMAL, 25);
e = PxConfigWriteInt("Section Five", "LineFive", PXCONFIG_FMT_INT_HEX, 25);
f = PxConfigWriteShort("Section Five", "LineSix", PXCONFIG_FMT_INT_DECIMAL, 3.9);
g = PxConfigWriteLong("Section Five", "LineSeven", PXCONFIG_FMT_INT_HEX, 9.5);
h = PxConfigWriteString("Section Five", "LineEight", PXCONFIG_FMT_STRING, "Hello there.");

with w in list(a,b,c,d,e,f,g,h) do
princ("Line written? ", w, "\n");

cl = PxConfigClose();
princ("\nClosed? (0 = no, -1 = yes): ", cl,"\n");

system("echo \n");
system("echo --------- End example ----------");
system("echo \n");
system("echo Running diff to compare old and new config files:");
system("diff configtest.txt configtest2.txt");
system("echo \n");
system("echo Removing new copy of config file.");
system("rm configtest2.txt");
system("echo \n");



/*********  Sample output from program  *********

			
Copying config file to new file to run tests.

--------- Start example ----------

Opened? (0 = no, -1 = yes): -1

Line written? -1
Line written? -1
Line written? -1
Line written? -1
Line written? -1
Line written? -1
Line written? -1
Line written? -1

Closed? (0 = no, -1 = yes): -1

--------- End example ----------

Running diff to compare old and new config files:
21c21
< LineTwo		= 11
---
> LineTwo = j
23c23
< LineFour	= -0x15
---
> LineFour = 25
26a27,33
> [Section Five]
> LineEight = Hello there.
> LineSeven = 0x9
> LineSix = 3
> LineFive = 0x19
> LineThree = 0x6A
> LineOne = TRUE

Removing new copy of config file.


************** End of sample output **************/

See Also

PxConfigRead, PxConfigOpen, PxConfigForceEmptySection,

In Photon documentation: PxConfigWriteBool, PxConfigWriteChar, PxConfigWriteInt, PxConfigWriteLong, PxConfigWriteShort, PxConfigWriteString.