X-Git-Url: https://git.llucax.com/z.facultad/75.06/jacu.git/blobdiff_plain/34b3f0f225a6f51a548576c37a30671052ef5dd2..bd0062922151710408edae80d0f67db20114c6c9:/src/vfile/vfile.c diff --git a/src/vfile/vfile.c b/src/vfile/vfile.c index 444deb2..469ef76 100644 --- a/src/vfile/vfile.c +++ b/src/vfile/vfile.c @@ -98,6 +98,11 @@ int vfclose(VFILE* vfp) return ret; } +int vfeof(VFILE* vfp) +{ + return vfp->lastvol && feof(vfp->fp); +} + int vfgetc(VFILE* vfp) { int c; @@ -127,20 +132,29 @@ int vfputc(int c, VFILE* vfp) size_t vfread(void* ptr, size_t size, size_t nmemb, VFILE* vfp) { - int i = 0; + int c; + size_t i = 0; size_t total = size * nmemb; /* leo uno a uno y si hay error salgo. */ - while (i < total && (((char*)ptr)[i++] = vfgetc(vfp)) != EOF); - return i % size; + while (i < total) + { + if ((c = vfgetc(vfp)) == EOF) + { + PERR("vfread: EOF"); + break; + } + else ((char*)ptr)[i++] = c; + } + return i / size; } size_t vfwrite(const void *ptr, size_t size, size_t nmemb, VFILE* vfp) { - int i = 0; + size_t i = 0; size_t total = size * nmemb; /* escribo uno a uno y si hay error salgo. */ while (i < total && (vfputc(((char*)ptr)[i++], vfp)) != EOF); - return i % size; + return i / size; } int vfvol_close(VFILE* vfp) @@ -201,3 +215,54 @@ int vfvol_open_next(VFILE* vfp) return 0; } +long vfsize(const char* path) +{ + VFILE* vfp = vfopen(path, "r", 0); + long size; + if (!vfp) return -1; /* error */ + if (fseek(vfp->fp, 0l, SEEK_END) == -1) + { + vfclose(vfp); + return -1; /* error */ + } + else + { + size = ftell(vfp->fp); + if (size == -1) + { + vfclose(vfp); + return -1; /* error */ + } + } + while (!vfp->lastvol) /* mientras no sea el Ășltimo volumen */ + { + if (vfvol_open_next(vfp)) + { + vfclose(vfp); + return -1; /* error */ + } + if (fseek(vfp->fp, 0l, SEEK_END) == -1) + { + vfclose(vfp); + return -1; /* error */ + } + else + { + long curr_size = ftell(vfp->fp); + if (curr_size == -1) + { + vfclose(vfp); + return -1; /* error */ + } + size += curr_size; + } + if (vfvol_close(vfp)) + { + vfclose(vfp); + return -1; /* error */ + } + } + vfclose(vfp); + return size; +} +