Utils.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright (C) 2011, 2012 Google Inc.
  2. //
  3. // This file is part of YouCompleteMe.
  4. //
  5. // YouCompleteMe is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // YouCompleteMe is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  17. #include "Utils.h"
  18. #include <cmath>
  19. #include <limits>
  20. #include <boost/filesystem.hpp>
  21. #include <boost/filesystem/fstream.hpp>
  22. namespace fs = boost::filesystem;
  23. namespace YouCompleteMe {
  24. bool AlmostEqual( double a, double b ) {
  25. return std::abs( a - b ) <=
  26. ( std::numeric_limits< double >::epsilon() *
  27. std::max( std::abs( a ), std::abs( b ) ) );
  28. }
  29. std::string ReadUtf8File( const fs::path &filepath ) {
  30. fs::ifstream file( filepath, std::ios::in | std::ios::binary );
  31. std::vector< char > contents( ( std::istreambuf_iterator< char >( file ) ),
  32. std::istreambuf_iterator< char >() );
  33. if ( contents.size() == 0 )
  34. return std::string();
  35. return std::string( contents.begin(), contents.end() );
  36. }
  37. void WriteUtf8File( const fs::path &filepath, const std::string &contents ) {
  38. fs::ofstream file;
  39. file.open( filepath );
  40. file << contents;
  41. file.close();
  42. }
  43. } // namespace YouCompleteMe