]> Creatis software - clitk.git/blob - utilities/CxImage/xiofile.h
cosmetic
[clitk.git] / utilities / CxImage / xiofile.h
1 #if !defined(__xiofile_h)
2 #define __xiofile_h
3
4 #include "xfile.h"
5 //#include <TCHAR.h>
6
7 class DLL_EXP CxIOFile : public CxFile
8         {
9 public:
10         CxIOFile(FILE* fp = NULL)
11         {
12                 m_fp = fp;
13                 m_bCloseFile = (bool)(fp==0);
14         }
15
16         ~CxIOFile()
17         {
18                 Close();
19         }
20 //////////////////////////////////////////////////////////
21         bool Open(LPCTSTR filename, LPCTSTR mode)
22         {
23                 if (m_fp) return false; // Can't re-open without closing first
24
25                 m_fp = _tfopen(filename, mode);
26                 if (!m_fp) return false;
27
28                 m_bCloseFile = true;
29
30                 return true;
31         }
32 //////////////////////////////////////////////////////////
33         virtual bool Close()
34         {
35                 int iErr = 0;
36                 if ( (m_fp) && (m_bCloseFile) ){ 
37                         iErr = fclose(m_fp);
38                         m_fp = NULL;
39                 }
40                 return (bool)(iErr==0);
41         }
42 //////////////////////////////////////////////////////////
43         virtual size_t  Read(void *buffer, size_t size, size_t count)
44         {
45                 if (!m_fp) return 0;
46                 return fread(buffer, size, count, m_fp);
47         }
48 //////////////////////////////////////////////////////////
49         virtual size_t  Write(const void *buffer, size_t size, size_t count)
50         {
51                 if (!m_fp) return 0;
52                 return fwrite(buffer, size, count, m_fp);
53         }
54 //////////////////////////////////////////////////////////
55         virtual bool Seek(long offset, int origin)
56         {
57                 if (!m_fp) return false;
58                 return (bool)(fseek(m_fp, offset, origin) == 0);
59         }
60 //////////////////////////////////////////////////////////
61         virtual long Tell()
62         {
63                 if (!m_fp) return 0;
64                 return ftell(m_fp);
65         }
66 //////////////////////////////////////////////////////////
67         virtual long    Size()
68         {
69                 if (!m_fp) return -1;
70                 long pos,size;
71                 pos = ftell(m_fp);
72                 fseek(m_fp, 0, SEEK_END);
73                 size = ftell(m_fp);
74                 fseek(m_fp, pos,SEEK_SET);
75                 return size;
76         }
77 //////////////////////////////////////////////////////////
78         virtual bool    Flush()
79         {
80                 if (!m_fp) return false;
81                 return (bool)(fflush(m_fp) == 0);
82         }
83 //////////////////////////////////////////////////////////
84         virtual bool    Eof()
85         {
86                 if (!m_fp) return true;
87                 return (bool)(feof(m_fp) != 0);
88         }
89 //////////////////////////////////////////////////////////
90         virtual long    Error()
91         {
92                 if (!m_fp) return -1;
93                 return ferror(m_fp);
94         }
95 //////////////////////////////////////////////////////////
96         virtual bool PutC(unsigned char c)
97         {
98                 if (!m_fp) return false;
99                 return (bool)(fputc(c, m_fp) == c);
100         }
101 //////////////////////////////////////////////////////////
102         virtual long    GetC()
103         {
104                 if (!m_fp) return EOF;
105                 return getc(m_fp);
106         }
107 //////////////////////////////////////////////////////////
108         virtual char *  GetS(char *string, int n)
109         {
110                 if (!m_fp) return NULL;
111                 return fgets(string,n,m_fp);
112         }
113 //////////////////////////////////////////////////////////
114         virtual long    Scanf(const char *format, void* output)
115         {
116                 if (!m_fp) return EOF;
117                 return fscanf(m_fp, format, output);
118         }
119 //////////////////////////////////////////////////////////
120 protected:
121         FILE *m_fp;
122         bool m_bCloseFile;
123         };
124
125 #endif