mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-19 13:02:13 +01:00
Add C/C++ FILE* buffer adaptor.
* add function msgpack_fbuffer_write(); * add class msgpack::fbuffer; * add tests buffer.fbuffer and buffer.fbuffer_c .
This commit is contained in:
parent
505660e1fa
commit
2c1a1fd4f8
47
src/msgpack/fbuffer.h
Normal file
47
src/msgpack/fbuffer.h
Normal 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
56
src/msgpack/fbuffer.hpp
Normal 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:
|
||||
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 */
|
||||
|
@ -1,4 +1,6 @@
|
||||
#include <msgpack.hpp>
|
||||
#include <msgpack/fbuffer.hpp>
|
||||
#include <msgpack/fbuffer.h>
|
||||
#include <msgpack/zbuffer.hpp>
|
||||
#include <gtest/gtest.h>
|
||||
#include <string.h>
|
||||
@ -70,3 +72,47 @@ 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<char>(ch));
|
||||
}
|
||||
EXPECT_EQ(EOF, fgetc(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));
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user