本文整理汇总了C++中FilePath::open_r方法的典型用法代码示例。如果您正苦于以下问题:C++ FilePath::open_r方法的具体用法?C++ FilePath::open_r怎么用?C++ FilePath::open_r使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FilePath
的用法示例。
在下文中一共展示了FilePath::open_r方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: readStringFromFile
Error readStringFromFile(const FilePath& filePath,
std::string* pStr,
string_utils::LineEnding lineEnding)
{
using namespace boost::system::errc ;
// open file
boost::shared_ptr<std::istream> pIfs;
Error error = filePath.open_r(&pIfs);
if (error)
return error;
try
{
// set exception mask (required for proper reporting of errors)
pIfs->exceptions(std::istream::failbit | std::istream::badbit);
// copy file to string stream
std::ostringstream ostr;
boost::iostreams::copy(*pIfs, ostr);
*pStr = ostr.str();
string_utils::convertLineEndings(pStr, lineEnding);
// return success
return Success();
}
catch(const std::exception& e)
{
Error error = systemError(boost::system::errc::io_error,
ERROR_LOCATION);
error.addProperty("what", e.what());
error.addProperty("path", filePath.absolutePath());
return error;
}
}
示例2: detectLineEndings
bool detectLineEndings(const FilePath& filePath, LineEnding* pType)
{
if (!filePath.exists())
return false;
boost::shared_ptr<std::istream> pIfs;
Error error = filePath.open_r(&pIfs);
if (error)
{
LOG_ERROR(error);
return false;
}
// read file character-by-character using a streambuf
try
{
std::istream::sentry se(*pIfs, true);
std::streambuf* sb = pIfs->rdbuf();
while(true)
{
int ch = sb->sbumpc();
if (ch == '\n')
{
// using posix line endings
*pType = string_utils::LineEndingPosix;
return true;
}
else if (ch == '\r' && sb->sgetc() == '\n')
{
// using windows line endings
*pType = string_utils::LineEndingWindows;
return true;
}
else if (ch == EOF)
{
break;
}
else if (pIfs->fail())
{
LOG_WARNING_MESSAGE("I/O Error reading file " +
filePath.absolutePath());
break;
}
}
}
CATCH_UNEXPECTED_EXCEPTION
// no detection possible (perhaps the file is empty or has only one line)
return false;
}
示例3: setBody
Error setBody(const FilePath& filePath,
const Filter& filter,
std::streamsize buffSize = 128)
{
// open the file
boost::shared_ptr<std::istream> pIfs;
Error error = filePath.open_r(&pIfs);
if (error)
return error;
// send the file from its stream
try
{
return setBody(*pIfs, filter, buffSize);
}
catch(const std::exception& e)
{
Error error = systemError(boost::system::errc::io_error,
ERROR_LOCATION);
error.addProperty("what", e.what());
error.addProperty("path", filePath.absolutePath());
return error;
}
}