+/* vim: set noexpandtab tabstop=4 shiftwidth=4:
+ *----------------------------------------------------------------------------
+ * emufs
+ *----------------------------------------------------------------------------
+ * This file is part of emufs.
+ *
+ * emufs is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation; either version 2 of the License, or (at your option) any later
+ * version.
+ *
+ * emufs is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with emufs; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ *----------------------------------------------------------------------------
+ * Creado: jue abr 8 18:10:35 ART 2004
+ * Autores: Nicolás Dimov <sagardua@uolsinectis.com.ar>
+ *----------------------------------------------------------------------------
+ *
+ * $Id$
+ *
+ */
+
+/** \file
+ *
+ * Manejo de archivos de índice de registros.
+ *
+ * Implementación del manejo de archivos de índice de registros.
+ *
+ */
+
#include "idx.h"
-#include "tipo3.h"
+#include <stdlib.h>
+#include <strings.h>
+
+FILE* emufs_idx_abrir(EMUFS* efs, const char* mode)
+{
+ FILE* f;
+ char* filename;
+
+ filename = (char*) malloc(sizeof(char) * (strlen(efs->nombre)
+ + strlen(EMUFS_IDX_EXT) + 1));
+ if (filename == NULL) {
+ /* TODO Manejo de errores */
+ return NULL;
+ }
+ strcpy(filename, efs->nombre);
+ strcat(filename, EMUFS_IDX_EXT);
+ f = fopen(filename, mode);
+ free(filename);
+ return f;
+}
int emufs_idx_buscar_mayor_id(EMUFS *emu)
{
int id, max = -1;
FILE *f_idx;
- BLOCK_REG_T reg;
- char name_f_idx[255];
+ EMUFS_IDX reg;
+ char name_f_idx[255]; /* TODO usar malloc para no limitar el tamaño de nombre de archivo */
strcpy(name_f_idx,emu->nombre);
- strcat(name_f_idx,".idx");
+ strcat(name_f_idx, EMUFS_IDX_EXT);
if ( (f_idx = fopen(name_f_idx,"r")) == NULL) return -1; /*ERROR*/
+ id = -1;
while ( !feof(f_idx) ){
/* Me aseguro de leer la cantidad de bytes correcta */
- if (fread(®,sizeof(BLOCK_REG_T),1,f_idx) != 1) continue;
+ if (fread(®,sizeof(EMUFS_IDX),1,f_idx) != 1) continue;
if ( reg.id_reg >= max )
max = reg.id_reg;
}
return id;
}
+
+/*busca el registro ID en el archivo ".idx" y devuelve el nro de bloque en el que se encuentra*/
+int emufs_idx_buscar_registro(EMUFS *emu, int ID)
+{
+ FILE* f_idx;
+ EMUFS_IDX reg;
+ char name_f_idx[255];
+ strcpy(name_f_idx,emu->nombre);
+ strcat(name_f_idx, EMUFS_IDX_EXT);
+
+ if ( (f_idx = fopen(name_f_idx,"r")) == NULL) return -1; /*ERROR*/
+ reg.id_reg = -1;
+ reg.block = -1;
+ while ( !feof(f_idx) ){
+ if (fread(®,sizeof(EMUFS_IDX),1,f_idx) != 1) continue;
+ if ( reg.id_reg == ID ){
+ break;
+ }
+ }
+
+ fclose(f_idx);
+ return reg.block;
+}
+
+/* agrega un registro al final del archivo */
+int emufs_idx_agregar(EMUFS *emu, int num_bloque, int ID_aux)
+{
+ FILE *f_idx;
+ EMUFS_IDX reg;
+ char name_f_idx[255];
+
+ strcpy(name_f_idx,emu->nombre);
+ strcat(name_f_idx, EMUFS_IDX_EXT);
+
+ if ( (f_idx = fopen(name_f_idx,"a+"))==NULL ) return -1;
+
+ reg.block = num_bloque;
+ reg.id_reg = ID_aux;
+ fwrite(®,sizeof(EMUFS_IDX),1,f_idx);
+ fclose(f_idx);
+ return 0;
+}
+