Пример #1
0
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
0
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
0
 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;
    }
 }