Text File. More...

#include <rtextfile.h>

Inheritance diagram for RTextFile:
[legend]
Collaboration diagram for RTextFile:
[legend]

Public Types

enum  RemType { NoComment, SingleLineComment, MultiLineComment, SingleMultiLineComment }
 
enum  ParseSpaceType { LeaveSpaces, SkipAllSpaces }
 

Public Member Functions

 RTextFile (void)
 
 RTextFile (const RURI &uri, const RCString &encoding="Latin1")
 
 RTextFile (RIOFile &file, const RCString &encoding="Latin1")
 
virtual void Open (RIO::ModeType mode=RIO::Read)
 
void Open (const RURI &uri, RIO::ModeType mode=RIO::Read, const RCString &encoding="Latin1")
 
virtual void Close (void)
 
virtual void Seek (off_t pos)
 
virtual void SeekRel (off_t pos)
 
RChar GetNextChar (void) const
 
RChar GetChar (void)
 
const RChar GetCur (void) const
 
RString GetChars (size_t size)
 
RString GetUntilEnd (void)
 
void SkipEol (void)
 
bool CurString (const RString &str, bool CaseSensitive=true, bool skip=true)
 
void SkipComments (void)
 
void SkipSpaces (void)
 
size_t SkipCountSpaces (RChar car)
 
virtual void SetEncoding (const RCString &name)
 
RCString GetEncoding (void) const
 
void SetRemStyle (RemType style)
 
void SetParseSpace (ParseSpaceType parse)
 
ParseSpaceType GetParseSpace (void) const
 
void SetRem (const RString &c)
 
void SetRem (const RString &b, const RString &e)
 
bool MustAddSeparator (void) const
 
void SetAddSeparator (bool add)
 
RString GetWord (void)
 
RString GetToken (const RString &endingchar)
 
RString GetTokenString (const RString &endingstr)
 
RString GetLine (bool skipempty=true)
 
long GetInt (void)
 
unsigned long GetUInt (void)
 
RTextFileoperator>> (RString &str)
 
RTextFileoperator>> (char &nb)
 
RTextFileoperator>> (unsigned char &nb)
 
RTextFileoperator>> (short &nb)
 
RTextFileoperator>> (unsigned short &nb)
 
RTextFileoperator>> (int &nb)
 
RTextFileoperator>> (unsigned int &nb)
 
RTextFileoperator>> (long &nb)
 
RTextFileoperator>> (unsigned long &nb)
 
RTextFileoperator>> (float &nb)
 
RTextFileoperator>> (double &nb)
 
RTextFileoperator>> (long double &nb)
 
void WriteLine (void)
 
void WriteStr (const RString &str, bool invalid=false)
 
void WriteStr (const char *c, bool invalid=false)
 
void WriteStr (const char *c, size_t l, bool invalid=false)
 
RTextFileoperator<< (const char *c)
 
RTextFileoperator<< (const RString &str)
 
void WriteLong (const long nb)
 
RTextFileoperator<< (const char nb)
 
RTextFileoperator<< (const short nb)
 
RTextFileoperator<< (const int nb)
 
RTextFileoperator<< (const long nb)
 
void WriteULong (const unsigned long nb)
 
RTextFileoperator<< (const unsigned char nb)
 
RTextFileoperator<< (const unsigned int nb)
 
RTextFileoperator<< (const unsigned long nb)
 
void WriteBool (const bool b)
 
RTextFileoperator<< (const bool b)
 
void WriteChar (const char c)
 
void WriteFloat (const float nb)
 
RTextFileoperator<< (const float nb)
 
void WriteDouble (const double nb)
 
RTextFileoperator<< (const double nb)
 
void WriteLongDouble (const long double nb)
 
RTextFileoperator<< (const long double nb)
 
void WriteTime (void)
 
void WriteLog (const RString &entry, bool invalid)
 
unsigned long GetLineNb (void) const
 
unsigned long GetLastLine (void) const
 
void SetSeparator (const RString &str)
 
void SetSeparator (const char *str)
 
virtual ~RTextFile (void)
 
