Skip to main content
GameDev.net gamedev.net
🔒 Locked

saving and restoring data issues

Started by metrix Sep 16, 2006 at 1:55 PM 3 replies 1.2k views
Original Post
metrix
metrix
Hello, A few questions about saving and restoring data in a cross platform application: Say I have a written a cross platform application - say PC and Mac - and I want to be able to save some status of my program (not necessarily a game) on the PC and then load the same information back into the program on a Mac (or maybe another PC). Question 1: What about integer size in bytes? For example, what if on the PC my integer size is 4 bytes, and on the Mac its 2 bytes. How do I handle that? How do I guarantee byte size over different platforms? Question 2: Is endianess a problem with floating point types? Question 3: Is there a technique of saving my data in some sort of custom file format that makes resolving these issues easier? I've been searching the internet, but I have not really found anything helpful, so I'm not sure if these questions are silly or not and I'm just misunderstanding the whole problem :) If someone could enlighten me that would be great. metrix
T_Janssen
T_Janssen
It's all in the writing (encoding) and reading (decoding) of the data to be transfered.

1. to take into account type/data sizes, have a standard size in the data format of your file. For example, for ints, reserve 4 bytes of space in the data (be sure that every target supports the values you put in there).

2. yes.

3. you can, for example, read and write per byte. This way you force your file format to be one type of endianess.
You can also do a check if the current system is big or little endian and read/write the data accordingly.


Hope it enlightens you a bit,
greetings.
Neex
Neex
Quote:
Question 1: What about integer size in bytes? For example, what
if on the PC my integer size is 4 bytes, and on the Mac its 2 bytes.
How do I handle that? How do I guarantee byte size over different platforms?

First, you're probably pretty safe assuming that a byte is 8 bits. Most hardware designers have now reached a consensus on that one :)

As for the size of integers, when you care exactly about how many bytes your types are using, don't use char/short/int/long, use types that explicitly tell you their size. On Linux (for example) stdint.h provides types such as int8_t, uint8_t, int16_t, etc... You can easily define your own similar types.

Quote:
Question 3: Is there a technique of saving my data in some sort of
custom file format that makes resolving these issues easier?

I'm going to avoid this question and answer a slightly different one... :)

The main problem is if you dump/load structs directly to/from disk, since you're assuming that the memory layout of structs will be the same on every platform. Of course, size, endianness and packing differences ruin that.

The main thing you need to do is to have something in your program checking and converting data read from the file to the equivalent data in memory. If you use a binary file format, you might specify that all integers be stored on disk in 32-bit big-endian format. Your loading code can act knowing that it is reading a 32-bit big-endian integer, and then convert appropriately for the platform on which the program is running (e.g. convert to little-endian).

Another way would be to use a text-based format. All integers then have to be parsed (probably by the std lib) and so converting to the correct format for your platform is than effectively automatic. (Of course you would still have to worry about wrapping on platforms with 16-bit integers for example).

Hope that rambling helped somewhat.
metrix
metrix
T_Janssen & Neex:
Thanks for the help, I have a good idea now of how I want to do this.

Anonymous:
Thanks for the suggestion, I looked into using XML, however for my
purposes I think the text based solution will be best.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.