简体   繁体   English

如何根据要处理的文件更改void *的大小

[英]c++ How can I change the size of a void* according to a file I want to process

I am currently trying to make a program that can read a .blend file. 我目前正在尝试制作一个可以读取.blend文件的程序。 Well trying is the important part, since I am already stuck on reading the file block info. 好尝试是重要的部分,因为我已经被困在读取文件块信息中了。

Im gonna quickly explain my problem, please refer this page for context 我将快速解释我的问题,请参阅此页面获取上下文

So in the .blend header there is a char that determines wheter or not the pointer size, later used in the file info block (Or just fileBlock on the linked webpage) among other things, is 4 or 8 bytes long. 因此,.blend标头中有一个字符,该字符确定指针的大小,而不是指针大小,此后在文件信息块(或仅在链接网页上的fileBlock)中使用的大小为4或8个字节。 From what I have read, in c++ the void pointer only changes size according to the target platform it was compiled for ( 8 bytes for 64 bit and 4 bytes for 32 bits ). 据我所读,在c ++中,void指针仅根据编译时针对的目标平台更改大小(64位为8字节,而32位为4字节)。 However .blend files can have either one, regardless of the platform I presume. 但是,无论我使用哪种平台,.blend文件都可以具有一个文件。

Now since blender itself does also read its own files using c, there must be a way to change the pointer to match the required pointer size, according to the info in the header. 现在,由于Blender本身也确实使用c读取了自己的文件,因此必须根据标题中的信息,更改指针以匹配所需的指针大小。 However my best guess would be to dynamically allocate a void pointer array to either one or two pointers, which then makes actually using the data even more complicated. 但是,我最好的猜测是将一个空指针数组动态分配给一个或两个指针,这实际上使使用数据变得更加复杂。

Please help me find the intended way of handling the different pointer sizes! 请帮助我找到处理不同指针大小的预期方式!

Yup, that's painful. 是的,那很痛苦。 The solution is not to treat them as C++ at all. 解决方案是根本不将其视为C ++。 Instead, create your own class BlendPointer to abstract this away. 相反,创建您自己的class BlendPointer可以将其抽象化。 Those would be read from a BlendFile , and that BlendFile would store whether its BlendPointer s are 4 or 8 bytes on disk. 这些将从BlendFile读取,并且BlendFile将存储其BlendPointer是磁盘上的4字节还是8字节。

Go back to the top of the wiki page and you will find the File Header structure. 返回Wiki页面的顶部,您将找到File Header结构。 The header of a blend file starts with "BLENDER" which is followed by the pointer size for the file - 混合文件的标题以“ BLENDER”开头,后跟文件的指针大小-

Size of a pointer 指针大小
All pointers in the file are stored in this format 文件中的所有指针都以这种格式存储
'_' (underscore) means 4 bytes or 32 bit '_'(下划线)表示4字节或32位
'-' (minus) means 8 bytes or 64 bits. “-”(减号)表示8字节或64位。

So by reading the eighth byte of the file you know the size of the pointers in the file. 因此,通过读取文件的第八个字节,您可以知道文件中指针的大小。

if (file_bytes[7] == "_")
    ptr_size = 4;
else if (file_bytes[7] == "-")
    ptr_size = 8;

The copy of blender creating the file determines the sizes used for the file, so a 32bit build will save 32bit pointers in the file while a 64 bit build will save 64bit pointers. 创建文件的Blender副本确定了文件使用的大小,因此32位版本将在文件中保存32位指针,而64位版本将在64位指针中保存。

You should also read the next byte, it tells you whether the file was saved as big or little endian, to see if you need to do any byte swapping. 您还应该阅读下一个字节,它告诉您文件是保存为大字节还是小字节,以查看是否需要进行任何字节交换。 The use of blender on big endian machines might be getting smaller, but you may still come across big endian files. 在大字节序的计算机上使用Blender可能会越来越少,但是您仍然可能会遇到大字节序的文件。

Another important thing that doesn't seem to be mentioned, is that blend files can be compressed and often are. 似乎没有提及的另一件重要事情是,混合文件可以压缩并且经常可以压缩。 Reading a compressed blend file will mean using gzread() to read the file. 读取压缩的混合文件将意味着使用gzread()读取文件。 A compressed file has the first two bytes set to 0x1f 0x8b 压缩文件的前两个字节设置为0x1f 0x8b

You will find the code that blender uses to read blend files in source/blender/blenloader . 您将在source/blender/blenloader找到Blender用于读取混合文件的source/blender/blenloader

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM