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 220 221 222 223 224 225 226 227 228
|
#include "ps_filebuffer.hxx"
using namespace std;
void PS_FileBuffer::put(const void *_data, int _length) {
if (is_readonly) {
fprintf(stderr, "sorry, i can't write to files opened readonly\n");
CRASH();
}
if (_length > BUFFER_SIZE) {
fprintf(stderr, "sorry, i can't write %i bytes at once, only %i\n", _length, BUFFER_SIZE);
CRASH();
}
if (_length == 0) return;
if (size + _length < BUFFER_SIZE) {
memcpy(&buffer[size], _data, _length);
size += _length;
}
else {
flush();
memcpy(buffer, _data, _length);
size = _length;
}
}
void PS_FileBuffer::get(void *_data, int _length) {
if (_length > BUFFER_SIZE) {
fprintf(stderr, "sorry, i can't read %i bytes at once, only %i\n", _length, BUFFER_SIZE);
CRASH();
}
if (_length == 0) return;
if (position + _length <= size) {
memcpy(_data, &buffer[position], _length);
position += _length;
}
else {
refill();
memcpy(_data, buffer, _length);
position = _length;
}
}
void PS_FileBuffer::put_ulong(unsigned long int _ul) {
if (_ul <= 0x7F) { // bit7 == 0 -> 1 byte integer
put_char(_ul);
}
else if (_ul <= 0x3FFF) {
put_char((_ul>>8) | 0x80); // bit7==1 && bit6==0 -> 2 byte integer
put_char(_ul & 0xFF);
}
else if (_ul <= 0x1FFFFF) {
put_char((_ul>>16) | 0xC0); // bit7==1 && bit6==1 && bit5==0 -> 3 byte integer
put_char((_ul>>8) & 0xFF);
put_char(_ul & 0xFF);
}
else if (_ul <= 0x0FFFFFFF) {
put_char((_ul>>24) | 0xE0); // bit7==1 && bit6==1 && bit5==1 && bit4==0 -> 4 byte integer
put_char((_ul>>16) & 0xFF);
put_char((_ul>>8) & 0xFF);
put_char(_ul & 0xFF);
}
else { // else -> 5 byte integer
put_char(0xF0);
put_char((_ul>>24) & 0xFF);
put_char((_ul>>16) & 0xFF);
put_char((_ul>>8) & 0xFF);
put_char(_ul & 0xFF);
}
}
void PS_FileBuffer::get_ulong(unsigned long int &_ul) {
unsigned char c;
get_char(c);
if ((c & 0x80) == 0) { // 1-byte
_ul = c;
}
else {
if ((c & 0xC0) == 0x80) { // 2-byte
_ul = (unsigned long)(c & 0x3F) << 8;
}
else {
if ((c & 0xE0) == 0xC0) { // 3-byte
_ul = (unsigned long)(c & 0x1F) << 16;
}
else {
if ((c & 0xF0) == 0xE0) { // 4-byte
_ul = (unsigned long)(c & 0x0F) << 24;
}
else {
get_char(c);
_ul = (unsigned long)c << 24;
}
get_char(c);
_ul = (unsigned long)c << 16;
}
get_char(c);
_ul |= (unsigned long)c << 8;
}
get_char(c);
_ul |= c;
}
}
void PS_FileBuffer::peek(void *_data, int _length) {
if (_length > BUFFER_SIZE) {
fprintf(stderr, "sorry, i can't read %i bytes at once, only %i\n", _length, BUFFER_SIZE);
CRASH();
}
if (position + _length <= size) {
memcpy(_data, &buffer[position], _length);
}
else {
refill();
memcpy(_data, buffer, _length);
}
}
void PS_FileBuffer::flush() {
ssize_t written = write(file_handle, buffer, size);
if (written != size) {
fprintf(stderr, "failed to write %i bytes to file %s (total_write = %lu)\n", size, file_name, total_write);
CRASH();
}
total_write += written;
size = 0;
position = 0;
}
void PS_FileBuffer::refill() {
// move unread data to start of buffer
int unread = size-position;
memcpy(buffer, &buffer[position], unread);
// read data from file
ssize_t readen = read(file_handle, &buffer[size-position], BUFFER_SIZE-unread);
if (readen < 1) {
fprintf(stderr, "failed to refill buffer from file %s (total_read = %lu)\n", file_name, total_read);
CRASH();
}
total_read += readen;
size = unread+readen;
position = 0;
}
void PS_FileBuffer::reinit(const char *_name, bool _readonly) {
// finish old file
if (!is_readonly) flush();
if (file_name) free(file_name);
if (file_handle != -1) close(file_handle);
// init. file
file_name = strdup(_name);
is_readonly = _readonly;
if (is_readonly) {
file_flags = O_RDONLY;
file_mode = 0;
}
else {
file_flags = O_WRONLY | O_CREAT | O_EXCL;
file_mode = S_IRUSR | S_IWUSR;
}
file_handle = open(file_name, file_flags, file_mode);
if (file_handle == -1) {
if (_readonly) {
fprintf(stderr, "failed to open file '%s' for reading\n", file_name);
}
else {
fprintf(stderr, "failed to create file '%s' for writing\nmaybe it already exists ?\n", file_name);
}
CRASH();
}
// init. buffer
clear();
// debug
total_read = 0;
total_write = 0;
}
PS_FileBuffer::PS_FileBuffer(const char *_name, bool _readonly)
:
file_name(strdup(_name)),
is_readonly(_readonly),
file_pos(-1)
{
// init. file
if (is_readonly) {
file_flags = O_RDONLY;
file_mode = 0;
}
else {
file_flags = O_WRONLY | O_CREAT | O_EXCL;
file_mode = S_IRUSR | S_IWUSR;
}
file_handle = open(file_name, file_flags, file_mode);
if (file_handle == -1) {
if (_readonly) {
fprintf(stderr, "failed to open file '%s' for reading\n", file_name);
}
else {
fprintf(stderr, "failed to create file '%s' for writing\nmaybe it already exists ?\n", file_name);
}
CRASH();
}
// init. buffer
size = 0;
position = 0;
buffer = (unsigned char *)malloc(BUFFER_SIZE);
if (!buffer) {
fprintf(stderr, "failed to allocate memory for buffer for file %s\n", file_name);
CRASH();
}
// debug
total_read = 0;
total_write = 0;
}
|