OScript API/Built-in Package Index |
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
Error return value indicating that the Fileprefs parameter passed was not open or did not have an associated preferences file.
Error return value indicating that no section was found for the given section name.
Error return value indicating that no value was found for the given name.
Error return value indicating that the write to the preferences file failed.
Return value indicating that function was performed successfully.
Error return value indicating that buffer used to get a preference was too small.
Adds a preference of the form prefName=prefValue to the section sectionName.
Closes the associated preference object.
Creates a new preferences file if one does not already exist as specified by the given path.
Removes the preference of the given name and its associated value from the given section.
Returns a string of the value associated with a given named preference as a file path specification, meaning that all file separators are translated properly based on the current platform.
Returns the String value associated with the given preference in the given section.
Returns a List of all the section names in the given preference file.
Returns an Assoc of all the preference name/value pairs in a given named section of the preference file.
Returns a Fileprefs object associated with the given file path.
Forces a reread of the preference file.
Error return value indicating that the Fileprefs parameter passed was not open or did not have an associated preference file.
Error return value indicating that no section was found for the given section name.
Error return value indicating that no value was found for the given preference name.
Error return value indicating that the file could not be written.
Return value (defined as zero) indicating that a fileprefs function was performed successfully.
Error return value indicating that the buffer used to get the preference was too small to hold the full value.
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.
The Fileprefs object.
The preference file section name.
The name of the preference to add.
The value to associate with the named preference.
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.
The Fileprefs object.
Integer OK if the operation was successful.
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.
The file path specification of the preference file, including path and file name.
Removes the preference named prefName and its associated value from section sectionName.
The Fileprefs object.
The preference file section name.
The name of the preference to delete.
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.
The Fileprefs object.
The preference file section name.
The name of the preference to retrieve.
String result or Error if unsuccessful.
Returns the string value associated with the preference prefName in section sectionName. Only the first 254 characters of the value will be returned.
The Fileprefs object.
The file preference section name.
The name of the preference name to retrieve.
String result if successful; Error otherwise.
Use Fileprefs.GetPath() to retrieve values which are file paths, and Fileprefs.AddPref() to add a preference.
Returns a List of all the section names in the given preference file.
The Fileprefs object.
List if successful; Error otherwise.
Returns an Assoc containing a map of all preference name/value pairs in a given section of the preference file.
The Fileprefs object.
The name of the preference section for which all preferences are returned..
An Assoc containing all the preferences in the given section, mapping each String preference name to its String value.
Returns a Fileprefs object associated with the file specified by the prefFilePath.
The file path specification of the preference file to open.
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.
Valid Fileprefs object if successful; otherwise Error.
Copyright © 2022 OpenText Corporation. All rights reserved. |