C12Adapter Opensource C++ Interface
MIniFile Class Reference

Class that allows reading and writing the Windows-like INI files sequentially. More...

Inheritance diagram for MIniFile:

Public Types

enum  LineType {
  LineEof = 0,
  LineKey = 1,
  LineNameValue = 2
}
 Type of the current line that was read from the INI file. More...
 
enum  { MAX_LINE_LENGTH = 1024 }
 Maximum input line length.
 

Public Member Functions

 MIniFile ()
 Create a new INI file object, whether for reading from it, or for writing. More...
 
 MIniFile (const MStdString &fileName, bool modeWrite=false)
 Create and initialize a new INI file object. More...
 
virtual ~MIniFile ()
 Object destructor.
 
void Init (const MStdString &fileName, bool modeWrite=false)
 Initialize the INI file object to work on the file name specified. More...
 
void ReInit ()
 Reinitialize the INI file for a repeat operation. More...
 
void Done ()
 Frees the resources not needed after the object is being used.
 
LineType ReadLine ()
 Read the next record from the file. More...
 
const MStdStringGetKey () const
 Returns the current key, as read. More...
 
const MStdStringGetName () const
 Returns the current name, as read. More...
 
const MVariantGetValue () const
 Returns the current value, as read. More...
 
MStdString GetStringValue () const
 Returns the current value as string. More...
 
MStdString GetFileName () const
 Get file name of the INI file. More...
 
int GetFileLineNumber () const
 Get the current line number. More...
 
void WriteKey (const MStdString &key)
 Write the key string into the file. More...
 
void WriteNameValue (const MStdString &name, const MVariant &value)
 Write the name-equals-value string into the file. More...
 
void ThrowError (const MStdString &errorMessage)
 Throws an exception and use current file name and line number associated with the file. More...
 
void Throw (MConstLocalChars errorMessage)
 Throws an internationalized exception and uses the current file name and line number associated with the file. More...
 
bool GetRespectValueType () const
 
void SetRespectValueType (bool yes)
 
- Public Member Functions inherited from MObject
virtual ~MObject ()
 Object destructor.
 
virtual const MClassGetClass () const =0
 Get the final class of the object. More...
 
virtual unsigned GetEmbeddedSizeof () const
 For embedded object types, return the size of the class. More...
 
bool IsEmbeddedObject () const
 Tell if the object is of embedded kind. More...
 
SHOW_INTERNAL MVariant Call (const MStdString &name, const MVariant &params)
 Call the object service with parameters, given as variant. More...
 
MVariant Call0 (const MStdString &name)
 Call the object service with no parameters. More...
 
MVariant Call1 (const MStdString &name, const MVariant &p1)
 Call the object service with one parameter. More...
 
MVariant Call2 (const MStdString &name, const MVariant &p1, const MVariant &p2)
 Call the object service with two parameter. More...
 
MVariant Call3 (const MStdString &name, const MVariant &p1, const MVariant &p2, const MVariant &p3)
 Call the object service with three parameter. More...
 
MVariant Call4 (const MStdString &name, const MVariant &p1, const MVariant &p2, const MVariant &p3, const MVariant &p4)
 Call the object service with four parameter. More...
 
MVariant Call5 (const MStdString &name, const MVariant &p1, const MVariant &p2, const MVariant &p3, const MVariant &p4, const MVariant &p5)
 Call the object service with five parameter. More...
 
MVariant Call6 (const MStdString &name, const MVariant &p1, const MVariant &p2, const MVariant &p3, const MVariant &p4, const MVariant &p5, const MVariant &p6)
 Call the object service with six parameter. More...
 
virtual MVariant CallV (const MStdString &name, const MVariant::VariantVector &params)
 Call the object service with parameters, given as variant vector. More...
 
virtual bool IsPropertyPresent (const MStdString &name) const
 Tell if the property with the given name exists.
 
virtual bool IsServicePresent (const MStdString &name) const
 Tell if the service with the given name exists.
 
virtual MVariant GetProperty (const MStdString &name) const
 Get the property value using name of the property. More...
 
