Merge branch 'fbuffer'

This commit is contained in:
Nobuyuki Kubota 2014-07-02 17:53:18 +09:00
commit f50148a9cc
4 changed files with 153 additions and 0 deletions

View File

@ -47,6 +47,7 @@ nobase_include_HEADERS = \
msgpack/version.h \ msgpack/version.h \
msgpack/vrefbuffer.h \ msgpack/vrefbuffer.h \
msgpack/zbuffer.h \ msgpack/zbuffer.h \
msgpack/fbuffer.h \
msgpack/pack.h \ msgpack/pack.h \
msgpack/unpack.h \ msgpack/unpack.h \
msgpack/object.h \ msgpack/object.h \
@ -58,6 +59,7 @@ nobase_include_HEADERS += \
msgpack/sbuffer.hpp \ msgpack/sbuffer.hpp \
msgpack/vrefbuffer.hpp \ msgpack/vrefbuffer.hpp \
msgpack/zbuffer.hpp \ msgpack/zbuffer.hpp \
msgpack/fbuffer.hpp \
msgpack/pack.hpp \ msgpack/pack.hpp \
msgpack/unpack.hpp \ msgpack/unpack.hpp \
msgpack/object.hpp \ msgpack/object.hpp \

47
src/msgpack/fbuffer.h Normal file
View File

@ -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 <stdio.h>
#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 */

56
src/msgpack/fbuffer.hpp Normal file
View File

@ -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 <cstdio>
#include <stdexcept>
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 */

View File

@ -1,4 +1,6 @@
#include <msgpack.hpp> #include <msgpack.hpp>
#include <msgpack/fbuffer.hpp>
#include <msgpack/fbuffer.h>
#include <msgpack/zbuffer.hpp> #include <msgpack/zbuffer.hpp>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <string.h> #include <string.h>
@ -70,3 +72,49 @@ TEST(buffer, zbuffer)
zbuf.flush(); 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<char>(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);
}