Jump to content
  • Advertisement
Sign in to follow this  
cozzie

Moving from plain "ascii" to XML?

This topic is 629 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,
I'm planning to move from reading ascii/ plain text from files, to XML.
To explain myself better, my 3d engine reads data from files, just an example:

File: settings.dat
Contents:
#XRESOLUTION 1280
#YRESOLUTION 720
#WINDOWED false
Etc.

In my codebase I simply use an fstream and read out the values linearly through the file, checking the labels.
Now I want to change this to XML, example:

<d3d11_settings>
<xresolution>1280</xresolution>
<yresolution>720<yresolution>
<windowed>true</windowed>
</d3d11_settings>

My questions are:
- what tools can I use to generate/ manually create the XSD? (defining field names, minoccurs/maxoccurs, data type etc.)
- assuming I have the XSD, what (parser) library do you advice?

I'm trying to keep things simple, for example the library should simply return me the xresolution value when I request it (from the loaded XML file).
Any input is appreciated.

Share this post


Link to post
Share on other sites
Advertisement

I'm planning to move from reading ascii/ plain text from files, to XML.
...
I'm trying to keep things simple,

 

You have simple.  Moving to XML buys you what exactly?  Bloated and less readable data files?  Slower parsing?  Dependencies on third party XML parsers?

Share this post


Link to post
Share on other sites

http://yaml.org/ , in particular, yaml-cpp. From their example/tutorial:

YAML::Node config = YAML::LoadFile("config.yaml");

if (config["lastLogin"]) {
  std::cout << "Last logged in: " << config["lastLogin"].as<DateTime>() << "\n";
}

const std::string username = config["username"].as<std::string>();
const std::string password = config["password"].as<std::string>();
login(username, password);
config["lastLogin"] = getCurrentDateTime();

std::ofstream fout("config.yaml");
fout << config;

Share this post


Link to post
Share on other sites

As has been said before the current format is already much simpler than XML... I wouldn't bother with XML unless you end up with complex enough structures to warrant deep trees. Doesn't look like that's the case here.

 

If you really want to switch to something more standard, I guess you could go with INI files, though there's a bunch of libraries out there to look for... and it may even be feasible to just write your own parser (the main reason they took off back then is that they're that simple, after all). Or even modify your own, I mean, replace the space with an equals sign and you're halfway there =P

Share this post


Link to post
Share on other sites

Note that parsers which respect XSDs are about 10x larger libraries and much slower, too.

 

And on this point, note that you probably don't need the functionality that XSD provides at runtime in any case. XSD is useful for validation, and for authoring content in aware text editors, and for enabling XSLT transformations, but you don't need any of that after release. Your XML files will already have been authored and transformed and ought already have been validated by the time you ship them to customers.

Share this post


Link to post
Share on other sites

Lets see, verbose XML:

<d3d11_settings>
  <xresolution>1280</xresolution>
  <yresolution>720<yresolution>
  <windowed>true</windowed>
</d3d11_settings>

If you're doing XML you should probably do it like:

<d3d11_settings
 xresolution="1280"
 yresolution="720"
 windowed="true" />

ie, use attributes.

 

If you're doing JSON:

"d3d11_settings": {
  "xresolution": 1280,
  "yresolution": 720,
  "windowed": true
}

ie, nicer.

 

If you're doing YAML:

d3d11_settings:
  xresolution: 1280
  yresolution: 720
  windowed: true

ie, "look ma! no double quotes!".

 

I'd use something else than XML, YAML is nice. JSON is not as nice, but its much more widely used (thus more info on it, more libs, etc). 

 

EDIT: Fixed the JSON as fastcall mentions below.

 

Anyway, this is my YAML window stuff as an example:

width: 1280
height: 720
fullscreen: false
vsync: yes
softsync: false
vfov: 75
renderdist: 256
resizable: yes

Basically using it as a normal key-value thingy, except i can just call "load" and I get a Map<String,Object> out of it. My renderer pipeline is also made in YAML, using the compact notation, here is a render pass:

dirlight_pass: &dirlight_pass
  name: dirlight_pass
  program: df_dir_light
  drawing:
    name: gbuffer
    targets: [light_acc]
  sampling:
    name: gbuffer
    targets: [albedo, normal, misc, depth]
  samplers:
    - [default, default, default, default]
  batch: LIGHT_DIR
  updaters: [DIRLIGHT]
  descriptors:
    writing: COLOR_ONLY
    depth:
      func: GREATER
      range: [1.0, 1.0]
      testing: YES
      clamping: NO
    scissor: DEFAULT
    viewport: DEFAULT
    stencil: NO
    triangle: DEFAULT
    blending: NO
    clearing: COLOR_ONLY
Edited by TheChubu

Share this post


Link to post
Share on other sites
Chubu’s JSON example is not quite correct. There are other datatypes besides string in JSON:
{
    "d3d11_settings": {
        "xresolution": 1280,
        "yresolution" 720,
        "windowed": true
    }
}
Though, I would probably combine the resolution properties into an array:

"resolution": [1280,720],



Call me old fashioned, but a simple `key=value` with `#comments` is dead simple and suitable for most configuration. INI, perhaps... Edited by fastcall22

Share this post


Link to post
Share on other sites
Another option would be using Lua. It's a full blown scripting language, that actually started out as being used for config files.

If you intend to add scripting support to your library as well this might be a good match.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!