storage::VVEStorage_BINReader Class Reference

Implementation reading an BIN file. More...

#include <storage/storage.h>

Inheritance diagram for storage::VVEStorage_BINReader:
Inheritance graph
[legend]

List of all members.

Public Types

typedef ptrdiff_t reference_t
 Type of a reference identifier.

Public Member Functions

bool checkNextField (const QString &name)
 Test if there is a field with matching name and type.
virtual bool endCompound ()
 End the current compound.
virtual bool endReference ()
 Stop the current reference.
virtual QString filename () const
 Name of the file being processed.
virtual bool ignore (const QString &name)
 Instruct a reader to ignore the next field if called name.
virtual bool reader ()
virtual bool serialize (const QString &filename, Model *model)
 Actually read the file.
virtual void setLastError (int error, const QString &err)
 Change the text of the last error.
virtual bool setOption (int option, int value)
 Change an option.
virtual bool startCompound (const QString &name)
 Start a new compound.
virtual int startReference (const QString &name, const QString &ref_type, reference_t ref)
 Start a reference.
virtual bool writer ()

Protected Member Functions

bool checkFileStatus ()
void elementUsed ()
const QStringnextElementName ()
unsigned char nextElementType ()
template<typename Read , typename Store >
bool readData (QFile *file, Store &value)
template<typename T >
bool readValue (const QString &name, T &value)
bool resolveFrame (bool named_field=true)
bool skipNext ()
bool skipToEnd ()

Protected Attributes

QFilefile
std::stack< Frameframe_stack
 Stack of frames. A frame groups fields.
bool has_next_element_name
QString next_element_name
int top_level_compounds
 Number of unnamed compounds on top of the stack.
int type_check
NUMBER_CLASS type_class [NB_STORAGE_TYPES]
TYPES type_conversion [NB_STORAGE_TYPES]
size_t type_size [NB_STORAGE_TYPES]

Detailed Description

Implementation reading an BIN file.

Definition at line 111 of file storage_bin.h.


Member Typedef Documentation

Type of a reference identifier.

Reimplemented from storage::VVEStorage.

Definition at line 115 of file storage_bin.h.


Member Function Documentation

bool storage::VVEStorage_BINReader::checkNextField ( const QString name  )  [virtual]

Test if there is a field with matching name and type.

For writer, this should always return False.

Implements storage::VVEStorage.

Definition at line 1194 of file storage_bin.cpp.

01195   {
01196     resolveFrame();
01197     const QString& next_name = nextElementName();
01198     DEBUG_PRINT("Checking if field " << name << " is the next one: " << (next_name == name) << endl);
01199     return next_name == name;
01200   }

bool storage::VVEStorage_BINReader::endCompound (  )  [virtual]

End the current compound.

Implements storage::VVEStorage.

Definition at line 1002 of file storage_bin.cpp.

References frame_stack, setLastError(), and storage::USER_ERROR.

01003   {
01004     if(frame_stack.empty())
01005     {
01006       setLastError(USER_ERROR, "Trying to close a compound that wasn't opened");
01007       return false;
01008     }
01009     if(frame_stack.top().unnamed_subframes > 0)
01010     {
01011       DEBUG_PRINT("Ending unnamed compound" << endl);
01012       frame_stack.top().unnamed_subframes--;
01013       return true;
01014     }
01015     DEBUG_PRINT("Ending named compound" << endl);
01016     frame_stack.pop();
01017     return skipToEnd();
01018   }

bool storage::VVEStorage_BINReader::endReference (  )  [virtual]

Stop the current reference.

Implements storage::VVEStorage.

Definition at line 1061 of file storage_bin.cpp.

01062   {
01063     DEBUG_PRINT("End of reference" << endl);
01064     return skipToEnd();
01065   }

virtual QString storage::VVEStorage_BINReader::filename (  )  const [inline, virtual]

Name of the file being processed.

Implements storage::VVEStorage.

Definition at line 147 of file storage_bin.h.

References QFile::fileName().

00148     {
00149       if(file)
00150         return file->fileName();
00151       else
00152         return "";
00153     }

bool storage::VVEStorage_BINReader::ignore ( const QString  )  [virtual]

Instruct a reader to ignore the next field if called name.

This method is useful only for truly serial reader (like the BIN one)

