简体   繁体   English

独立于平台的资源系统(如Qt资源系统)

[英]Platform independent resource system (like the Qt Resource system)

Is there a platform independent resource system for C++ like the one that comes with Qt (but without the Qt dependency)? 是否有一个独立于C ++的平台资源系统,如Qt附带的那个(但没有Qt依赖)?

I would like to access arbitrary data from within my C++ source code. 我想从我的C ++源代码中访问任意数据。 That is, not only icons but also translations or shaders, etc. 也就是说,不仅是图标,还有翻译或着色器等。

Alternatively some sort of virtual file system library to access eg a ZIP compressed file or such would also fit my needs. 或者,某些类型的虚拟文件系统库可以访问例如ZIP压缩文件等,这也符合我的需要。

I rolled my own system for a C++ web server project that basically took a bunch of files (HTML, CSS, JS, PNGs, etc.) and created C++ headers containing the data encoded as static const char* . 我为C ++ Web服务器项目编写了自己的系统,它基本上采用了大量文件(HTML,CSS,JS,PNG等)并创建了包含编码为static const char*的数据的C ++头文件。 I then #include those headers where I need access to the data. 然后我#include那些我需要访问数据的标题。 The app that encodes the 'resource' files executes as a pre-build step. 编码“资源”文件的应用程序作为预构建步骤执行。 The encoding app itself used boost::filesystem to create the resource headers, so works on Windows/*nix. 编码应用程序本身使用boost::filesystem来创建资源头,因此适用于Windows / * nix。

A typical resource file might look like this: 典型的资源文件可能如下所示:

namespace resource
{
  // Generated from mainPage.htm
  static const char* mainPage_[] =
  {
    "<html>...</html>"
  };
}

For binary content I encode using the \\x notation. 对于二进制内容,我使用\\x表示法进行编码。 I also make sure to line-wrap the data so it is readable in an editor. 我还确保对数据进行换行,以便在编辑器中可读。

I did have some issues though - the MS compiler doesn't allow a static const char* to be bigger than 64Kb which was a PITA. 我确实遇到了一些问题 - MS编译器不允许static const char*大于64Kb,这是一个PITA。 Luckily the only files larger than this were JavaScript files that I could easily split into smaller chunks - large images would be a problem though. 幸运的是,唯一比这更大的文件是我可以轻松拆分成较小块的JavaScript文件 - 但是大图像会成为问题。

这个问题xxd答案正是你要找的。

We're using ICU ResourceBundle s for that and are pretty satisfied with it. 我们正在使用ICU ResourceBundle ,并且非常满意。

Using the pkgdata tool , packageing of ResourceBundles is pretty flexible: as a shared library, static library, or as files that can be memory-mapped by ICU. 使用pkgdata工具 ,ResourceBundles的打包非常灵活:作为共享库,静态库或可由ICU进行内存映射的文件。

I have just patched them onto the end of the executable at link time as a binary blob. 我刚刚在链接时将它们作为二进制blob修补到可执行文件的末尾。 With the last 4bytes being the size of the previous block and then have the program read the data items from the tail. 最后4个字节是前一个块的大小,然后让程序从尾部读取数据项。

Another approach if you need a more filesystem type structure (though I havem't tried it) would be to put everything in a zip file and append that to the end. 另一种方法是,如果你需要一个更多的文件系统类型结构(虽然我没有尝试过)将把所有内容放在一个zip文件中并将其追加到最后。 Again you would need some easily findabale size of the added data. 同样,您需要一些易于查找的添加数据大小。

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

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