[HEADERS]
[reactos.git] / irc / ArchBlackmann / File.h
1 // File.h
2 // (C) 2002-2004 Royce Mitchell III
3 // Dually licensed under BSD & LGPL
4
5 #ifndef FILE_H
6 #define FILE_H
7
8 #ifdef WIN32
9 # include <stdio.h> // fgetc
10 # include <io.h>
11 #elif defined(UNIX)
12 # include <sys/stat.h>
13 # include <unistd.h>
14 #endif
15 #include <assert.h>
16 #include <string>
17
18 class File
19 {
20 public:
21 #ifdef WIN32
22 typedef __int64 fileoff_t;
23 typedef unsigned __int64 filesize_t;
24 #else//_MSC_VER
25 typedef __off64_t fileoff_t;
26 typedef __size64_t filesize_t;
27 #endif//_MSC_VER
28
29 File() : _f(0)
30 {
31 }
32
33 File ( const std::string& filename, const char* mode ) : _f(0)
34 {
35 open ( filename, mode );
36 }
37
38 File ( const std::wstring& filename, const wchar_t* mode ) : _f(0)
39 {
40 open ( filename, mode );
41 }
42
43 File ( const char* filename, const char* mode ) : _f(0)
44 {
45 open ( filename, mode );
46 }
47
48 File ( const wchar_t* filename, const wchar_t* mode ) : _f(0)
49 {
50 open ( filename, mode );
51 }
52
53 ~File()
54 {
55 close();
56 }
57
58 bool open ( const std::string& filename, const char* mode )
59 {
60 assert ( !_f );
61 return ( _f = fopen ( filename.c_str(), mode ) ) != 0;
62 }
63
64 bool open ( const std::wstring& filename, const wchar_t* mode )
65 {
66 assert ( !_f );
67 return ( _f = _wfopen ( filename.c_str(), mode ) ) != 0;
68 }
69
70 bool open ( const char* filename, const char* mode )
71 {
72 assert ( !_f );
73 return ( _f = fopen ( filename, mode ) ) != 0;
74 }
75
76 bool open ( const wchar_t* filename, const wchar_t* mode )
77 {
78 assert ( !_f );
79 return ( _f = _wfopen ( filename, mode ) ) != 0;
80 }
81
82 fileoff_t seek ( fileoff_t offset );
83
84 int get()
85 {
86 return fgetc ( _f );
87 }
88
89 bool put ( int c )
90 {
91 return fputc ( c, _f ) != EOF;
92 }
93
94 std::string getline ( bool strip_crlf = false );
95 std::wstring wgetline ( bool strip_crlf = false );
96
97 // this function searches for the next end-of-line and puts all data it
98 // finds until then in the 'line' parameter.
99 //
100 // call continuously until the function returns false ( no more data )
101 bool next_line ( std::string& line, bool strip_crlf );
102 bool next_line ( std::wstring& line, bool strip_crlf );
103
104 bool read ( void* data, unsigned len )
105 {
106 return len == fread ( data, 1, len, _f );
107 }
108
109 bool write ( const void* data, unsigned len )
110 {
111 return len == fwrite ( data, 1, len, _f );
112 }
113
114 bool write ( const std::string& data )
115 {
116 return data.length() == fwrite ( data.c_str(), 1, data.length(), _f );
117 }
118
119 bool write ( const std::wstring& data )
120 {
121 return data.length() == fwrite ( data.c_str(), sizeof(data[0]), data.length(), _f );
122 }
123
124 filesize_t length() const;
125
126 void close();
127
128 bool isopened() const
129 {
130 return _f != 0;
131 }
132
133 bool is_open() const
134 {
135 return _f != 0;
136 }
137
138 bool eof() const
139 {
140 return feof(_f) ? true : false;
141 }
142
143 FILE* operator * ()
144 {
145 return _f;
146 }
147
148 operator FILE*()
149 {
150 return _f;
151 }
152
153 void printf ( const char* fmt, ... );
154 void printf ( const wchar_t* fmt, ... );
155
156 static bool LoadIntoString ( std::string& s, const std::string& filename );
157 static bool LoadIntoString ( std::string& s, const std::wstring& filename );
158 static bool SaveFromString ( const std::string& filename, const std::string& s, bool binary );
159 static bool SaveFromString ( const std::wstring& filename, const std::string& s, bool binary );
160 static bool SaveFromBuffer ( const std::string& filename, const char* buf, size_t buflen, bool binary );
161 static bool SaveFromBuffer ( const std::wstring& filename, const char* buf, size_t buflen, bool binary );
162
163 static std::string TempFileName ( const std::string& prefix );
164 static std::wstring TempFileName ( const std::wstring& prefix );
165
166 private:
167 File(const File&) {}
168 const File& operator = ( const File& ) { return *this; }
169
170 FILE * _f;
171 };
172
173 #endif//FILE_H