File: FileStream.h

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (68 lines) | stat: -rw-r--r-- 1,492 bytes parent folder | download | duplicates (3)
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
#pragma once

#include "Path.h"
#include "Stream.h"

// Wrapper around a file handle on windows. However, noting windows-specific is exposed
// by this class, which allows it to be ported to other operating systems.
class FileStream : public Stream {
public:
	FileStream(const Path &path, Mode mode);
	virtual ~FileStream();

	virtual Stream *clone() const;

	void close();

	using Stream::read;
	virtual nat read(nat size, void *to);
	using Stream::write;
	virtual void write(nat size, const void *from);

	virtual nat64 pos() const;
	virtual nat64 length() const;
	virtual void seek(nat64 to);

	virtual bool valid() const;

	// Flush buffer.
	void flush();

private:
	static const nat bufferSize = 10 * 1024;

	Path name;
	Mode mode;
	nat64 fileSize;

#if defined(WINDOWS)
	HANDLE file;
#elif defined(POSIX)
	int file;
#endif

	// The buffer is always flushed when seeking. When reading data,
	// the buffer is pre-loaded until bufferFill. When writing, the buffer may
	// contain dirty data until bufferPos.
	// The seek-pointer of the underlying file will always point to the beginning of this buffer.
	nat64 bufferStart;
	byte *buffer;
	nat bufferPos;
	nat bufferFill;
	bool bufferDirty;

	// Fill the buffer from the file. Returns false if eof.
	bool fillBuffer();

	void openFile(const String &name, Mode mode);

	// Raw seek, does not affect the buffer.
	void rawSeek(nat64 to);
	nat64 rawPos() const;
};


/**
 * Read an entire text file.
 */
String readTextFile(const Path &file);