X-Git-Url: https://git.llucax.com/z.facultad/75.06/emufs.git/blobdiff_plain/357a3c6aefa500f808be203a13c3baa1c0e2e541..c4b24cdb8c9c9c83ad2b4cad26560b3487d15bd7:/emufs/idx.c diff --git a/emufs/idx.c b/emufs/idx.c index ae717e7..7e48545 100644 --- a/emufs/idx.c +++ b/emufs/idx.c @@ -78,7 +78,6 @@ EMUFS_REG_ID emufs_idx_buscar_mayor_id_libre(EMUFS* emu, int* err) 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 */ @@ -210,9 +209,12 @@ EMUFS_REG_ID emufs_idx_get_new_id(EMUFS* efs, int* err) { 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"); @@ -222,26 +224,28 @@ EMUFS_REG_ID emufs_idx_get_new_id(EMUFS* efs, int* err) return id; } -unsigned int emufs_idx_get_count(EMUFS *emu) +EMUFS_REG_ID emufs_idx_get_count(EMUFS *emu) { FILE *fp; char name_f_idx[255]; - long tam; + EMUFS_REG_ID 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; + if ( (fp = fopen(name_f_idx, "rb"))==NULL){ + PERR("No se pudo abrir el archvo"); + return -1; + } - fseek(fp, 0, SEEK_END); + fseek(fp, 0l, SEEK_END); tam = ftell(fp); fclose(fp); return tam/sizeof(EMUFS_IDX); } -unsigned long emufs_idx_get_id_att(EMUFS *emu, unsigned int pos) +EMUFS_REG_ID emufs_idx_get_id_at(EMUFS *emu, long pos) { FILE *fp; char name_f_idx[255]; @@ -250,8 +254,10 @@ unsigned long emufs_idx_get_id_att(EMUFS *emu, unsigned int pos) 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; + 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); @@ -260,3 +266,27 @@ unsigned long emufs_idx_get_id_att(EMUFS *emu, unsigned int pos) 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; +}