PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
PStream Class Reference

#include <PStream.h>

Inheritance diagram for PStream:
PDevice PEBLObjectBase

Public Member Functions

 PStream (const std::string &filename, StreamDirection dir, StreamType type)
 The Standard constructor.
 
 PStream (const PStream &ps)
 
virtual ~PStream ()
 The Standard destructor.
 
void Open (const std::string &filename, StreamDirection dir, StreamType type)
 
void WriteChar (const char character)
 This method sends a single character to the stream.
 
void WriteString (const std::string &buffer)
 This method just writes the char* string to the stream.
 
void WriteBuffer (const std::string &buffer, unsigned int length)
 
char ReadChar ()
 
std::string ReadToken (const char separator)
 This reads up until the next separator token (or eof character)
 
std::string ReadLine ()
 
std::string ReadLineClean ()
 
bool Eof ()
 
bool Eol ()
 
bool Close ()
 
virtual PEBL_DEVICE_TYPE GetDeviceType ()
 
virtual bool SetProperty (std::string, Variant v)
 
virtual Variant GetProperty (std::string) const
 
virtual ObjectValidationError ValidateProperty (std::string, Variant v) const
 
virtual ObjectValidationError ValidateProperty (std::string) const
 
- Public Member Functions inherited from PDevice
 PDevice ()
 The Standard constructor.
 
virtual ~PDevice ()
 The Standard destructor.
 
virtual int GetState (int iface) const
 
- Public Member Functions inherited from PEBLObjectBase
 PEBLObjectBase ()
 Standard Constructor.
 
 PEBLObjectBase (ComplexDataType cdt)
 Standard Constructor.
 
 PEBLObjectBase (const PEBLObjectBase &pob)
 
virtual ~PEBLObjectBase ()
 Standard Destructor.
 
virtual bool InitializeProperty (std::string name, Variant v)
 
Variant GetProperty (std::string) const
 
ComplexDataType GetType ()
 
virtual std::string ObjectName () const
 
virtual std::ostream & PrintProperties (std::ostream &out)
 
virtual Variant GetPropertyList ()
 

Protected Member Functions

virtual std::ostream & SendToStream (std::ostream &out) const
 

Additional Inherited Members

- Protected Attributes inherited from PEBLObjectBase
ComplexDataType mCDT
 
std::map< std::string, VariantmProperties
 

Detailed Description

This is the base File Stream class. It is used to read and write files. It is also a subclass of PDevice, which means that it can be added to the event loop for interesting between-process communications, and probably even used as a basis for other input devices.

Definition at line 59 of file PStream.h.

Constructor & Destructor Documentation

◆ PStream() [1/2]

PStream::PStream ( const std::string &  filename,
StreamDirection  dir,
StreamType  type 
)

The Standard constructor.

◆ PStream() [2/2]

PStream::PStream ( const PStream ps)

Definition at line 112 of file PStream.cpp.

113{
114 mFileStream = NULL;
115}
#define NULL
Definition BinReloc.cpp:317

References NULL.

◆ ~PStream()

PStream::~PStream ( )
virtual

The Standard destructor.

This is the standard pNode destructor.

Definition at line 401 of file PStream.cpp.

402{
403 // Standard Destructor
404
405}

Member Function Documentation

◆ Close()

bool PStream::Close ( )

Definition at line 394 of file PStream.cpp.

395{
396 mFileStream->close();
397 return true;
398}

Referenced by PEBLStream::FileClose(), PEBLStream::FileReadList(), PEBLStream::FileReadTable(), PEBLStream::FileReadText(), and main().

◆ Eof()

bool PStream::Eof ( )

Definition at line 388 of file PStream.cpp.

389{
390 return mFileStream->eof();
391}

Referenced by PEBLStream::EndOfFile(), PEBLStream::FileReadList(), PEBLStream::FileReadTable(), PEBLStream::FileReadText(), and main().

◆ Eol()

bool PStream::Eol ( )

Definition at line 356 of file PStream.cpp.

357{
358 // std::cout << "EOL:" << mFileStream->tellg() << "["<<mFileStream->peek()<<"]" << endl;
359 if(mFileStream->peek() == 10) //LF
360 return true;
361
362 if(mFileStream->peek() == 13) //CR (precedes an LF in DOS.
363 {
364
365
366 streampos curr = mFileStream->tellg();
367 mFileStream->seekg(1,ios_base::cur);//look forward one
368 //cout<< "Next character: " << mFileStream->peek() <<endl;
369 //std::cout<< "13 matchses:\n";
370 if(mFileStream->peek() == 10)
371 {
372
373 return true;
374 }
375 else
376 {
377 //reverse out of it if this happens to not be paired with a LF.
378 mFileStream->seekg(-1,ios_base::cur);
379 return true;
380 }
381 }
382 if(mFileStream->eof()) //EOF --treat as an EOL.)
383 return true;
384
385 return false;
386}

Referenced by PEBLStream::EndOfLine(), and ReadToken().

◆ GetDeviceType()

virtual PEBL_DEVICE_TYPE PStream::GetDeviceType ( )
inlinevirtual