- Public Member Functions inherited from RIOFile
 RIOFile (void)
 
 RIOFile (const RURI &uri)
 
 RIOFile (RIOFile &file)
 
RURI GetRealName (void) const
 
void Open (const RURI &uri, RIO::ModeType mode)
 
bool IsOpen (void) const
 
size_t Read (char *buffer, size_t nb, bool move=true)
 
void Write (const char *buffer, size_t nb)
 
virtual void SeekToEnd (void)
 
void Truncate (off_t newsize)
 
bool End (void) const
 
off_t GetSize (void) const
 
off_t GetPos (void) const
 
virtual ~RIOFile (void)
 
- Public Member Functions inherited from RFile
 RFile (void)
 
 RFile (const RURI &uri)
 
 RFile (const RFile &file)
 
void Open (const RURI &uri, RIO::ModeType mode)
 
int Compare (const RFile &file) const
 
int Compare (const RFile *file) const
 
int Compare (const RString &uri) const
 
const RURIGetURI (void) const
 
void SetURI (const RURI &uri)
 
const RString GetFileName (void) const
 
virtual ~RFile (void)
 

Static Public Member Functions

static bool Eol (RChar car)
 
- Static Public Member Functions inherited from RFile
static RChar GetDirSeparator (void)
 
static void RemoveFile (const RURI &uri)
 
static void RenameFile (const RURI &olduri, const RURI &newuri)
 
static RURI GetTempFile (void)
 
static bool Exists (const RURI &uri)
 
static bool IsDir (const RURI &uri)
 

Protected Member Functions

void Next (void)
 
RString GetRealNb (void)
 
void WriteSeparator (void)
 

Private Member Functions

void Begin (void)
 
void FillBuffer (void)
 
void ReadChars (void)
 
bool BeginComment (void)
 
bool EndComment (void)
 

Private Attributes

char Buffer [512]
 
RChar Chars [40]
 
size_t SizeChars [40]
 
RChar Resting
 
size_t PosChars
 
RCharNextRead
 
size_t * SizeNextRead
 
RCharNextWrite
 
size_t * SizeNextWrite
 
size_t SkipBytes
 
bool NewLine
 
RString Rem
 
RString BeginRem
 
RString EndRem
 
RemType CommentType
 
RemType ActivComment
 
ParseSpaceType ParseSpace
 
RString Separator
 
bool SkipSeparator
 
size_t Line
 
size_t LastLine
 
RTextEncodingCodec
 
RChar Cur
 

Additional Inherited Members

- Protected Attributes inherited from RIOFile
bool CanWrite
 
bool CanRead
 
- Protected Attributes inherited from RFile
RIO::ModeType Mode
 
RURI URI
 

Detailed Description

Text File.

The RTextFile class implements some basic functions needed when working with text files.

The file can contain some comments. These comments can be either on a single line, beginning with a given set of characters ('', by default) and ending at the end of the line, or be multi-line, beginning and ending with corresponding set of characters (C comments delimiters by default).

When multiple elements are inserted on the same text line, before each insertion, and except for the first one, a set of characters representing a separator (' ' by default) can be added. By default, the separation is added, but this can be change with the RTextFile::SetAddSeparator method.

The user can write his own operators to read and write with RTextFile. Here is an example:

#include <rtextfile.h>
using namespace R;
class Point
{
public:
int X,Y;
Point(void) : X(0), Y(0) {}
Point(int x,int y) : X(x), Y(y) {}
};
{
return(f<<pt.X<<pt.Y);
}
{
return(f>>pt.X>>pt.Y);
}
int main(int argc, char *argv[])
{
Point Pt1(2,3);
Point Pt2;
RTextFile Out("/home/user/Test.txt");
Out.Open(RIO::Create);
Out<<Pt1;
RTextFile In("/home/user/Test.txt");
In.Open(RIO::Read);
In>>Pt2;
cout<<"Pt2=("<<Pt2.X<<","<<Pt2.Y<<")"<<endl;
}
Note
Internally, when a file is read, a buffer holds a small number of Unicode characters (Chars), and a buffer holds the size in bytes of each character read (SizeChars). Two pointers (NextRead and SizeNextRead) parse these buffers when characters already read must be treated. Two other buffers (NextWrite and SizeNextWrite) parse the buffers when new characters must be read from the file.
Each character is decoded one by one. This multiplies the call to RTextEnconding which is time-consumming, but ensure that the file always now the exact position (in byte) of every Unicode character read.

