ReactOS 0.4.15-dev-8102-g108db8f
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
10bool File::open ( const char* filename, const char* mode )
11{
12 close();
13 _f = fopen ( filename, mode );
14 return _f != 0;
15}
16
17std::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 )
47bool 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/*
57example usage:
58
59bool mycallback ( const std::string& line, int line_number, long lparam )
60{
61 std::cout << line << std::endl;
62 return true; // continue enumeration
63}
64
65File f ( "file.txt", "rb" ); // open file for binary read-only ( i.e. "rb" )
66f.enum_lines ( mycallback, 0, true );
67*/
68
69bool 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);
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
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}
@ lparam
Definition: SystemMenu.c:31
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
char * strchr(const char *String, int ch)
Definition: utclib.c:501
char * strpbrk(const char *String, const char *Delimiters)
Definition: utclib.c:302
#define fileno
Definition: acwin.h:102
#define fstat
Definition: acwin.h:100
Definition: File.h:16
FILE * _f
Definition: File.h:108
bool next_line(std::string &line, bool strip_crlf)
Definition: File.cpp:47
static bool SaveFromString(const char *filename, const std::string &s, bool binary)
Definition: File.cpp:122
static bool LoadIntoString(std::string &s, const char *filename)
Definition: File.cpp:108
std::string getline(bool strip_crlf=false)
Definition: File.cpp:17
bool eof()
Definition: File.h:91
size_t length()
Definition: File.cpp:88
void close()
Definition: File.cpp:99
bool enum_lines(bool(*callback)(const std::string &line, int line_number, long lparam), long lparam, bool strip_crlf)
Definition: File.cpp:69
bool open(const char *filename, const char *mode)
Definition: File.cpp:10
static char * file_stat(DOS_FILE *file)
Definition: check.c:279
GLdouble s
Definition: gl.h:2039
GLenum mode
Definition: glext.h:6217
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLuint in
Definition: glext.h:9616
GLfloat GLfloat p
Definition: glext.h:8902
const GLuint GLenum const GLvoid * binary
Definition: glext.h:7538
GLenum GLsizei len
Definition: glext.h:6722
_Check_return_ _CRTIMP int __cdecl _fileno(_In_ FILE *_File)
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
_Check_return_opt_ _CRTIMP char *__cdecl fgets(_Out_writes_z_(_MaxCount) char *_Buf, _In_ int _MaxCount, _Inout_ FILE *_File)
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
const char * filename
Definition: ioapi.h:137
static IPrintDialogCallback callback
Definition: printdlg.c:326
static FILE * out
Definition: regtests2xml.c:44
#define getline
Definition: schily.h:567
_Check_return_ _CRTIMP long __cdecl _filelength(_In_ int _FileHandle)
static long line_number
Definition: main.cpp:17
Definition: parser.c:49
Definition: stat.h:55