File: rtstreams.stdlib.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 (168 lines) | stat: -rwxr-xr-x 3,209 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
/*
 * rtstreams.stdlib.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 the STL ////////////////////

#ifndef __LRT_STREAMS_STDLIB__
#define __LRT_STREAMS_STDLIB__

#include "rtstreams.h"

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

#include <stdio.h>

namespace lrt {

FileInputStream::FileInputStream(const String &filename, bool textMode) :
	open(false), markPos(0)
{
	String fname = File(filename).getName();
	handle = ::fopen(fname.cStr(), (textMode ? "rt" : "rb"));
	if(handle) open = true;
	else _fail = true;
}

FileInputStream::FileInputStream(const File &file, bool textMode) : 
	open(false), markPos(0)
{
	String fname = file.getName();
	handle = ::fopen(fname.cStr(), (textMode ? "rt" : "rb"));
	if(handle) open = true;
	else _fail = true;
}

bool FileInputStream::markSupported()
{
	return true;
}

void FileInputStream::mark()
{
	if(!open) return;
	markPos = ::ftell((FILE*) handle);
}

void FileInputStream::reset()
{
	if(!open) return;
	::fseek((FILE*) handle, markPos, SEEK_SET);
}

int FileInputStream::read()
{
	if(!open) return -1;
	unsigned char ret;
	int rval = ::fread(&ret, 1, 1, (FILE*) handle);
	if(rval == 0)
	{
		if(ferror((FILE*) handle)) _fail = true;
		return -1;
	
	}
	return ret;
}

bool FileInputStream::eos()
{
	if(!open) return true;
	return (feof((FILE*) handle) != 0);
}

void FileInputStream::close()
{
	if(open) {
		::fclose((FILE*) handle);
		open = false;
	}
}

FileInputStream::~FileInputStream()
{
	close();
    // delete handle;  // must not be deleted!
}



FileOutputStream::FileOutputStream(const String &fileName, bool append)
	: open(false), handle(0)
{
	String fname = File(fileName).getName();
	handle = ::fopen(fname.cStr(), (append ? "ab" : "wb"));
	if(handle) open = true;
	else _fail = true;
}

FileOutputStream::FileOutputStream(const File &file, bool append)
	: open(false), handle(0)
{
	String fname = file.getName();
	handle = ::fopen(fname.cStr(), (append ? "ab" : "wb"));
	if(handle) open = true;
	else _fail = true;
}

bool FileOutputStream::write(int b)
{
	if(!open) return false;
	char temp = (char)b;
	int rval = ::fwrite(&temp, 1, 1, (FILE*) handle);
	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 = ::fwrite(ptr + off, 1, len, (FILE*) handle);
	if(rval < len)
	{
		_fail = true;
		return false;
	}
	else return true;
}

void FileOutputStream::flush()
{
	if(!open) return;
	::fflush((FILE*) handle);
}

void FileOutputStream::close()
{
	if(open && handle) {
		::fclose((FILE*) handle);
		open = false;
	}
}

FileOutputStream::~FileOutputStream()
{
	close();
    // delete handle;  // must not be deleted!
}

} // namespace

#endif