From 5cf30c2bcf4af906192340d9c4a846d41365e859 Mon Sep 17 00:00:00 2001 From: Joel Pineda Date: Tue, 17 Sep 2013 08:35:57 -0600 Subject: [PATCH] Modificando header --- Proyecto_OA.pro | 2 -- Proyecto_OA.pro.user | 2 +- adtfilerecord.cpp | 71 +++++++++++++++++++++++++++++++++++++++---- adtfilerecord.h | 17 ++++++++--- fileheader.cpp | 72 -------------------------------------------- fileheader.h | 29 ------------------ mainwindow.cpp | 22 +++++++------- mainwindow.h | 1 - 8 files changed, 89 insertions(+), 127 deletions(-) delete mode 100644 fileheader.cpp delete mode 100644 fileheader.h diff --git a/Proyecto_OA.pro b/Proyecto_OA.pro index e074263..7040336 100644 --- a/Proyecto_OA.pro +++ b/Proyecto_OA.pro @@ -17,7 +17,6 @@ SOURCES += main.cpp\ adtfile.cpp \ adtfilerecord.cpp \ field.cpp \ - fileheader.cpp \ newfieldwindow.cpp \ modifyfieldwindow.cpp \ record.cpp \ @@ -28,7 +27,6 @@ HEADERS += mainwindow.h \ adtfile.h \ adtfilerecord.h \ field.h \ - fileheader.h \ newfieldwindow.h \ modifyfieldwindow.h \ record.h \ diff --git a/Proyecto_OA.pro.user b/Proyecto_OA.pro.user index 5b81942..ef9a371 100644 --- a/Proyecto_OA.pro.user +++ b/Proyecto_OA.pro.user @@ -1,6 +1,6 @@ - + ProjectExplorer.Project.ActiveTarget diff --git a/adtfilerecord.cpp b/adtfilerecord.cpp index ea95fd6..4459c6b 100644 --- a/adtfilerecord.cpp +++ b/adtfilerecord.cpp @@ -2,23 +2,18 @@ ADTFileRecord::ADTFileRecord():ADTFile() { - this->header = new FileHeader(); } ADTFileRecord::ADTFileRecord(string Name,int recordLength):ADTFile() { this->fileName = Name; this->recordLength = recordLength; - - this->header = new FileHeader(); } ADTFileRecord::~ADTFileRecord() { this->FS.flush(); this->FS.close(); - - delete header; } void ADTFileRecord::seekgRecord(int recordNumber) @@ -112,7 +107,7 @@ void ADTFileRecord::readHeader(char* header) Field* newField = new Field(name, Type, key, length, decimal); - this->header->addField(newField); + this->addField(newField); } } @@ -125,3 +120,67 @@ int ADTFileRecord::getDataStart() { return this->dataStart; } + +void ADTFileRecord::addField(Field* field) +{ + this->fields.push_back(field); +} + +void ADTFileRecord::modifyField(int n, Field* field) +{ + this->fields.at(n).setName(field->getName()); + if(field->isKey()){ + for(int i = 0; i < this->fields.size(); i++){ + if(this->fields.at(i)->isKey()){ + this->fields.at(i)->setKey(0); + i = this->fields.size(); + } + } + this->fields.at(n)->setKey(1); + } + delete field; +} + +void ADTFileRecord::removeField(int n) +{ + this->fields.erase(this->fields.begin() + n); +} + +string ADTFileRecord::toStringHeader() const +{ + stringstream ss; + ss<<'$'; + for(size_t i = 0; i < this->fields.size(); i++){ + ss<fields.at(i)->getName()<<',' + <fields.at(i)->getType()<<','; + + if(this->fields.at(i)->isKey()){ + ss<<'1'; + }else{ + ss<<'0'; + } + + ss<fields.at(i)->getLength()<<',' + <fields.at(i)->getDecimalPlaces(); + + if(i < fields.size() -1){ + ss<<'/'; + } + } + ss<<'$'; +} + +vector ADTFileRecord::getFields() +{ + return this->fields; +} + +int ADTFileRecord::fieldsSize() +{ + return this->fields.size(); +} + +Field* ADTFileRecord::getField(int n) +{ + return this->fields.at(n); +} diff --git a/adtfilerecord.h b/adtfilerecord.h index f22fade..504c34e 100644 --- a/adtfilerecord.h +++ b/adtfilerecord.h @@ -1,6 +1,7 @@ #ifndef ADTFILERECORD_H #define ADTFILERECORD_H +#include #include #include #include @@ -27,20 +28,26 @@ class ADTFileRecord:public ADTFile virtual bool updateRecord(int, string); virtual bool deleteRecord(int); //virtual void compact(); - void setDataStart(int); - void setRecordLength(int); - void readHeader(char*); - FileHeader* getFileHeader(); + virtual void setDataStart(int); + virtual void setRecordLength(int); + virtual void readHeader(char*); virtual int getDataStart(); + virtual void addField(Field*); + virtual void modifyField(int, Field*); + virtual void removeField(int); + virtual Field* getField(int); + virtual string toStringHeader() const; + virtual int fieldsSize() const; + vector getFields(); private: int recordLength; int dataStart; - FileHeader* header; string fileName; fstream FS; QStack AvailList; + vector fields; }; #endif // ADTFILERECORD_H diff --git a/fileheader.cpp b/fileheader.cpp deleted file mode 100644 index ff24888..0000000 --- a/fileheader.cpp +++ /dev/null @@ -1,72 +0,0 @@ -#include "fileheader.h" - -FileHeader::FileHeader() -{ -} - -FileHeader::~FileHeader() -{ -} - -void FileHeader::addField(Field* field) -{ - this->fields.push_back(field); -} - -void FileHeader::modifyField(int n, Field* field) -{ - this->fields.at(n)->setName(field->getName()); - if(field->isKey()){ - for(int i = 0; i < this->fields.size(); i++){ - if(this->fields.at(i)->isKey()){ - this->fields.at(i)->setKey(0); - i = this->fields.size(); - } - } - this->fields.at(n)->setKey(1); - } - delete field; -} - -void FileHeader::removeField(int n) -{ - this->fields.erase(this->fields.begin() + n); -} - -Field* FileHeader::getField(int n) -{ - return this->fields.at(n); -} - -string FileHeader::toString() const -{ - stringstream ss; - ss<<'$'; - for(size_t i = 0; i < this->fields.size(); i++){ - ss<fields.at(i)->getName()<<',' - <fields.at(i)->getType()<<','; - - if(this->fields.at(i)->isKey()){ - ss<<'1'; - }else{ - ss<<'0'; - } - - ss<fields.at(i)->getLength()<<',' - <fields.at(i)->getDecimalPlaces(); - - if(i < fields.size() -1){ - ss<<'/'; - } - } - ss<<'$'; -} - -int FileHeader::fieldsSize() const{ - return this->fields.size(); -} - -vector FileHeader::getFields() -{ - return this->fields; -} diff --git a/fileheader.h b/fileheader.h deleted file mode 100644 index ace40fc..0000000 --- a/fileheader.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef FILEHEADER_H -#define FILEHEADER_H - -#include -#include -#include - -#include "field.h" - -using namespace std; - -class FileHeader -{ -public: - FileHeader(); - ~FileHeader(); - - void addField(Field*); - void modifyField(int,Field*); - void removeField(int); - Field* getField(int); - string toString() const; - int fieldsSize() const; - vector getFields(); -private: - vector fields; -}; - -#endif // FILEHEADER_H diff --git a/mainwindow.cpp b/mainwindow.cpp index 539ad51..a2714c3 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -19,8 +19,8 @@ void MainWindow::on_NewFile_triggered() this->fileRecord.close(); } - while(this->fileRecord.getFileHeader()->fieldsSize() != 0){ - this->fileRecord.getFileHeader()->removeField(0); + while(this->fileRecord.fieldsSize() != 0){ + this->fileRecord.removeField(0); } QString directory = QFileDialog::getExistingDirectory(this,"New File",""); @@ -96,14 +96,14 @@ void MainWindow::on_createField_triggered() Field* newField = createField->getField(); if(newField != NULL){ - for(int i = 0; i < this->fileRecord.getFileHeader()->fieldsSize(); i++){ - if(this->fileRecord.getFileHeader()->getField(i)->isKey()){ + for(int i = 0; i < this->fileRecord.fieldsSize(); i++){ + if(this->fileRecord.getField(i)->isKey()){ newField->setKey(0); - i = this->fileRecord.getFileHeader()->fieldsSize(); + i = this->fileRecord.fieldsSize(); } } - this->fileRecord.getFileHeader()->addField(newField); + this->fileRecord.addField(newField); }else{ cout<<"Campo NULL"<fileRecord.seekp(0,ios_base::beg); this->fileRecord.seekg(0,ios_base::beg); - string header = this->fileRecord.getFileHeader()->toString(); + string header = this->fileRecord.toStringHeader(); cout<fileRecord.setDataStart(header.size()); @@ -126,7 +126,7 @@ void MainWindow::on_modifyField_triggered() if(this->fileRecord.isOpen()){ ModifyFieldWindow* modifyField = new ModifyFieldWindow(); cout<<"Marca1"<setFields(this->fileRecord.getFileHeader()->getFields()); + modifyField->setFields(this->fileRecord.getFields()); modifyField->exec(); cout<<"Marca2"<getIndex(); - this->fileRecord.getFileHeader()->modifyField(index, newField); + this->fileRecord.modifyField(index,newField); }else{ cout<<"Campo NULL"<fileRecord.getFileHeader()->fieldsSize() <= 0){ + if(this->fileRecord.fieldsSize() <= 0){ QMessageBox::warning(this,"Error","El archivo abierto no contiene campos"); return; } @@ -168,7 +168,7 @@ void MainWindow::on_listField_triggered() model->setHorizontalHeaderItem(4,new QStandardItem(QString("Decimales"))); QString tmp; - vector fields = this->fileRecord.getFileHeader()->getFields(); + vector fields = this->fileRecord.getFields(); stringstream ss; for(int i = 0; i < fields.size(); i++){ diff --git a/mainwindow.h b/mainwindow.h index 03146e7..6a804ef 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -2,7 +2,6 @@ #define MAINWINDOW_H #include "adtfilerecord.h" -#include "fileheader.h" #include "field.h" #include "newfieldwindow.h" #include "modifyfieldwindow.h"