From 49b121630e9d2209696969851f27a0976226977a Mon Sep 17 00:00:00 2001 From: malaterre Date: Wed, 26 Oct 2005 20:06:51 +0000 Subject: [PATCH] ENH: Continue cleaning of open/write/seek ... --- src/gdcmmpeg2/src/mpeg2dec/mpeg2dec.c | 47 ++++++++++++++++++++------- src/gdcmmpeg2/src/mpeg2dec/mpeg2dec.h | 17 +++++++--- src/gdcmmpeg2/src/mpeg2dec/store.c | 32 +++++++++++------- src/gdcmmpeg2/src/mpeg2dec/subspic.c | 25 +++++++------- 4 files changed, 80 insertions(+), 41 deletions(-) diff --git a/src/gdcmmpeg2/src/mpeg2dec/mpeg2dec.c b/src/gdcmmpeg2/src/mpeg2dec/mpeg2dec.c index 9cf11c9e..ea6d1648 100644 --- a/src/gdcmmpeg2/src/mpeg2dec/mpeg2dec.c +++ b/src/gdcmmpeg2/src/mpeg2dec/mpeg2dec.c @@ -62,6 +62,7 @@ int my_open(char *filename) { return open(filename,O_RDONLY|O_BINARY); } + off_t my_seek(int infile, off_t offset,int whence) { return lseek(infile, offset, whence); @@ -75,34 +76,58 @@ int my_close(int infile) return close(infile); } -ostream *my_fopen(const char *path, const char *mode) +int my_fopenr(const char *path, const char *mode, istream *os) { FILE *fd = fopen(path, mode); - ostream *os = (ostream*)malloc(sizeof(ostream)); - os->Fd = fd; - return os; + if(fd) + { + os->InFd = fd; + return 1; //success + } + else + os->InFd = NULL; + return 0; +} + +int my_fopen(const char *path, const char *mode, ostream *os) +{ + FILE *fd = fopen(path, mode); + if(fd) + { + os->OutFd = fd; + return 1; //success + } + else + os->OutFd = NULL; + return 0; } int my_fseek(ostream *stream, long offset, int whence) { - return fseek(stream->Fd, offset, whence); + return fseek(stream->OutFd, offset, whence); +} +int my_fseekr(istream *stream, long offset, int whence) +{ + return fseek(stream->InFd, offset, whence); } -size_t my_fread(void *ptr, size_t size, size_t nmemb, ostream *stream) +size_t my_fread(void *ptr, size_t size, size_t nmemb, istream *stream) { - return fread(ptr, size, nmemb, stream->Fd); + return fread(ptr, size, nmemb, stream->InFd); } size_t my_fwrite(const void *ptr, size_t size, size_t nmemb, ostream *stream) { - return fwrite(ptr, size, nmemb, stream->Fd); + return fwrite(ptr, size, nmemb, stream->OutFd); } +int my_fcloser(istream *fp) +{ + return fclose(fp->InFd); +} int my_fclose(ostream *fp) { - FILE *fd = fp->Fd; - free(fp); - return fclose(fd); + return fclose(fp->OutFd); } #include diff --git a/src/gdcmmpeg2/src/mpeg2dec/mpeg2dec.h b/src/gdcmmpeg2/src/mpeg2dec/mpeg2dec.h index e8068678..6cf7ebe7 100644 --- a/src/gdcmmpeg2/src/mpeg2dec/mpeg2dec.h +++ b/src/gdcmmpeg2/src/mpeg2dec/mpeg2dec.h @@ -130,16 +130,25 @@ #include typedef struct { - FILE* Fd; + FILE* InFd; +} istream; +typedef struct +{ + FILE* OutFd; } ostream; -int my_open(char *filename); +//int my_open(char *filename); int my_printf(const char *format, ...); int my_fprintf(const char *format, ...); int my_sprintf(char *str, const char *format, ...); void my_exit(int status); -ostream *my_fopen(const char *path, const char *mode); +//ostream *my_fopen(const char *path, const char *mode); +int my_fopenr(const char *path, const char *mode, istream *os); +int my_fopen(const char *path, const char *mode, ostream *os); int my_fseek(ostream *stream, long offset, int whence); -size_t my_fread(void *ptr, size_t size, size_t nmemb, ostream *stream); +int my_fseekr(istream *stream, long offset, int whence); +size_t my_fread(void *ptr, size_t size, size_t nmemb, istream *stream); +size_t my_fwrite(const void *ptr, size_t size, size_t nmemb, ostream *stream); int my_fclose(ostream *fp); +int my_fcloser(istream *fp); diff --git a/src/gdcmmpeg2/src/mpeg2dec/store.c b/src/gdcmmpeg2/src/mpeg2dec/store.c index 7bc9906b..db1f0391 100644 --- a/src/gdcmmpeg2/src/mpeg2dec/store.c +++ b/src/gdcmmpeg2/src/mpeg2dec/store.c @@ -28,7 +28,6 @@ */ #include /* for malloc */ -#include #include /* for strcat */ #include "config.h" @@ -53,7 +52,7 @@ static void conv420to422 _ANSI_ARGS_((unsigned char *src, unsigned char *dst)); #define OBFRSIZE 4096 static unsigned char obfr[OBFRSIZE]; static unsigned char *optr; -static int outfile; +static ostream *outfile; /* * store a picture as either one frame or two fields @@ -157,7 +156,10 @@ int offset,incr,width,height; if (!Quiet_Flag) my_fprintf("saving %s\n",name); - if ((outfile = open(name,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1) + //if ((outfile = open(name,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1) + ostream file; + outfile = &file; + if(!my_fopen(name, "wb", outfile)) { my_sprintf(Error_Text,"Couldn't create %s\n",name); Error(Error_Text); @@ -173,9 +175,9 @@ int offset,incr,width,height; } if (optr!=obfr) - write(outfile,obfr,optr-obfr); + my_fwrite(obfr,optr-obfr,1,outfile); - close(outfile); + my_fclose(outfile); } /* @@ -219,7 +221,10 @@ int offset, incr, height; if (!Quiet_Flag) my_fprintf("saving %s\n",outname); - if ((outfile = open(outname,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1) + //if ((outfile = open(outname,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1) + ostream file; + outfile = &file; + if(!my_fopen(outname, "wb", outfile)) { my_sprintf(Error_Text,"Couldn't create %s\n",outname); Error(Error_Text); @@ -243,9 +248,9 @@ int offset, incr, height; } if (optr!=obfr) - write(outfile,obfr,optr-obfr); + my_fwrite(obfr,optr-obfr,1,outfile); - close(outfile); + my_fclose(outfile); } /* @@ -312,7 +317,10 @@ int tgaflag; if (!Quiet_Flag) my_fprintf("saving %s\n",outname); - if ((outfile = open(outname,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1) + //if ((outfile = open(outname,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1) + ostream file; + outfile = &file; + if(! my_fopen(outname, "wb", outfile)) { my_sprintf(Error_Text,"Couldn't create %s\n",outname); Error(Error_Text); @@ -371,9 +379,9 @@ int tgaflag; } if (optr!=obfr) - write(outfile,obfr,optr-obfr); + my_fwrite(obfr,optr-obfr,1,outfile); - close(outfile); + my_fclose(outfile); } static void putbyte(c) @@ -383,7 +391,7 @@ int c; if (optr == obfr+OBFRSIZE) { - write(outfile,obfr,OBFRSIZE); + my_fwrite(obfr,OBFRSIZE,1,outfile); optr = obfr; } } diff --git a/src/gdcmmpeg2/src/mpeg2dec/subspic.c b/src/gdcmmpeg2/src/mpeg2dec/subspic.c index 702b9beb..a9b8d515 100644 --- a/src/gdcmmpeg2/src/mpeg2dec/subspic.c +++ b/src/gdcmmpeg2/src/mpeg2dec/subspic.c @@ -27,8 +27,6 @@ * */ -//#include - #include "config.h" #include "global.h" @@ -243,7 +241,7 @@ int Height; { int Size; int Bytes_Read; - int Infile; + istream Infile; Size = Width*Height; @@ -251,14 +249,14 @@ int Height; my_printf("SUBS: reading %s\n", filename); #endif - if(!(Infile=my_open(Filename))<0) + if(!my_fopenr(Filename, "rb", &Infile)) { my_printf("ERROR: unable to open reference filename (%s)\n", Filename); return(-1); } /*abort();*/ - Bytes_Read = read(Infile, Frame, Size); + Bytes_Read = my_fread(Frame, Size, 1, &Infile); if(Bytes_Read!=Size) { @@ -266,7 +264,7 @@ int Height; Bytes_Read, Size, Filename); } - close(Infile); + my_fcloser(&Infile); return(0); } @@ -283,14 +281,13 @@ unsigned char *frame[3]; int framenum; { /* int err = 0; */ - //FILE *fd; - ostream *fd; + istream fd; int line; int size, offset; /*abort();*/ - if (!(fd = my_fopen(filename,"rb"))) + if (!my_fopenr(filename,"rb", &fd)) { my_sprintf(Error_Text,"Couldn't open %s\n",filename); return(-1); @@ -320,28 +317,28 @@ int framenum; /* seek to location in big file where desired frame begins */ /* note: this offset cannot exceed a few billion bytes due to the */ /* obvious limitations of 32-bit integers */ - my_fseek(fd, offset, SEEK_SET); + my_fseekr(&fd, offset, SEEK_SET); /* Y */ for (line=0; line