summaryrefslogtreecommitdiff
path: root/tests/examplefiles/hybris_File.hy
blob: 9c86c641f3eec3145abae2c3ffc2aefd8cb431d8 (plain)
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
/*
 * This file is part of the Hybris programming language.
 *
 * Copyleft of Francesco Morucci aka merlok <merlok@ihteam.net>
 *
 * Hybris is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Hybris is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Hybris.  If not, see <http://www.gnu.org/licenses/>.
*/
import std.io.file;

class File {
	
	protected file, fileName, mode;

	public method File( fileName, mode ){
		me.fileName = fileName;
		me.mode = mode;
		me.file = fopen ( me.fileName, me.mode);
	}

	private method isBinary(){
		return me.mode.find("b") != false;
	}
	
	public method File ( file ){
		me.file = file;
	}

	private method __expire() {
		me.close();
	}
	
	public method close(){
		fclose( me.file );
	}
	
	public method readLine(){
		return line = fgets( me.file );
	}

	public method getFileName(){
		return me.fileName;
	}

	public method getSize(){
		return fsize( me.fileName );
	}

	public method getPosition(){
		return ftell( me.file );
	}
	
	public method readAll(){
		text = "";
		line = "";
		while ( ( line = fgets(me.file) ) != 0 ){
			text += line;
		}
		return text;
	}

	public method read(){
		byte = ' ';
		if ( fread( me.file, byte) > 0 ) {
			return byte;
		}
		else {
			return -1;
		}
	}

	public method read( bytes ) {
		word = "";
		byte = ' ';
		if ( fread( me.file, byte, bytes) > 0 ) {
			word += byte;
		}
		else {
			return -1;
		}
		return word;
	}

	public method read ( seek, seekType ){
		if ( me.seek( seek, seekType) == 0 ) {
			return -1;
		}

		return  me.read();
	}

	public method read ( bytes, seek, seekType ){
		if ( me.seek( seek, seekType) == 0 ) {
			return -1;
		}

		return  me.read( bytes );
	}

	public method readType ( type ){
		if ( me.isBinary() == false ) {
			return -1;
		}
		if ( fread (me.file, type ) > 0 ) {
			return type;
		} 
		else {
			return -1;
		}
	}

	operator >> ( object ){
		return me.readType(object);
	}

	public method readType ( type, bytes ){
		if ( me.isBinary() == false ) {
			return -1;
		}
		if ( fread (me.file, type, bytes ) > 0){
			return type;
		}
		else {
			return -1;
		}
	}

	public method readType ( type, seek, seekType ){
		if ( ( me.isBinary() == false ) | ( me.seek( seek, seekType) == 0 ) ) {
			return -1;
		}

		return me.readType( type );
	}
	
	public method  readType(  type, bytes, seek, seekType){
		if ( ( me.isBinary() == false ) | ( me.seek( seek, seekType) == 0 ) ) {
			return -1;
		}

		return me.readType( type, bytes );
	}
	
	public method write( data ){
		return fwrite( me.file, data );
	}

	operator << ( object ){
		return me.write(object);
	}

	public method write ( data, bytes ){
		return fwrite( me.file, data, bytes);
	}
	
	public method seek( pos, mode ){
		return fseek( me.file, pos, mode );
	}

	public method merge ( fileName ){
		text = file ( fileName );
		return me.write ( me.file, text );
	}
}