Now you can download a copy of these docs so you can use them offline! Download now
DataIOStream.cpp
1 #include "networktables2/connection/DataIOStream.h"
2 
3 //TODO remove this when alloca is solved
4 #ifdef WIN32
5 #include <malloc.h>
6 #endif
7 
9 class InertStream : public IOStream
10 {
11 protected:
12  virtual int read(void* ptr, int numbytes) {return numbytes;} //return success
13  virtual int write(const void* ptr, int numbytes) {return numbytes;}
14  virtual void flush() {}
15  virtual void close() {}
16 };
17 
18 static InertStream s_InertStream;
19 
20 DataIOStream::DataIOStream(IOStream* _iostream) :
21  iostream(_iostream)
22 {
23 }
24 DataIOStream::~DataIOStream()
25 {
26  close();
27  if (iostream!=&s_InertStream)
28  delete iostream;
29 }
30 
31 void DataIOStream::SetIOStream(IOStream* stream)
32 {
33  IOStream *temp=iostream;
34  iostream=stream ? stream : &s_InertStream; //We'll never assign NULL
35  if (temp!=&s_InertStream)
36  delete temp;
37 }
38 
39 void DataIOStream::close()
40 {
41  iostream->close();
42 }
43 
44 void DataIOStream::writeByte(uint8_t b)
45 {
46  iostream->write(&b, 1);
47 }
48 void DataIOStream::write2BytesBE(uint16_t s)
49 {
50  writeByte((uint8_t)(s >> 8));
51  writeByte((uint8_t)s);
52 }
53 void DataIOStream::writeString(std::string& str)
54 {
55  write2BytesBE(str.length());
56  iostream->write(str.c_str(), str.length());
57 }
58 void DataIOStream::flush()
59 {
60  iostream->flush();
61 }
62 uint8_t DataIOStream::readByte()
63 {
64  uint8_t value;
65  iostream->read(&value, 1);
66  return value;
67 }
68 uint16_t DataIOStream::read2BytesBE()
69 {
70  uint16_t value;
71  value = readByte()<<8 | readByte();
72  return value;
73 }
74 std::string* DataIOStream::readString()
75 {
76 
77  unsigned int byteLength = read2BytesBE();
78 #ifndef WIN32
79  uint8_t bytes[byteLength+1];//FIXME figure out why this doesn't work on windows
80 #else
81  uint8_t* bytes = (uint8_t*)alloca(byteLength+1);
82 #endif
83  iostream->read(bytes, byteLength);
84  bytes[byteLength] = 0;
85  return new std::string((char*)bytes);//FIXME implement UTF-8 aware version
86 }
This is used in case NULL is passed so all logic calls to IOstream can continue to assume there is ne...
Definition: DataIOStream.cpp:9

Generated on Sat Apr 26 2014 12:26:45 for WPILibC++ by doxygen 1.8.6