file_contents_as_string.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "file_contents_as_string.h"
  9. #include <fstream>
  10. #include <cstdio>
  11. #include <cassert>
  12. IGL_INLINE bool igl::file_contents_as_string(
  13. const std::string file_name,
  14. std::string & content)
  15. {
  16. std::ifstream ifs(file_name.c_str());
  17. // Check that opening the stream worked successfully
  18. if(!ifs.good())
  19. {
  20. fprintf(
  21. stderr,
  22. "IOError: file_contents_as_string() cannot open %s\n",
  23. file_name.c_str());
  24. return false;
  25. }
  26. // Stream file contents into string
  27. content = std::string(
  28. (std::istreambuf_iterator<char>(ifs)),
  29. (std::istreambuf_iterator<char>()));
  30. return true;
  31. }
  32. IGL_INLINE std::string igl::file_contents_as_string(
  33. const std::string file_name)
  34. {
  35. std::string content;
  36. #ifndef NDEBUG
  37. bool ret =
  38. #endif
  39. file_contents_as_string(file_name,content);
  40. assert(ret && "file_contents_as_string failed to read string from file");
  41. return content;
  42. }