如何设计自定义二进制文件格式?

发布于 2024-12-07 23:50:57 字数 591 浏览 0 评论 0原文

每个应用程序都有自己的自定义二进制文件格式(例如.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝色星空 2024-12-14 23:50:57

我最喜欢的关于文件格式主题的文章位于 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.

习惯成性 2024-12-14 23:50:57

简而言之 - 如果您只需要序列化,这意味着您将从流中读取和写入流,那么您可以在这里使用简单的方法并按成员发出您的 scructs 成员,或者使用任何序列化库,从 < code>CArchive to ....无论你喜欢什么。

如果没有,并且您将需要直接访问文件内的数据,那么...您将使用您的要求,他们将通过一定的技能告诉您所拥有的文件的布局。

是的,这里有一个广泛的话题。例如,

我的软件需要一个缩略图数据库。每个缩略图都有一个时间戳,我知道它们的大小会有所不同。要求是:

  • 顺序写入(缩略图将附加到数据库末尾)
  • 缩略图将按升序附加
  • 直接读取(给定时间,在 o(1) 中获取缩略图)
  • 稍后不会对数据库进行修改
  • 缩略图将在 15 秒内 是的

,这里的要求很简单,但它们代表了自己。

我创建了两个文件,一个带有索引,另一个带有图片。

存储:将图像附加到数据文件中,将图像在数据文件中的索引附加到索引文件中。
读取:使用简单索引查找文件中的索引(索引为 (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:

  • sequential write (thumbs will be appended to the end of the database)
  • thumbs will be appended in ascending order
  • direct read (given time, get thumbnail in o(1) )
  • no later modification to the database
  • thumbnails will be in 15 seconds interval

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文