Skip to content

Commit f50148a

Browse files
committed
Merge branch 'fbuffer'
2 parents 9fc8ec5 + 9d8e6b9 commit f50148a

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ nobase_include_HEADERS = \
4747
msgpack/version.h \
4848
msgpack/vrefbuffer.h \
4949
msgpack/zbuffer.h \
50+
msgpack/fbuffer.h \
5051
msgpack/pack.h \
5152
msgpack/unpack.h \
5253
msgpack/object.h \
@@ -58,6 +59,7 @@ nobase_include_HEADERS += \
5859
msgpack/sbuffer.hpp \
5960
msgpack/vrefbuffer.hpp \
6061
msgpack/zbuffer.hpp \
62+
msgpack/fbuffer.hpp \
6163
msgpack/pack.hpp \
6264
msgpack/unpack.hpp \
6365
msgpack/object.hpp \

src/msgpack/fbuffer.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* MessagePack for C FILE* buffer adaptor
3+
*
4+
* Copyright (C) 2013 Vladimir Volodko
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
#ifndef MSGPACK_FBUFFER_H__
19+
#define MSGPACK_FBUFFER_H__
20+
21+
#include <stdio.h>
22+
23+
#ifdef __cplusplus
24+
extern "C" {
25+
#endif
26+
27+
28+
/**
29+
* @defgroup msgpack_fbuffer FILE* buffer
30+
* @ingroup msgpack_buffer
31+
* @{
32+
*/
33+
34+
static inline int msgpack_fbuffer_write(void* data, const char* buf, unsigned int len)
35+
{
36+
return (1 == fwrite(buf, len, 1, (FILE *)data)) ? 0 : -1;
37+
}
38+
39+
/** @} */
40+
41+
42+
#ifdef __cplusplus
43+
}
44+
#endif
45+
46+
#endif /* msgpack/fbuffer.h */
47+

src/msgpack/fbuffer.hpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// MessagePack for C++ FILE* buffer adaptor
3+
//
4+
// Copyright (C) 2013 Vladimir Volodko
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
#ifndef MSGPACK_FBUFFER_HPP__
19+
#define MSGPACK_FBUFFER_HPP__
20+
21+
#include <cstdio>
22+
#include <stdexcept>
23+
24+
namespace msgpack {
25+
26+
27+
class fbuffer {
28+
public:
29+
explicit fbuffer(FILE* file) : m_file(file) { }
30+
31+
public:
32+
void write(const char* buf, unsigned int len)
33+
{
34+
if (1 != fwrite(buf, len, 1, m_file)) {
35+
throw std::runtime_error("fwrite() failed");
36+
}
37+
}
38+
39+
FILE* file() const
40+
{
41+
return m_file;
42+
}
43+
44+
private:
45+
fbuffer(const fbuffer&);
46+
fbuffer& operator= (const fbuffer&);
47+
48+
private:
49+
FILE* m_file;
50+
};
51+
52+
53+
} // namespace msgpack
54+
55+
#endif /* msgpack/fbuffer.hpp */
56+

test/buffer.cc

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include <msgpack.hpp>
2+
#include <msgpack/fbuffer.hpp>
3+
#include <msgpack/fbuffer.h>
24
#include <msgpack/zbuffer.hpp>
35
#include <gtest/gtest.h>
46
#include <string.h>
@@ -70,3 +72,49 @@ TEST(buffer, zbuffer)
7072
zbuf.flush();
7173
}
7274

75+
76+
TEST(buffer, fbuffer)
77+
{
78+
FILE* file = tmpfile();
79+
EXPECT_TRUE( file != NULL );
80+
81+
msgpack::fbuffer fbuf(file);
82+
EXPECT_EQ(file, fbuf.file());
83+
84+
fbuf.write("a", 1);
85+
fbuf.write("a", 1);
86+
fbuf.write("a", 1);
87+
88+
fflush(file);
89+
rewind(file);
90+
for (size_t i=0; i < 3; ++i) {
91+
int ch = fgetc(file);
92+
EXPECT_TRUE(ch != EOF);
93+
EXPECT_EQ('a', static_cast<char>(ch));
94+
}
95+
EXPECT_EQ(EOF, fgetc(file));
96+
fclose(file);
97+
}
98+
99+
100+
TEST(buffer, fbuffer_c)
101+
{
102+
FILE* file = tmpfile();
103+
void* fbuf = (void*)file;
104+
105+
EXPECT_TRUE( file != NULL );
106+
EXPECT_EQ(0, msgpack_fbuffer_write(fbuf, "a", 1));
107+
EXPECT_EQ(0, msgpack_fbuffer_write(fbuf, "a", 1));
108+
EXPECT_EQ(0, msgpack_fbuffer_write(fbuf, "a", 1));
109+
110+
fflush(file);
111+
rewind(file);
112+
for (size_t i=0; i < 3; ++i) {
113+
int ch = fgetc(file);
114+
EXPECT_TRUE(ch != EOF);
115+
EXPECT_EQ('a', (char) ch);
116+
}
117+
EXPECT_EQ(EOF, fgetc(file));
118+
fclose(file);
119+
}
120+

0 commit comments

Comments
 (0)