Reimplemented from PDevice.

Definition at line 86 of file PStream.h.

86{return PDT_STREAM;};
@ PDT_STREAM
Definition PDevice.h:49

References PDT_STREAM.

◆ GetProperty()

Variant PStream::GetProperty ( std::string  name) const
virtual

Definition at line 430 of file PStream.cpp.

431{
432 //PEBLObjectBase::PrintProperties(cout);
433 return PEBLObjectBase::GetProperty(name);
434}
Variant GetProperty(std::string) const

References PEBLObjectBase::GetProperty().

◆ Open()

void PStream::Open ( const std::string &  filename,
StreamDirection  dir,
StreamType  type 
)

Definition at line 118 of file PStream.cpp.

119{
120
121 mStreamFileName = filename;
122 mStreamDirection = dir;
123 mStreamType = type;
124
125 //Now, create the stream with proper flags and ready it for
126 //input/output.
127
128
129 if(mStreamDirection==sdRead)
130 {
131 mFileStream->open(mStreamFileName.c_str(), ios_base::in);
132 if(!mFileStream->is_open())
133 {
134 PError::SignalFatalError("Unable to open file ["+ mStreamFileName+"] for reading");
135 }
136 }
137 else if (mStreamDirection==sdWrite)
138 {
139 mFileStream->open(mStreamFileName.c_str(), ios_base::out);
140 if(!mFileStream->is_open())
141 {
142 PError::SignalFatalError("Unable to open file ["+ mStreamFileName+"] for writing");
143 }
144
145 }
146 else if (mStreamDirection == sdAppend)
147 {
148 mFileStream->open(mStreamFileName.c_str(), ios_base::out | ios_base::app);
149 if(!mFileStream->is_open())
150 {
151 PError::SignalFatalError("Unable to open file ["+ mStreamFileName+"] for appending");
152 }
153 }
154 else
155 {
156 //error
157 PError::SignalFatalError("Error occurred in PStream::Open");
158 }
159 //Binary/ascii/unicode/etc could be done here as well.
160
161
162 //If we didn't manage to encode anything, call an error
163 if(!mFileStream)
164 {
165 PError::SignalFatalError("Error initiating file [" + mStreamFileName + ".");
166 }
167
168}
@ sdAppend
Definition PStream.h:40
@ sdWrite
Definition PStream.h:39
@ sdRead
Definition PStream.h:38
void SignalFatalError(const std::string &message)

References sdAppend, sdRead, sdWrite, and PError::SignalFatalError().

Referenced by main().

◆ ReadChar()

char PStream::ReadChar ( )

This reads a single character from the filestream and returns it.

Definition at line 230 of file PStream.cpp.

231{
232 if(mStreamDirection == sdRead)
233 {
234
235 char tmp;
236 mFileStream->get(tmp);
237 return tmp;
238
239 }
240 else
241 {
242
243 PError::SignalFatalError("Error in PStream::ReadChar--trying to Read a Write stream\n");
244 return 0;
245 }
246}

References sdRead, and PError::SignalFatalError().

Referenced by PEBLStream::FileReadCharacter(), and main().

◆ ReadLine()

std::string PStream::ReadLine ( )

This reads up until the next eol/eof character, including the newline character at the end.

Definition at line 290 of file PStream.cpp.

291{
292 if(mStreamDirection == sdRead)
293 {
294 std::string tmpstring;
295 char tmp;
296 mFileStream->get(tmp);
297
298
299 while( !mFileStream->eof())
300 {
301 tmpstring += tmp;
302 if(tmp == 10) //10 is the newline character
303 break; //Break out if the next character is eol
304
305 mFileStream->get(tmp);
306 }
307
308
309 return tmpstring;
310 }
311
312 else
313 {
314 PError::SignalFatalError("Error in PStream::ReadLine--trying to Read a Write stream\n");
315 return 0;
316 }
317}

References sdRead, and PError::SignalFatalError().

Referenced by PEBLStream::FileReadText(), and main().

◆ ReadLineClean()

std::string PStream::ReadLineClean ( )

Definition at line 323 of file PStream.cpp.

324{
325 if(mStreamDirection == sdRead)
326 {
327 std::string tmpstring;
328 char tmp;
329 mFileStream->get(tmp);
330
331
332 while( !mFileStream->eof())
333 {
334
335 if(tmp == 10) //10 is the newline character
336 break; //Break out if the next character is eol
337
338 tmpstring += tmp;
339 mFileStream->get(tmp);
340 }
341
342
343 return tmpstring;
344 }
345
346 else
347 {
348 PError::SignalFatalError("Error in PStream::ReadLine--trying to Read a Write stream\n");
349 return 0;
350 }
351}

References sdRead, and PError::SignalFatalError().

Referenced by PEBLStream::FileReadLine(), PEBLStream::FileReadList(), and PEBLStream::FileReadTable().

◆ ReadToken()

std::string PStream::ReadToken ( const char  separator)

This reads up until the next separator token (or eof character)

Definition at line 251 of file PStream.cpp.

