int BaseDeDatos::insertar(const Registro& r){ if (this->existente(r)){ return ERR_DUPLICADO; } if (this->datosRequeridos(r)){ return ERR_CAMPO_REQUERIDO; } /* Insertar en cache */ this->bufferRegistros.push_back(r); /* Insertar en archivo */ fstream bdfile; bdfile.open (ARCHIVO_BASE, ios_base::out | ios_base::binary | ios_base::app); t_registro reg = r.getRegistroASerializar(); bdfile.write((char*)®, Registro::getSize()); bdfile.close(); return SUCCESS; }
int BaseDeDatos::modificar(Registro& exist, const Registro& modif){ if (!this->existente(exist)){ return ERR_NO_EXISTE; } vector<Registro>::iterator it = currrentPosition; if (this->existente(modif) && !exist.compararPorNombre(modif)){ return ERR_DUPLICADO; } exist.llenarVacios(*it); Registro nuevo = modif; nuevo.llenarVacios(*it); /* Modificar en cache */ it = this->bufferRegistros.insert(it, nuevo); if ((it+1) != this->bufferRegistros.end()){ this->bufferRegistros.erase(it+1); } currrentPosition = it; /* Modificar en el archivo */ t_registro reg = nuevo.getRegistroASerializar(); t_registro rbusq; Registro busq; bool encontrado = false; fstream bdfile; bdfile.open (ARCHIVO_BASE, ios_base::out | ios_base::in | ios_base::binary); while(!bdfile.eof() && !encontrado){ bdfile.read((char*)&rbusq, Registro::getSize()); busq.crearDesdeRegistro(rbusq); if (exist.compararPorNombre(busq)){ bdfile.seekp(- Registro::getSize(), ios_base::cur); bdfile.write((char*)®, Registro::getSize()); encontrado = true; } } bdfile.close(); return SUCCESS; }