Skip to content

Commit fdb0eef

Browse files
committed
Added StringStream
1 parent c56a216 commit fdb0eef

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

stream/servers/string_stream.hpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef MANGLE_STREAM_STRINGSERVER_H
2+
#define MANGLE_STREAM_STRINGSERVER_H
3+
4+
#include "memory_stream.hpp"
5+
6+
namespace Mangle {
7+
namespace Stream {
8+
9+
/** A specialized MemoryStream that takes an std::string as input. A
10+
local copy of the string is stored in the instance.
11+
12+
NOTE on using MemoryStream::clone(): Clones are invalidated once
13+
the original StringStream goes out of scope and is deallocated.
14+
*/
15+
class StringStream : public MemoryStream
16+
{
17+
const std::string str;
18+
19+
public:
20+
StringStream(const std::string &_str)
21+
: str(_str)
22+
{
23+
// Call this after initializing the class variable str, since we
24+
// need the locally allocated pointer.
25+
set(str.c_str(), str.length());
26+
}
27+
28+
static MemoryStreamPtr Open(const std::string &str)
29+
{ return MemoryStreamPtr(new StringStream(str)); }
30+
};
31+
32+
}} // namespaces
33+
#endif

stream/tests/Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
GCC=g++ -I../ -Wall -Werror
22

3-
all: memory_server_test buffer_filter_test file_server_test slice_filter_test file_write_test iostream_test cap_test buffer_writer_test copy_test
3+
all: memory_server_test buffer_filter_test file_server_test slice_filter_test file_write_test iostream_test cap_test buffer_writer_test copy_test string_stream_test
44

55
# CURRENTLY DISABLED for dependency reasons (the examples should be OK)
66
# ogre_client_test audiere_client_test
@@ -27,6 +27,9 @@ file_write_test: file_write_test.cpp ../stream.hpp ../servers/outfile_stream.hpp
2727
memory_server_test: memory_server_test.cpp ../stream.hpp ../servers/memory_stream.hpp
2828
$(GCC) $< -o $@
2929

30+
string_stream_test: string_stream_test.cpp ../stream.hpp ../servers/memory_stream.hpp ../servers/string_stream.hpp
31+
$(GCC) $< -o $@
32+
3033
buffer_filter_test: buffer_filter_test.cpp ../stream.hpp ../servers/memory_stream.hpp ../filters/buffer_stream.hpp
3134
$(GCC) $< -o $@
3235

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Size: 12
2+
Pos: 0
3+
Seeking...
4+
Pos: 3
5+
Reading: 4
6+
Four bytes: lo w
7+
Eof: 0
8+
Pos: 7
9+
Seeking again...
10+
Pos: 12
11+
Eof: 1
12+
Seek to 6
13+
Eof: 0
14+
Pos: 6
15+
Over-reading: 6
16+
Result: world!
17+
Eof: 1
18+
Pos: 12
19+
Finally, reading the entire string: 11
20+
Result: hello world
21+
Eof: 0
22+
Pos: 11
23+
Entire stream from pointer: hello world!

stream/tests/string_stream_test.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "../servers/string_stream.hpp"
2+
3+
#include <iostream>
4+
#include <string.h>
5+
6+
using namespace Mangle::Stream;
7+
using namespace std;
8+
9+
int main()
10+
{
11+
StreamPtr inp = StringStream::Open("hello world!");
12+
13+
cout << "Size: " << inp->size() << endl;
14+
cout << "Pos: " << inp->tell() << "\nSeeking...\n";
15+
inp->seek(3);
16+
cout << "Pos: " << inp->tell() << endl;
17+
char data[12];
18+
memset(data, 0, 12);
19+
cout << "Reading: " << inp->read(data, 4) << endl;
20+
cout << "Four bytes: " << data << endl;
21+
cout << "Eof: " << inp->eof() << endl;
22+
cout << "Pos: " << inp->tell() << "\nSeeking again...\n";
23+
inp->seek(33);
24+
cout << "Pos: " << inp->tell() << endl;
25+
cout << "Eof: " << inp->eof() << "\nSeek to 6\n";
26+
inp->seek(6);
27+
cout << "Eof: " << inp->eof() << endl;
28+
cout << "Pos: " << inp->tell() << endl;
29+
cout << "Over-reading: " << inp->read(data, 200) << endl;
30+
cout << "Result: " << data << endl;
31+
cout << "Eof: " << inp->eof() << endl;
32+
cout << "Pos: " << inp->tell() << endl;
33+
inp->seek(0);
34+
cout << "Finally, reading the entire string: " << inp->read(data,11) << endl;
35+
cout << "Result: " << data << endl;
36+
cout << "Eof: " << inp->eof() << endl;
37+
cout << "Pos: " << inp->tell() << endl;
38+
39+
cout << "Entire stream from pointer: " << (char*)inp->getPtr() << endl;
40+
41+
return 0;
42+
}

0 commit comments

Comments
 (0)