From: Leandro Lucarella Date: Sun, 30 May 2004 07:58:15 +0000 (+0000) Subject: Un poco de orden: X-Git-Tag: svn_import_r684~80 X-Git-Url: https://git.llucax.com/z.facultad/75.06/emufs.git/commitdiff_plain/6a1699c5c4c2ba05db94118fe15188372ab7d37e?ds=sidebyside Un poco de orden: - Se separa el MERGEFILE. - Se renombra el archivo de prueba. --- diff --git a/emufs/external_sort/Makefile b/emufs/external_sort/Makefile index 33ccead..7cc6c15 100644 --- a/emufs/external_sort/Makefile +++ b/emufs/external_sort/Makefile @@ -9,7 +9,7 @@ ################ # Nombre del ejecutable. -TARGETS = s_ext bufford_test bufford_test_sort +TARGETS = s_ext bufford_test sort_test # Opciones para el compilador C. CFLAGS = -Wall -ggdb -DDEBUG @@ -25,7 +25,7 @@ CXXFLAGS = $(CFLAGS) -fno-inline all: $(TARGETS) bufford_test: bufford.o bufford_test.o -bufford_test_sort: bufford.o bufford_test_sort.o +sort_test: bufford.o mergefile.o sort_test.o clean: @$(RM) -fv *.o $(TARGETS) diff --git a/emufs/external_sort/mergefile.c b/emufs/external_sort/mergefile.c new file mode 100644 index 0000000..2d59fb1 --- /dev/null +++ b/emufs/external_sort/mergefile.c @@ -0,0 +1,129 @@ +/* vim: set noexpandtab tabstop=4 shiftwidth=4 wrap: + *---------------------------------------------------------------------------- + * 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: dom may 30 04:54:53 ART 2004 + * Autores: Leandro Lucarella + *---------------------------------------------------------------------------- + * + * $Id: tipo1.c 548 2004-05-28 22:44:27Z llucare $ + * + */ + +/** \file + * + * Archivo temporal con un fragmento ordenado del archivo original. + * + * Implementación de un archivo temporal con un fragmento ordenado del archivo + * original utilizado para hacer el ordenamiento por fusión (merge sort). + * + */ + +#include "mergefile.h" +#include +#include +#include +#include + +char* mergefile_makefilename(int i) +{ + size_t size = sizeof(MERGEFILE_TEMPLATE) + 8; /* 8 más para el int */ + char* filename = malloc(size); + if (!filename) return 0; + /* Hasta que me de la memoria para crear el nombre del archivo */ + while (snprintf(filename, size, MERGEFILE_TEMPLATE, i) >= size) { + char* new; + size += 8; + new = realloc(filename, size); + if (new) { + filename = new; + } else { + free(filename); + return 0; + } + } + return filename; +} + +MERGEFILE* mergefile_new(int i) +{ + MERGEFILE* mf = malloc(sizeof(MERGEFILE)); + if (!mf) { + return 0; + } + /* asigno el nombre de archivo */ + if (!(mf->filename = mergefile_makefilename(i))) { + free(mf); + return 0; + } + /* abre archivo */ + if (!(mf->fp = fopen(mf->filename, "rb"))) { + free(mf->filename); + free(mf); + return 0; + } + /* obtiene dato */ + if (fscanf(mf->fp, "%i", &(mf->next)) <= 0) { + fclose(mf->fp); + free(mf->filename); + free(mf); + return 0; + } + mf->more = 1; + return mf; +} + +void mergefile_delete(MERGEFILE* mf) +{ + assert(mf); + assert(mf->fp); + assert(mf->filename); + fclose(mf->fp); + remove(mf->filename); + free(mf->filename); + free(mf); +} + +int mergefile_peek_next(MERGEFILE* mf) +{ + assert(mf); + assert(mf->fp); + assert(mf->more); + return mf->next; +} + +int mergefile_pop_next(MERGEFILE* mf) +{ + int ret; + assert(mf); + assert(mf->fp); + assert(mf->more); + ret = mf->next; + /* obtiene dato, si no hay más, se activa el flag */ + if (fscanf(mf->fp, "%i", &(mf->next)) <= 0) mf->more = 0; + return ret; +} + +int mergefile_has_more(MERGEFILE* mf) +{ + assert(mf); + assert(mf->fp); + return mf->more; +} + diff --git a/emufs/external_sort/mergefile.h b/emufs/external_sort/mergefile.h new file mode 100644 index 0000000..1183d14 --- /dev/null +++ b/emufs/external_sort/mergefile.h @@ -0,0 +1,67 @@ +/* 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: dom may 30 04:57:18 ART 2004 + * Autores: Leandro Lucarella + *---------------------------------------------------------------------------- + * + * $Id: tipo1.h 542 2004-05-28 19:45:02Z rmarkie $ + * + */ + +/** \file + * + * Archivo temporal con un fragmento ordenado del archivo original. + * + * Interfaz de un archivo temporal con un fragmento ordenado del archivo + * original utilizado para hacer el ordenamiento por fusión (merge sort). + * + */ + +#ifndef _MERGEFILE_H_ +#define _MERGEFILE_H_ + +#include + +#define MERGEFILE_TEMPLATE "sorted_chunk.%i" + +typedef struct +{ + FILE* fp; + char* filename; + int next; + int more; +} +MERGEFILE; + +char* mergefile_makefilename(int i); + +MERGEFILE* mergefile_new(int i); + +void mergefile_delete(MERGEFILE* mf); + +int mergefile_peek_next(MERGEFILE* mf); + +int mergefile_pop_next(MERGEFILE* mf); + +int mergefile_has_more(MERGEFILE* mf); + +#endif /* _MERGEFILE_H_ */ + diff --git a/emufs/external_sort/bufford_test_sort.c b/emufs/external_sort/sort_test.c similarity index 63% rename from emufs/external_sort/bufford_test_sort.c rename to emufs/external_sort/sort_test.c index 81b87f7..65c653d 100644 --- a/emufs/external_sort/bufford_test_sort.c +++ b/emufs/external_sort/sort_test.c @@ -1,5 +1,6 @@ #include "bufford.h" +#include "mergefile.h" #include #include #include @@ -14,103 +15,6 @@ typedef struct RECORD; */ -#define MERGEFILE_TEMPLATE "sorted_chunk.%i" - -typedef struct -{ - FILE* fp; - char* filename; - int next; - int more; -} -MERGEFILE; - -char* mergefile_makefilename(int i) -{ - size_t size = sizeof(MERGEFILE_TEMPLATE) + 8; /* 8 más para el int */ - char* filename = malloc(size); - if (!filename) return 0; - /* Hasta que me de la memoria para crear el nombre del archivo */ - while (snprintf(filename, size, MERGEFILE_TEMPLATE, i) >= size) { - char* new; - size += 8; - new = realloc(filename, size); - if (new) { - filename = new; - } else { - free(filename); - return 0; - } - } - return filename; -} - -MERGEFILE* mergefile_new(int i) -{ - MERGEFILE* mf = malloc(sizeof(MERGEFILE)); - if (!mf) { - return 0; - } - /* asigno el nombre de archivo */ - if (!(mf->filename = mergefile_makefilename(i))) { - free(mf); - return 0; - } - /* abre archivo */ - if (!(mf->fp = fopen(mf->filename, "rb"))) { - free(mf->filename); - free(mf); - return 0; - } - /* obtiene dato */ - if (fscanf(mf->fp, "%i", &(mf->next)) <= 0) { - fclose(mf->fp); - free(mf->filename); - free(mf); - return 0; - } - mf->more = 1; - return mf; -} - -void mergefile_delete(MERGEFILE* mf) -{ - assert(mf); - assert(mf->fp); - assert(mf->filename); - fclose(mf->fp); - remove(mf->filename); - free(mf->filename); - free(mf); -} - -int mergefile_peek_next(MERGEFILE* mf) -{ - assert(mf); - assert(mf->fp); - assert(mf->more); - return mf->next; -} - -int mergefile_pop_next(MERGEFILE* mf) -{ - int ret; - assert(mf); - assert(mf->fp); - assert(mf->more); - ret = mf->next; - /* obtiene dato, si no hay más, se activa el flag */ - if (fscanf(mf->fp, "%i", &(mf->next)) <= 0) mf->more = 0; - return ret; -} - -int mergefile_has_more(MERGEFILE* mf) -{ - assert(mf); - assert(mf->fp); - return mf->more; -} - int cmp(void* x, void* y) { int xx = *(int*)x;