Member Enumeration Documentation

enum RemType

The RemType enum represents the different style of comments used which text files. The comments are skipped when the file is read.

Enumerator
NoComment 

No comments are possible.

SingleLineComment 

Only single line comments are possible.

MultiLineComment 

Only multiple line comments are possible.

SingleMultiLineComment 

Both type of comments are possible.

The ParseSpaceType enum represents the different way to handle spaces. If SkipAllSpaces is chosen, leading spaces and empty lines are not seen by the programmer.

Enumerator
LeaveSpaces 

Leave spaces.

SkipAllSpaces 

Skip all spaces.

Constructor & Destructor Documentation

RTextFile ( void  )

Default constructor.

RTextFile ( const RURI uri,
const RCString encoding = "Latin1" 
)

Construct a text file.

Parameters
uriURI of the file.
encodingThe encoding scheme of the file.
RTextFile ( RIOFile file,
const RCString encoding = "Latin1" 
)

Construct a text file.

Parameters
fileA generic input/output file that should be treated as text file.
encodingThe encoding scheme of the file.
virtual ~RTextFile ( void  )
virtual

Destruct the file.

Member Function Documentation

virtual void Open ( RIO::ModeType  mode = RIO::Read)
virtual

Open the file

Parameters
modeThe open mode for the file.

Reimplemented from RIOFile.

Reimplemented in RXMLParser, RCSVFile, RXMLFile, and RHTMLFile.

void Open ( const RURI uri,
RIO::ModeType  mode = RIO::Read,
const RCString encoding = "Latin1" 
)

Open the file

Parameters
uriURI of the file.
modeThe open mode for the file.
encodingThe encoding scheme of the file.
virtual void Close ( void  )
virtual

Close the file.

Reimplemented from RIOFile.

Reimplemented in RCSVFile, and RXMLFile.

virtual void Seek ( off_t  pos)
virtual

Go to a specific position of the file.

Warning
this method doesn't take the count of lines into account.
Parameters
posPosition to reach.

Reimplemented from RIOFile.

virtual void SeekRel ( off_t  pos)
virtual

Move for a given number of bytes from the current position of the file.

Warning
this method doesn't take the count of lines into account.
Parameters
posRelative position.

Reimplemented from RIOFile.

RChar GetNextChar ( void  ) const

Read the next character but without to move the internal pointer of the file.

RChar GetChar ( void  )

Read the next character and move the internal pointer of the file.

const RChar GetCur ( void  ) const

Get the last character read in the buffer.

RString GetChars ( size_t  size)

Get the next characters.

Parameters
sizeNumber to characters to read.
Returns
a RString.
RString GetUntilEnd ( void  )

Read the rest of the file.

Returns
A RString containing the file.
void Begin ( void  )
private

Go to the beginning of the file.

void FillBuffer ( void  )
private

Must the buffer be filled again.

void ReadChars ( void  )
private

Read a series of 20 characters of variable size and store the result in an internal buffer. If the file is at the end, nothing is done.

void SkipEol ( void  )

This function skip a end of line.

This function is for internal uses only

bool BeginComment ( void  )
private

This function returns true if the current position is the beginning of a comment (Only used if in read mode).

bool EndComment ( void  )
private

This function returns true if the current position is the ending of a comment and skip the end characters if MultiLineComment.

void Next ( void  )
protected

Goes to the next character.

static bool Eol ( RChar  car)
static

Return true if the character defines the end of a line.

Parameters
carCharacter.
bool CurString ( const RString str,
bool  CaseSensitive = true,
bool  skip = true 
)

Verify if a string is the next one in the buffer. If the string is found, it may be skipped.

