Data Converter

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.

How Useful Was This Interpreter?

5 (Very)
0
No votes
4
1
20%
3 (So-So)
1
20%
2
2
40%
1 (Terrible)
1
20%
 
Total votes: 5

eeheeehe
Gnoblar
Posts: 22
Joined: Sun Mar 20, 2005 9:46 pm

Data Converter

Post by eeheeehe »

I had been looking for an easy way to pull data from a file and use it in the game as something other than Ogre::String. After looking through the forums, I found no answer, but did find a few people who wanted to know the same thing. So Im posting this code here so that other people can use it.

The Header File

Code: Select all

#ifndef __SETUP_FILE_VARIABLES__
#define __SETUP_FILE_VARIABLES__

#include "Ogre.h"
#include "OgreConfigFile.h"
#include "OgreStringConverter.h"
#include <stdlib.h> 

class SetupFileVar
{
public:

float SetupFileFloat(const char* file, const char* value)
{
     ConfigFile setupfile;
     setupfile.load(file);
     
     Ogre::String resultOgreString = setupfile.getSetting(value);
     const char* resultString = resultOgreString.c_str();
     
     float result = atof(resultString);
     return result;     
}

int SetupFileInt(const char* file, const char* value)
{
     ConfigFile setupfile;
     setupfile.load(file);
     
     Ogre::String resultOgreString = setupfile.getSetting(value);
     const char* resultString = resultOgreString.c_str();
     
     int result = atoi(resultString);
     return result;     
}

const char* SetupFileString(const char* file, const char* value)
{
      ConfigFile setupfile;
      setupfile.load(file);
      
      Ogre::String resultOgreString = setupfile.getSetting(value);
      const char* resultString = resultOgreString.c_str();
      
      return resultString;
}

bool SetupFileBool(const char* file, const char* value)
{
     ConfigFile setupfile;
     setupfile.load(file);
     
     Ogre::String resultOgreString = setupfile.getSetting(value);
     const char* resultString = resultOgreString.c_str();
     
     //bool result;
     
     if(resultString = "TRUE")
     {
         return true;
     }else{
         return false;
     }
         
}

Ogre::String SetupFileOgreString(const char* file, const char* value)
{
     ConfigFile setupfile;
     setupfile.load(file);
     
     Ogre::String resultOgreString = setupfile.getSetting(value);
     return resultOgreString;     
}


};

#endif

Calling the program
Change this line:

Code: Select all

class RSApplication : public ExampleApplication
to this:

Code: Select all

class RSApplication : public ExampleApplication, SetupFileVar
To retrieve a variable from a file (MyConfigFile.cfg) use the following syntax:
  • Float

    Code: Select all

    float myfloat = SetupFileVars::SetupFileFloat("MyConfigFile.cfg", "MyFloat");
    
  • Integer

    Code: Select all

    int myInt = SetupFileVars::SetupFileInt("MyConfigFile.cfg", "MyInt");
    
  • C String

    Code: Select all

    const char* myString = SetupFileVars::SetupFileString("MyConfigFile.cfg", "MyString");
    
  • Integer

    Code: Select all

    bool myBool = SetupFileVars::SetupFileBool("MyConfigFile.cfg", "MyBool");
    
  • Integer

    Code: Select all

    Ogre::String myInt = SetupFileVars::SetupFileOgreString("MyConfigFile.cfg", "MyOgreString");
    
If there is a mistake in this, please post it here, or if you make a modification that makes the executables work better.
User avatar
sinbad
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 19269
Joined: Sun Oct 06, 2002 11:19 pm
Location: Guernsey, Channel Islands
x 66

Post by sinbad »

I'm having trouble understanding why you think you need this. What's so hard about this:

Code: Select all

ConfigFile cfg;
cfg.load("settings.cfg");
bool settingBool = StringConverter::parseBool(cfg.getSetting("mybool"));
int settingInt = StringConverter::parseInt(cfg.getSetting("myint"));
Real settingReal = StringConverter::parseReal(cfg.getSetting("myReal"));
Vector3 settingVector = StringConverter::parseVector3(cfg.getSetting("myVector"));
// etc
So, I think this code is redundant. Your code also has a number of problems, such as loading the file every time for retrieving a single setting, not supporting file sections, not supporting multi-settings, and not supporting vectors, matrices, quaternions, being obsessed with c-strings (shudder - you can always convert an Ogre::String to a horrible old c-string using the c_str() method anyway). Maybe you just didn't know about StringConverter?
User avatar
Mr.Bloodworth
Halfling
Posts: 91
Joined: Fri Mar 25, 2005 3:19 am

Post by Mr.Bloodworth »

In English this does what?
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179

Post by jacmoe »

Mr.Bloodworth wrote:In English this does what?
What is "this" and what is "what" ? :)
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
eeheeehe
Gnoblar
Posts: 22
Joined: Sun Mar 20, 2005 9:46 pm

Thank You for the Input

Post by eeheeehe »

@Sinbad:

lol, as a matter of fact, I did not know about the StringConverter, or how to use it :P Thanks for being honest about it though Sinbad. :D This was just a quick code and I didn't want to improve on it if nobody liked it. But now that I know there is an actual code for it, then I will have to look it up and use that instead.

Thank you for your honest input. I would rather know that something was bad and find a better solution than think that something was good, and it really sucks.

Or in short:
Im glad that a developer pointed this out, instead of a player.