XFuConfiguration.cpp Source File

Main Page Class Hierarchy Alphabetical List Compound List File List Compound Members File Members Related Pages

# XFuConfiguration.cpp

Go to the documentation of this file.

    00001 /*! \file
    00002  * X-Forge Util <br>
    00003  * Copyright 2000-2003 Fathammer Ltd
    00004  *
    00005  * \brief XFuConfiguration.cpp is the implementation file for the XFuConfiguration
    00006  * class. For more information, see XFuConfiguration.h
    00007  *
    00008  * $Id: XFuConfiguration.cpp,v 1.19 2003/09/23 13:33:42 toni Exp $
    00009  * $Date: 2003/09/23 13:33:42 $
    00010  * $Revision: 1.19 $
    00011  */
    00012
    00013 #include <xfcore/XFcCore.h>
    00014 #include <xfutil/XFuConfiguration.h>
    00015
    00016
    00017 XFuConfiguration * XFuConfiguration::create()
    00018 {
    00019     return new XFuConfiguration;
    00020 }
    00021
    00022
    00023 XFuConfiguration * XFuConfiguration::create(const CHAR *aFilename)
    00024 {
    00025     XFuConfiguration *config = new XFuConfiguration;
    00026     if (config != NULL)
    00027     {
    00028         if (!config->load(aFilename))
    00029         {
    00030             delete config;
    00031             return NULL;
    00032         }
    00033     }
    00034     return config;
    00035 }
    00036
    00037
    00038 XFuConfiguration * XFuConfiguration::create(XFcFile *aFile)
    00039 {
    00040     XFuConfiguration *config = new XFuConfiguration;
    00041     if (config != NULL)
    00042     {
    00043         if (!config->load(aFile))
    00044         {
    00045             delete config;
    00046             return NULL;
    00047         }
    00048     }
    00049     return config;
    00050 }
    00051
    00052
    00053 XFuConfiguration::XFuConfiguration() : mProperties(NULL)
    00054 {
    00055 }
    00056
    00057
    00058 XFuConfiguration::~XFuConfiguration()
    00059 {
    00060     clear();
    00061 }
    00062
    00063
    00064 CHAR * XFuConfiguration::get(const CHAR *aKey)
    00065 {
    00066     if (mProperties == NULL) return NULL;
    00067     XFcHashtableIterator<XFuStringKey, void *> it = mProperties->find(XFuStringKey(aKey));
    00068     return (it.isValid() ? (CHAR *)it.getValue() : NULL);
    00069 }
    00070
    00071
    00072 INT32 XFuConfiguration::getINT32(const CHAR *aKey)
    00073 {
    00074     CHAR *value = get(aKey);
    00075     if (value == NULL)
    00076         return 0;
    00077     else
    00078         return XFcStringToolkit::toINT32(value);
    00079 }
    00080
    00081
    00082 FLOAT32 XFuConfiguration::getFLOAT32(const CHAR *aKey)
    00083 {
    00084     CHAR *value = get(aKey);
    00085     if (value == NULL)
    00086         return 0;
    00087     else
    00088         return XFcStringToolkit::toFLOAT32(value);
    00089 }
    00090
    00091
    00092 void XFuConfiguration::put(const CHAR *aKey, const CHAR *aValue)
    00093 {
    00094     if (mProperties == NULL || aKey == NULL || aValue == NULL) return;
    00095     CHAR *oldVal = get(aKey);
    00096     if (oldVal != NULL)
    00097     {
    00098         delete[] get(aKey);
    00099         mProperties->remove(XFuStringKey(aKey));
    00100     }
    00101     CHAR *val = XFcStringToolkit::copy(aValue);
    00102     mProperties->put(XFuStringKey(aKey), (void *)val);
    00103 }
    00104
    00105
    00106 void XFuConfiguration::clear()
    00107 {
    00108     if (mProperties != NULL)
    00109     {
    00110         XFcHashtableIterator<XFuStringKey, void *> it;
    00111         for (it = mProperties->begin(); it != mProperties->end(); ++it)
    00112             delete[] (CHAR *)it.getValue();
    00113         delete mProperties;
    00114         mProperties = NULL;
    00115     }
    00116 }
    00117
    00118
    00119 INT XFuConfiguration::load(XFcFile *aFile)
    00120 {
    00121     if (aFile == NULL)
    00122         return 0;
    00123
    00124     if (mProperties == NULL)
    00125         mProperties = new XFcHashtable<XFuStringKey, void *>;
    00126     if (mProperties == NULL)
    00127         return 0;
    00128
    00129     // check length of rest of the file
    00130     INT32 orgFilePos = aFile->tell();
    00131     aFile->seek(0, SEEK_END);
    00132     INT32 fileLen = aFile->tell();
    00133     aFile->seek(orgFilePos, SEEK_SET);
    00134
    00135     // read until end of file
    00136     while (aFile->tell() < fileLen)
    00137     {
    00138         // read a line, covert it to 16 bit and remove leading and trailing white spaces
    00139         CHAR8 *buffer8 = buffer8 = readLine(aFile);
    00140         CHAR *buffer = XFcStringToolkit::copy(buffer8);
    00141         CHAR *trimmedBuffer = strDupTrim(buffer);
    00142
    00143         // parse the line and add the property key and value to the property hashtable
    00144         processLine(trimmedBuffer);
    00145
    00146         delete[] trimmedBuffer;
    00147         delete[] buffer;
    00148         delete[] buffer8;
    00149     }
    00150
    00151     return 1;
    00152 }
    00153
    00154
    00155 INT XFuConfiguration::load(const CHAR *aFilename)
    00156 {
    00157     XFcFile *file = XFcFile::open(aFilename, XFCSTR("rb"));
    00158     if (file == NULL)
    00159         return 0;
    00160
    00161     INT result = load(file);
    00162
    00163     file->close();
    00164
    00165     return result;
    00166 }
    00167
    00168
    00169 void XFuConfiguration::processLine(const CHAR *aStr)
    00170 {
    00171     // valid property definition?
    00172     INT32 len = XFcStringToolkit::getLength(aStr);
    00173     if (len > 0 && aStr[0] != '#')
    00174     {
    00175
    00176         // find the first = char
    00177         INT32 valStart = 0;
    00178         while (valStart < len && aStr[valStart] != '=') valStart++;
    00179
    00180         if (valStart != len && valStart != 0)
    00181         {
    00182
    00183             INT32 i, p;
    00184
    00185             CHAR * key = new CHAR[valStart + 1];
    00186
    00187             p = 0;
    00188             for (i = 0; i < valStart; i++) key[p++] = aStr[i];
    00189             // find last non white space char of the key
    00190             do --p; while (key[p] == ' ' || key[p] == '\t');
    00191             key[p + 1] = '\0';
    00192
    00193             // find first non white space char of the value
    00194             do valStart++; while (aStr[valStart] == ' ' || aStr[valStart] == '\t');
    00195
    00196             CHAR * val = new CHAR[len - valStart + 1];
    00197
    00198             p = 0;
    00199             for (i = valStart; i < len; i++) val[p++] = aStr[i];
    00200             val[p] = '\0';
    00201
    00202             put(key, val);
    00203
    00204             delete[] key;
    00205             delete[] val;
    00206
    00207         }
    00208
    00209     }
    00210 }
    00211
    00212
    00213 CHAR * XFuConfiguration::strDupTrim(const CHAR *aStr)
    00214 {
    00215     INT32 len = XFcStringToolkit::getLength(aStr);
    00216
    00217     // find first non white space char
    00218     INT32 start = 0;
    00219     while (start < len && (aStr[start] == ' ' || aStr[start] == '\t'))
    00220         start++;
    00221
    00222     // find last non white space char
    00223     INT32 end = len - 1;
    00224     while (end >= 0 && (aStr[end] == ' ' || aStr[end] == '\t'))
    00225         end--;
    00226
    00227     CHAR *buf;
    00228     INT32 p = 0;
    00229     if (end == -1)
    00230     {
    00231         // only spaces so the result is an empty string
    00232         buf = new CHAR[1];
    00233     }
    00234     else
    00235     {
    00236         // copy the chars between the first and the last white space
    00237         buf = new CHAR[end - start + 2];
    00238         INT32 i;
    00239         for (i = start; i <= end; i++) buf[p++] = aStr[i];
    00240     }
    00241
    00242     buf[p] = '\0';
    00243     return buf;
    00244 }
    00245
    00246
    00247 CHAR8 * XFuConfiguration::readLine(XFcFile *aFile)
    00248 {
    00249     INT32 currentPos = aFile->tell();
    00250
    00251     INT32 count = 0;
    00252     CHAR8 c = 0;
    00253
    00254     // find end of line
    00255     while (c != 10)
    00256     {
    00257         if (aFile->read(&c, sizeof(CHAR8), 1) == 0) break;
    00258         count++;
    00259     }
    00260
    00261     aFile->seek(currentPos, SEEK_SET);
    00262
    00263     // read until end of line
    00264     CHAR8 *buffer = new CHAR8[count + 1];
    00265     if (count > 0) aFile->read(buffer, sizeof(CHAR8), count);
    00266
    00267     buffer[count] = '\0';
    00268
    00269     // remove trailing CR and LF
    00270     if (count >= 1 && buffer[count - 1] == 10) buffer[count - 1] = '\0';
    00271     if (count >= 2 && buffer[count - 2] == 13) buffer[count - 2] = '\0';
    00272
    00273     return buffer;
    00274 }
    00275

|  | X-Forge Documentation Confidential Copyright © 2002-2003 Fathammer |  | Documentation generated with doxygen by Dimitri van Heesch