通过函数打开流

Opening stream via function

本文关键字:函数      更新时间:2023-10-16

我需要关于[io](f)stream s的不可复制性的帮助。

我需要提供一个关于fstreams的破解包装,以便在Windows上处理文件名中包含unicode字符的文件。为此,我设计了一个包装器函数:

bool open_ifstream( istream &stream, const string &filename )
{
#ifdef __GLIBCXX__
    FILE* result = _wfopen( convert_to_utf16(filename).c_str(), L"r" );
    if( result == 0 )
        return false;
    __gnu_cxx::stdio_filebuf<char>* buffer = new __gnu_cxx::stdio_filebuf<char>( result, std::ios_base::in, 1 );
    istream stream2(buffer);
    std::swap(stream, stream2);
#elif defined(_MSC_VER)
    stream.open( convert_to_utf16(filename) );
#endif
    return !!stream;
}

当然,std::swap线是罪魁祸首。我也尝试过从函数返回流,但它导致了同样的问题。std::istream的复制构造函数是deleted。我也尝试过std::move,但没有帮助。我该如何解决这个问题?

编辑:多亏了@tibur的想法,我终于找到了一个很好的方法来实现Keep It Simple (TM),而且功能强大。从某种意义上说,它仍然很黑客,因为它依赖于使用的Windows标准C++库,但由于只有两个真正的库在使用,这对我来说并不是一个真正的问题。

#include <fstream>
#include <memory>
#if _WIN32
# if __GLIBCXX__
#  include<ext/stdio_filebuf.h>
unique_ptr<istream> open_ifstream( const string &filename )
{
    FILE* c_file = _wfopen( convert_to_utf16(filename).c_str(), L"r" );
    __gnu_cxx::stdio_filebuf<char>* buffer = new __gnu_cxx::stdio_filebuf<char>( c_file, std::ios_base::in, 1 );
    return std::unique_ptr<istream>( new istream(buffer) );
}
# elif _MSC_VER
unique_ptr<ifstream> open_ifstream( const string &filename )
{
    return unique_ptr<ifstream>(new ifstream( convert_to_utf16(filename)) );
}
# else
# error unknown fstream implementation
# endif
#else
unique_ptr<ifstream> open_ifstream( const string &filename )
{
    return unique_ptr<ifstream>(new ifstream(filename) );
}
#endif

在用户代码中:

auto stream_ptr( open_ifstream(filename) );
auto &stream = *stream_ptr;
if( !stream )
    return emit_error( "Unable to open nectar file: " + filename );

这取决于C++0x <memory>auto关键字。当然,您不能只使用close作为生成的stream变量,但GNU Libstdc++std::istream析构函数确实负责关闭文件,因此任何地方都不需要额外的内存管理。

关于:

ifstream * open_ifstream(const string &filename);

您不能直接使用rdbuf成员函数来设置stream的缓冲区吗?

这里有一个不太直观的想法:

#include <iconv.h>
#include <algorithm>
void windowify(std::string & filename)
{
#ifdef WIN32
  assert(filename.length() < 1000);
  wchar_t wbuf[1000];
  char    cbuf[1000];
  char * ip = &cbuf[0];
  char * op = reinterpret_cast<char*>(&wbuf[0]);
  size_t ib = filename.length(), ob = 1000;
  std::fill(cbuf + filename.length(), cbuf + 1000, 0);
  std::copy(filename.begin(), filename.end(), cbuf);
  iconv_t cd = iconv_open("WCHAR_T", "UTF-8");
  iconv(cd, &ip, &ib, &op, &ob);
  iconv_close(cd);
  wchar_t sfnbuf[1000];
  std::fill(cbuf, cbuf + 1000, 0);
  ib = GetShortPathNameW(wbuf, sfnbuf, 1000);
  ob = 1000;
  ip = reinterpret_cast<char*>(&wbuf[0]);
  op = &cbuf[0];
  cd = iconv_open("UTF-8", "WCHAR_T");
  iconv(cd, &ip, &ib, &op, &ob);
  iconv_close(cd);
  filename = std::string(cbuf);
#endif
}

用法:

std::string filename = getFilename();
windowify(filename);
std::ifstream infile(filename.c_str());

我建议一个小的改进:使用_wopen(或_wsopen_s)而不是_wfopen。您将获得一个文件描述符(int),可以将其传递给stdio_filebuf来代替FILE*。通过这种方式,您应该避免泄漏任何资源(正如marcin所指出的)