C stdlib/stdio 的隐藏函数

发布于 2024-11-15 20:30:22 字数 808 浏览 3 评论 0原文

我正在编写一个游戏,现在我能够通过 sqlite 使用类及其方法实现文件系统。为了让生活更轻松,我计划编写一些函数,例如 fopenfclosefreadrename 等. 能够隐藏基本函数并将我的调用定向到我的文件系统而不是原始文件系统。对于前三个函数,这些原型对我来说一切正常:

文件 *fopen(String _Filename, String _Mode); // 我有自己优化的文件结构

void fclose(File *_File);

size_t fread(String *_DstBuf, size_t _ElementSize, size_t _Count, 文件 *_File);

这工作得很好,因为我返回另一个结构或参数,除了 File* 而不是 FILE* ,但是重命名函数似乎有点更棘手!

int rename(String _OldFilename, String _NewFilename);

这几乎是相同的原型。除了我使用 std::string (typedef'ed String)而不是 const char*!知道如何说服我的编译器使用我的函数或忽略 stdio-one 吗?

I am writing a game and for now i was able to implement a filesystem via sqlite with a class and its methods. To make life more easy i have planned to write some functions like fopen,fclose,fread,rename, etc. to be able to shadow the base functions and to direct my calls to my filesystem rather than to the original one. For the first three function everything worked fine for me with these prototypes:

File *fopen(String _Filename, String _Mode); // i have my own optimized File struct

void fclose(File *_File);

size_t fread(String *_DstBuf, size_t _ElementSize, size_t _Count, File *_File);

This worked fine as i am either returning another struct or the parameters except a File* and not a FILE*, however the rename function seems to be a bit trickier!

int rename(String _OldFilename, String _NewFilename);

This is nearly the same prototype. except that i use std::string (typedef'ed String) than const char*! Any idea how i could convince my compiler either to use my function or to ignore the stdio-one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

动次打次papapa 2024-11-22 20:30:22

为什么你不能简单地以任何其他名称使用你自己的函数?

如果整个冲突与重载解析有关,您应该简单地隐藏实际原型;您可以将它们转发到您自己的函数。

但是,我建议不要在这里使用一般方法:即使“修复”到位,您最多也将包含排序问题,甚至可能包含重复的链接符号。

如果您的函数不执行相同的操作,请让它们使用其他名称。由于您使用的是 C++,因此您可以在 MyFsFunctions.h 中执行此卑鄙伎俩(否则是不明智的):

namespace MyFsFunctions 
{
     // prototypes for fopen, fclose, fwrite, fread etc
}

using namespace MyFsFunctions;
// or:
using MyFsFunctions::fopen;
using MyFsFunctions::fclose;
using MyFsFunctions::fread;
using MyFsFunctions::fwrite; // etc...

我很确定您仍然希望(需要)隐藏确切的函数原型(或者编译器可能仍然会抱怨不明确)标识符参考)。

其他提示:

  1. 使用保险丝文件系统驱动程序(在 Linux/UNIX/MacOS 上;可能有点过分,但实现它似乎更强大,甚至可能比您在这里所做的更简单)。
  2. 总是有 C 宏(邪恶的 -10 分)
  3. gnu 链接器有一些选项可以让您“替换”链接符号 - 主要用于调试目的,但您可以在此处利用这些选项

And what is the reason that you cannot simply use your own functions by any other name?

If the whole conflict is with overload resolution, you should simply just shadow the actual prototypes; You can make them forwards to your own functions.

However, I recommend against the general approach here: even with that 'fix' in place you will at the very best have include ordering issues, and possibly even duplicate link symbols.

If your functions don't do the same, make them use another name. Since you are using c++, you could do this vile trick (otherwise ill-advised) in MyFsFunctions.h:

namespace MyFsFunctions 
{
     // prototypes for fopen, fclose, fwrite, fread etc
}

using namespace MyFsFunctions;
// or:
using MyFsFunctions::fopen;
using MyFsFunctions::fclose;
using MyFsFunctions::fread;
using MyFsFunctions::fwrite; // etc...

I'm pretty sure you will still want (need) to shadow the exact function prototypes (or the compiler may still complain about ambiguous identifiers references).

Other hints:

  1. use a fuse file system driver (on Linux/UNIX/MacOS; might be overkill, but implementing it seems a lot more robust and may even simpler than what you do here).
  2. there is always C macros (-10 points for evil)
  3. gnu linker has options that let's you 'replace' link symbols - mainly for debugging purposes, but you can leverage those here
盛夏已如深秋| 2024-11-22 20:30:22

如何使用标准签名实现重命名,它所做的只是调用您的String版本?

对我来说听起来并不复杂。像这样的东西:

int rename(const char *charOld, const char *charNew)
{
    std::string stdOld(charOld);
    std::string stdNew(charNew);
    return rename(stdOld, stdNew);
}

How about implementing a rename with the standard signature that all it will do would be calling your Stringed version?

Doesn't sound complicated to me. Something like this:

int rename(const char *charOld, const char *charNew)
{
    std::string stdOld(charOld);
    std::string stdNew(charNew);
    return rename(stdOld, stdNew);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文