Improved Comments in whole emulator. Finalized emulator section in docu.

This commit is contained in:
t-moe
2015-06-07 15:29:46 +02:00
parent 3e4fbab00a
commit 4b5768cedc
8 changed files with 131 additions and 146 deletions

View File

@@ -7,51 +7,53 @@ extern "C" {
#include <QFileInfoList>
#include <QDateTime>
QDir rootdir ("./emulated");
QDir rootdir ("./emulated"); //Create a QDir which points to the "root" of the emulated filesystem
bool ll_filesystem_init() {
if(!rootdir.exists()) {
if(!rootdir.exists()) { //if our root dir is nonexistent
qWarning() << "Filesystem can not be emulated because the 'emulated' folder does not exist";
return false;
return false; //mark an error
}
return true;
}
DIRECTORY_STRUCT* ll_filesystem_dir_open(const char* path) {
QDir d(rootdir);
d.cd(path);
QDir d(rootdir); //Make a copy of the rootdir QDir instance
d.cd(path); //Change the directory to the passed path
if(!d.exists()) {
return NULL;
return NULL; //mark an error
}
DIRECTORY_STRUCT* directory = new DIRECTORY_STRUCT();
//get all files and directories which are important to us. Filter out . and .. symlinks (linux)
QFileInfoList entries = d.entryInfoList(QDir::NoDotAndDotDot|QDir::Files|QDir::Dirs|QDir::Readable|QDir::Writable|QDir::Hidden|QDir::System);
//Fill the directory structure for the user
directory->path = path;
directory->num_files = entries.count();
directory->files = new FILE_STRUCT[directory->num_files];
directory->files = new FILE_STRUCT[directory->num_files]; //allocate array of file structs
for(int i=0; i<entries.count(); i++){
for(int i=0; i<entries.count(); i++){ //foreach file that we found
QFileInfo fi = entries.at(i);
FILE_STRUCT* entry = &(directory->files[i]);
FILE_STRUCT* entry = &(directory->files[i]); //get the pointer to the current filestruct (which should be filled)
entry->fattrib = 0;
entry->fname = new char[fi.fileName().length()+1];
strcpy(entry->fname,fi.fileName().toStdString().c_str());
if(fi.isDir()) {
entry->fattrib|=F_DIR;
entry->fname = new char[fi.fileName().length()+1]; //reserve memory for filename
strcpy(entry->fname,fi.fileName().toStdString().c_str()); //copy filename into struct
if(fi.isDir()) { //it's a direcory
entry->fattrib|=F_DIR; //set directory attribute
entry->fsize = 0;
} else {
entry->fsize = fi.size();
} else { //it's a file
entry->fsize = fi.size(); //set filesize
}
if(fi.isHidden()) {
if(fi.isHidden()) { //the file is hidden
entry->fattrib|=F_HID;
}
if(!fi.isWritable()) {
entry->fattrib|=F_RDO;
if(!fi.isWritable()) { //the file is not writable
entry->fattrib|=F_RDO; //set readonly attribue
}
//Set date & time of file in structure
QDateTime dt = fi.lastModified();
entry->fdate.year = dt.date().year()-1980;
entry->fdate.year = dt.date().year()-1980; //year is realtive to 1980
entry->fdate.month = dt.date().month();
entry->fdate.day = dt.date().day();
entry->ftime.hour = dt.time().hour();
@@ -60,22 +62,22 @@ DIRECTORY_STRUCT* ll_filesystem_dir_open(const char* path) {
}
return directory;
return directory; //return filled directory struct
}
void ll_filesystem_dir_close(DIRECTORY_STRUCT* dir) {
if(dir!=NULL) {
for(int i=0; i<dir->num_files; i++) {
delete dir->files[i].fname;
if(dir!=NULL) { //passed handle is valid
for(int i=0; i<dir->num_files; i++) { //foreach file
delete dir->files[i].fname; //delete filename buffer
}
delete[] dir->files;
delete dir;
delete[] dir->files; //delete file array
delete dir; //delete structure itself
}
}
struct QT_FILE_HANDLE : FILE_HANDLE {
QFile* file;
//Struct that represents a file handle for the emulator
struct QT_FILE_HANDLE : FILE_HANDLE { //..derived from the FILE_HANDLE (of the Filesystem modul)
QFile* file; //Pointer to the open QFile* instance
};
@@ -83,17 +85,19 @@ FILE_HANDLE* ll_filesystem_file_open(const char* filename) {
if(!rootdir.exists()) {
return NULL;
}
QString filepath = rootdir.absoluteFilePath(filename);
QFile* f = new QFile(filepath);
if(!f->exists()) {
return NULL;
QString filepath = rootdir.absoluteFilePath(filename); //get the absolute path to the requested file
QFile* f = new QFile(filepath); //create a QFile instance to the requested file
if(!f->exists()) { //File does not exist
return NULL; //mark error
}
if(!f->open(QFile::ReadWrite)) {
return NULL;
if(!f->open(QFile::ReadWrite)) { //try to open the file, it it fails then ...
return NULL; //... mark error
}
QT_FILE_HANDLE* fh = new QT_FILE_HANDLE();
//At this point we have a valid QFile instance, pointing to an existing file
QT_FILE_HANDLE* fh = new QT_FILE_HANDLE(); //Create Structure to return to user
fh->file = f;
fh->fname = filename;
fh->fpos =0;
@@ -102,12 +106,13 @@ FILE_HANDLE* ll_filesystem_file_open(const char* filename) {
}
void ll_filesystem_file_close(FILE_HANDLE* handle) {
if(handle!=NULL) {
QT_FILE_HANDLE* fh = static_cast<QT_FILE_HANDLE*>(handle);
if(fh->file->isOpen()) {
fh->file->close();
if(handle!=NULL) { //passed handle is valid
QT_FILE_HANDLE* fh = static_cast<QT_FILE_HANDLE*>(handle); //cast pointer to QT_FILE_HANDLE
if(fh->file->isOpen()) { //if the file is still open
fh->file->close(); //close the file
}
delete fh;
delete fh->file; //delete QFile instance
delete fh; //delete the fle
}
}
@@ -115,18 +120,18 @@ FILE_STATUS ll_filesystem_file_seek(FILE_HANDLE* handle, uint32_t offset) {
if(handle==NULL) {
return F_INVALIDPARAM;
}
QT_FILE_HANDLE* fh = static_cast<QT_FILE_HANDLE*>(handle);
if(!fh->file->isOpen()) {
QT_FILE_HANDLE* fh = static_cast<QT_FILE_HANDLE*>(handle); //cast pointer to QT_FILE_HANDLE
if(!fh->file->isOpen()) { //file is not open
return F_DISKERROR;
}
if(offset>=fh->file->size()) {
if(offset>=fh->file->size()) { //offset exeeds filesize
return F_INVALIDPARAM;
}
if(fh->file->seek(offset)) {
fh->fpos = offset;
if(fh->file->seek(offset)) { //try to seek to desired offset
fh->fpos = offset; //update offset in FILE_HANDLE (for user)
return F_OK;
} else {
} else { //seek failed
return F_DISKERROR;
}
}
@@ -135,20 +140,20 @@ FILE_STATUS ll_filesystem_file_read(FILE_HANDLE* handle, uint8_t* buf, uint32_t
if(handle==NULL || buf==NULL) {
return F_INVALIDPARAM;
}
QT_FILE_HANDLE* fh = static_cast<QT_FILE_HANDLE*>(handle);
if(!fh->file->isOpen()) {
QT_FILE_HANDLE* fh = static_cast<QT_FILE_HANDLE*>(handle); //cast pointer to QT_FILE_HANDLE
if(!fh->file->isOpen()) { //file is not open
return F_DISKERROR;
}
if(!fh->file->isReadable()) {
if(!fh->file->isReadable()) { //file is not readable
return F_EACCESS;
}
qint64 bytesRead = fh->file->read((char*)buf,size);
if(bytesRead<0) {
qint64 bytesRead = fh->file->read((char*)buf,size); //try to read desired amount of bytes
if(bytesRead<0) { //read failed
return F_DISKERROR;
}
fh->fpos+=bytesRead;
if(bytesRead!=size) {
return F_EOF;
fh->fpos+=bytesRead; //increase file position (for user)
if(bytesRead!=size) { //we got less bytes than expected
return F_EOF; //we reached the end of the file
} else {
return F_OK;
}
@@ -158,24 +163,22 @@ FILE_STATUS ll_filesystem_file_write(FILE_HANDLE* handle, uint8_t* buf, uint32_t
if(handle==NULL) {
return F_INVALIDPARAM;
}
QT_FILE_HANDLE* fh = static_cast<QT_FILE_HANDLE*>(handle);
if(!fh->file->isOpen()) {
QT_FILE_HANDLE* fh = static_cast<QT_FILE_HANDLE*>(handle); //cast pointer to QT_FILE_HANDLE
if(!fh->file->isOpen()) { //file is not open
return F_DISKERROR;
}
if(!fh->file->isWritable()) {
if(!fh->file->isWritable()) { //file is not writable
return F_EACCESS;
}
qint64 bytesWritten = fh->file->write((char*)buf,size);
if(bytesWritten<0) {
qint64 bytesWritten = fh->file->write((char*)buf,size); //try to write desired amount of bytes
if(bytesWritten<0) { //write failed
return F_DISKERROR;
}
fh->fpos+=bytesWritten;
if(bytesWritten!=size) {
return F_EOF;
fh->fpos+=bytesWritten; //increase file position (for user)
if(bytesWritten!=size) { //we wrote less bytes than expected
return F_EOF; //we reached the end of the file
} else {
return F_OK;
}
}