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

# XFuConfigurationData.cpp

Go to the documentation of this file.

    00001 /*! \file
    00002  * X-Forge Util <br>
    00003  * Copyright 2000-2003 Fathammer Ltd
    00004  *
    00005  * $Id: XFuConfigurationData.cpp,v 1.12 2003/08/12 13:34:08 lars Exp $
    00006  * $Date: 2003/08/12 13:34:08 $
    00007  * $Revision: 1.12 $
    00008  */
    00009
    00010 #include <xforge.h>
    00011 #include <xfutil/XFuPrinter.h>
    00012 #include <xfutil/XFuRLESpritePrinter.h>
    00013 #include <xfutil/XFuTokenizer.h>
    00014 #include <xfutil/XFuConfigurationData.h>
    00015
    00016
    00017 #define XFUCD_TYPE_DELIMITER_CHAR       (CHAR)':'
    00018 #define XFUCD_TYPE_DELIMITER_STR        XFCSTR(":")
    00019 #define XFUCD_TYPE_IMAGE_SHORT_CHAR     (CHAR)'i'
    00020 #define XFUCD_TYPE_IMAGE_SHORT_STR      XFCSTR("i")
    00021 #define XFUCD_TYPE_IMAGE_LONG           XFCSTR("image")
    00022 #define XFUCD_TYPE_PRINTER_SHORT_CHAR   (CHAR)'p'
    00023 #define XFUCD_TYPE_PRINTER_SHORT_STR    XFCSTR("p")
    00024 #define XFUCD_TYPE_PRINTER_LONG         XFCSTR("printer")
    00025 #define XFUCD_TYPE_SOUND_SHORT_CHAR     (CHAR)'s'
    00026 #define XFUCD_TYPE_SOUND_SHORT_STR      XFCSTR("s")
    00027 #define XFUCD_TYPE_SOUND_LONG           XFCSTR("sound")
    00028 #define XFUCD_TYPE_MUSIC_SHORT_CHAR     (CHAR)'m'
    00029 #define XFUCD_TYPE_MUSIC_SHORT_STR      XFCSTR("m")
    00030 #define XFUCD_TYPE_MUSIC_LONG           XFCSTR("music")
    00031
    00032
    00033 XFuConfigurationData::XFuConfigurationData() :
    00034     mImages(NULL), mPrinters(NULL), mSounds(NULL), mMusics(NULL)
    00035 {
    00036 }
    00037
    00038
    00039 XFuConfigurationData * XFuConfigurationData::create()
    00040 {
    00041     XFuConfigurationData *res = new XFuConfigurationData;
    00042     if (res != NULL && !res->init())
    00043     {
    00044         delete res;
    00045         return NULL;
    00046     }
    00047     return res;
    00048 }
    00049
    00050
    00051 XFuConfigurationData * XFuConfigurationData::create(const CHAR *aFilename)
    00052 {
    00053     XFuConfigurationData *res = create();
    00054     if (res != NULL)
    00055     {
    00056         if (!res->load(aFilename))
    00057         {
    00058             delete res;
    00059             return NULL;
    00060         }
    00061     }
    00062     return res;
    00063 }
    00064
    00065
    00066 XFuConfigurationData * XFuConfigurationData::create(XFcFile *aFile)
    00067 {
    00068     XFuConfigurationData *res = create();
    00069     if (res != NULL)
    00070     {
    00071         if (!res->load(aFile))
    00072         {
    00073             delete res;
    00074             return NULL;
    00075         }
    00076     }
    00077     return res;
    00078 }
    00079
    00080
    00081 INT XFuConfigurationData::init()
    00082 {
    00083     mImages = new ImageSet;
    00084     mPrinters = new PrinterSet;
    00085     mSounds = new SoundSet;
    00086     mMusics = new MusicSet;
    00087     if (mImages == NULL ||
    00088         mPrinters == NULL ||
    00089         mSounds == NULL ||
    00090         mMusics == NULL)
    00091     {
    00092         delete mImages;
    00093         delete mPrinters;
    00094         delete mSounds;
    00095         delete mMusics;
    00096         return 0;
    00097     }
    00098     return 1;
    00099 }
    00100
    00101
    00102 XFuConfigurationData::~XFuConfigurationData()
    00103 {
    00104     // delete all images
    00105     ImageSet::iterator imageIt = mImages->begin();
    00106     for (; imageIt != mImages->end(); ++imageIt)
    00107         delete imageIt.getValue();
    00108     delete mImages;
    00109     mImages = NULL;
    00110
    00111     // delete all printers
    00112     PrinterSet::iterator printerIt = mPrinters->begin();
    00113     for (; printerIt != mPrinters->end(); ++printerIt)
    00114         delete printerIt.getValue();
    00115     delete mPrinters;
    00116     mPrinters = NULL;
    00117
    00118     // delete all sounds
    00119     SoundSet::iterator soundIt = mSounds->begin();
    00120     for (; soundIt != mSounds->end(); ++soundIt)
    00121         delete soundIt.getValue();
    00122     delete mSounds;
    00123     mSounds = NULL;
    00124
    00125     // delete all music
    00126     MusicSet::iterator musicIt = mMusics->begin();
    00127     for (; musicIt != mMusics->end(); ++musicIt)
    00128         delete musicIt.getValue();
    00129     delete mMusics;
    00130     mMusics = NULL;
    00131
    00132     // parent cleanup
    00133     XFuConfiguration::clear();
    00134 }
    00135
    00136
    00137 CHAR * XFuConfigurationData::get(const CHAR *aKey)
    00138 {
    00139     XFCASSERT(aKey != NULL && "XFuConfigurationData::get");
    00140     return XFuConfiguration::get(aKey);
    00141 }
    00142
    00143
    00144 CHAR * XFuConfigurationData::makePrefixedKey(const CHAR aPrefix, const CHAR *aKey)
    00145 {
    00146     CHAR *key = new CHAR[XFcStringToolkit::getLength(aKey) + 3];
    00147     XFcStringToolkit::format(key, "%c%c%s", aPrefix, XFUCD_TYPE_DELIMITER_CHAR, aKey);
    00148     return key;
    00149 }
    00150
    00151
    00152 XFcGLResourceSurface * XFuConfigurationData::getImage(const CHAR *aKey)
    00153 {
    00154     XFCASSERT(aKey != NULL && "XFuConfigurationData::getImage");
    00155     CHAR *key = makePrefixedKey(XFUCD_TYPE_IMAGE_SHORT_CHAR, aKey);
    00156     ImageSet::iterator it = mImages->find(XFuStringKey(get(key)));
    00157     delete[] key;
    00158     if (it.isValid())
    00159     {
    00160         if (it.getValue() == NULL)
    00161             loadImage(it);
    00162         return it.getValue();
    00163     }
    00164     else
    00165         return NULL;
    00166 }
    00167
    00168
    00169 XFuPrinter * XFuConfigurationData::getPrinter(const CHAR *aKey)
    00170 {
    00171     XFCASSERT(aKey != NULL && "XFuConfigurationData::getPrinter");
    00172     CHAR *key = makePrefixedKey(XFUCD_TYPE_PRINTER_SHORT_CHAR, aKey);
    00173     PrinterSet::iterator it = mPrinters->find(XFuStringKey(get(key)));
    00174     delete[] key;
    00175     if (it.isValid())
    00176     {
    00177         if (it.getValue() == NULL)
    00178             loadPrinter(it);
    00179         return it.getValue();
    00180     }
    00181     else
    00182         return NULL;
    00183 }
    00184
    00185
    00186 XFcRectangle XFuConfigurationData::parseRectangleString(const CHAR *aValue)
    00187 {
    00188     XFcRectangle rect(0, 0, 0, 0);
    00189     XFuTokenizer tok;
    00190     INT32 tokens;
    00191
    00192     if (aValue != NULL)
    00193     {
    00194         tok.tokenize(aValue, XFCSTR(","));
    00195         tokens = tok.getTokenCount();
    00196         XFCASSERT(tokens >= 1 && tokens <= 4 && "XFuConfigurationData::parseRectangleString: Invalid tokens.");
    00197
    00198         if (tokens >= 1) rect.mX = XFcStringToolkit::toINT32(tok.getToken(0));
    00199         if (tokens >= 2) rect.mY = XFcStringToolkit::toINT32(tok.getToken(1));
    00200         if (tokens >= 3) rect.mWidth = XFcStringToolkit::toINT32(tok.getToken(2));
    00201         if (tokens >= 4) rect.mHeight = XFcStringToolkit::toINT32(tok.getToken(3));
    00202     }
    00203
    00204     return rect;
    00205 }
    00206
    00207
    00208 INT XFuConfigurationData::getRectangle(XFcRectangle &aDest, const CHAR *aKey)
    00209 {
    00210     XFCASSERT(aKey != NULL && "XFuConfigurationData::getRectangle");
    00211     const CHAR *value = get(aKey);
    00212
    00213     if (value != NULL)
    00214     {
    00215         aDest = parseRectangleString(value);
    00216         return 1;
    00217     }
    00218     return 0;
    00219 }
    00220
    00221
    00222 XFuConfigurationDataSoundInfo * XFuConfigurationData::getSound(const CHAR *aKey)
    00223 {
    00224     XFCASSERT(aKey != NULL && "XFuConfigurationData::getSound");
    00225     CHAR *key = makePrefixedKey(XFUCD_TYPE_SOUND_SHORT_CHAR, aKey);
    00226     SoundSet::iterator it = mSounds->find(XFuStringKey(get(key)));
    00227     delete[] key;
    00228     if (it.isValid())
    00229         return it.getValue();
    00230     else
    00231         return NULL;
    00232 }
    00233
    00234
    00235 XFuConfigurationDataMusicInfo * XFuConfigurationData::getMusic(const CHAR *aKey)
    00236 {
    00237     XFCASSERT(aKey != NULL && "XFuConfigurationData::getMusic");
    00238     CHAR *key = makePrefixedKey(XFUCD_TYPE_MUSIC_SHORT_CHAR, aKey);
    00239     MusicSet::iterator it = mMusics->find(XFuStringKey(get(key)));
    00240     delete[] key;
    00241     if (it.isValid())
    00242         return it.getValue();
    00243     else
    00244         return NULL;
    00245 }
    00246
    00247
    00248 INT XFuConfigurationData::load(const CHAR *aFilename)
    00249 {
    00250     XFCASSERT(aFilename != NULL && "XFuConfigurationData::load(const CHAR *)");
    00251     return XFuConfiguration::load(aFilename);
    00252 }
    00253
    00254
    00255 INT XFuConfigurationData::load(XFcFile *aFile)
    00256 {
    00257     XFCASSERT(aFile != NULL && "XFuConfigurationData::load(XFcFile *)");
    00258     return XFuConfiguration::load(aFile);
    00259 }
    00260
    00261
    00262 void XFuConfigurationData::put(const CHAR *aKey, const CHAR *aValue)
    00263 {
    00264     XFCASSERT(aKey != NULL && aValue != NULL && "XFuConfigurationData::put");
    00265     if (aKey == NULL || aKey[0] == 0)
    00266         return;
    00267
    00268     XFuTokenizer tok;
    00269     tok.tokenize(aKey, XFUCD_TYPE_DELIMITER_STR);
    00270
    00271     if (tok.getTokenCount() > 1)
    00272     {
    00273         CHAR reformatPrefix = 0;
    00274         const CHAR *type = tok.getToken(0);
    00275         // TODO: Add type registration interface.
    00276         if (XFcStringToolkit::compare(type, XFUCD_TYPE_IMAGE_SHORT_STR) == 0 ||
    00277             XFcStringToolkit::compare(type, XFUCD_TYPE_IMAGE_LONG) == 0)
    00278         {
    00279             reformatPrefix = XFUCD_TYPE_IMAGE_SHORT_CHAR;
    00280             addImage(aValue);
    00281         }
    00282         else if (XFcStringToolkit::compare(type, XFUCD_TYPE_PRINTER_SHORT_STR) == 0 ||
    00283                  XFcStringToolkit::compare(type, XFUCD_TYPE_PRINTER_LONG) == 0)
    00284         {
    00285             reformatPrefix = XFUCD_TYPE_PRINTER_SHORT_CHAR;
    00286             addPrinter(aValue);
    00287         }
    00288         else if (XFcStringToolkit::compare(type, XFUCD_TYPE_SOUND_SHORT_STR) == 0 ||
    00289                  XFcStringToolkit::compare(type, XFUCD_TYPE_SOUND_LONG) == 0)
    00290         {
    00291             reformatPrefix = XFUCD_TYPE_SOUND_SHORT_CHAR;
    00292             addSound(aValue);
    00293         }
    00294         else if (XFcStringToolkit::compare(type, XFUCD_TYPE_MUSIC_SHORT_STR) == 0 ||
    00295                  XFcStringToolkit::compare(type, XFUCD_TYPE_MUSIC_LONG) == 0)
    00296         {
    00297             reformatPrefix = XFUCD_TYPE_MUSIC_SHORT_CHAR;
    00298             addMusic(aValue);
    00299         }
    00300
    00301         // If reformatPrefix is set, reformat the key with the shortened prefix
    00302         // and add the entry with the revised key. Otherwise the original key
    00303         // is added.
    00304         if (reformatPrefix != 0)
    00305         {
    00306             const CHAR *key = tok.getToken(1);
    00307             CHAR *newkey = new CHAR[XFcStringToolkit::getLength(key) + 3];
    00308             XFcStringToolkit::format(newkey, "%c%c%s", reformatPrefix, XFUCD_TYPE_DELIMITER_CHAR, key);
    00309             XFuConfiguration::put(newkey, aValue);
    00310             delete[] newkey;
    00311         }
    00312         else
    00313             XFuConfiguration::put(aKey, aValue);
    00314     }
    00315     else
    00316         XFuConfiguration::put(aKey, aValue);
    00317 }
    00318
    00319
    00320 void XFuConfigurationData::addImage(const CHAR *aValue)
    00321 {
    00322     ImageSet::iterator it = mImages->find(XFuStringKey(aValue));
    00323     if (!it.isValid())
    00324     {
    00325         XFuStringKey valueKey(aValue);
    00326         mImages->put(valueKey, NULL);
    00327
    00328         if (!mDeferredLoadingEnabled)
    00329         {
    00330             ImageSet::iterator it = mImages->find(valueKey);
    00331             XFCASSERT(it.isValid() && "XFuConfigurationData::addImage: Image property not found!");
    00332             loadImage(it);
    00333         }
    00334     }
    00335 }
    00336
    00337
    00338 void XFuConfigurationData::loadImage(ImageSet::iterator &aIterator)
    00339 {
    00340     // don't try load the image if it has already been loaded
    00341     if (aIterator.getValue() != NULL)
    00342         return;
    00343
    00344     XFuTokenizer tok;
    00345
    00346     CHAR *key = XFcStringToolkit::copy(aIterator.getKey().mKey);
    00347     tok.tokenize(key, XFCSTR(","));
    00348     const CHAR *filename = NULL;
    00349     INT32 format = XFCGF_DEFAULT;
    00350
    00351     if (tok.getTokenCount() > 1)
    00352     {
    00353         filename = tok.getToken(0);
    00354         format = XFcStringToolkit::toINT32(tok.getToken(1));
    00355     }
    00356     else
    00357         filename = key;
    00358
    00359     XFcGLResourceSurface *rs = XFcGLResourceSurface::create(filename, format);
    00360     XFCASSERT(rs && "XFuConfigurationData::loadImage: Cannot create resource surface image!");
    00361     aIterator.setValue(rs);
    00362
    00363     delete[] key;
    00364 }
    00365
    00366
    00367 void XFuConfigurationData::addPrinter(const CHAR *aValue)
    00368 {
    00369     PrinterSet::iterator it = mPrinters->find(XFuStringKey(aValue));
    00370     if (!it.isValid())
    00371     {
    00372         XFuStringKey valueKey(aValue);
    00373         mPrinters->put(valueKey, NULL);
    00374
    00375         if (!mDeferredLoadingEnabled)
    00376         {
    00377             PrinterSet::iterator it = mPrinters->find(valueKey);
    00378             XFCASSERT(it.isValid() && "XFuConfigurationData::addPrinter: Printer property not found!");
    00379             loadPrinter(it);
    00380         }
    00381     }
    00382 }
    00383
    00384
    00385 void XFuConfigurationData::loadPrinter(PrinterSet::iterator &aIterator)
    00386 {
    00387     // don't try load the printer if it has already been loaded
    00388     if (aIterator.getValue() != NULL)
    00389         return;
    00390
    00391     XFuPrinter *pr = NULL;
    00392     XFuTokenizer tok;
    00393
    00394     CHAR *key = XFcStringToolkit::copy(aIterator.getKey().mKey);
    00395     tok.tokenize(key, XFCSTR(","));
    00396     const CHAR *filename = tok.getToken(0);
    00397     XFCASSERT(tok.getTokenCount() >= 3 && "XFuConfigurationData::addPrinter: Not enough tokens for printer!");
    00398     INT32 type = XFcStringToolkit::toINT32(tok.getToken(2));
    00399     XFCASSERT((type == 0 || type == 1) && "XFuConfigurationData::addPrinter: Unknown printer type!");
    00400
    00401     if (tok.getTokenCount() > 3)
    00402     {
    00403         UINT32 colorkey = (UINT32)XFcStringToolkit::toINT32(tok.getToken(3));
    00404         if (type == 0)
    00405             pr = XFuPrinter::create(filename, colorkey);
    00406         else if (type == 1)
    00407             pr = XFuRLESpritePrinter::create(filename, 0xffffff, colorkey);
    00408     }
    00409     else
    00410     {
    00411         if (type == 0)
    00412             pr = XFuPrinter::create(filename);
    00413         else if (type == 1)
    00414             pr = XFuRLESpritePrinter::create(filename);
    00415     }
    00416     XFCASSERT(pr != NULL && "XFuConfigurationData::loadPrinter: Cannot create printer!");
    00417     pr->setLetterSpacing(XFcStringToolkit::toINT32(tok.getToken(1)));
    00418     aIterator.setValue(pr);
    00419
    00420     delete[] key;
    00421 }
    00422
    00423
    00424 // TODO: Add possible loading of sound when sounds are changed to resources.
    00425 void XFuConfigurationData::addSound(const CHAR *aValue)
    00426 {
    00427     SoundSet::iterator it = mSounds->find(XFuStringKey(aValue));
    00428     if (!it.isValid())
    00429     {
    00430         XFuTokenizer tok;
    00431
    00432         tok.tokenize(aValue, XFCSTR(","));
    00433
    00434         XFuConfigurationDataSoundInfo *si = new XFuConfigurationDataSoundInfo;
    00435
    00436         if (tok.getTokenCount() == 1)
    00437         {
    00438             si->mFilename = XFcStringToolkit::copy(aValue);
    00439         }
    00440         else
    00441         {
    00442             si->mFilename = XFcStringToolkit::copy(tok.getToken(0));
    00443             if ((tok.getTokenCount() > 1) && (XFcStringToolkit::toINT32(tok.getToken(1)) != -1))
    00444             {
    00445                 si->mSamplingRate = XFcStringToolkit::toFLOAT32(tok.getToken(1));
    00446                 si->mFlags |= XFUCDAIF_SAMPLING_RATE;
    00447             }
    00448             if ((tok.getTokenCount() > 2) && (XFcStringToolkit::toINT32(tok.getToken(2)) != -1))
    00449             {
    00450                 si->mVolume = XFcStringToolkit::toFLOAT32(tok.getToken(2));
    00451                 si->mFlags |= XFUCDAIF_VOLUME;
    00452             }
    00453             if ((tok.getTokenCount() > 3) && (XFcStringToolkit::toINT32(tok.getToken(3)) != -1))
    00454             {
    00455                 si->mPanning = XFcStringToolkit::toFLOAT32(tok.getToken(3));
    00456                 si->mFlags |= XFUCDAIF_PANNING;
    00457             }
    00458             if ((tok.getTokenCount() > 4) && (XFcStringToolkit::toINT32(tok.getToken(4)) != -1))
    00459             {
    00460                 si->mPriority = (UINT16)XFcStringToolkit::toINT32(tok.getToken(4));
    00461                 si->mFlags |= XFUCDAIF_PRIORITY;
    00462             }
    00463             if ((tok.getTokenCount() > 5) && (XFcStringToolkit::toINT32(tok.getToken(5)) != -1))
    00464             {
    00465                 si->mLooping = XFcStringToolkit::toINT32(tok.getToken(5));
    00466                 if (si->mLooping) si->mFlags |= XFUCDAIF_LOOPING;
    00467             }
    00468             if ((tok.getTokenCount() > 6) && (XFcStringToolkit::toINT32(tok.getToken(6)) != -1))
    00469             {
    00470                 si->mLoopStart = XFcStringToolkit::toINT32(tok.getToken(6));
    00471                 si->mFlags |= XFUCDAIF_LOOPSTART;
    00472             }
    00473             if ((tok.getTokenCount() > 7) && (XFcStringToolkit::toINT32(tok.getToken(7)) != -1))
    00474             {
    00475                 si->mLoopEnd = XFcStringToolkit::toINT32(tok.getToken(7));
    00476                 si->mFlags |= XFUCDAIF_LOOPEND;
    00477             }
    00478         }
    00479
    00480         mSounds->put(XFuStringKey(aValue), si);
    00481     }
    00482 }
    00483
    00484
    00485 // TODO: Add possible loading of music when musics are changed to resources.
    00486 void XFuConfigurationData::addMusic(const CHAR *aValue)
    00487 {
    00488     MusicSet::iterator it = mMusics->find(XFuStringKey(aValue));
    00489     if (!it.isValid())
    00490     {
    00491         XFuTokenizer tok;
    00492
    00493         tok.tokenize(aValue, XFCSTR(","));
    00494
    00495         XFuConfigurationDataMusicInfo *mi = new XFuConfigurationDataMusicInfo;
    00496
    00497         XFCASSERT("addMusic: Invalid amount of parameters for music resource" && tok.getTokenCount() >= 4);
    00498
    00499         mi->mFilename = XFcStringToolkit::copy(tok.getToken(0));
    00500         mi->mSamplingRate = XFcStringToolkit::toFLOAT32(tok.getToken(1));
    00501         mi->m16Bit = XFcStringToolkit::toINT32(tok.getToken(2));
    00502         mi->mStereo = XFcStringToolkit::toINT32(tok.getToken(3));
    00503         mi->mFlags |= XFUCDAIF_SAMPLING_RATE;
    00504
    00505         if ((tok.getTokenCount() > 4) && (XFcStringToolkit::toINT32(tok.getToken(4)) != -1))
    00506         {
    00507             mi->mVolume = XFcStringToolkit::toFLOAT32(tok.getToken(4));
    00508             mi->mFlags |= XFUCDAIF_VOLUME;
    00509         }
    00510         if ((tok.getTokenCount() > 5) && (XFcStringToolkit::toINT32(tok.getToken(5)) != -1))
    00511         {
    00512             mi->mPriority = (UINT16)XFcStringToolkit::toINT32(tok.getToken(5));
    00513             mi->mFlags |= XFUCDAIF_PRIORITY;
    00514         }
    00515
    00516         mMusics->put(XFuStringKey(aValue), mi);
    00517     }
    00518 }
    00519
    00520
    00521 void XFuConfigurationData::setDeferredLoadingEnabled(INT aEnabled)
    00522 {
    00523     mDeferredLoadingEnabled = aEnabled;
    00524 }
    00525

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