The built-in functions in the Fileprefs Package allow the creation and manipulation of Fileprefs values. The Fileprefs type encapsulates a preferences file with unique named sections each having name value pairs. The format of a Fileprefs file is similar to a windows INI file. The functions described below are used to manipulate the values in the preferences file. In a multithreaded server environment the preferences in any individual file are managed so that all threads using the same file always have access to the same consistent preferences. All of the Fileprefs built-in functions are thread-safe. Values are currently limited to being no longer than 254 characters in length. For additional information please see the OScript Language Reference.
The major functionalities offered in the Fileprefs Package are the following:
Here is an example which demonstrates how easily Fileprefs are created and used:
String fname = "c:\tmp\s.prefs" List names = { "color", "name", "date", "count", "somepath" } List values = { "blue", "Joe", Date.Now(), 4, "c:\tmp\s.txt" } List sections = { "colors", "misc", "agents", "misc", "misc" } Integer i Fileprefs f String name String section Dynamic value Assoc readvals if ( !IsError( Fileprefs.Create( fname ) ) ) if (!IsError( f = Fileprefs.Open( fname ) ) ) for ( i = 1; i <= Length( names ); i += 1 ) value = values[ i ] if ( Type( value ) != StringType ) value = Str.ValueToString( value ) end Fileprefs.AddPref( f, sections[ i ], names[ i ], value ) end Fileprefs.Close( f ) f = Fileprefs.Open( fname ) sections = Fileprefs.ListSections( f ) for section in sections readvals = Fileprefs.ListValues( f, section ) for name in Assoc.Keys( readvals ) value = Str.StringToValue( readvals.( name ) ) if ( IsUndefined( value ) ) value = readvals.( name ) end Echo( "[", section, "] ", name, '=', Str.String( value ) ) end end Fileprefs.Close( f ) else Echo( "Could not open " + fname ) end else Echo( "Could not create " + fname ) end
The output of the example is:
[colors] color=blue [misc] count=4 [misc] name=Joe [misc] somepath=c:\tmp\s.txt [agents] date=Fri Sep 11 16:42:15 1998
The contents of the preferences file is (note that this should not be absolutely relied upon as it may be subject to change in the future, however it is included here for purposes of demonstration and clarification):
[colors] color=blue [misc] somepath=c:\tmp\s.txt count=4 name=Joe [agents] date=D/1998/9/11:16:53:31
Integer NO_FILE
Error return value indicating that the Fileprefs parameter passed was not open or did not have an associated preference file.
NO_SECTIONInteger NO_SECTION
Error return value indicating that no section was found for the given section name.
NO_VALUEInteger NO_VALUE
Error return value indicating that no value was found for the given preference name.
NO_WRITEInteger NO_WRITE
Error return value indicating that the file could not be written.
VALUE_TRUNCATEDInteger VALUE_TRUNCATED
Error return value indicating that the buffer used to get the preference was too small to hold the full value.
OKInteger OK
Return value (defined as zero) indicating that a fileprefs function was performed successfully.
AddPrefInteger AddPref( Fileprefs theFileprefs, String sectionName, String prefName, String prefValue )
Adds a preference of the form prefName=prefValue in the section sectionName. If the section does not yet exist then a new section with the proper sectionName will be created. If a preference already exists in the given section as prefName=someOtherValue then it will be replaced with the new value.
theFileprefs | - | The Fileprefs object. |
sectionName | - | The preference file section name. |
prefName | - | The name of the preference to add. |
prefValue | - | The value to associate with the named preference. |
Integer Close( Fileprefs theFileprefs )
Closes the associated preference object. In a multithreaded application this call may have no direct relationship to the "openness" of the actual preference file, since multiple threads may be accessing the Fileprefs object at once.
theFileprefs | - | The Fileprefs object. |
Integer Create( String prefFilePath )
Creates a new preference file if one does not already exist with the given prefFilePath. Open must be called with the same path to open the new Fileprefs object and perform other operations on it.
Integer DelPref( Fileprefs theFileprefs, String sectionName, String prefName )
Removes the preference named prefName and its associated value from section sectionName.
String GetPath( Fileprefs theFileprefs, String sectionName, String prefName )
Returns the string value associated with preference prefName in section sectionName, except, unlike Fileprefs.GetPref(), the string is returned as a file path specification, meaning that all file separators are translated properly for the current platform. Only the first 254 characters of the value will be returned.
theFileprefs | - | The Fileprefs object. |
sectionName | - | The preference file section name. |
prefName | - | The name of the preference to retrieve. |
String GetPref( Fileprefs theFileprefs, String sectionName, String prefName )
Returns the string value associated with the preference prefName in section sectionName. Only the first 254 characters of the value will be returned.
theFileprefs | - | The Fileprefs object. |
sectionName | - | The file preference section name. |
prefName | - | The name of the preference name to retrieve. |
Use Fileprefs.GetPath() to retrieve values which are file paths, and Fileprefs.AddPref() to add a preference.
List ListSections( Fileprefs theFileprefs )
Returns a List of all the section names in the given preference file.
theFileprefs | - | The Fileprefs object. |
Assoc ListValues( Fileprefs theFileprefs, String sectionName )
Returns an Assoc containing a map of all preference name/value pairs in a given section of the preference file.
theFileprefs | - | The Fileprefs object. |
sectionName | - | The name of the preference section for which all preferences are returned.. |
Fileprefs Open( String prefFilePath, [Boolean cache=TRUE] )
Returns a Fileprefs object associated with the file specified by the prefFilePath.
prefFilePath | - | The file path specification of the preference file to open. |
cache | - | True if the file prefs data should be cached by the system, false if not. Default is TRUE. If FALSE, the prefs data will be released from memory when the FilePrefs object goes out of scope. |
Integer Refresh( Fileprefs theFileprefs )
Forces a reread of the preference file. It is expensive and should be avoided.