virtual void SetProperty (const MStdString &name, const MVariant &value)
 Set the property using name of the property, and value. More...
 
virtual MStdStringVector GetAllPropertyNames () const
 Return the list of publicly available properties, persistent or not. More...
 
virtual MStdStringVector GetAllPersistentPropertyNames () const
 Return the list of persistent properties. More...
 
virtual void SetPersistentPropertiesToDefault ()
 Set the persistent properties of the object to their default values. More...
 
virtual MVariant GetPersistentPropertyDefaultValue (const MStdString &name) const
 Get the default value of persistent property with the name given. More...
 
virtual void SetPersistentPropertyToDefault (const MStdString &name)
 Set the persistent property with the name given to default value. More...
 
virtual const char * GetType () const
 Get the name of the type for the object (could be the same as class name).
 
virtual void SetType (const MStdString &)
 Intentionally, it will set the name of the type for the object, but the service will not allow setting the name to anything other than the current name. More...
 
virtual void Validate ()
 Validate internal structures of the object. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from MObject
static const MClassGetStaticClass ()
 Get the declared class of this particular object. More...
 
static bool IsClassPresent (const MStdString &name)
 Tells if the given class name is available. More...
 
- Static Public Attributes inherited from MObject
static const MClass s_class
 Class of MObject.
 
- Protected Member Functions inherited from MObject
 MObject ()
 Object constructor, protected as the class is abstract.
 
void DoSetPersistentPropertiesToDefault (const MClass *staticClass)
 Set the persistent properties to their default values for one object provided the class for that object. More...
 

Detailed Description

Class that allows reading and writing the Windows-like INI files sequentially.

Note that there is no searching through the file for certain key and value implemented, the file has to be read until a desired data is found.

The syntax of the INI file is as follows:

[Key1]
Name1=1
Name2 = 2 ; blanks are stripped from both name and value
; comment can be here
[Key2] ; or here
Name4=Value4
Name5=Value5 ; comment
Name5="Value that can; have; semicolons" ; comment
; Comment
SomeFileName = c:\example\of\a\string\that\is\taken\as.is ; comment can be here
V1= TRUE ; Boolean TRUE, FALSE
V2 = 1u ; Unsigned number, C syntax
V3 = 0xFF ; hex
V4 = {1, 2, 3} ; array of three variants
V5 = {"key1" : 1, "key2" : 2} ; map of two key-value pairs
V6 = {"key2" : 'a', "key3" : {1, 2, 3}} ; can be of any complexity

The characters after the semicolon to the end of the line are ignored, except in the case when semicolons are inside strings. Obviously, if the strings are not enclosed in quotes, such string cannot have semicolons.

Blanks around the equal sign are not significant (unlike in many Windows implementations of ini file). Leading and trailing blanks are always stripped down, including ones that precede the semicolon comment.

It is allowed to have multiple keys with the same name.

One cannot use the same object for both reading and writing simultaneously. Whether the reading or writing is to be performed is specified in the parameter to the Init service.

During the reading, the blank lines are skipped. During writing one single blank line is added before each key. The comments are not stored in the INI file, and because of it the output file will not have them.

The errors that can happen during handling of INI file are of MException type.

Member Enumeration Documentation

Type of the current line that was read from the INI file.

Enumerator
LineEof 

End of file read, no more lines available.

LineKey 

The "[Key]" is read.

LineNameValue 

Name = value pair is read.

Constructor & Destructor Documentation

MIniFile::MIniFile ( )

Create a new INI file object, whether for reading from it, or for writing.

The constructor does not specify the mode, and the object itself is useless until Init is called.

MIniFile::MIniFile ( const MStdString fileName,
bool  modeWrite = false 
)

Create and initialize a new INI file object.

Precondition
There is only one reason for this service to fail, and that is the file fails to open for the access rights requested. Therefore, the file should exist and the user has to have appropriate rights.
Parameters
fileNameFile name to open for either reading or writing File can be absolute or relative path, file extension has to be present, no defaults are assumed.
modeWriteWhether this is for writing the ini file, otherwise read-only.
See also
Init - to open or reopen an ini file using the same object

