Back to index

lightning-sunbird  0.9+nobinonly
Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes
nsINIParser Class Reference

#include <nsINIParser.h>

Collaboration diagram for nsINIParser:
Collaboration graph
[legend]

List of all members.

Public Types

enum  {
  OK = 0, E_READ = -701, E_MEM = -702, E_PARAM = -703,
  E_NO_SEC = -704, E_NO_KEY = -705, E_SEC_CORRUPT = -706, E_SMALL_BUF = -707
}
enum  {
  OK = 0, E_READ = -701, E_MEM = -702, E_PARAM = -703,
  E_NO_SEC = -704, E_NO_KEY = -705, E_SEC_CORRUPT = -706, E_SMALL_BUF = -707
}

Public Member Functions

 nsINIParser (char *aFilename)
 nsINIParser
 ~nsINIParser ()
int GetString (char *aSection, char *aKey, char *aValBuf, int *aIOValBufSize)
 GetString.
int GetStringAlloc (char *aSection, char *aKey, char **aOutBuf, int *aOutBufSize)
 GetStringAlloc.
int GetError ()
 GetError.
 nsINIParser (char *aFilename)
 nsINIParser
 ~nsINIParser ()
int GetString (char *aSection, char *aKey, char *aValBuf, int *aIOValBufSize)
 GetString.
int GetStringAlloc (char *aSection, char *aKey, char **aOutBuf, int *aOutBufSize)
 GetStringAlloc.
int GetError ()
 GetError.

Static Public Member Functions

static char * ResolveName (char *aINIRoot)
 ResolveName.
static char * ResolveName (char *aINIRoot)
 ResolveName.

Private Member Functions

int FindSection (char *aSection, char **aOutSecPtr)
int FindKey (char *aSecPtr, char *aKey, char *aVal, int *aIOValSize)
int FindSection (char *aSection, char **aOutSecPtr)
int FindKey (char *aSecPtr, char *aKey, char *aVal, int *aIOValSize)

Private Attributes

char * mFileBuf
int mFileBufSize
int mError

Detailed Description

Definition at line 49 of file nsINIParser.h.


Member Enumeration Documentation

anonymous enum
Enumerator:
OK 
E_READ 
E_MEM 
E_PARAM 
E_NO_SEC 
E_NO_KEY 
E_SEC_CORRUPT 
E_SMALL_BUF 

Definition at line 129 of file nsINIParser.h.

    {
        OK                  = 0,
        E_READ              = -701,
        E_MEM               = -702,
        E_PARAM             = -703,
        E_NO_SEC            = -704,
        E_NO_KEY            = -705,
        E_SEC_CORRUPT       = -706,
        E_SMALL_BUF         = -707
    };
anonymous enum
Enumerator:
OK 
E_READ 
E_MEM 
E_PARAM 
E_NO_SEC 
E_NO_KEY 
E_SEC_CORRUPT 
E_SMALL_BUF 

Definition at line 129 of file nsINIParser.h.

    {
        OK                  = 0,
        E_READ              = -701,
        E_MEM               = -702,
        E_PARAM             = -703,
        E_NO_SEC            = -704,
        E_NO_KEY            = -705,
        E_SEC_CORRUPT       = -706,
        E_SMALL_BUF         = -707
    };

Constructor & Destructor Documentation

nsINIParser::nsINIParser ( char *  aFilename)

nsINIParser

Construct a new INI parser for the file specified.

Parameters:
aFilenamepath to INI file

Definition at line 43 of file nsINIParser.cpp.

{
    FILE    *fd = NULL;
    long    eofpos = 0;
    int     rd = 0;

    mFileBuf = NULL;
    mFileBufSize = 0;
    mError = OK;
    DUMP("nsINIParser");

    /* param check */
    if (!aFilename)
    {
        mError = E_PARAM;
        return;
    }

    /* open the file */
    fd = fopen(aFilename, "r");
    if (!fd)
        goto bail;
    
    /* get file size */
    if (fseek(fd, 0, SEEK_END) != 0)
        goto bail;
    eofpos = ftell(fd);
    if (eofpos == 0)
        goto bail;

    /* malloc an internal buf the size of the file */
    mFileBuf = (char *) malloc(eofpos + 1);
    if (!mFileBuf)
    {
        mError = E_MEM;
        return;
    }
    mFileBufSize = eofpos;

    /* read the file in one swoop */
    if (fseek(fd, 0, SEEK_SET) != 0)
        goto bail;
    rd = fread((void *)mFileBuf, 1, eofpos, fd);
    if (!rd)
        goto bail;
    mFileBuf[mFileBufSize] = '\0';

    /* close file */
    fclose(fd);

    /* Make sure the buffer is null-terminated. */
    mFileBuf[eofpos] = '\0';

    return;

bail:
    mError = E_READ;
    return;
}

Here is the call graph for this function:

Definition at line 103 of file nsINIParser.cpp.

{
    DUMP("~nsINIParser");
}
nsINIParser::nsINIParser ( char *  aFilename)

