1
0

util.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright 2016 Google Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "flatbuffers/util.h"
  17. namespace flatbuffers {
  18. bool FileExistsRaw(const char *name) {
  19. std::ifstream ifs(name);
  20. return ifs.good();
  21. }
  22. bool LoadFileRaw(const char *name, bool binary, std::string *buf) {
  23. if (DirExists(name)) return false;
  24. std::ifstream ifs(name, binary ? std::ifstream::binary : std::ifstream::in);
  25. if (!ifs.is_open()) return false;
  26. if (binary) {
  27. // The fastest way to read a file into a string.
  28. ifs.seekg(0, std::ios::end);
  29. auto size = ifs.tellg();
  30. (*buf).resize(static_cast<size_t>(size));
  31. ifs.seekg(0, std::ios::beg);
  32. ifs.read(&(*buf)[0], (*buf).size());
  33. } else {
  34. // This is slower, but works correctly on all platforms for text files.
  35. std::ostringstream oss;
  36. oss << ifs.rdbuf();
  37. *buf = oss.str();
  38. }
  39. return !ifs.bad();
  40. }
  41. static LoadFileFunction g_load_file_function = LoadFileRaw;
  42. static FileExistsFunction g_file_exists_function = FileExistsRaw;
  43. bool LoadFile(const char *name, bool binary, std::string *buf) {
  44. assert(g_load_file_function);
  45. return g_load_file_function(name, binary, buf);
  46. }
  47. bool FileExists(const char *name) {
  48. assert(g_file_exists_function);
  49. return g_file_exists_function(name);
  50. }
  51. bool DirExists(const char *name) {
  52. #ifdef _WIN32
  53. #define flatbuffers_stat _stat
  54. #define FLATBUFFERS_S_IFDIR _S_IFDIR
  55. #else
  56. #define flatbuffers_stat stat
  57. #define FLATBUFFERS_S_IFDIR S_IFDIR
  58. #endif
  59. struct flatbuffers_stat file_info;
  60. if (flatbuffers_stat(name, &file_info) != 0) return false;
  61. return (file_info.st_mode & FLATBUFFERS_S_IFDIR) != 0;
  62. }
  63. LoadFileFunction SetLoadFileFunction(LoadFileFunction load_file_function) {
  64. LoadFileFunction previous_function = g_load_file_function;
  65. g_load_file_function = load_file_function ? load_file_function : LoadFileRaw;
  66. return previous_function;
  67. }
  68. FileExistsFunction SetFileExistsFunction(
  69. FileExistsFunction file_exists_function) {
  70. FileExistsFunction previous_function = g_file_exists_function;
  71. g_file_exists_function = file_exists_function ?
  72. file_exists_function : FileExistsRaw;
  73. return previous_function;
  74. }
  75. } // namespace flatbuffers