diff --git a/src/Makefile.am b/src/Makefile.am index 59c65010..37993f26 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -47,6 +47,7 @@ nobase_include_HEADERS = \ msgpack/version.h \ msgpack/vrefbuffer.h \ msgpack/zbuffer.h \ + msgpack/fbuffer.h \ msgpack/pack.h \ msgpack/unpack.h \ msgpack/object.h \ @@ -58,6 +59,7 @@ nobase_include_HEADERS += \ msgpack/sbuffer.hpp \ msgpack/vrefbuffer.hpp \ msgpack/zbuffer.hpp \ + msgpack/fbuffer.hpp \ msgpack/pack.hpp \ msgpack/unpack.hpp \ msgpack/object.hpp \ diff --git a/src/msgpack/fbuffer.h b/src/msgpack/fbuffer.h new file mode 100644 index 00000000..352f7269 --- /dev/null +++ b/src/msgpack/fbuffer.h @@ -0,0 +1,47 @@ +/* + * MessagePack for C FILE* buffer adaptor + * + * Copyright (C) 2013 Vladimir Volodko + * + * 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. + */ +#ifndef MSGPACK_FBUFFER_H__ +#define MSGPACK_FBUFFER_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +/** + * @defgroup msgpack_fbuffer FILE* buffer + * @ingroup msgpack_buffer + * @{ + */ + +static inline int msgpack_fbuffer_write(void* data, const char* buf, unsigned int len) +{ + return (1 == fwrite(buf, len, 1, (FILE *)data)) ? 0 : -1; +} + +/** @} */ + + +#ifdef __cplusplus +} +#endif + +#endif /* msgpack/fbuffer.h */ + diff --git a/src/msgpack/fbuffer.hpp b/src/msgpack/fbuffer.hpp new file mode 100644 index 00000000..548ef0bf --- /dev/null +++ b/src/msgpack/fbuffer.hpp @@ -0,0 +1,56 @@ +// +// MessagePack for C++ FILE* buffer adaptor +// +// Copyright (C) 2013 Vladimir Volodko +// +// 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. +// +#ifndef MSGPACK_FBUFFER_HPP__ +#define MSGPACK_FBUFFER_HPP__ + +#include +#include + +namespace msgpack { + + +class fbuffer { +public: + explicit fbuffer(FILE* file) : m_file(file) { } + +public: + void write(const char* buf, unsigned int len) + { + if (1 != fwrite(buf, len, 1, m_file)) { + throw std::runtime_error("fwrite() failed"); + } + } + + FILE* file() const + { + return m_file; + } + +private: + fbuffer(const fbuffer&); + fbuffer& operator= (const fbuffer&); + +private: + FILE* m_file; +}; + + +} // namespace msgpack + +#endif /* msgpack/fbuffer.hpp */ + diff --git a/test/buffer.cc b/test/buffer.cc index 0b66f4e2..35d731a5 100644 --- a/test/buffer.cc +++ b/test/buffer.cc @@ -1,4 +1,6 @@ #include +#include +#include #include #include #include @@ -70,3 +72,49 @@ TEST(buffer, zbuffer) zbuf.flush(); } + +TEST(buffer, fbuffer) +{ + FILE* file = tmpfile(); + EXPECT_TRUE( file != NULL ); + + msgpack::fbuffer fbuf(file); + EXPECT_EQ(file, fbuf.file()); + + fbuf.write("a", 1); + fbuf.write("a", 1); + fbuf.write("a", 1); + + fflush(file); + rewind(file); + for (size_t i=0; i < 3; ++i) { + int ch = fgetc(file); + EXPECT_TRUE(ch != EOF); + EXPECT_EQ('a', static_cast(ch)); + } + EXPECT_EQ(EOF, fgetc(file)); + fclose(file); +} + + +TEST(buffer, fbuffer_c) +{ + FILE* file = tmpfile(); + void* fbuf = (void*)file; + + EXPECT_TRUE( file != NULL ); + EXPECT_EQ(0, msgpack_fbuffer_write(fbuf, "a", 1)); + EXPECT_EQ(0, msgpack_fbuffer_write(fbuf, "a", 1)); + EXPECT_EQ(0, msgpack_fbuffer_write(fbuf, "a", 1)); + + fflush(file); + rewind(file); + for (size_t i=0; i < 3; ++i) { + int ch = fgetc(file); + EXPECT_TRUE(ch != EOF); + EXPECT_EQ('a', (char) ch); + } + EXPECT_EQ(EOF, fgetc(file)); + fclose(file); +} +