如何设计自定义二进制文件格式?
每个应用程序都有自己的自定义二进制文件格式(例如.mpq、.wad)。最重要的是,它通常是拉链的。
所以,我的问题是,如何巧妙地布局文件的二进制内容。一开始有类似“目录”的结构吗?将所有内容转储到一个文件中是否更好?
假设您有一个 Shapes
数组,并且每个 Shape
中都有变形的顶点数据(因此顶点数据已从最初加载的文件中更改,因此应该重新得救)。
class Shape
{
vector<Vertex> verts ;
} ;
class Sphere : public Shape { } ; // ...more geometric shapes (Tet, Cube) are defined..
class Model : public Shape { } ; // general model "Shape" loaded from file
vector<Shape*> shapes ; // save me! contents are mix of Model, Sphere, Tet..
// each with variable number of verts
Every application has its own custom binary file format (e.g. .mpq, .wad). On top of that, its commonly zipped.
So, my question is, how do you artfully/skillfully layout the binary contents of your file. Do you have a "table of contents" like structure at the beginning? Is it better to dump everything in one file?
So say you have an array of Shapes
, and in each Shape
is deformed vertex data (so the vertex data has changed from the file it was originally loaded from, so it should be saved anew).
class Shape
{
vector<Vertex> verts ;
} ;
class Sphere : public Shape { } ; // ...more geometric shapes (Tet, Cube) are defined..
class Model : public Shape { } ; // general model "Shape" loaded from file
vector<Shape*> shapes ; // save me! contents are mix of Model, Sphere, Tet..
// each with variable number of verts
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最喜欢的关于文件格式主题的文章位于 http://www.fadden.com/ techmisc/file-formats.htm。
除此之外,它可能取决于您存储的数据类型以及该数据将如何使用(主要是通过网络传输吗?查找访问有多重要?等等......)。
从那篇文章开始;如果您已经有了需要设计的格式,它可能会帮助您明确想法。
My favorite article on the topic of file formats is at http://www.fadden.com/techmisc/file-formats.htm.
Beyond that, it probably comes down to what kind of data you are storing, and how that data will be used (will it be transmitted across a network, primarily? How important is seek access? Etc...).
Start with that article; it may help crystallize your thoughts if you already have a format that needs designing.
简而言之 - 如果您只需要序列化,这意味着您将从流中读取和写入流,那么您可以在这里使用简单的方法并按成员发出您的 scructs 成员,或者使用任何序列化库,从 < code>CArchive to ....无论你喜欢什么。
如果没有,并且您将需要直接访问文件内的数据,那么...您将使用您的要求,他们将通过一定的技能告诉您所拥有的文件的布局。
是的,这里有一个广泛的话题。例如,
我的软件需要一个缩略图数据库。每个缩略图都有一个时间戳,我知道它们的大小会有所不同。要求是:
,这里的要求很简单,但它们代表了自己。
我创建了两个文件,一个带有索引,另一个带有图片。
存储:将图像附加到数据文件中,将图像在数据文件中的索引附加到索引文件中。
读取:使用简单索引查找文件中的索引(索引为
(timestamp-timestamp_start)/15
)。使用该索引来获取图像数据。In short - if your only need serialization, which means that you'll read and write from and to a stream, than you can use no-brainer here and emit your scructs member by member, or use any serialization library there is, from
CArchive to
.... whatever you see fancy.If not, and you will have a need to directly access your data inside the file, then... you'll use your requirements and they will, with some skill, tell you what will be layout of the file you are having.
And yeah, to broad topic to dwell here. For example,
I have a need for a database of thumbnails for my software. Each thumbnail has a timestamp, and I know that they will be of a different size. Requirements are:
Yes, requirements ARE simple here, but they stand for themselves.
I created two files, one with indexes and other with pictures.
Storing: append data file with image, append index file with index of the image in the data file.
Reading: find the index in the file using simple indexing ( index is
(timestamp-timestamp_start)/15
). Use that index to fetch image data.