8 fprintf(stderr,
"sorry, i can't write to files opened readonly\n");
12 fprintf(stderr,
"sorry, i can't write %i bytes at once, only %i\n", _length,
BUFFER_SIZE);
15 if (_length == 0)
return;
17 memcpy(&
buffer[size], _data, _length);
22 memcpy(
buffer, _data, _length);
30 fprintf(stderr,
"sorry, i can't read %i bytes at once, only %i\n", _length,
BUFFER_SIZE);
33 if (_length == 0)
return;
34 if (position + _length <= size) {
35 memcpy(_data, &
buffer[position], _length);
40 memcpy(_data,
buffer, _length);
50 else if (_ul <= 0x3FFF) {
51 put_char((_ul>>8) | 0x80);
54 else if (_ul <= 0x1FFFFF) {
55 put_char((_ul>>16) | 0xC0);
56 put_char((_ul>>8) & 0xFF);
59 else if (_ul <= 0x0FFFFFFF) {
60 put_char((_ul>>24) | 0xE0);
61 put_char((_ul>>16) & 0xFF);
62 put_char((_ul>>8) & 0xFF);
67 put_char((_ul>>24) & 0xFF);
68 put_char((_ul>>16) & 0xFF);
69 put_char((_ul>>8) & 0xFF);
80 if ((c & 0x80) == 0) {
84 if ((c & 0xC0) == 0x80) {
85 _ul = (
unsigned long)(c & 0x3F) << 8;
88 if ((c & 0xE0) == 0xC0) {
89 _ul = (
unsigned long)(c & 0x1F) << 16;
92 if ((c & 0xF0) == 0xE0) {
93 _ul = (
unsigned long)(c & 0x0F) << 24;
97 _ul = (
unsigned long)c << 24;
100 _ul = (
unsigned long)c << 16;
103 _ul |= (
unsigned long)c << 8;
113 fprintf(stderr,
"sorry, i can't read %i bytes at once, only %i\n", _length,
BUFFER_SIZE);
116 if (position + _length <= size) {
117 memcpy(_data, &
buffer[position], _length);
121 memcpy(_data,
buffer, _length);
127 ssize_t written = write(file_handle,
buffer, size);
128 if (written != size) {
129 fprintf(stderr,
"failed to write %i bytes to file %s (total_write = %lu)\n", size, file_name, total_write);
132 total_write += written;
138 void PS_FileBuffer::refill() {
140 int unread = size-position;
145 fprintf(stderr,
"failed to refill buffer from file %s (total_read = %lu)\n", file_name, total_read);
148 total_read += readen;
149 size = unread+readen;
155 if (!is_readonly) flush();
156 if (file_name) free(file_name);
157 if (file_handle != -1) close(file_handle);
160 file_name = strdup(_name);
161 is_readonly = _readonly;
163 file_flags = O_RDONLY;
167 file_flags = O_WRONLY | O_CREAT | O_EXCL;
168 file_mode = S_IRUSR | S_IWUSR;
170 file_handle = open(file_name, file_flags, file_mode);
171 if (file_handle == -1) {
173 fprintf(stderr,
"failed to open file '%s' for reading\n", file_name);
176 fprintf(stderr,
"failed to create file '%s' for writing\nmaybe it already exists ?\n", file_name);
189 PS_FileBuffer::PS_FileBuffer(
const char *_name,
bool _readonly) :
190 file_name(strdup(_name)),
191 is_readonly(_readonly),
197 file_flags = O_RDONLY;
201 file_flags = O_WRONLY | O_CREAT | O_EXCL;
202 file_mode = S_IRUSR | S_IWUSR;
204 file_handle = open(file_name, file_flags, file_mode);
205 if (file_handle == -1) {
207 fprintf(stderr,
"failed to open file '%s' for reading\n", file_name);
210 fprintf(stderr,
"failed to create file '%s' for writing\nmaybe it already exists ?\n", file_name);
220 fprintf(stderr,
"failed to allocate memory for buffer for file %s\n", file_name);
void put_ulong(unsigned long int _ul)
void get(void *_data, int _length)
char buffer[MESSAGE_BUFFERSIZE]
void peek(void *_data, int _length)
void put(const void *_data, int _length)
static const int BUFFER_SIZE
void get_ulong(unsigned long int &_ul)
void reinit(const char *name, bool _readonly)