File: rtstreams.epoc.cpp

package info (click to toggle)
robotour 3.1.1-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,596 kB
  • ctags: 2,972
  • sloc: cpp: 17,705; sh: 3,060; ansic: 2,778; makefile: 144
file content (219 lines) | stat: -rwxr-xr-x 4,409 bytes parent folder | download
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * rtstreams.epoc.cpp
 * 
 * Copyright (c) 2000-2004 by Florian Fischer (florianfischer@gmx.de)
 * and Martin Trautmann (martintrautmann@gmx.de) 
 * 
 * This file may be distributed and/or modified under the terms of the 
 * GNU General Public License version 2 as published by the Free Software 
 * Foundation. 
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 * 
 */

////////////// File Streams using EPOC ////////////////////

#ifndef __LRT_STREAMS_EPOC__
#define __LRT_STREAMS_EPOC__

#include "rtstreams.h"

#include "rtstring.h"
#include "rtfile.h"

#include "rtetools.h"

#include <f32file.h>

namespace lrt {

FileInputStream::FileInputStream(const String &filename, bool textMode) : 
		markPos(0), open(false), session(0), file(0), buf(0), pos(0)
{
	String fname = File(filename).getName();
	init(fname, textMode);
}

FileInputStream::FileInputStream(const File &file, bool textMode) : 
		markPos(0), open(false), session(0), file(0), buf(0), pos(0)
{
	String fname = file.getName();
	init(fname, textMode);
}

void FileInputStream::init(const String &fname, bool textMode) 
{
	buf = new TBuf8<80>;
	session = new RFs();
	if(session->Connect() < 0) { _fail = true; return; }
	file = new RFile();
	TFileMode mode = EFileShareReadersOnly;
	if(textMode) mode = (TFileMode)(mode | EFileStreamText);
	if(file->Open(*session, ESTRING(fname), mode) < 0) 
		_fail = true;
	else open = true;
}
  
bool FileInputStream::markSupported()
{
	return true;
}

void FileInputStream::mark()
{
	if(!open) return;
	markPos = 0;
	file->Seek(ESeekCurrent, markPos);
	// undo read-ahead
	markPos -= buf->Length();
	markPos += pos;
}

void FileInputStream::reset()
{
	if(!open) return;
	file->Seek(ESeekStart, markPos);
	buf->SetLength(0);
	pos = 0;
}

int FileInputStream::read()
{
	if(!open) return -1;
	if(pos >= buf->Length()) {
		int rval = file->Read(*buf, 79);
		if(rval < 0) _fail = true;
		if((rval < 0) || (buf->Length() == 0))
			return -1;
		pos = 0;
	}
	return (*buf)[pos++];
}

bool FileInputStream::eos()
{
	if(!open) return true;
	
	// attempt to fetch new data if the buffer is empty
	if(pos >= buf->Length()) {
		int rval = file->Read(*buf, 79);
		if(rval < 0) _fail = true;
		if((rval < 0) || (buf->Length() == 0))
			return true;
		pos = 0;
	}

	// do we have more data in our buffer?
	if(pos < buf->Length())
		return false;
	else return true;
}

void FileInputStream::close()
{
	if(open) {
		file->Close();
		session->Close();
		open = false;
	}
}

FileInputStream::~FileInputStream()
{
	close();
	delete file;
	delete session;
	delete buf;
}



FileOutputStream::FileOutputStream(const String &fileName, bool append) : open(false)
{
	String fname = File(fileName).getName();
	init(fname, append);
}

FileOutputStream::FileOutputStream(const File &file, bool append) : open(false)
{
	String fname = file.getName();
	init(fname, append);
}

void FileOutputStream::init(const String &fname, bool append)
{
	session = new RFs();
	if(session->Connect() < 0) { _fail = true; return; }
	file = new RFile();
	TFileMode mode = EFileShareExclusive;
	int ret;
	if(!append)
		ret = file->Replace(*session, ESTRING(fname), mode);
	else
	{
		mode = (TFileMode)(mode | EFileWrite);
		ret = file->Open(*session, ESTRING(fname), mode);
		int seek = 0;
		file->Seek(ESeekEnd, seek);
	}
	if(ret < 0) 
		_fail = true;
	else
		open = true;
}

bool FileOutputStream::write(int b)
{
	if(!open) return false;
	TBuf8<1> buf(1);
	buf[0] = (unsigned char)b;
	int rval = file->Write(buf, 1);
	if(rval < 0)
	{
		_fail = true;
		return false;
	}
	else return true;
}

bool FileOutputStream::write(const Array<char> &b, int off, int len)
{
	if(!open) return false;
	const char *ptr = b.getData();
	int rval = file->Write(_L8(ptr + off), len);
	if(rval < 0)
	{
		_fail = true;
		return false;
	}
	else return true;
}

void FileOutputStream::flush()
{
	if(!open) return;
	file->Flush();
}

void FileOutputStream::close()
{
	if(open) {
		file->Close();
		session->Close();
		open = false;
	}
}

FileOutputStream::~FileOutputStream()
{
	close();
	delete file;
	delete session;
}

} // namespace

#endif