Fixed-length Records File. More...

#include <rrecfile.h>

Inheritance diagram for RRecFile< C, bOrder >:
[legend]
Collaboration diagram for RRecFile< C, bOrder >:
[legend]

Public Member Functions

 RRecFile (void)
 
 RRecFile (const RURI &uri, size_t recsize)
 
virtual void Open (RIO::ModeType mode)
 
void Open (const RURI &uri, size_t recsize, RIO::ModeType mode=RIO::Read)
 
virtual void Close (void)
 
void GoToRec (size_t nb)
 
void GoToRec (size_t c, size_t l, size_t maxc)
 
template<class TUse >
size_t Search (const TUse &tag)
 
bool FindRecord (void)
 
size_t GetRecNb (void) const
 
size_t GetCurrentRec (void) const
 
void ReadRec (C &rec)
 
void WriteRec (C &rec)
 
void Start (void)
 
void Next (void)
 
void Prev (void)
 
C * operator() (void)
 
virtual ~RRecFile (void)
 
- Public Member Functions inherited from RBinaryFile
 RBinaryFile (void)
 
 RBinaryFile (const RURI &name)
 
 RBinaryFile (RIOFile &file)
 
void Open (const RURI &uri, RIO::ModeType mode)
 
RBinaryFileoperator>> (bool &b)
 
RBinaryFileoperator>> (char *str)
 
RBinaryFileoperator>> (RString &str)
 
RBinaryFileoperator>> (char &nb)
 
RBinaryFileoperator>> (unsigned char &nb)
 
RBinaryFileoperator>> (int &nb)
 
RBinaryFileoperator>> (unsigned int &nb)
 
RBinaryFileoperator>> (long &nb)
 
RBinaryFileoperator>> (unsigned long &nb)
 
RBinaryFileoperator>> (long long &nb)
 
RBinaryFileoperator>> (unsigned long long &nb)
 
RBinaryFileoperator>> (float &nb)
 
RBinaryFileoperator>> (double &nb)
 
RBinaryFileoperator<< (bool b)
 
RBinaryFileoperator<< (char *str)
 
RBinaryFileoperator<< (RString &str)
 
RBinaryFileoperator<< (char nb)
 
RBinaryFileoperator<< (unsigned char nb)
 
RBinaryFileoperator<< (int nb)
 
RBinaryFileoperator<< (unsigned int nb)
 
RBinaryFileoperator<< (long nb)
 
RBinaryFileoperator<< (unsigned long nb)
 
RBinaryFileoperator<< (long long nb)
 
RBinaryFileoperator<< (unsigned long long nb)
 
RBinaryFileoperator<< (float nb)
 
RBinaryFileoperator<< (double nb)
 
virtual ~RBinaryFile (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 Seek (off_t pos)
 
virtual void SeekRel (off_t pos)
 
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)
 

Protected Attributes

Current
 
bool Find
 
bool Dirty
 
size_t RecSize
 
- Protected Attributes inherited from RIOFile
bool CanWrite
 
bool CanRead
 
- Protected Attributes inherited from RFile
RIO::ModeType Mode
 
RURI URI
 

Additional Inherited Members

- 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)
 

Detailed Description

template<class C, bool bOrder = false>
class R::RRecFile< C, bOrder >

Fixed-length Records File.

Template Parameters
CThe class of the elements to be read/write.
bOrderSpecify if the elements are ordered in the file.

The RRecFile class implements some basic functions needed when working with files for records. This files can be accessed as ordered or not. Opening a non ordered file as ordered gives problems.

As for the RContainer class, the TUse represent a class or a structure used for the comparaisons.

At least, one read function must be implemented in the class C:

void Read(R::RecFile<C,bOrder>& f);

The user can write his own operators to read and write record from a RRecFile. Here is an example:

#include <rrecfile.h>
using namespace R;
class Person
{
public:
R::RString LastName;
R::RString FirstName;
Person(void) {}
Person(const R::RString& l,const RString& f) : LastName(l), FirstName(f) {}
Person(const Person& p) : LastName(p.LastName), FirstName(p.FirstName) {}
int Compare(const Person& p) const {return(LastName.Compare(p.LastName));}
int Compare(const R::RString& name) const {return(LastName.Compare(name));}
};
void Person::Read(R::RRecFile<Person,true>& f)
{
char Buf[41];
f.Read(Buf,40);
LastName=Buf;
f.Read(Buf,40);
FirstName=Buf;
}
void Person::Write(R::RRecFile<Person,true>& f)
{
char Buf[41];
strcpy(Buf,LastName.Latin1());
f.Write(Buf,40);
strcpy(Buf,FirstName.Latin1());
f.Write(Buf,40);
}
int main(int argc, char *argv[])
{
R::RRecFile<Person,true> File("/home/pfrancq/test.bin",80); // Each record have a size of 80 bytes.
// Create container -> create file -> write data
std::cout<<"Create"<<std::endl;
Cont.InsertPtr(new Person("Jagger","Mike"));
Cont.InsertPtr(new Person("Gillian","Ian"));
Cont.InsertPtr(new Person("Plant","Robert"));
Cont.InsertPtr(new Person("Coverdale","David"));
Cur.Set(Cont);
for(Cur.Start();!Cur.End();Cur.Next())
Cur()->Write(File);
File.Close();
// Clean container -> open file -> read data
std::cout<<"Read"<<std::endl;
Cont.Clear();
for(File.Start();!File.End();File.Next())
Cont.InsertPtr(new Person(*File()));
Cur.Set(Cont);
for(Cur.Start();!Cur.End();Cur.Next())
std::cout<<" "<<Cur()->LastName<<", "<<Cur()->FirstName<<std::endl;
File.Close();
// Just find the firstname of "Gillian" directly in the file
std::cout<<"Find"<<std::endl;
if(File.Search("Gillian"))
std::cout<<" "<<File()->LastName<<", "<<File()->FirstName<<std::endl;
File.Close();
}

Constructor & Destructor Documentation

RRecFile ( void  )

Default constructor.

RRecFile ( const RURI uri,
size_t  recsize 
)

Construct a binary file for records.

Parameters
uriURI of the file.
recsizeSize of a given record.
virtual ~RRecFile ( void  )
virtual

Destruct the file.

Member Function Documentation

virtual void Open ( RIO::ModeType  mode)
virtual

Open the file

Parameters
modeThe open mode for the file.

Reimplemented from RBinaryFile.

void Open ( const RURI uri,
size_t  recsize,
RIO::ModeType  mode = RIO::Read 
)

Open the file

Parameters
uriURI of the file.
recsizeSize of a given record.
modeThe open mode for the file.
virtual void Close ( void  )
virtual

Close the file

Reimplemented from RIOFile.

void GoToRec ( size_t  nb)

Seek the file to a specific record number.

Parameters
nbNumber of record.
void GoToRec ( size_t  c,
size_t  l,
size_t  maxc 
)

Seek the file to a specific record number if the file represent a matrix.

Parameters
cColumn.
lLine.
maxcNumber of Columns.
Remarks
It is presume that the file is store lines by lines.
size_t Search ( const TUse &  tag)

This function returns the number of a record represented by tag, and it is used when the file is ordered. If the record was found, it becomes the current one accessible through the operator().

Template Parameters
TUseThe type of tag, the file uses the Compare(TUse &) member function of the records.
Parameters
tagThe tag used.
Returns
Returns the number of the record if it exists or the index where is has to inserted.
bool FindRecord ( void  )

Is the file pointing to a valid record?

size_t GetRecNb ( void  ) const

Get the number of records in the file.

size_t GetCurrentRec ( void  ) const

Get the current record number.

void ReadRec ( C &  rec)

Read the current record.

Remarks
This method change the current position of the file to next one.
void WriteRec ( C &  rec)

Write the current record.

void Start ( void  )

Start the file at the first record.

void Next ( void  )

Goto the next record.

void Prev ( void  )

Goto the previous record.

C* operator() ( void  )

Return the current record.

Remarks
This method change the current position of the file to next one.

Field Documentation

C Current
protected

Current record.

bool Find
protected

Was the searched record found?

bool Dirty
protected

Is the record in memory dirty?

size_t RecSize
protected

Size of a record.