252{
253 if(mStreamDirection == sdRead)
254 {
255
256 std::string tmpstring; //Create a stream to use to capture input.
257 char tmp;
258 while(!mFileStream->eof() )
259 {
260 tmp = mFileStream->peek();
261 if (Eol())
262 {
263 //mFileStream->get(tmp);
264 break;
265 }
266
267 else if(tmp == separator )
268 {
269 mFileStream->get(tmp);
270 break;
271 }
272 else
273 {
274 mFileStream->get(tmp);
275 tmpstring += tmp;
276 }
277 }
278 return tmpstring;
279 }
280 else
281 {
282 PError::SignalFatalError("Error in PStream::ReadToken--trying to Read a Write stream\n");
283 return 0;
284 }
285}
bool Eol()
Definition PStream.cpp:356

References Eol(), sdRead, and PError::SignalFatalError().

Referenced by PEBLStream::FileReadWord(), and main().

◆ SendToStream()

ostream & PStream::SendToStream ( std::ostream &  out) const
protectedvirtual

Reimplemented from PDevice.

Definition at line 408 of file PStream.cpp.

409{
410 out << "<Generic File Stream>" << flush;
411 return out;
412}

◆ SetProperty()

bool PStream::SetProperty ( std::string  name,
Variant  v 
)
virtual

Reimplemented from PEBLObjectBase.

Definition at line 416 of file PStream.cpp.

417{
418 if(name=="FILENAME")
419 {
420 PEBLObjectBase::SetProperty("FILENAME",v);
421 }else if(name=="DIRECTION")
422 {
423 PEBLObjectBase::SetProperty("DIRECTION",v);
424 }
425 return true;
426}
virtual bool SetProperty(std::string name, Variant v)

References PEBLObjectBase::SetProperty().

◆ ValidateProperty() [1/2]

ObjectValidationError PStream::ValidateProperty ( std::string  name) const
virtual

Reimplemented from PEBLObjectBase.

Definition at line 444 of file PStream.cpp.

445{
446
447 if(name == "FILENAME" || name=="DIRECTION")
448 {
449 return OVE_VALID;
450 }else
451 {
452
454 }
455}
@ OVE_INVALID_PROPERTY_NAME
Definition PEBLObject.h:40
@ OVE_VALID
Definition PEBLObject.h:39

References OVE_INVALID_PROPERTY_NAME, and OVE_VALID.

◆ ValidateProperty() [2/2]

ObjectValidationError PStream::ValidateProperty ( std::string  name,
Variant  v 
) const
virtual

Reimplemented from PEBLObjectBase.

Definition at line 438 of file PStream.cpp.

439{
440 return ValidateProperty(name);
441}
virtual ObjectValidationError ValidateProperty(std::string, Variant v) const
Definition PStream.cpp:438

References ValidateProperty().

Referenced by ValidateProperty().

◆ WriteBuffer()

void PStream::WriteBuffer ( const std::string &  buffer,
unsigned int  length 
)

This method sends the number of bytes specified by the length parameter from the buffer to the stream. This could be dangerous if the user is allowed to specify these things themselves.

Definition at line 190 of file PStream.cpp.

191{
192
193 if(mStreamDirection == sdWrite ||
194 mStreamDirection == sdAppend)
195 {
196
197 int len = length < buffer.size()? length: (int)(buffer.size());
198 string tmp;
199 std::copy(buffer.begin(), buffer.begin()+len, tmp.begin());
200 (* mFileStream) << tmp << flush;
201
202 }
203 else
204 {
205 PError::SignalFatalError("Error in PStream::WriteBuffer--trying to write a Read stream: "+ string(mStreamFileName) + ".");
206 }
207}

References sdAppend, sdWrite, and PError::SignalFatalError().

Referenced by main().

◆ WriteChar()

void PStream::WriteChar ( const char  character)

This method sends a single character to the stream.

Definition at line 172 of file PStream.cpp.

173{
174 if(mStreamDirection == sdWrite ||
175 mStreamDirection == sdAppend)
176 {
177 (*mFileStream) << character << flush;
178 }
179 else
180 {
181 PError::SignalFatalError("Error in PStream::WriteChar--trying to write a Read stream: "+ string(mStreamFileName) + ".");
182 }
183
184}

References sdAppend, sdWrite, and PError::SignalFatalError().

Referenced by main().

◆ WriteString()

void PStream::WriteString ( const std::string &  buffer)

This method just writes the char* string to the stream.

Definition at line 211 of file PStream.cpp.

212{
213
214 if(mStreamDirection == sdWrite ||
215 mStreamDirection == sdAppend)
216 {
217 (* mFileStream) << buffer << flush;
218
219 }
220 else
221 {
222 PError::SignalFatalError("Error in PStream::WriteString--trying to write a Read stream: "+ string(mStreamFileName) + ".");
223 }
224
225}

References sdAppend, sdWrite, and PError::SignalFatalError().

Referenced by PEBLStream::FilePrint(), PEBLStream::FilePrint_(), and main().


The documentation for this class was generated from the following files: