strcpy(name_f_idx, emu->nombre);
strcat(name_f_idx, EMUFS_IDX_EXT);
- (*err) = 0;
if ((f_idx = fopen(name_f_idx, "rb")) == NULL) {
PERR("No se puede abrir archivo");
*err = 4; /* EMUFS_ERROR_CANT_OPEN_FILE */
*err = 3; /* EMUFS_ERROR_FILE_READ */
return EMUFS_NOT_FOUND;
}
- if (reg.n_idreg >= max) {
- max = reg.n_idreg;
+ if (reg.id_reg >= max) {
+ max = reg.id_reg;
found = 1;
}
}
/* *err = 3; * EMUFS_ERROR_FILE_READ */
return EMUFS_NOT_FOUND;
}
- if (reg.n_idreg == reg_id) {
+ if (reg.id_reg == reg_id) {
fclose(f_idx);
- return reg.n_location;
+ return reg.location;
}
}
fclose(f_idx);
}
/* agrega un registro al final del archivo */
-int emufs_idx_agregar(EMUFS *emu, EMUFS_REG_ID n_idreg, EMUFS_BLOCK_ID n_location)
+int emufs_idx_agregar(EMUFS *emu, EMUFS_REG_ID id_reg, EMUFS_BLOCK_ID location)
{
FILE *f_idx;
EMUFS_IDX reg;
if ( (f_idx = fopen(name_f_idx,"a+"))==NULL ) return -1;
/* Note: Location = Bloque para Tipo 1 y 3, Offset para Tipo 2 */
- reg.n_idreg = n_idreg;
- reg.n_location = n_location;
+ reg.id_reg = id_reg;
+ reg.location = location;
fwrite(®,sizeof(EMUFS_IDX),1,f_idx);
fclose(f_idx);
return 0;
FILE *f_idx;
EMUFS_IDX reg;
char name_f_idx[255];
- long actual, final, cant, i, tam, a;
+ long actual, final, cant, i, tam;
strcpy(name_f_idx,emu->nombre);
strcat(name_f_idx, EMUFS_IDX_EXT);
while ( !feof(f_idx) ){
/*busco cual tengo que borrar*/
if ( fread(®, sizeof(EMUFS_IDX), 1, f_idx) != 1 ) continue;
- if ( reg.n_idreg == idreg ) {
+ if ( reg.id_reg == idreg ) {
break;
}
}
cant = (final-actual)/sizeof(EMUFS_IDX);
/*apunto al siguiente del que quiero borrar*/
/*leo todos los que quedan*/
- for(i=0; i<cant-1; i++) {
+ for(i=0; i<cant; i++) {
final = actual + sizeof(EMUFS_IDX);
fseek(f_idx, final, SEEK_SET);
fread(®, sizeof(EMUFS_IDX), 1, f_idx);
{
EMUFS_REG_ID id;
- (*err) = 0;
id = emufs_did_get_last(efs, err);
if (id == EMUFS_NOT_FOUND) {
+ if (*err) {
+ PERR("error al obtener ultimo id");
+ return id;
+ }
id = emufs_idx_buscar_mayor_id_libre(efs, err);
if (*err) {
PERR("error al obtener id mayor");
return id;
}
+EMUFS_REG_ID emufs_idx_get_count(EMUFS *emu)
+{
+ FILE *fp;
+ char name_f_idx[255];
+ EMUFS_REG_ID tam;
+
+ strcpy(name_f_idx,emu->nombre);
+ strcat(name_f_idx, EMUFS_IDX_EXT);
+
+ if ( (fp = fopen(name_f_idx, "rb"))==NULL){
+ PERR("No se pudo abrir el archvo");
+ return -1;
+ }
+
+ fseek(fp, 0l, SEEK_END);
+ tam = ftell(fp);
+ fclose(fp);
+
+ return tam/sizeof(EMUFS_IDX);
+}
+
+EMUFS_REG_ID emufs_idx_get_id_at(EMUFS *emu, long pos)
+{
+ FILE *fp;
+ char name_f_idx[255];
+ EMUFS_IDX id;
+
+ strcpy(name_f_idx,emu->nombre);
+ strcat(name_f_idx, EMUFS_IDX_EXT);
+
+ if ( (fp = fopen(name_f_idx, "rb")) == NULL){
+ PERR("No se pudo abrir el archivo");
+ return -1;
+ }
+
+ fseek(fp, pos*sizeof(EMUFS_IDX), SEEK_SET);
+ fread(&id, sizeof(EMUFS_IDX), 1, fp);
+ fclose(fp);
+
+ return id.id_reg;
+}
+
+int emufs_idx_existe_id(EMUFS *emu, int ID)
+{
+ FILE *fp;
+ char name_f_idx[255];
+ EMUFS_IDX id;
+
+ strcpy(name_f_idx,emu->nombre);
+ strcat(name_f_idx, EMUFS_IDX_EXT);
+
+ if ( (fp = fopen(name_f_idx, "rb")) == NULL){
+ PERR("No se pudo abrir el archivo");
+ return -1;
+ }
+
+ while ( !feof(fp) ){
+ fread(&id, sizeof(EMUFS_IDX), 1, fp);
+ if ( id.id_reg == ID ){
+ fclose(fp);
+ return 0;
+ }
+ }
+ fclose(fp);
+ return -1;
+}