ReactOS  0.4.13-dev-242-g611e6d7
File.cpp
Go to the documentation of this file.
1 // File.cpp
2 // This file is (C) 2002-2003 Royce Mitchell III and released under the BSD license
3 
4 #ifdef _MSC_VER
5 #pragma warning ( disable : 4786 )
6 #endif//_MSC_VER
7 
8 #include "File.h"
9 
10 bool File::open ( const char* filename, const char* mode )
11 {
12  close();
13  _f = fopen ( filename, mode );
14  return _f != 0;
15 }
16 
17 std::string File::getline ( bool strip_crlf /*= false*/ )
18 {
19  std::string s = "";
20  char buf[256];
21  for ( ;; )
22  {
23  *buf = 0;
24  fgets ( buf, sizeof(buf)-1, _f );
25  if ( !*buf )
26  break;
27  s += buf;
28  if ( strchr ( "\r\n", buf[strlen(buf)-1] ) )
29  break;
30  }
31  if ( strip_crlf )
32  {
33  char* p = strpbrk ( &s[0], "\r\n" );
34  if ( p )
35  {
36  *p = '\0';
37  s.resize ( p-&s[0] );
38  }
39  }
40  return s;
41 }
42 
43 // this function searches for the next end-of-line and puts all data it
44 // finds until then in the 'line' parameter.
45 //
46 // call continuously until the function returns false ( no more data )
47 bool File::next_line ( std::string& line, bool strip_crlf )
48 {
49  line = getline(strip_crlf);
50  // indicate that we're done *if*:
51  // 1) there's no more data, *and*
52  // 2) we're at the end of the file
53  return line.size()>0 || !eof();
54 }
55 
56 /*
57 example usage:
58 
59 bool mycallback ( const std::string& line, int line_number, long lparam )
60 {
61  std::cout << line << std::endl;
62  return true; // continue enumeration
63 }
64 
65 File f ( "file.txt", "rb" ); // open file for binary read-only ( i.e. "rb" )
66 f.enum_lines ( mycallback, 0, true );
67 */
68 
69 bool File::enum_lines ( bool (*callback)(const std::string& line, int line_number, long lparam), long lparam, bool strip_crlf )
70 {
71  int line_number = 0;
72  for ( ;; )
73  {
74  std::string s = getline(strip_crlf);
75  line_number++;
76  if ( !s.size() )
77  {
78  if ( eof() )
79  return true;
80  else
81  continue;
82  }
83  if ( !(*callback) ( s, line_number, lparam ) )
84  return false;
85  }
86 }
87 
88 size_t File::length()
89 {
90 #ifdef WIN32
91  return _filelength ( _fileno(_f) );
92 #elif defined(UNIX)
93  struct stat file_stat;
94  verify(fstat(fileno(_f), &file_stat) == 0);
95  return file_stat.st_size;
96 #endif
97 }
98 
100 {
101  if ( _f )
102  {
103  fclose(_f);
104  _f = 0;
105  }
106 }
107 
108 /*static*/ bool File::LoadIntoString ( std::string& s, const char* filename )
109 {
110  File in ( filename, "rb" );
111  if ( !in.isopened() )
112  return false;
113  size_t len = in.length();
114  s.resize ( len + 1 );
115  if ( !in.read ( &s[0], len ) )
116  return false;
117  s[len] = '\0';
118  s.resize ( len );
119  return true;
120 }
121 
122 /*static*/ bool File::SaveFromString ( const char* filename, const std::string& s, bool binary )
123 {
124  File out ( filename, binary ? "wb" : "w" );
125  if ( !out.isopened() )
126  return false;
127  out.write ( s.c_str(), s.size() );
128  return true;
129 }
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
char * strpbrk(const char *String, const char *Delimiters)
Definition: utclib.c:302
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
bool enum_lines(bool(*callback)(const std::string &line, int line_number, long lparam), long lparam, bool strip_crlf)
Definition: File.cpp:69
size_t length()
Definition: File.cpp:88
int line_number
Definition: parser.yy.c:775
const char * filename
Definition: ioapi.h:135
_Check_return_ _CRTIMP long __cdecl _filelength(_In_ int _FileHandle)
std::string getline(bool strip_crlf=false)
Definition: File.cpp:17
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
Definition: parser.c:48
static bool SaveFromString(const char *filename, const std::string &s, bool binary)
Definition: File.cpp:122
MmuTrapHandler callback[0x30]
Definition: mmuobject.c:44
_Check_return_ _CRTIMP int __cdecl fileno(_In_ FILE *_File)
bool eof()
Definition: File.h:91
static FILE * out
Definition: regtests2xml.c:44
const GLuint GLenum const GLvoid * binary
Definition: glext.h:7538
Definition: stat.h:55
GLenum GLsizei len
Definition: glext.h:6722
GLdouble s
Definition: gl.h:2039
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
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
_Check_return_opt_ _CRTIMP char *__cdecl fgets(_Out_writes_z_(_MaxCount) char *_Buf, _In_ int _MaxCount, _Inout_ FILE *_File)
static char * file_stat(DOS_FILE *file)
Definition: check.c:279
bool next_line(std::string &line, bool strip_crlf)
Definition: File.cpp:47
GLuint in
Definition: glext.h:9616
char * strchr(const char *String, int ch)
Definition: utclib.c:501
_CRTIMP int __cdecl fstat(int _Desc, struct stat *_Stat)
Definition: stat.h:341
Definition: File.h:15
_Check_return_ _CRTIMP int __cdecl _fileno(_In_ FILE *_File)
GLfloat GLfloat p
Definition: glext.h:8902