Changeset 9417 in orxonox.OLD for branches/terrain/src/lib/util/filesys
- Timestamp:
- Jul 24, 2006, 1:23:47 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/terrain/src/lib/util/filesys/binary_file.cc
r9415 r9417 5 5 int bytesize( const char _c ) 6 6 { 7 switch( _c) { 8 case 'f': 9 case 'i': 10 return 4; 11 case 'b': 12 return 1; 13 case 's': 14 return 2; 15 default: 16 return 0; 17 } 7 switch( _c) 8 { 9 case 'f': 10 case 'i': 11 return 4; 12 case 'b': 13 return 1; 14 case 's': 15 return 2; 16 default: 17 return 0; 18 } 18 19 } 19 20 20 21 pDataSemantics BinaryFile::compileSemantics( const char *_semantics ) 21 22 { 22 const char *curr = _semantics; 23 char *tmp = new char[strlen(_semantics)]; 24 //Find out how many subchunks exist. 25 pDataSemantics chunk = new DataSemantics(); 26 bool terminated = false; 27 int open = 0; 28 while ( *curr ) { 29 switch( *curr ) { 30 case 'i': 31 case 'f': 32 case 'b': 33 case 's': 34 chunk->numChunkNodes++; 35 break; 36 case '(': 37 terminated = false; 38 chunk->numChunkNodes++; 39 while ( *curr && terminated == false ) { 40 if ( *curr == ')' ) 41 terminated = true; 42 curr++; 43 } 44 if ( !terminated ) { 45 printf( "Syntax error: ')' is missing" ); 46 delete chunk; 47 return NULL; 48 } 49 break; 50 default: 51 break; 52 } 53 curr++; 54 } 55 //Compile the chunks... 56 chunk->chunks = new pDataSemantics[chunk->numChunkNodes]; 57 int currChunk = 0; 58 curr = _semantics; 59 while ( *curr ) { 60 switch( *curr ) { 61 case 'i': 62 case 'f': 63 case 'b': 64 case 's': 65 chunk->chunks[currChunk] = new DataSemantics(); 66 chunk->chunks[currChunk]->isTerminal = true; 67 chunk->chunks[currChunk]->type = bytesize( *curr ); 68 chunk->chunks[currChunk]->totalSize = bytesize( *curr ); 69 currChunk++; 70 break; 71 case '(': 72 curr++; 73 open = 0; 74 memset( tmp, 0, strlen( _semantics ) ); 75 char * cp = tmp; 76 while ( *curr ) { 77 if ( *curr == '(' ) 78 open++; 79 if ( *curr == ')' ) { 80 if ( open ) 81 open--; 82 else 83 break; 84 } 85 *cp = *curr; 86 cp++; 87 curr++; 88 } 89 *cp = '\0'; 90 chunk->chunks[currChunk] = compileSemantics( tmp ); 91 currChunk++; 92 break; 93 case ')': 94 printf( "Syntax error: ')' shouldn't appear here\n" ); 95 delete[] tmp; delete chunk; 96 return NULL; 97 break; 98 default: 99 int multiplier = 1, quantifier = 0; 100 const char *start = curr; 101 const char *end = curr; 102 while ( isdigit( *end ) ) { 103 end++; 104 } 105 for ( const char *d = end-1; d >=start; --d ) { 106 quantifier+=multiplier*(*d-'0'); 107 multiplier*=10; 108 } 109 if ( start == end ) { 110 printf( "Syntax error: '%c' is not known.\n", *curr ); 111 delete[] tmp; delete chunk; 112 return NULL; 113 } 114 if ( currChunk ) { 115 chunk->chunks[currChunk-1]->quantifier = quantifier; 116 chunk->chunks[currChunk-1]->totalSize*= quantifier; 117 } 118 else { 119 printf( "Syntax error: Quantifier without a preceding type\n" ); 120 delete[] tmp; delete chunk; 121 return NULL; 122 } 123 curr = end-1; 124 break; 125 } 126 curr++; 127 } 128 for ( int i = 0; i < chunk->numChunkNodes; ++i ) { 129 chunk->totalSize+= chunk->chunks[i]->totalSize; 130 } 131 delete[] tmp; 132 return chunk; 23 const char *curr = _semantics; 24 char *tmp = new char[strlen(_semantics)]; 25 //Find out how many subchunks exist. 26 pDataSemantics chunk = new DataSemantics(); 27 bool terminated = false; 28 int open = 0; 29 while ( *curr ) 30 { 31 switch( *curr ) 32 { 33 case 'i': 34 case 'f': 35 case 'b': 36 case 's': 37 chunk->numChunkNodes++; 38 break; 39 case '(': 40 terminated = false; 41 chunk->numChunkNodes++; 42 while ( *curr && terminated == false ) 43 { 44 if ( *curr == ')' ) 45 terminated = true; 46 curr++; 47 } 48 if ( !terminated ) 49 { 50 printf( "Syntax error: ')' is missing" ); 51 delete chunk; 52 return NULL; 53 } 54 break; 55 default: 56 break; 57 } 58 curr++; 59 } 60 //Compile the chunks... 61 chunk->chunks = new pDataSemantics[chunk->numChunkNodes]; 62 int currChunk = 0; 63 curr = _semantics; 64 while ( *curr ) 65 { 66 switch( *curr ) 67 { 68 case 'i': 69 case 'f': 70 case 'b': 71 case 's': 72 chunk->chunks[currChunk] = new DataSemantics(); 73 chunk->chunks[currChunk]->isTerminal = true; 74 chunk->chunks[currChunk]->type = bytesize( *curr ); 75 chunk->chunks[currChunk]->totalSize = bytesize( *curr ); 76 currChunk++; 77 break; 78 case '(': { 79 curr++; 80 open = 0; 81 memset( tmp, 0, strlen( _semantics ) ); 82 char * cp = tmp; 83 while ( *curr ) 84 { 85 if ( *curr == '(' ) 86 open++; 87 if ( *curr == ')' ) 88 { 89 if ( open ) 90 open--; 91 else 92 break; 93 } 94 *cp = *curr; 95 cp++; 96 curr++; 97 } 98 *cp = '\0'; 99 chunk->chunks[currChunk] = compileSemantics( tmp ); 100 currChunk++; 101 } 102 break; 103 case ')': 104 printf( "Syntax error: ')' shouldn't appear here\n" ); 105 delete[] tmp; delete chunk; 106 return NULL; 107 break; 108 default: 109 int multiplier = 1, quantifier = 0; 110 const char *start = curr; 111 const char *end = curr; 112 while ( isdigit( *end ) ) 113 { 114 end++; 115 } 116 for ( const char *d = end-1; d >=start; --d ) 117 { 118 quantifier+=multiplier*(*d-'0'); 119 multiplier*=10; 120 } 121 if ( start == end ) 122 { 123 printf( "Syntax error: '%c' is not known.\n", *curr ); 124 delete[] tmp; delete chunk; 125 return NULL; 126 } 127 if ( currChunk ) 128 { 129 chunk->chunks[currChunk-1]->quantifier = quantifier; 130 chunk->chunks[currChunk-1]->totalSize*= quantifier; 131 } 132 else 133 { 134 printf( "Syntax error: Quantifier without a preceding type\n" ); 135 delete[] tmp; delete chunk; 136 return NULL; 137 } 138 curr = end-1; 139 break; 140 } 141 curr++; 142 } 143 for ( int i = 0; i < chunk->numChunkNodes; ++i ) 144 { 145 chunk->totalSize+= chunk->chunks[i]->totalSize; 146 } 147 delete[] tmp; 148 return chunk; 133 149 } 134 150 135 151 void convert_data( pDataSemantics _comp, void *_data ) 136 152 { 137 char *ptr = (char*)_data; 138 short *sptr; 139 int *iptr; 140 if ( _comp->isTerminal ) { 141 switch( _comp->type ) { 142 case TT_INT: 143 iptr = (int*)ptr; 144 for ( int i = 0; i < _comp->quantifier; ++i ) { 145 swap32( iptr ); 146 iptr++; 147 } 148 break; 149 case TT_SHORT: 150 sptr = (short*)ptr; 151 for ( int i = 0; i < _comp->quantifier; ++i ) { 152 swap16( sptr ); 153 sptr++; 154 } 155 break; 156 case TT_BYTE: 157 break; 158 } 159 } 160 else { 161 for ( int j = 0; j < _comp->quantifier; ++j ) { 162 for ( int i = 0; i < _comp->numChunkNodes; ++i ) { 163 convert_data( _comp->chunks[i], ptr ); 164 ptr+= _comp->chunks[i]->totalSize; 165 } 166 } 167 168 } 153 char *ptr = (char*)_data; 154 short *sptr; 155 int *iptr; 156 if ( _comp->isTerminal ) 157 { 158 switch( _comp->type ) 159 { 160 case TT_INT: 161 iptr = (int*)ptr; 162 for ( int i = 0; i < _comp->quantifier; ++i ) 163 { 164 swap32( iptr ); 165 iptr++; 166 } 167 break; 168 case TT_SHORT: 169 sptr = (short*)ptr; 170 for ( int i = 0; i < _comp->quantifier; ++i ) 171 { 172 swap16( sptr ); 173 sptr++; 174 } 175 break; 176 case TT_BYTE: 177 break; 178 } 179 } 180 else 181 { 182 for ( int j = 0; j < _comp->quantifier; ++j ) 183 { 184 for ( int i = 0; i < _comp->numChunkNodes; ++i ) 185 { 186 convert_data( _comp->chunks[i], ptr ); 187 ptr+= _comp->chunks[i]->totalSize; 188 } 189 } 190 191 } 169 192 } 170 193 void BinaryFile::read( const char *_semantics, int _chunks, 171 void* _buf, size_t& _bytesRead ) 172 { 173 pDataSemantics comp = compileSemantics( _semantics ); 174 fread( _buf, comp->totalSize, _chunks, this->handle() ); 175 if ( byteorder != NATIVE_BYTEORDER ) { 176 for ( int i = 0; i < _chunks; ++i ) { 177 convert_data( comp, (char*)_buf+i*comp->totalSize ); 178 } 179 } 180 _bytesRead = comp->totalSize*_chunks; 181 delete comp; 194 void* _buf, size_t& _bytesRead ) 195 { 196 pDataSemantics comp = compileSemantics( _semantics ); 197 fread( _buf, comp->totalSize, _chunks, this->handle() ); 198 if ( byteorder != NATIVE_BYTEORDER ) 199 { 200 for ( int i = 0; i < _chunks; ++i ) 201 { 202 convert_data( comp, (char*)_buf+i*comp->totalSize ); 203 } 204 } 205 _bytesRead = comp->totalSize*_chunks; 206 delete comp; 182 207 } 183 208 void BinaryFile::read( const char *_semantics, 184 void* _buf, size_t& _bytesRead ) 185 { 186 pDataSemantics comp = compileSemantics( _semantics ); 187 fread( _buf, comp->totalSize, 1, this->handle() ); 188 189 if ( byteorder != NATIVE_BYTEORDER ) { 190 convert_data( comp, _buf ); 191 } 192 delete comp; 209 void* _buf, size_t& _bytesRead ) 210 { 211 pDataSemantics comp = compileSemantics( _semantics ); 212 fread( _buf, comp->totalSize, 1, this->handle() ); 213 214 if ( byteorder != NATIVE_BYTEORDER ) 215 { 216 convert_data( comp, _buf ); 217 } 218 delete comp; 193 219 } 194 220 195 221 void BinaryFile::write( const char *_semantics, 196 void* _buf, size_t& _bytesWritten ) 197 { 198 pDataSemantics comp = compileSemantics( _semantics ); 199 if ( byteorder != NATIVE_BYTEORDER ) { 200 convert_data( comp, _buf ); 201 } 202 _bytesWritten = comp->totalSize; 203 fwrite( _buf, comp->totalSize, 1, this->handle() ); 204 delete comp; 205 } 222 void* _buf, size_t& _bytesWritten ) 223 { 224 pDataSemantics comp = compileSemantics( _semantics ); 225 if ( byteorder != NATIVE_BYTEORDER ) 226 { 227 convert_data( comp, _buf ); 228 } 229 _bytesWritten = comp->totalSize; 230 fwrite( _buf, comp->totalSize, 1, this->handle() ); 231 delete comp; 232 }
Note: See TracChangeset
for help on using the changeset viewer.