Member Function Documentation

int MIniFile::GetFileLineNumber ( ) const
inline

Get the current line number.

Precondition
The file is open successfully, and at least one line is read. Otherwise the value returned is zero.
MStdString MIniFile::GetFileName ( ) const
inline

Get file name of the INI file.

Full path is returned.

Precondition
The file is successfully open. Otherwise an empty string is returned.
const MStdString& MIniFile::GetKey ( ) const
inline

Returns the current key, as read.

Note that if the current line is the name-value line, the key returned is the nearest key above, or an empty string if there was no key read.

Returns
String representation of the key, can be empty
const MStdString& MIniFile::GetName ( ) const
inline

Returns the current name, as read.

Note that if the current line is the key line, the name returned relates to the previous name=value pair.

Returns
String representation of the name in the name=value pair, can be empty
bool MIniFile::GetRespectValueType ( ) const
inline

When reading ini, this option controls expected value types

This option allows handling some special ini file cases such a having values that are file names, possibly with back slashes, that are not enclosed in quotes.

Default value:
False
Possible values:
  • False - Prefer string data types for values not enclosed by quotes
  • True - Attempt to interpret values as constants
MStdString MIniFile::GetStringValue ( ) const

Returns the current value as string.

Note that if the current line is the key line, the value returned relates to the previous name=value pair.

Returns
String representation of the value
const MVariant& MIniFile::GetValue ( ) const
inline

Returns the current value, as read.

Note that if the current line is the key line, the value returned relates to the previous name=value pair.

Returns
Variant of any type, whatever is given in the name=value pair, can be an empty variant or an empty string
void MIniFile::Init ( const MStdString fileName,
bool  modeWrite = false 
)

Initialize the INI file object to work on the file name specified.

Precondition
There is only one reason for this service to fail, and that is the file fails to open for the access rights requested. Therefore, the file should exist and the user has to have appropriate rights.
Parameters
fileNameFile name to open for either reading or writing File can be absolute or relative path, file extension has to be present, no defaults are assumed.
modeWriteWhether this is for writing the ini file, otherwise read-only.
LineType MIniFile::ReadLine ( )

Read the next record from the file.

Skip empty lines. Note that the keys are also read through this service.

Precondition
The file is open for reading successfully, the syntax is correct, and no IO error happened. Otherwise an exception is raised.
Returns
an indication of what happened:
  • LineEof = 0 - End of file read, no more lines available
  • LineKey = 1 - The "[Key]" is read
  • LineNameValue = 2 - Name = value pair is read
void MIniFile::ReInit ( )

Reinitialize the INI file for a repeat operation.

When reading the ini file start reading from the beginning of the file, and when writing truncate the file to zero size.

void MIniFile::SetRespectValueType ( bool  yes)
inline

When reading ini, this option controls expected value types

This option allows handling some special ini file cases such a having values that are file names, possibly with back slashes, that are not enclosed in quotes.

Default value:
False
Possible values:
  • False - Prefer string data types for values not enclosed by quotes
  • True - Attempt to interpret values as constants
void MIniFile::Throw ( MConstLocalChars  errorMessage)

Throws an internationalized exception and uses the current file name and line number associated with the file.

Parameters
errorMessageError message to throw
void MIniFile::ThrowError ( const MStdString errorMessage)

Throws an exception and use current file name and line number associated with the file.

Parameters
errorMessageError message to throw
void MIniFile::WriteKey ( const MStdString key)

Write the key string into the file.

The file is added with [key] where key is the value of the given string, without quotes.

Precondition
This operation is only valid if the file is open for writing. If the file was open for reading, an IO error is raised through MException.
Parameters
keyThe key string, shall have no ']' in it or an exception is thrown
void MIniFile::WriteNameValue ( const MStdString name,
const MVariant value 
)

Write the name-equals-value string into the file.

Precondition
This operation is only valid if the file is open for writing. If the file was open for reading, an IO error is raised through MException.