]> Creatis software - gdcm.git/commitdiff
ENH: Continue cleaning of open/write/seek ...
authormalaterre <malaterre>
Wed, 26 Oct 2005 20:06:51 +0000 (20:06 +0000)
committermalaterre <malaterre>
Wed, 26 Oct 2005 20:06:51 +0000 (20:06 +0000)
src/gdcmmpeg2/src/mpeg2dec/mpeg2dec.c
src/gdcmmpeg2/src/mpeg2dec/mpeg2dec.h
src/gdcmmpeg2/src/mpeg2dec/store.c
src/gdcmmpeg2/src/mpeg2dec/subspic.c

index 9cf11c9ec8915d976203fc5a11308e9f1e1366f9..ea6d164810c138eab19c098ba5154e2fae238ea4 100644 (file)
@@ -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 <stdarg.h>
 
index e80686780ae4b5a1576f2d0fe427af9edbf273b9..6cf7ebe741fb63a41667ea8380fc9bed3f697078 100644 (file)
 #include <stdio.h>
 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);
 
index 7bc9906bb6f32dd674b073e9d9117d18b3ad765d..db1f039184355c0d37f6c62acd1da410396f64eb 100644 (file)
@@ -28,7 +28,6 @@
  */
 
 #include <stdlib.h> /* for malloc */
-#include <fcntl.h>
 #include <string.h> /* 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;
   }
 }
index 702b9beb94c2b624121ea847ebcf6c8f563f75fc..a9b8d515867add5361d913e714a23bf32db0a8e9 100644 (file)
@@ -27,8 +27,6 @@
  *
  */
 
-//#include <fcntl.h>
-
 #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<Coded_Picture_Height; line++)
   {
-    my_fread(frame[0]+(line*Coded_Picture_Width),1,Coded_Picture_Width,fd);
+    my_fread(frame[0]+(line*Coded_Picture_Width),1,Coded_Picture_Width,&fd);
   }
 
   /* Cb */
   for (line=0; line<Chroma_Height; line++)
   {
-    my_fread(frame[1]+(line*Chroma_Width),1,Chroma_Width,fd);
+    my_fread(frame[1]+(line*Chroma_Width),1,Chroma_Width,&fd);
   }
 
   /* Cr */
   for (line=0; line<Chroma_Height; line++)
   {
-    my_fread(frame[2]+(line*Chroma_Width),1,Chroma_Width,fd);
+    my_fread(frame[2]+(line*Chroma_Width),1,Chroma_Width,&fd);
   }
 
 
-  my_fclose(fd);
+  my_fcloser(&fd);
   return(0);
 }