From: Ricardo Markiewicz Date: Sun, 11 Apr 2004 08:18:38 +0000 (+0000) Subject: * Agrego idx_get_count e idx_get_at para recuperar los registros X-Git-Tag: svn_import_r684~530 X-Git-Url: https://git.llucax.com/z.facultad/75.06/emufs.git/commitdiff_plain/357a3c6aefa500f808be203a13c3baa1c0e2e541 * Agrego idx_get_count e idx_get_at para recuperar los registros --- diff --git a/emufs/idx.c b/emufs/idx.c index 1ae3190..ae717e7 100644 --- a/emufs/idx.c +++ b/emufs/idx.c @@ -92,8 +92,8 @@ EMUFS_REG_ID emufs_idx_buscar_mayor_id_libre(EMUFS* emu, int* err) *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; } } @@ -129,9 +129,9 @@ EMUFS_BLOCK_ID emufs_idx_buscar_registro(EMUFS *emu, EMUFS_REG_ID reg_id) /* *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); @@ -140,7 +140,7 @@ EMUFS_BLOCK_ID emufs_idx_buscar_registro(EMUFS *emu, EMUFS_REG_ID reg_id) } /* 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; @@ -152,8 +152,8 @@ int emufs_idx_agregar(EMUFS *emu, EMUFS_REG_ID n_idreg, EMUFS_BLOCK_ID n_locatio 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; @@ -175,7 +175,7 @@ int emufs_idx_borrar(EMUFS *emu, EMUFS_REG_ID idreg) 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; } } @@ -221,3 +221,42 @@ EMUFS_REG_ID emufs_idx_get_new_id(EMUFS* efs, int* err) } return id; } + +unsigned int emufs_idx_get_count(EMUFS *emu) +{ + FILE *fp; + char name_f_idx[255]; + long tam; + + strcpy(name_f_idx,emu->nombre); + strcat(name_f_idx, EMUFS_IDX_EXT); + + fp = fopen(name_f_idx, "r"); + if (fp == NULL) return 0; + + fseek(fp, 0, SEEK_END); + tam = ftell(fp); + fclose(fp); + + return tam/sizeof(EMUFS_IDX); +} + +unsigned long emufs_idx_get_id_att(EMUFS *emu, unsigned int pos) +{ + FILE *fp; + char name_f_idx[255]; + EMUFS_IDX id; + + strcpy(name_f_idx,emu->nombre); + strcat(name_f_idx, EMUFS_IDX_EXT); + + fp = fopen(name_f_idx, "r"); + if (fp == NULL) return EMUFS_NOT_FOUND; + + fseek(fp, pos*sizeof(EMUFS_IDX), SEEK_SET); + fread(&id, sizeof(EMUFS_IDX), 1, fp); + fclose(fp); + + return id.id_reg; +} + diff --git a/emufs/idx.h b/emufs/idx.h index 40bf539..616df90 100644 --- a/emufs/idx.h +++ b/emufs/idx.h @@ -45,8 +45,8 @@ #define EMUFS_IDX_EXT ".idx" typedef struct emufs_idx_t { - unsigned long int n_idreg; - unsigned long int n_location; + unsigned long int id_reg; + unsigned long int location; } EMUFS_IDX; FILE* emufs_idx_abrir(EMUFS*, const char*); @@ -61,6 +61,10 @@ int emufs_idx_agregar(EMUFS*, EMUFS_BLOCK_ID, EMUFS_REG_ID); int emufs_idx_borrar(EMUFS*, EMUFS_REG_ID); +unsigned int emufs_idx_get_count(EMUFS *); + +unsigned long emufs_idx_get_id_att(EMUFS *, unsigned int pos); + EMUFS_REG_ID emufs_idx_get_new_id(EMUFS*, int*); #endif /* _EMUFS_IDX_H */