Parameters
strThe string to find
CaseSensitiveIs the search case sensitive.
skipMust the string be skipped?
Returns
True if the string is found.
void SkipComments ( void  )

This function skip comments (Only used if read mode).

void SkipSpaces ( void  )

This function skip spaces (Only used if read mode).

size_t SkipCountSpaces ( RChar  car)

This function skip spaces and counts the number of a given character at the beginning on the last line read (Only used if read mode).

Parameters
carCharacter (must be a space character such as a tab).
virtual void SetEncoding ( const RCString name)
virtual

Set the encoding of the file.

Parameters
nameName of the encoding.

Reimplemented in RXMLFile.

RCString GetEncoding ( void  ) const

Get the encoding of the file.

Returns
RString.
void SetRemStyle ( RemType  style)

Set the style of comments.

Parameters
styleThe Style.
void SetParseSpace ( ParseSpaceType  parse)

Set the way spaces are treated.

ParseSpaceType GetParseSpace ( void  ) const

Set the way spaces are treated.

void SetRem ( const RString c)

Set the string of comments when single line.

Parameters
cThis string represent the begin of a comment.
void SetRem ( const RString b,
const RString e 
)

Set the begin and the end strings of comments when in multi-line.

Parameters
bThis string represent the begin of a comment.
eThis string represent the end of a comment.
bool MustAddSeparator ( void  ) const

Look if a separator must be added when something is written.

Returns
true if it is the case.
void SetAddSeparator ( bool  add)

Define if a separator must be added when something is written.

Parameters
addMust the separator be added?
RString GetWord ( void  )

Get the next word contained in the file. A word is a suite of characters delimited by spaces.

RString GetToken ( const RString endingchar)

Get the next token contained in the file. A token is a suite of characters delimited either by spaces or by a specific ending character. The ending character is not skipped.

Parameters
endingcharString containing all possible ending characters.
RString GetTokenString ( const RString endingstr)

Get the next token contained in the file, i.e a suite of characters delimited either by spaces or by a specific string. The ending string is not skipped.

Parameters
endingstrEnding String.
RString GetLine ( bool  skipempty = true)

Return the next entire line in the file.

Parameters
skipemptySkip empty lines.
long GetInt ( void  )

Return the next integer contained in the file.

unsigned long GetUInt ( void  )

Return the next unsigned integer contained in the file.

RTextFile& operator>> ( RString str)

Operator for string.

RTextFile& operator>> ( char &  nb)

Operator for char.

RTextFile& operator>> ( unsigned char &  nb)

Operator for unsigned char.

RTextFile& operator>> ( short &  nb)

Operator for short.

RTextFile& operator>> ( unsigned short &  nb)

Operator for unsigned short.

RTextFile& operator>> ( int &  nb)

Operator for int.

RTextFile& operator>> ( unsigned int &  nb)

Operator for size_t.

RTextFile& operator>> ( long &  nb)

Operator for long.

RTextFile& operator>> ( unsigned long &  nb)

Operator for unsigned long.

RString GetRealNb ( void  )
protected

Return a next string contained in the that should be a real number with the following pattern:

[whitespaces][+|-][nnnnn][.nnnnn][e|E[+|-]nnnn]
Returns
RString.
RTextFile& operator>> ( float &  nb)

Operator for float.

RTextFile& operator>> ( double &  nb)

Operator for a double.

RTextFile& operator>> ( long double &  nb)

Operator for a long double.

void WriteLine ( void  )

Write an end of line in the file.

void WriteSeparator ( void  )
protected

Write a separator if necessary:

  • A seperator must be added.
  • The current pointer of the file is not at the beginning of a new line.
void WriteStr ( const RString str,
bool  invalid = false 
)

Write a string in the file. If the string is not the first thing on the line, a separator is eventually added before it.

Parameters
invalidIf true, invalid characters are allowed. If false, an exception is generated.
void WriteStr ( const char *  c,
bool  invalid = false 
)

Write a string in the file. If the string is not the first thing on the line, a separator is eventually added before it.

Parameters
escapeEscape invalid sequences of bytes?
invalidIf true, invalid characters are allowed. If false, an exception is generated.
void WriteStr ( const char *  c,
size_t  l,
bool  invalid = false 
)

