Text File. More...
#include <rtextfile.h>
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) |
RTextFile & | operator>> (RString &str) |
RTextFile & | operator>> (char &nb) |
RTextFile & | operator>> (unsigned char &nb) |
RTextFile & | operator>> (short &nb) |
RTextFile & | operator>> (unsigned short &nb) |
RTextFile & | operator>> (int &nb) |
RTextFile & | operator>> (unsigned int &nb) |
RTextFile & | operator>> (long &nb) |
RTextFile & | operator>> (unsigned long &nb) |
RTextFile & | operator>> (float &nb) |
RTextFile & | operator>> (double &nb) |
RTextFile & | operator>> (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) |
RTextFile & | operator<< (const char *c) |
RTextFile & | operator<< (const RString &str) |
void | WriteLong (const long nb) |
RTextFile & | operator<< (const char nb) |
RTextFile & | operator<< (const short nb) |
RTextFile & | operator<< (const int nb) |
RTextFile & | operator<< (const long nb) |
void | WriteULong (const unsigned long nb) |
RTextFile & | operator<< (const unsigned char nb) |
RTextFile & | operator<< (const unsigned int nb) |
RTextFile & | operator<< (const unsigned long nb) |
void | WriteBool (const bool b) |
RTextFile & | operator<< (const bool b) |
void | WriteChar (const char c) |
void | WriteFloat (const float nb) |
RTextFile & | operator<< (const float nb) |
void | WriteDouble (const double nb) |
RTextFile & | operator<< (const double nb) |
void | WriteLongDouble (const long double nb) |
RTextFile & | operator<< (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 RURI & | GetURI (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 |
RChar * | NextRead |
size_t * | SizeNextRead |
RChar * | NextWrite |
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 |
RTextEncoding * | Codec |
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:
- 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.
enum ParseSpaceType |
Constructor & Destructor Documentation
RTextFile | ( | void | ) |
Default constructor.
Construct a text file.
- Parameters
-
uri URI of the file. encoding The encoding scheme of the file.
Construct a text file.
- Parameters
-
file A generic input/output file that should be treated as text file. encoding The encoding scheme of the file.
|
virtual |
Destruct the file.
Member Function Documentation
|
virtual |
Open the file
- Parameters
-
mode The 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
-
uri URI of the file. mode The open mode for the file. encoding The encoding scheme of the file.
|
virtual |
|
virtual |
Go to a specific position of the file.
- Warning
- this method doesn't take the count of lines into account.
- Parameters
-
pos Position to reach.
Reimplemented from RIOFile.
|
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
-
pos Relative 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 | ) |
|
private |
Go to the beginning of the file.
|
private |
Must the buffer be filled again.
|
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
|
private |
This function returns true if the current position is the beginning of a comment (Only used if in read mode).
|
private |
This function returns true if the current position is the ending of a comment and skip the end characters if MultiLineComment.
|
protected |
Goes to the next character.
|
static |
Return true if the character defines the end of a line.
- Parameters
-
car Character.
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
-
str The string to find CaseSensitive Is the search case sensitive. skip Must 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
-
car Character (must be a space character such as a tab).
|
virtual |
void SetRemStyle | ( | RemType | style | ) |
Set the style of comments.
- Parameters
-
style The 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
-
c This string represent the begin of a comment.
Set the begin and the end strings of comments when in multi-line.
- Parameters
-
b This string represent the begin of a comment. e This 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
-
add Must 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.
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
-
endingchar String containing all possible ending characters.
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
-
endingstr Ending String.
RString GetLine | ( | bool | skipempty = true | ) |
Return the next entire line in the file.
- Parameters
-
skipempty Skip 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>> | ( | 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.
|
protected |
Return a next string contained in the that should be a real number with the following pattern:
- 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.
|
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
-
invalid If 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
-
escape Escape invalid sequences of bytes? invalid If 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
-
escape Escape invalid sequences of bytes? invalid If true, invalid characters are allowed. If false, an exception is generated.
RTextFile& operator<< | ( | const char * | c | ) |
<< Operator for char *.
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
-
invalid If 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
|
private |
Temporary buffer (Used only if read mode).
|
private |
Buffer of Unicode characters already read.
|
private |
Size of Unicode characters already read in the buffer.
|
private |
Resting (if any) character if a 4-byte Unicode character is read.
|
private |
Position of the next Unicode to handle.
|
private |
Next character to handle.
|
private |
Size of the next character to handle.
|
private |
Next character to treat.
|
private |
Size of the next character to treat.
|
private |
Number of bytes to skip the next time Unicode characters must be extracted.
|
private |
At NewLine? (Used only if created or append mode).
|
private |
String representing a single line comment.
|
private |
String representing the beginning of a multi-line comment.
|
private |
String representing the ending of a multi-line comment.
|
private |
The type of comments that are using for this file.
|
private |
The type of comments currently active.
|
private |
How to handle spaces.
|
private |
This string represent a separator for different elements on the same line
|
private |
Skip the separator each time something is written on the current line?
|
private |
This variable is holding the current line number.
|
private |
This variable is holding the last line where something was read (or written).
|
private |
Encoding of the text file.
|
private |
Current character in the buffer.