summaryrefslogtreecommitdiffstats
path: root/kernel/ksquirrel-libs/fileio.cpp
blob: b7cea5c9c93555e0783f4da21263d61b716cf266 (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
/*  This file is part of ksquirrel-libs (http://ksquirrel.sf.net)

    Copyright (c) 2004 Dmitry Baryshev <ksquirrel@tut.by>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation;
    either version 2 of the License, or (at your option) any later
    version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>

#include "ksquirrel-libs/fileio.h"

// implement read methods
ifstreamK::ifstreamK() : ifstream()
{}

bool ifstreamK::readK(void *data, int size)
{
    read((char *)data, size);

    return good();
}

bool ifstreamK::getS(char *s, const int sz)
{
    getline(s, sz);

    return good();
}

bool ifstreamK::be_getchar(u8 *c)
{
    return readK(c, 1);
}

bool ifstreamK::be_getshort(u16 *s)
{
    u8 buf[2];

    if(!readK(buf, 2))
	return false;

    *s = (buf[0] << 8) + buf[1];

    return good();
}

bool ifstreamK::be_getlong(u32 *l)
{
    u8 buf[4];

    if(!readK(buf, 4))
	return false;

    *l = (buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + (buf[3]);

    return good();
}

void ifstreamK::close()
{
    ifstream::close();
    
    clear();
}

bool ifstreamK::readCHex(u32 &hex)
{
    s8 prefix1, prefix2;
    s8 h[9], c;
    u32 i = 0;

    if(!readK(&prefix1, sizeof(s8)))
	return false;

    if(!readK(&prefix2, sizeof(s8)))
	return false;

    if(prefix1 != '0' || prefix2 != 'x')
	return false;

    while(true)
    {
	if(!readK(&c, sizeof(s8)))
	    return false;

	if(c < '0' || c > '9')
	{
	    if(c != 'A' && c != 'B' && c != 'C' && c != 'D' && c != 'E' && c != 'F')
	    {
		seekg(-1, ios::cur);
		break;
	    }
	}

	h[i++] = c;
    }

    h[i] = '\0';

    hex = strtol(h, NULL, 16);

    return good();
}


// implement write methods
ofstreamK::ofstreamK() : ofstream()
{}

bool ofstreamK::writeK(void *data, int size)
{
    write((char *)data, size);

    return good();
}

void ofstreamK::close()
{
    ofstream::close();

    clear();
}