From: Wouter van Oortmerssen Date: Thu, 31 Mar 2016 00:34:52 +0000 (-0700) Subject: add setloadfile and setfileexists functions to flatbuffers X-Git-Tag: v1.4.0~85 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e98b1912b356f1e8a19245979eeb979628c62c87;p=platform%2Fupstream%2Fflatbuffers.git add setloadfile and setfileexists functions to flatbuffers to allow for custom file loaders. The targeted use case is android runtime. (from CL 116980408) Change-Id: I8785c0acf714fab41d8f6fc9f1c52875423b8f5b --- diff --git a/CMakeLists.txt b/CMakeLists.txt index d2edf82..70a7dd5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,7 @@ set(FlatBuffers_Library_SRCS src/idl_parser.cpp src/idl_gen_text.cpp src/reflection.cpp + src/util.cpp ) set(FlatBuffers_Compiler_SRCS @@ -68,6 +69,7 @@ set(FlatBuffers_Sample_Text_SRCS include/flatbuffers/util.h src/idl_parser.cpp src/idl_gen_text.cpp + src/util.cpp samples/sample_text.cpp # file generated by running compiler on samples/monster.fbs ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h diff --git a/android/jni/Android.mk b/android/jni/Android.mk index 905fbe9..f5f83f1 100755 --- a/android/jni/Android.mk +++ b/android/jni/Android.mk @@ -33,7 +33,8 @@ include $(CLEAR_VARS) LOCAL_MODULE := flatbuffers_extra LOCAL_SRC_FILES := src/idl_parser.cpp \ src/idl_gen_text.cpp \ - src/reflection.cpp + src/reflection.cpp \ + src/util.cpp LOCAL_STATIC_LIBRARIES := flatbuffers include $(BUILD_STATIC_LIBRARY) diff --git a/include/flatbuffers/util.h b/include/flatbuffers/util.h index 3003796..cfb211f 100644 --- a/include/flatbuffers/util.h +++ b/include/flatbuffers/util.h @@ -111,33 +111,24 @@ inline int64_t StringToUInt(const char *str, int base = 10) { #endif } +typedef bool (*LoadFileFunction)(const char *filename, bool binary, + std::string *dest); +typedef bool (*FileExistsFunction)(const char *filename); + +LoadFileFunction SetLoadFileFunction(LoadFileFunction load_file_function); + +FileExistsFunction SetFileExistsFunction(FileExistsFunction + file_exists_function); + + // Check if file "name" exists. -inline bool FileExists(const char *name) { - std::ifstream ifs(name); - return ifs.good(); -} +bool FileExists(const char *name); // Load file "name" into "buf" returning true if successful // false otherwise. If "binary" is false data is read // using ifstream's text mode, otherwise data is read with // no transcoding. -inline bool LoadFile(const char *name, bool binary, std::string *buf) { - std::ifstream ifs(name, binary ? std::ifstream::binary : std::ifstream::in); - if (!ifs.is_open()) return false; - if (binary) { - // The fastest way to read a file into a string. - ifs.seekg(0, std::ios::end); - (*buf).resize(static_cast(ifs.tellg())); - ifs.seekg(0, std::ios::beg); - ifs.read(&(*buf)[0], (*buf).size()); - } else { - // This is slower, but works correctly on all platforms for text files. - std::ostringstream oss; - oss << ifs.rdbuf(); - *buf = oss.str(); - } - return !ifs.bad(); -} +bool LoadFile(const char *name, bool binary, std::string *buf); // Save data "buf" of length "len" bytes into a file // "name" returning true if successful, false otherwise. diff --git a/src/util.cpp b/src/util.cpp new file mode 100644 index 0000000..452e9ea --- /dev/null +++ b/src/util.cpp @@ -0,0 +1,71 @@ +/* + * Copyright 2016 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flatbuffers/util.h" + +namespace flatbuffers { + +bool FileExistsRaw(const char *name) { + std::ifstream ifs(name); + return ifs.good(); +} + +bool LoadFileRaw(const char *name, bool binary, std::string *buf) { + std::ifstream ifs(name, binary ? std::ifstream::binary : std::ifstream::in); + if (!ifs.is_open()) return false; + if (binary) { + // The fastest way to read a file into a string. + ifs.seekg(0, std::ios::end); + (*buf).resize(static_cast(ifs.tellg())); + ifs.seekg(0, std::ios::beg); + ifs.read(&(*buf)[0], (*buf).size()); + } else { + // This is slower, but works correctly on all platforms for text files. + std::ostringstream oss; + oss << ifs.rdbuf(); + *buf = oss.str(); + } + return !ifs.bad(); +} + +static LoadFileFunction g_load_file_function = LoadFileRaw; +static FileExistsFunction g_file_exists_function = FileExistsRaw; + +bool LoadFile(const char *name, bool binary, std::string *buf) { + assert(g_load_file_function); + return g_load_file_function(name, binary, buf); +} + +bool FileExists(const char *name) { + assert(g_file_exists_function); + return g_file_exists_function(name); +} + +LoadFileFunction SetLoadFileFunction(LoadFileFunction load_file_function) { + LoadFileFunction previous_function = g_load_file_function; + g_load_file_function = load_file_function ? load_file_function : LoadFileRaw; + return previous_function; +} + +FileExistsFunction SetFileExistsFunction( + FileExistsFunction file_exists_function) { + FileExistsFunction previous_function = g_file_exists_function; + g_file_exists_function = file_exists_function ? + file_exists_function : FileExistsRaw; + return previous_function; +} + +} // namespace flatbuffers