ReactOS  0.4.12-dev-934-g9a4676f
File.h
Go to the documentation of this file.
1 // File.h
2 // This file is (C) 2002-2003 Royce Mitchell III and released under the BSD license
3 
4 #ifndef FILE_H
5 #define FILE_H
6 
7 #ifdef WIN32
8 # include <io.h>
9 #elif defined(UNIX)
10 # include <sys/stat.h>
11 # include <unistd.h>
12 #endif
13 #include <string>
14 
15 class File
16 {
17 public:
18  File() : _f(0)
19  {
20  }
21 
22  File ( const char* filename, const char* mode ) : _f(0)
23  {
24  open ( filename, mode );
25  }
26 
28  {
29  close();
30  }
31 
32  bool open ( const char* filename, const char* mode );
33 
34  bool seek ( long offset )
35  {
36  return !fseek ( _f, offset, SEEK_SET );
37  }
38 
39  int get()
40  {
41  return fgetc ( _f );
42  }
43 
44  bool put ( int c )
45  {
46  return fputc ( c, _f ) != EOF;
47  }
48 
49  std::string getline ( bool strip_crlf = false );
50 
51  // this function searches for the next end-of-line and puts all data it
52  // finds until then in the 'line' parameter.
53  //
54  // call continuously until the function returns false ( no more data )
55  bool next_line ( std::string& line, bool strip_crlf );
56 
57  /*
58  example usage:
59 
60  bool mycallback ( const std::string& line, int line_number, long lparam )
61  {
62  std::cout << line << std::endl;
63  return true; // continue enumeration
64  }
65 
66  File f ( "file.txt", "rb" ); // open file for binary read-only ( i.e. "rb" )
67  f.enum_lines ( mycallback, 0, true );
68  */
69 
70  bool enum_lines ( bool (*callback)(const std::string& line, int line_number, long lparam), long lparam, bool strip_crlf );
71 
72  bool read ( void* data, unsigned len )
73  {
74  return len == fread ( data, 1, len, _f );
75  }
76 
77  bool write ( const void* data, unsigned len )
78  {
79  return len == fwrite ( data, 1, len, _f );
80  }
81 
82  size_t length();
83 
84  void close();
85 
86  bool isopened()
87  {
88  return _f != 0;
89  }
90 
91  bool eof()
92  {
93  return feof(_f) ? true : false;
94  }
95 
97  {
98  return _f;
99  }
100 
101  static bool LoadIntoString ( std::string& s, const char* filename );
102  static bool SaveFromString ( const char* filename, const std::string& s, bool binary );
103 
104 private:
105  File(const File&) {}
106  const File& operator = ( const File& ) { return *this; }
107 
108  FILE * _f;
109 };
110 
111 #endif//FILE_H
_Check_return_opt_ _CRTIMP int __cdecl fputc(_In_ int _Ch, _Inout_ FILE *_File)
bool enum_lines(bool(*callback)(const std::string &line, int line_number, long lparam), long lparam, bool strip_crlf)
Definition: File.cpp:69
int get()
Definition: File.h:39
GLintptr offset
Definition: glext.h:5920
size_t length()
Definition: File.cpp:88
int line_number
Definition: parser.yy.c:775
_Check_return_opt_ _CRTIMP size_t __cdecl fwrite(_In_reads_bytes_(_Size *_Count) const void *_Str, _In_ size_t _Size, _In_ size_t _Count, _Inout_ FILE *_File)
const char * filename
Definition: ioapi.h:135
std::string getline(bool strip_crlf=false)
Definition: File.cpp:17
_Check_return_opt_ _CRTIMP size_t __cdecl fread(_Out_writes_bytes_(_ElementSize *_Count) void *_DstBuf, _In_ size_t _ElementSize, _In_ size_t _Count, _Inout_ FILE *_File)
bool read(void *data, unsigned len)
Definition: File.h:72
static UINT WPARAM LPARAM lparam
Definition: combo.c:716
static bool LoadIntoString(std::string &s, const char *filename)
Definition: File.cpp:108
void close()
Definition: File.cpp:99
_Check_return_opt_ _CRTIMP int __cdecl fseek(_Inout_ FILE *_File, _In_ long _Offset, _In_ int _Origin)
Definition: parser.c:48
FILE * operator *()
Definition: File.h:96
File(const File &)
Definition: File.h:105
static bool SaveFromString(const char *filename, const std::string &s, bool binary)
Definition: File.cpp:122
#define SEEK_SET
Definition: jmemansi.c:26
_Check_return_ _CRTIMP int __cdecl feof(_In_ FILE *_File)
MmuTrapHandler callback[0x30]
Definition: mmuobject.c:44
File()
Definition: File.h:18
bool eof()
Definition: File.h:91
bool isopened()
Definition: File.h:86
const GLubyte * c
Definition: glext.h:8905
const GLuint GLenum const GLvoid * binary
Definition: glext.h:7538
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
bool write(const void *data, unsigned len)
Definition: File.h:77
GLenum GLsizei len
Definition: glext.h:6722
GLdouble s
Definition: gl.h:2039
FILE * _f
Definition: File.h:108
GLenum mode
Definition: glext.h:6217
char string[160]
Definition: util.h:11
bool open(const char *filename, const char *mode)
Definition: File.cpp:10
bool put(int c)
Definition: File.h:44
bool next_line(std::string &line, bool strip_crlf)
Definition: File.cpp:47
const File & operator=(const File &)
Definition: File.h:106
~File()
Definition: File.h:27
#define EOF
Definition: stdio.h:24
bool seek(long offset)
Definition: File.h:34
Definition: File.h:15
#define true
Definition: stdbool.h:37
File(const char *filename, const char *mode)
Definition: File.h:22
_Check_return_opt_ _CRTIMP int __cdecl fgetc(_Inout_ FILE *_File)