io_utils.cc 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright (C) 2015-2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <testutils/io_utils.h>
  7. #include <gtest/gtest.h>
  8. #include <fstream>
  9. #include <sstream>
  10. #include <string>
  11. namespace isc {
  12. namespace dhcp {
  13. namespace test {
  14. bool fileExists(const std::string& file_path) {
  15. struct stat statbuf;
  16. return(stat(file_path.c_str(), &statbuf) == 0);
  17. }
  18. std::string readFile(const std::string& file_path) {
  19. std::ifstream ifs;
  20. ifs.open(file_path.c_str(), std::ifstream::in);
  21. if (!ifs.good()) {
  22. return (std::string());
  23. }
  24. std::string buf;
  25. std::ostringstream output;
  26. while (!ifs.eof() && ifs.good()) {
  27. ifs >> buf;
  28. output << buf;
  29. }
  30. ifs.close();
  31. return (output.str());
  32. }
  33. }; // end of isc::dhcp::test namespace
  34. }; // end of isc::dhcp namespace
  35. }; // end of isc namespace