1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
// File.h
// This file is (C) 2002-2003 Royce Mitchell III and released under the BSD license

#ifndef FILE_H
#define FILE_H

#ifdef WIN32
#  include <io.h>
#elif defined(UNIX)
#  include <sys/stat.h>
#  include <unistd.h>
#endif
#include <string>

class File
{
public:
	File() : _f(0)
	{
	}

	File ( const char* filename, const char* mode ) : _f(0)
	{
		open ( filename, mode );
	}

	~File()
	{
		close();
	}

	bool open ( const char* filename, const char* mode );

	bool seek ( long offset )
	{
		return !fseek ( _f, offset, SEEK_SET );
	}

	int get()
	{
		return fgetc ( _f );
	}

	bool put ( int c )
	{
		return fputc ( c, _f ) != EOF;
	}

	std::string getline ( bool strip_crlf = false );

	// this function searches for the next end-of-line and puts all data it
	// finds until then in the 'line' parameter.
	//
	// call continuously until the function returns false ( no more data )
	bool next_line ( std::string& line, bool strip_crlf );

	/*
	example usage:

	bool mycallback ( const std::string& line, int line_number, long lparam )
	{
		std::cout << line << std::endl;
		return true; // continue enumeration
	}

	File f ( "file.txt", "rb" ); // open file for binary read-only ( i.e. "rb" )
	f.enum_lines ( mycallback, 0, true );
	*/

	bool enum_lines ( bool (*callback)(const std::string& line, int line_number, long lparam), long lparam, bool strip_crlf );

	bool read ( void* data, unsigned len )
	{
		return len == fread ( data, 1, len, _f );
	}

	bool write ( const void* data, unsigned len )
	{
		return len == fwrite ( data, 1, len, _f );
	}

	size_t length();

	void close();

	bool isopened()
	{
		return _f != 0;
	}

	bool eof()
	{
		return feof(_f) ? true : false;
	}

	FILE* operator * ()
	{
		return _f;
	}

	static bool LoadIntoString ( std::string& s, const char* filename );
	static bool SaveFromString ( const char* filename, const std::string& s, bool binary );

private:
	File(const File&) {}<--- Member variable 'File::_f' is not initialized in the constructor.
	const File& operator = ( const File& ) { return *this; }<--- Member variable 'File::_f' is not assigned a value in 'File::operator='.

	FILE * _f;
};

#endif//FILE_H