Skip to content

Commit

Permalink
Funcionando registros
Browse files Browse the repository at this point in the history
  • Loading branch information
Dark1024 committed Sep 22, 2013
1 parent e9fa10f commit 37aed04
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Proyecto_OA.pro.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 2.7.2, 2013-09-21T07:41:17. -->
<!-- Written by QtCreator 2.7.2, 2013-09-21T19:31:48. -->
<qtcreator>
<data>
<variable>ProjectExplorer.Project.ActiveTarget</variable>
Expand Down
56 changes: 56 additions & 0 deletions adtfilerecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,59 @@ bool ADTFileRecord::indexesIsEmpty()
return true;
return false;
}

Record* ADTFileRecord::getRecord(PrimaryIndex* pi)
{
if(!this->isOpen()){
return NULL;
}

this->FS.seekg(pi->getOffset(),ios_base::beg);
this->FS.seekp(pi->getOffset(),ios_base::beg);

char* tmp = new char[this->recordLength + 1];

streamoff rl = this->recordLength;
this->FS.read(tmp,rl);
tmp[this->recordLength] = '\0';

string str (tmp);
QString qstr = QString::fromStdString(str);
vector<string> record;

int counter = 0;
for(int i = 0; i < this->fields.size(); i++){
Field* currentField = fields.at(i);

QString qstr2 = qstr.mid(counter,currentField->getLength());

cout<<qstr2.toStdString()<<endl;

qstr2 = qstr2.replace("_"," ");

record.push_back(qstr2.toStdString());

counter += currentField->getLength();
}

Record* newRecord = new Record(this->fields,record);
return newRecord;
}

void ADTFileRecord::insertIndex(string key, PrimaryIndex* index)
{
this->indexes.insert(QString::fromStdString(key),index);
}

PrimaryIndex* ADTFileRecord::searchRecord(string key)
{
if(!this->isOpen()){
return NULL;
}

if(!this->indexes.contains(QString::fromStdString(key))){
return NULL;
}

return this->indexes.value(QString::fromStdString(key));
}
12 changes: 6 additions & 6 deletions adtfilerecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ using namespace std;

class ADTFileRecord:public ADTFile
{
public:
public:
ADTFileRecord();
ADTFileRecord(string, int);
virtual ~ADTFileRecord();
Expand Down Expand Up @@ -51,20 +51,20 @@ class ADTFileRecord:public ADTFile
vector<PrimaryIndex*> getIndexes();
bool addRecord(Record*);
bool indexesIsEmpty();
//Record* getRecord(PrimaryIndex*);
//PrimaryIndex* searchRecord(string);
Record* getRecord(PrimaryIndex*);
PrimaryIndex* searchRecord(string);
//bool deleteRecord(string);

void insertIndex(string, PrimaryIndex*);

private:
int recordLength;
streamoff dataStart;
string fileName;
fstream FS;
//fstream FS;

QMap<QString,PrimaryIndex*> indexes;
QStack<streamoff> AvailList;
vector<Field*> fields;
QMap<QString,PrimaryIndex*> indexes;
};

#endif // ADTFILERECORD_H
86 changes: 85 additions & 1 deletion mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ void MainWindow::on_OpenFile_triggered()

this->fileRecord.readHeader(header);

int rl = 0;
vector<Field*> fields = this->fileRecord.getFields();
for(int i = 0; i < fields.size(); i++){
rl += fields.at(i)->getLength();
}
this->fileRecord.setRecordLength(rl);

//LEER EL ARCHIVO DE INDICES

QString str = file.insert(file.size()-5,"-indices");
Expand All @@ -118,7 +125,7 @@ void MainWindow::on_OpenFile_triggered()
streamoff offset = atoll(list2.at(1).toStdString().c_str());

PrimaryIndex* newIndex = new PrimaryIndex(key,offset);
this->map.insert(key,newIndex);
this->fileRecord.insertIndex(key,newIndex);
}
}else{
QMessageBox::critical(this,"Error","Hubo un error al momento de cargar al archivo de indices");
Expand Down Expand Up @@ -214,6 +221,14 @@ void MainWindow::on_createField_triggered()
this->fileRecord.write(header.c_str(),header.size());
this->fileRecord.flush();

vector<Field*> fields = this->fileRecord.getFields();
int rl = 0;
for(int i = 0; i < fields.size(); i++){
rl += fields.at(i)->getLength();
}
this->fileRecord.setRecordLength(rl);


delete createField;
}else{
QMessageBox::critical(this,"Error","No tiene un archivo abierto para crear un Campo");
Expand Down Expand Up @@ -363,3 +378,72 @@ void MainWindow::on_insertRecord_triggered()
delete newRecord;
}
}

void MainWindow::on_searchRecord_triggered()
{
vector<Field*> fields = this->fileRecord.getFields();
QString str;

for(int i = 0; i< fields.size(); i++){
Field* currentField = fields.at(i);
if(currentField->isKey()){
InputDialog* idialog = new InputDialog();
idialog->setField(currentField);
idialog->exec();

if(idialog->getString().isEmpty()){
return;
}

str = idialog->getString();
delete idialog;
}
}

PrimaryIndex* index = this->fileRecord.searchRecord(str.toStdString());

if(index == NULL){
QMessageBox::critical(this,"Error","El registro que buscaba no se encontro");
return;
}else{
Record* record = this->fileRecord.getRecord(index);
vector<string> r = record->getRecord();

QStandardItemModel* model = new QStandardItemModel(1,1,this);
for(int i = 0; i < fields.size(); i++){
model->setHorizontalHeaderItem(i,new QStandardItem(QString::fromStdString(fields[i]->getName())));
}

for(int i = 0; i < fields.size(); i++){
model->setItem(0,i,new QStandardItem(QString::fromStdString(r[i])));
}
delete record;
ui->MWTable->setModel(model);
}
}

void MainWindow::on_listRecord_triggered()
{
QStandardItemModel* model = new QStandardItemModel(1,1,this);

vector<Field*> fields = this->fileRecord.getFields();
vector<PrimaryIndex*> indexes = this->fileRecord.getIndexes();

for(int i = 0; i < fields.size(); i++){
model->setHorizontalHeaderItem(i,new QStandardItem(QString::fromStdString(fields[i]->getName())));
}

for(int i = 0; i < indexes.size(); i++){
PrimaryIndex* currentIndex = indexes.at(i);
if(currentIndex != NULL){
Record* record = this->fileRecord.getRecord(currentIndex);
vector<string> r = record->getRecord();

for(int j = 0; j < fields.size(); j++){
model->setItem(i,j,new QStandardItem(QString::fromStdString(r[j])));
}
delete record;
ui->MWTable->setModel(model);
}
}
}
8 changes: 5 additions & 3 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class MainWindow : public QMainWindow
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void on_NewFile_triggered();

Expand All @@ -57,9 +57,11 @@ private slots:

void on_insertRecord_triggered();

private:
QMap<string,PrimaryIndex*> map;
void on_searchRecord_triggered();

void on_listRecord_triggered();

private:
ADTFile indicesFile;
ADTFileRecord fileRecord;
Ui::MainWindow *ui;
Expand Down

0 comments on commit 37aed04

Please sign in to comment.