Reimplemented from storage::VVEStorage.

Definition at line 938 of file storage_bin.cpp.

References storage::BAD_CONTENT, and setLastError().

00939   {
00940     const QString& next_name = nextElementName();
00941     if(next_name != name)
00942     {
00943       setLastError(BAD_CONTENT, QString("Trying skip element %1 when the next element is %2").arg(name).arg(next_name));
00944       return false;
00945     }
00946     return skipNext();
00947   }

virtual bool storage::VVEStorage_BINReader::reader (  )  [inline, virtual]
Returns:
True is the object is a storage reader

Implements storage::VVEStorage.

Definition at line 134 of file storage_bin.h.

Referenced by serialize().

00134 { return true; }

bool storage::VVEStorage_BINReader::serialize ( const QString filename,
Model model 
) [virtual]

Actually read the file.

Implements storage::VVEStorage.

Definition at line 785 of file storage_bin.cpp.

References QString::arg(), storage::BAD_CONTENT, QFile::close(), storage::find_type(), frame_stack, storage::IO_ERROR, storage::NO__ERROR, QFile::open(), reader(), Model::serialize(), storage::old::VVEStorage_BINReader::serialize(), setLastError(), storage::USER_ERROR, and Model::versionNumber().

00786   {
00787     last_error = NO__ERROR;
00788     last_error_string = QString();
00789 
00790     QFile _file(filename);
00791     file = &_file;
00792     has_next_element_name = false;
00793     references.clear();
00794     if(!file->open(QIODevice::ReadOnly))
00795     {
00796       setLastError(IO_ERROR, QString("Could not open file '%1' for reading").arg(filename));
00797       return false;
00798     }
00799     QByteArray first_line = file->readLine();
00800     if(first_line == "VVE BIN\n")
00801     {
00802       _file.close();
00803       old::VVEStorage_BINReader reader;
00804       return reader.serialize(filename, model);
00805     }
00806     if(first_line != "VVE BIN2\n")
00807     {
00808       setLastError(BAD_CONTENT, QString("File '%1' is not a VVE BIN file").arg(filename));
00809       return false;
00810     }
00811     DEBUG_PRINT("Starting reader for BIN2" << endl);
00812 
00813     read(file, file_version);
00814     file_version = QString("%1 BIN").arg(file_version);
00815     version_number = model->versionNumber(file_version);
00816 
00817     if(!checkFileStatus()) return false;
00818 
00819     // Read type conversion
00820     TYPES type;
00821     while((type = read_type(file)) != T_INVALID)
00822     {
00823       unsigned char t;
00824       unsigned char s;
00825       read(file, t);
00826       read(file, s);
00827       NUMBER_CLASS k = (NUMBER_CLASS)t;
00828       size_t size = (size_t)s;
00829       type_conversion[type] = find_type(k, size);
00830       type_size[type] = size;
00831       type_class[type] = k;
00832     }
00833 
00834     type_conversion[T_STRING] = T_STRING;
00835     type_conversion[T_BOOL] = T_BOOL;
00836 
00837     frame_stack.push(Frame(0,true)); // Prevent from using the top-level frame
00838     bool result = model->serialize(*this);
00839     file->close();
00840     if(result)
00841     {
00842       if(frame_stack.empty())
00843       {
00844         setLastError(USER_ERROR, "One too many compound have been closed.");
00845         return false;
00846       }
00847       frame_stack.pop();
00848       if(!frame_stack.empty())
00849       {
00850         setLastError(USER_ERROR, QString("%1 compounds have been opened and not closed.").arg(frame_stack.size()));
00851         return false;
00852       }
00853     }
00854     file = 0;
00855     return result;
00856   }

void storage::VVEStorage_BINReader::setLastError ( int  error,
const QString error_str 
) [virtual]

Change the text of the last error.

Any specific implementation might alter the text.

Reimplemented from storage::VVEStorage.

Definition at line 1202 of file storage_bin.cpp.

References QString::arg(), and QFile::pos().

Referenced by endCompound(), ignore(), serialize(), startCompound(), and startReference().

01203   {
01204     last_error = error;
01205     last_error_string = QString("Error at position %1 of the file:\n%2").arg(file->pos()).arg(err);
01206   }

bool storage::VVEStorage_BINReader::setOption ( int  ,
int   
) [virtual]

