| 1 | #ifndef PS_FILEBUFFER_HXX |
|---|
| 2 | #define PS_FILEBUFFER_HXX |
|---|
| 3 | |
|---|
| 4 | #include <stdio.h> |
|---|
| 5 | #include <stdlib.h> |
|---|
| 6 | #include <string.h> |
|---|
| 7 | #include <fcntl.h> |
|---|
| 8 | #include <sys/stat.h> |
|---|
| 9 | #include <unistd.h> |
|---|
| 10 | |
|---|
| 11 | using namespace std; |
|---|
| 12 | |
|---|
| 13 | class PS_FileBuffer { |
|---|
| 14 | private: |
|---|
| 15 | // filehandling |
|---|
| 16 | int file_handle; |
|---|
| 17 | char *file_name; |
|---|
| 18 | int file_flags; |
|---|
| 19 | int file_mode; |
|---|
| 20 | bool is_readonly; |
|---|
| 21 | long file_pos; |
|---|
| 22 | // bufferhandling |
|---|
| 23 | unsigned char *buffer; |
|---|
| 24 | int size; // how much is in buffer |
|---|
| 25 | int position; // where am i in the buffer |
|---|
| 26 | // debug |
|---|
| 27 | unsigned long int total_read; |
|---|
| 28 | unsigned long int total_write; |
|---|
| 29 | |
|---|
| 30 | PS_FileBuffer(); |
|---|
| 31 | |
|---|
| 32 | void refill(); // refill buffer from file |
|---|
| 33 | |
|---|
| 34 | public: |
|---|
| 35 | static const bool READONLY = true; |
|---|
| 36 | static const bool WRITEONLY = false; |
|---|
| 37 | static const int BUFFER_SIZE = 4096; |
|---|
| 38 | |
|---|
| 39 | // |
|---|
| 40 | // IO-functions |
|---|
| 41 | // |
|---|
| 42 | |
|---|
| 43 | // raw |
|---|
| 44 | void put ( const void *_data, int _length ); |
|---|
| 45 | void get ( void *_data, int _length ); |
|---|
| 46 | void peek( void *_data, int _length ); // read but don't advance position |
|---|
| 47 | |
|---|
| 48 | // char |
|---|
| 49 | void put_char( unsigned char _c ) { |
|---|
| 50 | if (is_readonly) { |
|---|
| 51 | fprintf( stderr, "sorry, i can't write to files opened readonly\n" ); |
|---|
| 52 | *(int *)0 = 0; |
|---|
| 53 | } |
|---|
| 54 | if (size+1 < BUFFER_SIZE) { |
|---|
| 55 | buffer[size] = _c; |
|---|
| 56 | ++size; |
|---|
| 57 | } else { |
|---|
| 58 | flush(); |
|---|
| 59 | buffer[0] = _c; |
|---|
| 60 | size = 1; |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | void get_char( unsigned char &_c ) { |
|---|
| 64 | if (position < size) { |
|---|
| 65 | _c = buffer[position]; |
|---|
| 66 | ++position; |
|---|
| 67 | } else { |
|---|
| 68 | refill(); |
|---|
| 69 | _c = buffer[0]; |
|---|
| 70 | position = 1; |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | unsigned char get_char() { |
|---|
| 74 | if (position < size) { |
|---|
| 75 | return buffer[position++]; |
|---|
| 76 | } else { |
|---|
| 77 | refill(); |
|---|
| 78 | position = 1; |
|---|
| 79 | return buffer[0]; |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | // long |
|---|
| 84 | void put_ulong( unsigned long int _ul ); |
|---|
| 85 | void get_ulong( unsigned long int &_ul ); |
|---|
| 86 | void put_long( long int _l ) { |
|---|
| 87 | unsigned long int ul; |
|---|
| 88 | if (_l < 0) { |
|---|
| 89 | ul = (-_l << 1) | 1; |
|---|
| 90 | } else { |
|---|
| 91 | ul = _l << 1; |
|---|
| 92 | } |
|---|
| 93 | put_ulong( ul ); |
|---|
| 94 | } |
|---|
| 95 | void get_long( long int &_l ) { |
|---|
| 96 | unsigned long int ul; |
|---|
| 97 | get_ulong( ul ); |
|---|
| 98 | if (ul & 1) { |
|---|
| 99 | _l = -(long)(ul >> 1); |
|---|
| 100 | } else { |
|---|
| 101 | _l = ul >> 1; |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | // int |
|---|
| 106 | void put_uint( unsigned int _ui ) { |
|---|
| 107 | put_ulong( _ui ); |
|---|
| 108 | } |
|---|
| 109 | void get_uint( unsigned int &_ui ) { |
|---|
| 110 | unsigned long int ul; |
|---|
| 111 | get_ulong( ul ); |
|---|
| 112 | _ui=(unsigned int)ul; |
|---|
| 113 | } |
|---|
| 114 | void put_int( int _i ) { |
|---|
| 115 | put_long( _i ); |
|---|
| 116 | } |
|---|
| 117 | void get_int( int &_i ) { |
|---|
| 118 | long int l; |
|---|
| 119 | get_long( l ); |
|---|
| 120 | _i=(int)l; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | // |
|---|
| 125 | // utility-functions |
|---|
| 126 | // |
|---|
| 127 | bool store_pos() { |
|---|
| 128 | if (!is_readonly) return false; // cannot jump in a writeable file |
|---|
| 129 | file_pos = lseek( file_handle, 0, SEEK_CUR ); |
|---|
| 130 | return (file_pos >= 0); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | bool restore_pos() { |
|---|
| 134 | if (!is_readonly) return false; // cannot jump in a writeable file |
|---|
| 135 | if (file_pos < 0) return false; |
|---|
| 136 | if (lseek( file_handle, file_pos, SEEK_SET ) < 0) return false; |
|---|
| 137 | size = 0; |
|---|
| 138 | position = 0; |
|---|
| 139 | refill(); |
|---|
| 140 | return true; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | bool empty() { |
|---|
| 144 | if (size == 0) return true; |
|---|
| 145 | return (position < size); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | void clear() { // 'clear' buffer |
|---|
| 149 | size = 0; |
|---|
| 150 | position = 0; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | bool isReadonly() { |
|---|
| 154 | return is_readonly; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | void flush(); // write buffer to file |
|---|
| 158 | |
|---|
| 159 | // |
|---|
| 160 | // initialization-functions |
|---|
| 161 | // |
|---|
| 162 | void reinit( const char *name, bool _readonly ); // reinit. with new file |
|---|
| 163 | |
|---|
| 164 | PS_FileBuffer( const char *_name, bool _readonly ); |
|---|
| 165 | |
|---|
| 166 | ~PS_FileBuffer() { |
|---|
| 167 | // finish file |
|---|
| 168 | if (!is_readonly) flush(); |
|---|
| 169 | if (file_name) free(file_name); |
|---|
| 170 | if (file_handle != -1) close(file_handle); |
|---|
| 171 | // finish buffer |
|---|
| 172 | if (buffer) free(buffer); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | }; |
|---|
| 176 | |
|---|
| 177 | #else |
|---|
| 178 | #error ps_filebuffer.hxx included twice |
|---|
| 179 | #endif |
|---|