Write a string of a give length in the file. If the string is not the first thing on the line, a separator is eventually added before it.

Parameters
escapeEscape invalid sequences of bytes?
invalidIf true, invalid characters are allowed. If false, an exception is generated.
RTextFile& operator<< ( const char *  c)

<< Operator for char *.

RTextFile& operator<< ( const RString str)

<< Operator for RString.

void WriteLong ( const long  nb)

Write a long in the file. If the number is not the first thing on the line, a space is add before it.

RTextFile& operator<< ( const char  nb)

<< Operator for char.

RTextFile& operator<< ( const short  nb)

<< Operator for short.

RTextFile& operator<< ( const int  nb)

<< Operator for int.

RTextFile& operator<< ( const long  nb)

<< Operator for long.

void WriteULong ( const unsigned long  nb)

Write a unsigned long in the file. If the number is not the first thing on the line, a separator is add before it.

RTextFile& operator<< ( const unsigned char  nb)

<< Operator for unsigned char.

RTextFile& operator<< ( const unsigned int  nb)

<< Operator for size_t.

RTextFile& operator<< ( const unsigned long  nb)

<< Operator for unsigned long.

void WriteBool ( const bool  b)

Write a bool in the file as '1' or '0' depend on his value. If the number is not the first thing on the line, a separator is add before it.

RTextFile& operator<< ( const bool  b)

<< Operator for bool.

void WriteChar ( const char  c)

Write a char in the file.

void WriteFloat ( const float  nb)

Write a float.

RTextFile& operator<< ( const float  nb)

<< Operator for float.

void WriteDouble ( const double  nb)

Write a double.

RTextFile& operator<< ( const double  nb)

<< Operator for double.

void WriteLongDouble ( const long double  nb)

Write a long double

RTextFile& operator<< ( const long double  nb)

<< Operator for long double.

void WriteTime ( void  )

Write the time in the file. If the time is not the first thing on the line, a separator is add before it.

void WriteLog ( const RString entry,
bool  invalid 
)

Write a log entry in the file. First the time is written, and then the entry. The entry is alone on a line, so end-of-lines are inserted if necessary before or after it.

Parameters
invalidIf true, invalid characters are allowed. If false, an exception is generated.
unsigned long GetLineNb ( void  ) const

Return the actual line number.

unsigned long GetLastLine ( void  ) const

Return the last line number where something was read.

void SetSeparator ( const RString str)

Specify the set of characters used as separator.

void SetSeparator ( const char *  str)

Specify the set of characters used as separator.

Field Documentation

char Buffer[512]
private

Temporary buffer (Used only if read mode).

RChar Chars[40]
private

Buffer of Unicode characters already read.

size_t SizeChars[40]
private

Size of Unicode characters already read in the buffer.

RChar Resting
private

Resting (if any) character if a 4-byte Unicode character is read.

size_t PosChars
private

Position of the next Unicode to handle.

RChar* NextRead
private

Next character to handle.

size_t* SizeNextRead
private

Size of the next character to handle.

RChar* NextWrite
private

Next character to treat.

size_t* SizeNextWrite
private

Size of the next character to treat.

size_t SkipBytes
private

Number of bytes to skip the next time Unicode characters must be extracted.

bool NewLine
private

At NewLine? (Used only if created or append mode).

RString Rem
private

String representing a single line comment.

RString BeginRem
private

String representing the beginning of a multi-line comment.

RString EndRem
private

String representing the ending of a multi-line comment.

RemType CommentType
private

The type of comments that are using for this file.

RemType ActivComment
private

The type of comments currently active.

ParseSpaceType ParseSpace
private

How to handle spaces.

RString Separator
private

This string represent a separator for different elements on the same line

bool SkipSeparator
private

Skip the separator each time something is written on the current line?

size_t Line
private

This variable is holding the current line number.

size_t LastLine
private

This variable is holding the last line where something was read (or written).

RTextEncoding* Codec
private

Encoding of the text file.

RChar Cur
private

Current character in the buffer.