Change an option.

The options are implementation-dependent. If the option is not supported, the function returns 0.

Reimplemented from storage::VVEStorage.

Definition at line 401 of file storage_bin.cpp.

References storage::TCO_Exact, storage::TCO_NoCheck, storage::TCO_Permissive, storage::TCO_PermissiveNoWarning, storage::TCO_Strict, and storage::TypeChecking.

00402   {
00403     switch(option)
00404     {
00405       case TypeChecking:
00406         switch(value)
00407         {
00408           case TCO_Exact:
00409           case TCO_Strict:
00410           case TCO_Permissive:
00411           case TCO_PermissiveNoWarning:
00412           case TCO_NoCheck:
00413             type_check = value;
00414             return true;
00415           default:
00416             return false;
00417         }
00418         break;
00419       default:
00420         return false;
00421     }
00422   }

bool storage::VVEStorage_BINReader::startCompound ( const QString name  )  [virtual]

Start a new compound.

Useful to organize complicated data structure

When writing, always returns true. When reading, returns true only if the compound exists.

Implements storage::VVEStorage.

Definition at line 916 of file storage_bin.cpp.

References storage::BAD_CONTENT, frame_stack, QString::isEmpty(), QFile::pos(), and setLastError().

00917   {
00918     if(name.isEmpty())
00919     {
00920       DEBUG_PRINT("Start unnamed compound " << endl);
00921       frame_stack.top().unnamed_subframes++; // Add an unnamed stack
00922       return true;
00923     }
00924     if(!resolveFrame())
00925       return false;
00926     DEBUG_PRINT("Start compound " << name << " on pos " << file->pos () << endl);
00927     const QString& next_name = nextElementName();
00928     if(next_name != name)
00929     {
00930       setLastError(BAD_CONTENT, QString("Next compound has name '%1' instead of the expected '%2'").arg(next_name, name));
00931       return false;
00932     }
00933     elementUsed();
00934     frame_stack.push(Frame());
00935     return true;
00936   }

int storage::VVEStorage_BINReader::startReference ( const QString name,
const QString ref_type,
reference_t  ref 
) [virtual]

Start a reference.

All operations preceding the call to stopReference should be stored in a section reserved for references.

References should be loaded only on need.

When writing, always returns true. When reading, returns true only if the reference exists. If the reference does not exist, it will create it, so the reader has to set the fields.

Returns:
the reference identifier or -1 if no reference is available.

Implements storage::VVEStorage.

Definition at line 1030 of file storage_bin.cpp.

References storage::BAD_CONTENT, QString::isEmpty(), setLastError(), and storage::TYPE_CHECK_ERROR.

01031   {
01032     if(!resolveFrame(!name.isEmpty()))
01033       return false;
01034     if(!name.isEmpty())
01035     {
01036       DEBUG_PRINT("Starting reference named " << name << endl);
01037       const QString& next_name = nextElementName();
01038       if(next_name != name)
01039       {
01040         setLastError(BAD_CONTENT, QString("Error, expected reference named '%1' but found element named '%2'").arg(name, next_name));
01041         return -1;
01042       }
01043     }
01044     else
01045     {
01046       DEBUG_PRINT("Starting unnamed reference" << endl);
01047     }
01048     unsigned char type = nextElementType();
01049     if(type != BT_START_REFERENCE)
01050     {
01051       setLastError(TYPE_CHECK_ERROR, QString("Element '%1' is not a reference").arg(name));
01052       return -1;
01053     }
01054     qint32 id;
01055     read(file, id);
01056     if(!checkFileStatus()) return false;
01057     elementUsed();
01058     return id;
01059   }

virtual bool storage::VVEStorage_BINReader::writer (  )  [inline, virtual]
Returns:
True is the object is a storage writer

Implements storage::VVEStorage.

Definition at line 135 of file storage_bin.h.

00135 { return false; }


Member Data Documentation

Stack of frames. A frame groups fields.

Definition at line 173 of file storage_bin.h.

Referenced by endCompound(), serialize(), and startCompound().

Number of unnamed compounds on top of the stack.

That can only happen if the named compound is replaced by an unnamed field.

Definition at line 180 of file storage_bin.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Fri May 31 15:38:15 2013 for VVE by  doxygen 1.6.3