Implementation writing an BIN file. More...
#include <storage/storage.h>
Public Types | |
typedef ptrdiff_t | reference_t |
Type of a reference identifier. | |
Public Member Functions | |
bool | checkNextField (const QString &) |
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 | reader () |
virtual bool | serialize (const QString &filename, Model *model) |
Actually save the file. | |
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 Types | |
typedef std::map< reference_t, int > | ref_t |
Protected Member Functions | |
bool | checkFileStatus () |
bool | createElement (const QString &name, unsigned char type) |
bool | resolveFrame (bool named_field=true) |
template<typename T > | |
bool | writeField (const QString &name, T &value) |
Protected Attributes | |
QFile * | file |
std::stack< Frame > | frame_stack |
Stack of frames. A frame group fields. | |
std::map< QString, ref_t > | references |
Implementation writing an BIN file.
Definition at line 47 of file storage_bin.h.
typedef ptrdiff_t storage::VVEStorage_BINWriter::reference_t |
Type of a reference identifier.
Reimplemented from storage::VVEStorage.
Definition at line 51 of file storage_bin.h.
bool storage::VVEStorage_BINWriter::checkNextField | ( | const QString & | name | ) | [inline, 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 74 of file storage_bin.h.
bool storage::VVEStorage_BINWriter::endCompound | ( | ) | [virtual] |
End the current compound.
Implements storage::VVEStorage.
Definition at line 317 of file storage_bin.cpp.
References frame_stack, storage::VVEStorage::setLastError(), and storage::USER_ERROR.
00318 { 00319 if(frame_stack.empty()) 00320 { 00321 setLastError(USER_ERROR, "Error, trying to finish a compound that didn't start"); 00322 return false; 00323 } 00324 if(frame_stack.top().unnamed_subframes > 0) 00325 { 00326 DEBUG_PRINT("Ending unnamed compound" << endl); 00327 frame_stack.top().unnamed_subframes--; 00328 DEBUG_PRINT(" unnamed compounds left: " << frame_stack.top().unnamed_subframes << endl); 00329 return true; 00330 } 00331 if(!frame_stack.top().has_fields) 00332 { 00333 unsigned char type = BT_START_COMPOUND; 00334 write(file, type); 00335 if(!checkFileStatus()) return false; 00336 } 00337 DEBUG_PRINT("Ending named compound" << endl); 00338 frame_stack.pop(); 00339 write_id(file, ""); // Mark of an end of compound 00340 return true; 00341 }
bool storage::VVEStorage_BINWriter::endReference | ( | ) | [virtual] |
Stop the current reference.
Implements storage::VVEStorage.
Definition at line 366 of file storage_bin.cpp.
virtual QString storage::VVEStorage_BINWriter::filename | ( | ) | const [inline, virtual] |
Name of the file being processed.
Implements storage::VVEStorage.
Definition at line 79 of file storage_bin.h.
References QFile::fileName().
00080 { 00081 if(file) 00082 return file->fileName(); 00083 else 00084 return ""; 00085 }
virtual bool storage::VVEStorage_BINWriter::reader | ( | ) | [inline, virtual] |
Implements storage::VVEStorage.
Definition at line 67 of file storage_bin.h.
Actually save the file.
Implements storage::VVEStorage.
Definition at line 189 of file storage_bin.cpp.
References QString::arg(), QFile::close(), QFile::error(), frame_stack, storage::IO_ERROR, storage::NO__ERROR, QFile::open(), Model::serialize(), storage::VVEStorage::setLastError(), storage::USER_ERROR, Model::version(), and Model::versionNumber().
00190 { 00191 last_error = NO__ERROR; 00192 last_error_string = QString(); 00193 00194 QFile _file(filename); 00195 file = &_file; 00196 references.clear(); 00197 if(!file->open(QIODevice::WriteOnly)) 00198 { 00199 DEBUG_PRINT("error: " << file->error() << ": " << file->errorString() << endl); 00200 setLastError(IO_ERROR, QString("Could not open file '%1' for writing").arg(filename)); 00201 return false; 00202 } 00203 file->write("VVE BIN2\n"); 00204 00205 file_version = model->version(); 00206 00207 write(file, file_version); 00208 00209 file_version = QString("%1 BIN").arg(file_version); 00210 version_number = model->versionNumber(file_version); 00211 00212 // Now write the size of the number types 00213 #define WRITE_TYPE_SIZE(id, T) \ 00214 write(file, (unsigned char)id); \ 00215 write(file, (unsigned char)TypeTraits<T>::type); \ 00216 write(file, (unsigned char)sizeof(T)); 00217 00218 FOR_ALL_TYPEIDS(WRITE_TYPE_SIZE) 00219 00220 #undef WRITE_TYPE_SIZE 00221 00222 write(file, (unsigned char)T_INVALID); 00223 00224 if(!checkFileStatus()) return false; 00225 00226 frame_stack.push(Frame(0,true)); // Prevent replacing the top-level 00227 bool result = model->serialize(*this); 00228 file->close(); 00229 if(result) 00230 { 00231 if(frame_stack.empty()) 00232 { 00233 setLastError(USER_ERROR, "One too many compound have been closed."); 00234 return false; 00235 } 00236 frame_stack.pop(); 00237 if(!frame_stack.empty()) 00238 { 00239 setLastError(USER_ERROR, QString("%1 compounds have been opened and not closed.").arg(frame_stack.size())); 00240 return false; 00241 } 00242 } 00243 file = 0; 00244 return result; 00245 }
bool storage::VVEStorage_BINWriter::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 298 of file storage_bin.cpp.
References frame_stack, and QString::isEmpty().
00299 { 00300 if(!name.isEmpty()) 00301 { 00302 DEBUG_PRINT("Starting compounds " << name << endl); 00303 if(!resolveFrame()) 00304 return false; 00305 write_id(file, name); 00306 frame_stack.push(Frame()); 00307 return checkFileStatus(); 00308 } 00309 else 00310 { 00311 DEBUG_PRINT("Starting unnamed compounds " << endl); 00312 frame_stack.top().unnamed_subframes++; // Add one invisible element 00313 } 00314 return true; 00315 }
int storage::VVEStorage_BINWriter::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.
Implements storage::VVEStorage.
Definition at line 343 of file storage_bin.cpp.
00344 { 00345 DEBUG_PRINT("Creating reference named " << name << endl); 00346 ref_t& ref_map = references[ref_type]; 00347 00348 if(!createElement(name, BT_START_REFERENCE)) 00349 return false; 00350 00351 ref_t::iterator found = ref_map.find(ref); 00352 int id = -1; 00353 if(found == ref_map.end()) 00354 { 00355 id = (int)ref_map.size(); 00356 ref_map[ref] = id; 00357 } 00358 else 00359 { 00360 id = found->second; 00361 } 00362 write(file, (qint32)id); 00363 return id; 00364 }
virtual bool storage::VVEStorage_BINWriter::writer | ( | ) | [inline, virtual] |
Implements storage::VVEStorage.
Definition at line 68 of file storage_bin.h.
std::stack<Frame> storage::VVEStorage_BINWriter::frame_stack [protected] |
Stack of frames. A frame group fields.
Definition at line 97 of file storage_bin.h.
Referenced by endCompound(), serialize(), and startCompound().