Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

LWFile.cpp

Go to the documentation of this file.
00001 
00002 /*
00003     TEDDY - General graphics application library
00004     Copyright (C) 1999-2002  Timo Suoranta
00005     tksuoran@cc.helsinki.fi
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2.1 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     $Id: LWFile.cpp,v 1.4 2002/01/11 14:34:59 tksuoran Exp $
00022 */
00023 
00024 
00025 #include "Teddy/TeddyConfig.h"
00026 #if defined( TEDDY_INCLUDE_LW_SCENE )
00027 
00028 
00029 #include "Teddy/Imports/LWFile.h"
00030 #include "Teddy/SysSupport/EndianIn.h"
00031 #include "Teddy/SysSupport/StdIO.h"
00032 using namespace Teddy::SysSupport;
00033 
00034 
00035 namespace Teddy   {
00036 namespace Imports {
00037 
00038 
00043 extern char *did( ID4 id4 ){
00044     static char id[5*10];
00045     static int  counter = 0;
00046 
00047     char *start = id + counter * 5;
00048     counter++;
00049     if( counter >9 ){
00050         counter = 0;
00051     }
00052 
00053     sprintf(
00054         start,
00055         "%c%c%c%c",
00056         (char)((id4 & 0xff000000)>>24),
00057         (char)((id4 & 0x00ff0000)>>16),
00058         (char)((id4 & 0x0000ff00)>> 8),
00059         (char)((id4 & 0x000000ff)    )
00060     );
00061     return start;
00062 }
00063 
00064 
00065 void LWFile::setType( ID4 file_type ){
00066     this->file_type = file_type;
00067 }
00068 
00069 
00070 ID4 LWFile::getType() const {
00071     return file_type;
00072 }
00073 
00074 
00075 void LWFile::setLen( Uint32 len ){
00076     file_len = len;
00077 }
00078 
00079 
00080 Uint32 LWFile::getLen() const {
00081     return file_len;
00082 }
00083 
00084 
00086 LWFile::LWFile( const std::string &name, Uint32 options = 0 ){
00087     this->dsp     = NULL;
00088     this->options = options;
00089     open( name );
00090 }
00091 
00092 
00094 LWFile::~LWFile(){
00095     close();
00096 }
00097 
00098 
00100 void LWFile::open( const std::string &name ){
00101     is = new EndianIn( name );
00102     is->set_bigendian();
00103     bytes_read = 0;
00104 
00105     // Initialize domain gurdians
00106     for( int i=0; i<DOMAIN_STACK_SIZE; i++ ){
00107         d_ends[i] = 0;
00108     }
00109     dsp = d_ends;
00110 }
00111 
00112 
00114 void LWFile::close(){
00115     is->close(), is = NULL;
00116 }
00117 
00118 
00120 void LWFile::setOptions( Uint32 options ){
00121     this->options = options;
00122 }
00123 
00124 
00126 Uint32 LWFile::getOptions(){
00127     return this->options;
00128 }
00129 
00130 
00131 //static int depth = 0;
00132 
00134 void LWFile::pushDomain( Uint32 len ){
00135     dmsg( M_LWO, "pushDomain len %lu pos %lu", len, bytes_read );
00136 //  cout << "PUSH " << ++depth << endl;
00137     //  FIX Add stack check
00138     *(++dsp) = bytes_read + len;
00139 }
00140 
00141 
00143 U4 LWFile::popDomain( bool skip_rest ){
00144 //  cout << "POP " << --depth << endl;
00145     if( skip_rest ){
00146         while( *dsp > bytes_read ){
00147             (void)read_U1();
00148         }
00149     }
00150     //  FIX Add stack check
00151     dsp--;
00152     dmsg( M_LWO, "popDomain pos %lu", bytes_read );
00153     return (*dsp) - (bytes_read);
00154 }
00155 
00156 
00158 U4 LWFile::domainLeft(){
00159     return *dsp - bytes_read;
00160 }
00161 
00162 
00164 U4 LWFile::bytesRead(){
00165     return bytes_read;
00166 }
00167 
00168 
00170 ID4 LWFile::read_ID4(){
00171     if( (*dsp) >= (bytes_read+4) ){
00172         ID4 data = is->read_long();
00173         bytes_read += 4;
00174         return data;
00175     }else{
00176         return 0;
00177     }
00178 }
00179 
00180 
00182 I1 LWFile::read_I1(){
00183     if( (*dsp) >= (bytes_read+1) ){
00184         I1 data = is->read_byte();
00185         bytes_read++;
00186         return data;
00187     }else{
00188         return 0;
00189     }
00190 }
00191 
00192 
00194 I2 LWFile::read_I2(){
00195     if( (*dsp) >= (bytes_read+2) ){
00196         I2 data = is->read_short();
00197         bytes_read += 2;
00198         return data;
00199     }else{
00200         return 0;
00201     }
00202 }
00203 
00204 
00205 I4 LWFile::read_I4(){
00206     if( (*dsp) >= (bytes_read+4) ){
00207         I4 data = is->read_long();
00208         bytes_read += 4;
00209         return data;
00210     }else{
00211         return 0;
00212     }
00213 }
00214 
00215 
00216 //1  Read U1
00217 U1 LWFile::read_U1(){
00218     if( (*dsp) >= (bytes_read+1) ){
00219         U1 data = is->read_byte();
00220         bytes_read++;
00221         return data;
00222     }else{
00223         return 0;
00224     }
00225 }
00226 
00227 
00229 U2 LWFile::read_U2(){
00230     if( (*dsp) >= (bytes_read+2) ){
00231         U2 data = is->read_short();
00232         bytes_read += 2;
00233         return data;
00234     }else{
00235         return 0;
00236     }
00237 }
00238 
00239 
00241 U4 LWFile::read_U4(){
00242     if( (*dsp) >= (bytes_read+4) ){
00243         U4 data = is->read_long();
00244         bytes_read += 4;
00245         return data;
00246     }else{
00247         return 0;
00248     }
00249 }
00250 
00251 
00253 F4 LWFile::read_F4(){
00254     if( (*dsp) >= (bytes_read+4) ){
00255         F4 data = is->read_float();
00256         bytes_read += 4;
00257         return data;
00258     }else{
00259         return 0;
00260     }
00261 }
00262 
00263 
00265 S0 LWFile::read_S0(){
00266     Uint8 *buffer = new Uint8[256];
00267     Uint8  c      = 0xff;
00268     int    i      = 0;
00269 
00270     while( (*dsp) >= (bytes_read+1) && c !=0 && i<255 ){
00271         buffer[i++] = c = read_U1();
00272     }
00273 
00274     if( i>254 ){
00275         buffer[i] = 0;
00276     }
00277 
00278     if( bytes_read & 1 ){
00279         (void)read_U1();
00280     }
00281 
00282     return (char*)(buffer);
00283 }
00284 
00285 
00287 VX LWFile::read_VX(){
00288     U2 data = read_U2();
00289 
00290     if( (data & 0xff00) == 0xff00 ){
00291         U2 data2 = read_U2();
00292         return ((data & 0x00ff) << 16) | data2;
00293     }else{
00294         return data;
00295     }
00296 }
00297 
00298 
00300 COL4 LWFile::read_COL4(){
00301     float red   = (float)(read_I1()/255.0f);
00302     float green = (float)(read_I1()/255.0f);
00303     float blue  = (float)(read_I1()/255.0f);
00304     return Color( red, green, blue );
00305 }
00306 
00307 
00309 COL12 LWFile::read_COL12(){
00310     float red   = read_F4();
00311     float green = read_F4();
00312     float blue  = read_F4();
00313     return Color( red, green, blue );
00314 }
00315 
00316 
00318 VEC12 LWFile::read_VEC12(){
00319     float x = read_F4();
00320     float y = read_F4();
00321     float z = read_F4();
00322     return Vector( x, y, -z );
00323 }
00324 
00325 
00327 FP4 LWFile::read_FP4(){
00328     return read_F4();
00329 }
00330 
00331 
00333 ANG4 LWFile::read_ANG4(){
00334     return read_F4();
00335 }
00336 
00337 
00338 FNAM0 LWFile::read_FNAM0(){
00339     return read_S0();
00340 }
00341 
00342 
00343 };  //  namespace Imports
00344 };  //  namespace Teddy
00345 
00346 
00347 #endif  //  TEDDY_INCLUDE_LW_SCENE