Updated fileheaders and styled files using astyle.

This commit is contained in:
t-moe
2015-06-08 11:00:52 +02:00
parent 7e61a129a3
commit fceb2d15b2
63 changed files with 6045 additions and 4691 deletions

View File

@@ -1,5 +1,19 @@
/**************************************************************************************************************************************
* Project: discoverpixy
* Website: https://github.com/t-moe/discoverpixy
* Authors: Aaron Schmocker, Timo Lang
* Institution: BFH Bern University of Applied Sciences
* File: emulator/qt/ll_filesystem.cpp
*
* Version History:
* Date Autor Email SHA Changes
* 2015-05-10 timolang@gmail.com e2bce8f Added filesystem module, tests and implementation for it in emulator.
* 2015-06-07 timolang@gmail.com 4b5768c Improved Comments in whole emulator. Finalized emulator section in docu.
*
**************************************************************************************************************************************/
extern "C" {
#include "ll_filesystem.h"
#include "ll_filesystem.h"
}
#include <QFile>
#include <QDir>
@@ -7,69 +21,78 @@ extern "C" {
#include <QFileInfoList>
#include <QDateTime>
QDir rootdir ("./emulated"); //Create a QDir which points to the "root" of the emulated filesystem
QDir rootdir("./emulated"); //Create a QDir which points to the "root" of the emulated filesystem
bool ll_filesystem_init() {
if(!rootdir.exists()) { //if our root dir is nonexistent
bool ll_filesystem_init()
{
if (!rootdir.exists()) { //if our root dir is nonexistent
qWarning() << "Filesystem can not be emulated because the 'emulated' folder does not exist";
return false; //mark an error
}
return true;
}
DIRECTORY_STRUCT* ll_filesystem_dir_open(const char* path) {
DIRECTORY_STRUCT* ll_filesystem_dir_open(const char* 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()) {
if (!d.exists()) {
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);
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]; //allocate array of file structs
for(int i=0; i<entries.count(); i++){ //foreach file that we found
for (int i = 0; i < entries.count(); i++) { //foreach file that we found
QFileInfo fi = entries.at(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]; //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->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 { //it's a file
entry->fsize = fi.size(); //set filesize
}
if(fi.isHidden()) { //the file is hidden
entry->fattrib|=F_HID;
if (fi.isHidden()) { //the file is hidden
entry->fattrib |= F_HID;
}
if(!fi.isWritable()) { //the file is not writable
entry->fattrib|=F_RDO; //set readonly attribue
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; //year is realtive to 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();
entry->ftime.min = dt.time().minute();
entry->ftime.sec = dt.time().second()/2;
entry->ftime.sec = dt.time().second() / 2;
}
return directory; //return filled directory struct
}
void ll_filesystem_dir_close(DIRECTORY_STRUCT* dir) {
if(dir!=NULL) { //passed handle is valid
for(int i=0; i<dir->num_files; i++) { //foreach file
void ll_filesystem_dir_close(DIRECTORY_STRUCT* dir)
{
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 file array
delete dir; //delete structure itself
}
@@ -81,17 +104,20 @@ struct QT_FILE_HANDLE : FILE_HANDLE { //..derived from the FILE_HANDLE (of the F
};
FILE_HANDLE* ll_filesystem_file_open(const char* filename) {
if(!rootdir.exists()) {
FILE_HANDLE* ll_filesystem_file_open(const char* filename)
{
if (!rootdir.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
if (!f->exists()) { //File does not exist
return NULL; //mark error
}
if(!f->open(QFile::ReadWrite)) { //try to open the file, it it fails then ...
if (!f->open(QFile::ReadWrite)) { //try to open the file, it it fails then ...
return NULL; //... mark error
}
@@ -100,35 +126,42 @@ FILE_HANDLE* ll_filesystem_file_open(const char* filename) {
QT_FILE_HANDLE* fh = new QT_FILE_HANDLE(); //Create Structure to return to user
fh->file = f;
fh->fname = filename;
fh->fpos =0;
fh->fpos = 0;
fh->fsize = f->size();
return fh;
}
void ll_filesystem_file_close(FILE_HANDLE* handle) {
if(handle!=NULL) { //passed handle is valid
void ll_filesystem_file_close(FILE_HANDLE* handle)
{
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
if (fh->file->isOpen()) { //if the file is still open
fh->file->close(); //close the file
}
delete fh->file; //delete QFile instance
delete fh; //delete the fle
}
}
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); //cast pointer to QT_FILE_HANDLE
if(!fh->file->isOpen()) { //file is not open
return F_DISKERROR;
}
if(offset>=fh->file->size()) { //offset exeeds filesize
FILE_STATUS ll_filesystem_file_seek(FILE_HANDLE* handle, uint32_t offset)
{
if (handle == NULL) {
return F_INVALIDPARAM;
}
if(fh->file->seek(offset)) { //try to seek to desired offset
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()) { //offset exeeds filesize
return F_INVALIDPARAM;
}
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 { //seek failed
@@ -136,49 +169,64 @@ FILE_STATUS ll_filesystem_file_seek(FILE_HANDLE* handle, uint32_t offset) {
}
}
FILE_STATUS ll_filesystem_file_read(FILE_HANDLE* handle, uint8_t* buf, uint32_t size) {
if(handle==NULL || buf==NULL) {
FILE_STATUS ll_filesystem_file_read(FILE_HANDLE* handle, uint8_t* buf, uint32_t size)
{
if (handle == NULL || buf == NULL) {
return F_INVALIDPARAM;
}
QT_FILE_HANDLE* fh = static_cast<QT_FILE_HANDLE*>(handle); //cast pointer to QT_FILE_HANDLE
if(!fh->file->isOpen()) { //file is not open
if (!fh->file->isOpen()) { //file is not open
return F_DISKERROR;
}
if(!fh->file->isReadable()) { //file is not readable
if (!fh->file->isReadable()) { //file is not readable
return F_EACCESS;
}
qint64 bytesRead = fh->file->read((char*)buf,size); //try to read desired amount of bytes
if(bytesRead<0) { //read failed
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; //increase file position (for user)
if(bytesRead!=size) { //we got less bytes than expected
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;
}
}
FILE_STATUS ll_filesystem_file_write(FILE_HANDLE* handle, uint8_t* buf, uint32_t size) {
if(handle==NULL) {
FILE_STATUS ll_filesystem_file_write(FILE_HANDLE* handle, uint8_t* buf, uint32_t size)
{
if (handle == NULL) {
return F_INVALIDPARAM;
}
QT_FILE_HANDLE* fh = static_cast<QT_FILE_HANDLE*>(handle); //cast pointer to QT_FILE_HANDLE
if(!fh->file->isOpen()) { //file is not open
if (!fh->file->isOpen()) { //file is not open
return F_DISKERROR;
}
if(!fh->file->isWritable()) { //file is not writable
if (!fh->file->isWritable()) { //file is not writable
return F_EACCESS;
}
qint64 bytesWritten = fh->file->write((char*)buf,size); //try to write desired amount of bytes
if(bytesWritten<0) { //write failed
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; //increase file position (for user)
if(bytesWritten!=size) { //we wrote less bytes than expected
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;
}
}