nsINIParser

Construct a new INI parser for the file specified.

Parameters:
aFilenamepath to INI file

Member Function Documentation

int nsINIParser::FindKey ( char *  aSecPtr,
char *  aKey,
char *  aVal,
int aIOValSize 
) [private]

Definition at line 246 of file nsINIParser.cpp.

{
    char *nextNL = NULL;
    char *secEnd = NULL;
    char *currLine = aSecPtr;
    char *nextEq = NULL;
    int  aKeyLen = strlen(aKey); 
    mError = E_NO_KEY;
    DUMP("FindKey");

    // param check
    if (!aSecPtr || !aKey || !aVal || !aIOValSize || (*aIOValSize <= 0))
    {
        mError = E_PARAM;
        return mError;
    }

    // determine the section end
    secEnd = aSecPtr;
find_end:
    if (secEnd)
        secEnd = strchr(secEnd, '['); // search for next sec start
    if (!secEnd)
    {
        secEnd = strchr(aSecPtr, '\0'); // else search for file end
        if (!secEnd)
        {
            mError = E_SEC_CORRUPT; // else this data is corrupt
            return mError;
        }
    }

    // handle start section token ('[') in values for i18n
    if (*secEnd == '[' && !(secEnd == aSecPtr || *(secEnd-1) == NL))
    {
        secEnd++;
        goto find_end;
    }

    while (currLine < secEnd)
    {
        nextNL = NULL;
        nextNL = strchr(currLine, NL);
        if (!nextNL)
            nextNL = mFileBuf + mFileBufSize;

        // ignore commented lines (starting with ;)
        if (currLine == strchr(currLine, ';'))
        {
            currLine = nextNL + 1;
            continue;
        }

        // extract key before '='
        nextEq = NULL;
        nextEq = strchr(currLine, '=');
        if (!nextEq || nextEq > nextNL) 
        {
            currLine = nextNL + 1;
            continue;
        }

        // if key matches we succeeded
        if (strncmp(currLine, aKey, aKeyLen) == 0
              && nextEq-currLine == aKeyLen)
        {
            // extract the value and return
            if (*aIOValSize < nextNL - nextEq)
            {
                mError = E_SMALL_BUF;
                *aVal = '\0';
                *aIOValSize = 0;
                return mError;
            }
                
            *aIOValSize = nextNL - (nextEq + 1); 
            strncpy(aVal, (nextEq + 1), *aIOValSize);
            *(aVal + *aIOValSize) = 0; // null terminate
            mError = OK;
            return mError;
        }
        else
        {
            currLine = nextNL + 1;
        }
    }

    return mError;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int nsINIParser::FindKey ( char *  aSecPtr,
char *  aKey,
char *  aVal,
int aIOValSize 
) [private]
int nsINIParser::FindSection ( char *  aSection,
char **  aOutSecPtr 
) [private]
int nsINIParser::FindSection ( char *  aSection,
char **  aOutSecPtr 
) [private]

Definition at line 195 of file nsINIParser.cpp.

{
    char *currChar = mFileBuf;
    char *nextSec = NULL;
    char *secClose = NULL;
    char *nextNL = NULL;
    int aSectionLen = strlen(aSection);
    mError = E_NO_SEC;
    DUMP("FindSection");

    // param check
    if (!aSection || !aOutSecPtr)
    {
        mError = E_PARAM;
        return mError;
    }

    while (currChar < (mFileBuf + mFileBufSize))
    {
        // look for first '['
        nextSec = NULL;
        nextSec = strchr(currChar, '[');
        if (!nextSec)
            break;
            
        currChar = nextSec + 1;

        // extract section name till first ']'
        secClose = NULL; nextNL = NULL;
        secClose = strchr(currChar, ']');
        nextNL = strchr(currChar, NL);
        if ((!nextNL) || (nextNL < secClose))
        {
            currChar = nextNL;
            continue;
        }

        // if section name matches we succeeded
        if (strncmp(aSection, currChar, aSectionLen) == 0
              && secClose-currChar == aSectionLen)
        {
            *aOutSecPtr = secClose + 1;
            mError = OK;
            break;
        }
    }

    return mError;
}

Here is the call graph for this function:

Here is the caller graph for this function:

GetError.

Exposes the last error on this instance. Useful for checking the state of the object after construction since the INI file is parsed once at object-allocation time.

Returns:
mError last error on ops on this object

GetError.

Exposes the last error on this instance. Useful for checking the state of the object after construction since the INI file is parsed once at object-allocation time.

Returns:
mError last error on ops on this object

Definition at line 154 of file nsINIParser.cpp.

{
    DUMP("GetError");
    return mError;
}

Here is the caller graph for this function:

int nsINIParser::GetString ( char *  aSection,
char *  aKey,
char *  aValBuf,
int aIOValBufSize 
)

GetString.

Gets the value of the specified key in the specified section of the INI file represented by this instance. The value is stored in the supplied buffer. The buffer size is provided as input and the actual bytes used by the value is set in the in/out size param.

Parameters:
aSectionsection name
aKeykey name
aValBufuser supplied buffer
aIOValBufSizebuf size on input; actual buf used on output
Returns:
mError operation success code
int nsINIParser::GetString ( char *  aSection,
char *  aKey,
char *  aValBuf,
int aIOValBufSize 
)

GetString.

Gets the value of the specified key in the specified section of the INI file represented by this instance. The value is stored in the supplied buffer. The buffer size is provided as input and the actual bytes used by the value is set in the in/out size param.

Parameters:
aSectionsection name
aKeykey name
aValBufuser supplied buffer
aIOValBufSizebuf size on input; actual buf used on output
Returns:
mError operation success code

Definition at line 109 of file nsINIParser.cpp.

{
    char *secPtr = NULL;
    mError = OK;
    DUMP("GetString");

    /* param check */
    if ( !aSection || !aKey || !aValBuf || 
         !aIOValBufSize || (*aIOValBufSize <= 0) )
        return E_PARAM;

    /* find the section if it exists */
    mError = FindSection(aSection, &secPtr);
    if (mError != OK)
        return mError;

    /* find the key if it exists in the valid section we found */
    mError = FindKey(secPtr, aKey, aValBuf, aIOValBufSize);

    return mError;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int nsINIParser::GetStringAlloc ( char *  aSection,
char *  aKey,
char **  aOutBuf,
int aOutBufSize 
)

GetStringAlloc.

Same as GetString() except the buffer is allocated to the exact size of the value. Useful when the buffer is allocated everytime rather than reusing the same buffer when calling this function multiple times.

Parameters:
aSectionsection name
aKeykey name
aOutBufbuffer to be allocated
aOutBufSizesize of newly allocated buffer
Returns:
mError operation success code
int nsINIParser::GetStringAlloc ( char *  aSection,
char *  aKey,
char **  aOutBuf,
int aOutBufSize 
)

GetStringAlloc.

Same as GetString() except the buffer is allocated to the exact size of the value. Useful when the buffer is allocated everytime rather than reusing the same buffer when calling this function multiple times.

Parameters:
aSectionsection name
aKeykey name
aOutBufbuffer to be allocated
aOutBufSizesize of newly allocated buffer
Returns:
mError operation success code

Definition at line 133 of file nsINIParser.cpp.

{
    char buf[MAX_VAL_SIZE];
    int bufsize = MAX_VAL_SIZE;
    mError = OK;
    DUMP("GetStringAlloc");

    mError = GetString(aSection, aKey, buf, &bufsize);
    if (mError != OK)
        return mError;

    *aOutBuf = (char *) malloc(bufsize + 1);
    strncpy(*aOutBuf, buf, bufsize);
    *(*aOutBuf + bufsize) = 0;
    *aOutBufSize = bufsize + 1;

    return mError;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static char* nsINIParser::ResolveName ( char *  aINIRoot) [static]

ResolveName.

Given a "root" name we append the runtime locale of the current system to the <root>.ini. If such a file exists we return this as the name else simply return <root>.ini.

NOTE: Returned string is allocated and caller is responsible ---- for its deallocation.

Parameters:
aINIRootthe "root" of the INI file name
Returns:
resolved the resolved INI file name (NULL if neither exist)
char * nsINIParser::ResolveName ( char *  aINIRoot) [static]

ResolveName.

Given a "root" name we append the runtime locale of the current system to the <root>.ini. If such a file exists we return this as the name else simply return <root>.ini.

NOTE: Returned string is allocated and caller is responsible ---- for its deallocation.

Parameters:
aINIRootthe "root" of the INI file name
Returns:
resolved the resolved INI file name (NULL if neither exist)

Definition at line 161 of file nsINIParser.cpp.

{
    char *resolved = NULL;
    char *locale = NULL;
    struct stat st_exists;

    /* param check */
    if (!aINIRoot)
        return NULL;

    locale = setlocale(LC_CTYPE, NULL);
    if (!locale) 
        return NULL;

    /* resolved string: "<root>.ini.<locale>\0" */
    resolved = (char *) malloc(strlen(aINIRoot) + 5 + strlen(locale) + 1);
    if (!resolved)
        return NULL;

    /* locale specific ini file name */
    sprintf(resolved, "%s.ini.%s", aINIRoot, locale);
    if (0 == stat(resolved, &st_exists))
        return resolved;

    /* fallback to general ini file name */
    sprintf(resolved, "%s.ini", aINIRoot);
    if (0 == stat(resolved, &st_exists))
        return resolved;
    
    /* neither existed so error returned */
    return NULL;
}

Here is the caller graph for this function:


Member Data Documentation

Definition at line 147 of file nsINIParser.h.

char * nsINIParser::mFileBuf [private]

Definition at line 145 of file nsINIParser.h.

Definition at line 146 of file nsINIParser.h.


The documentation for this class was generated from the following files: