【发布时间】:2014-05-02 16:49:33
【问题描述】:
我正在将一些代码移植到 Darwin OS X,作为更改的一部分,我们从 gcc 转到 clang 编译器。
在代码中,有一个功能可以追溯到 2005 年,并在互联网上的几个地方发布。它为几个不同的旧版本 GCC 提供了功能,除了它提供的最后一个版本,v3.4.0 或更高版本,我已经编辑了所有内容。该代码依赖于两个 GCC 特定类:__gnu_cxx::stdio_filebuf 和 __gnu_cxx::stdio_sync_filebuf。
//! Similar to fileno(3), but taking a C++ stream as argument instead of a
//! FILE*. Note that there is no way for the library to track what you do with
//! the descriptor, so be careful.
//! \return The integer file descriptor associated with the stream, or -1 if
//! that stream is invalid. In the latter case, for the sake of keeping the
//! code as similar to fileno(3), errno is set to EBADF.
//! \see The <A HREF="http://www.ginac.de/~kreckel/fileno/">upstream page at
//! http://www.ginac.de/~kreckel/fileno/</A> of this code provides more
//! detailed information.
template <typename charT, typename traits>
inline int
fileno_hack(const std::basic_ios<charT, traits>& stream)
{
// Some C++ runtime libraries shipped with ancient GCC, Sun Pro,
// Sun WS/Forte 5/6, Compaq C++ supported non-standard file descriptor
// access basic_filebuf<>::fd(). Alas, starting from GCC 3.1, the GNU C++
// runtime removes all non-standard std::filebuf methods and provides an
// extension template class __gnu_cxx::stdio_filebuf on all systems where
// that appears to make sense (i.e. at least all Unix systems). Starting
// from GCC 3.4, there is an __gnu_cxx::stdio_sync_filebuf, in addition.
// Sorry, darling, I must get brutal to fetch the darn file descriptor!
// Please complain to your compiler/libstdc++ vendor...
#if defined(__GLIBCXX__) || defined(__GLIBCPP__)
// OK, stop reading here, because it's getting obscene. Cross fingers!
# if defined(__GLIBCXX__) // >= GCC 3.4.0
// This applies to cin, cout and cerr when not synced with stdio:
typedef __gnu_cxx::stdio_filebuf<charT, traits> unix_filebuf_t;
unix_filebuf_t* fbuf = dynamic_cast<unix_filebuf_t*>(stream.rdbuf());
if (fbuf != NULL) {
return fbuf->fd();
}
// This applies to filestreams:
typedef std::basic_filebuf<charT, traits> filebuf_t;
filebuf_t* bbuf = dynamic_cast<filebuf_t*>(stream.rdbuf());
if (bbuf != NULL) {
// This subclass is only there for accessing the FILE*. Ouuwww, sucks!
struct my_filebuf : public std::basic_filebuf<charT, traits> {
int fd() { return this->_M_file.fd(); }
};
return static_cast<my_filebuf*>(bbuf)->fd();
}
// This applies to cin, cout and cerr when synced with stdio:
typedef __gnu_cxx::stdio_sync_filebuf<charT, traits> sync_filebuf_t;
sync_filebuf_t* sbuf = dynamic_cast<sync_filebuf_t*>(stream.rdbuf());
if (sbuf != NULL) {
return fileno(sbuf->file());
}
# endif
#else
# error "Does anybody know how to fetch the bloody file descriptor?"
return stream.rdbuf()->fd(); // Maybe a good start?
#endif
errno = EBADF;
return -1;
}
问题是,对于 OS X Mavericks 上的 clang 5.1,计算 std::basic_ios 的文件描述符的方法是什么?
【问题讨论】:
-
我可以告诉你,正在使用的 C++ 运行时很可能是 LLVM 项目的 libc++,并且它的
basic_filebuf类似乎包装了一个 stdioFILE,而不是直接使用 OS级文件描述符。但是我看不到任何方法可以从filebuf中提取FILE或从已经打开的FILE中创建filebuf。也许比我聪明的人知道一个。 -
你解决了吗?你做了什么?
-
@Paulo1205 问题从未解决,我的调用代码只处理
return -1和errno == EBADF的情况。
标签: c++ macos iostream porting file-descriptor