Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/Fred/JFile.cpp

Go to the documentation of this file.
00001 #ifndef __J2K__JFile_CPP__
00002 #define __J2K__JFile_CPP__
00003 
00004 #include <assert.h>
00005 #include <j2k/Fred/JFile.hpp>
00006 
00007 JFile::JFile() 
00008  : f( NULL ), fmode( "rb" ), recsize( 0 ), 
00009    fpath( NULL ), fpos( 0 ), current( 0 ) { }
00010 
00011 FILE* JFile::F() {
00012     assert( f != NULL ); // More sophisticated test needed.
00013   return f;
00014 }
00015 
00016 /*
00017 The fopen() function opens a file stream for the file specified by filename. 
00018 The mode string begins with one of the following sequences: 
00019 
00020 a   Append: create a new file or open the file for writing at its end. 
00021 a+  Append: open the file or create it for update, writing at end-of-file; 
00022     use the default file translation. 
00023 
00024 r   Open the file for reading. 
00025 r+  Open the file for update (reading and/or writing); 
00026     use the default file translation. 
00027 
00028 w   Create the file for writing, or truncate it to zero length. 
00029 w+  Create the file for update, or truncate it to zero length; 
00030     use the default file translation. 
00031 
00032 You can add the letter b to the end of any of the above sequences to indicate 
00033 that the file is (or must be) a binary file (this is an ANSI requirement 
00034 for portability to systems that make a distinction between text
00035 and binary files, such as DOS).
00036 
00037 Opening a file in read mode (r in the mode) fails if the file doesn't exist 
00038 or can't be read. Opening a file in append mode (a in the mode) causes 
00039 all subsequent writes to the file to be forced to the current end-of-file, 
00040 regardless of previous calls to the fseek() function. 
00041 When a file is opened with update mode (+ in the mode), 
00042 both input and output may be performed on the associated stream. 
00043 */
00044 
00045 JFile::JFile( const char* path, const char* mode, size_t recordsize ) 
00046 : fpath( path ), fmode( mode ), recsize( recordsize ), fpos( 0 ), current( 0 )
00047 { 
00048   printf( "\nOpening %s...\n", path ); 
00049   f = ::fopen( path, mode );
00050   if ( f == NULL ) {
00051     printf("Unable to open: %s \n", path );
00052     MC_OnError( errno, Error, "while opening a JFile." )
00053  // TFTP_FileNotFoundErrorHandler( Error, (JErrorNo)2, "can't open file", __LINE__, __FILE__ );
00054     exit(1);
00055   }
00056 }
00057 
00058 
00059 /*
00060 The fclose() function closes the stream specified by fp. 
00061 Any unwritten, buffered data is flushed before the file is closed. 
00062 Any unread, buffered data is discarded. 
00063 If the associated buffer was automatically allocated, it is deallocated.
00064 */
00065 JFile::~JFile() {
00066   if ( f != NULL ) {
00067     if ( fclose(f) == EOF ) {
00068       printf("Unable to close: %s \n", fpath );
00069       MC_OnError( errno, Error, "while closing a JFile." )
00070     }
00071   }
00072 }
00073 
00074 // Buffer provided just copy over...
00075 void JFile::getFileName( char* buffer )
00076 {
00077   long len = strlen( fpath );
00078   long   p   = len;
00079 
00080   for( ; p >= 0; p-- ) {
00081     if ( fpath[p] == '\\' || fpath[p] == '/' ) {
00082       memcpy( buffer, (fpath + p + 1), (len - p + 1) );
00083       return;
00084     }
00085   }
00086 
00087   // Direct filename, no path included.
00088   memcpy( buffer, fpath, len );
00089 }
00090 
00091 // No buffer provided
00092 char* JFile::getFileName()
00093 {    
00094   size_t len = strlen( fpath );
00095   char* s = new char[ len ];  
00096   memset( s, 0, len );
00097 
00098   getFileName( s );
00099 
00100   return s;
00101 }
00102 
00103 /*
00104 The open stream fp is closed, and the file specified by filename is opened, 
00105 associating its stream with fp. 
00106 
00107 The mode is described in the description of the fopen() function.
00108 */
00109 
00110 void JFile::reopen() {
00111   f = ::freopen( fpath, fmode, f );
00112   if ( f == NULL ) {
00113     printf("Unable to reopen: %s \n", fpath );
00114     MC_OnError( errno, Error, "while reopening a JFile." )
00115     exit(1);
00116   }
00117 }
00118 
00119 /* Read a character from a stream
00120    The next character from fp,
00121    cast as (int)(unsigned char),
00122    or EOF if end-of-file has been reached
00123    or if an error occurs (errno is set).
00124 */
00125 int JFile::getChar() {
00126   int c = fgetc( F() );
00127   MC_OnError( errno, Error, "while retrieving a character from a JFile." )  
00128   return c;
00129 }
00130 
00131 long JFile::getData( char* buffer, size_t length )
00132 {
00133   memset( buffer, 0, length );
00134   long p = 0;
00135   long neof = 1;
00136   for( ; p < (long)length; p++ ) {
00137 
00138     int c = fgetc( F() );
00139 //    char c;
00140 //    size_t rc = readChar( &c );
00141     if ( c == EOF ) { neof = -1; break; }
00142     MC_OnError( errno, Error, "while retrieving data from a JFile." )
00143     buffer[ p ] = (char)c;
00144   }
00145   p++;
00146   printf("[%u|%u]", p, length );
00147   if ( p == (long)length ) addCurrent( (long)length );
00148   return p * neof;
00149 }
00150 
00151 /* Push a character back onto an input stream
00152 
00153 The ungetc() function pushes the character specified by c 
00154 back onto the input stream pointed to by fp. 
00155 This character will be returned by the next read on the stream. 
00156 The pushed-back character is discarded if a call is made 
00157 to the fflush() function or to a file-positioning function 
00158 (fseek(), fsetpos() or rewind()) before the next read operation is performed. 
00159 */
00160 inline int JFile::ungetChar( int c ) {
00161   // No errno set in any case.
00162   return ::ungetc( c, F() );  
00163 }
00164 
00165 
00166 /* Write a character to a stream
00167 The fputc() function writes the character specified by c, 
00168 cast as (int)(unsigned char), to the stream specified by fp.
00169 
00170 The character written, cast as (int)(unsigned char),
00171 or EOF if an error occurred (errno is set).
00172 */
00173 void JFile::putChar( int c ) {
00174   register int rc = ::fputc( c, F() );
00175   MC_OnError( errno, Error, "while saving a character inside a JFile." )   
00176 }
00177 
00178 size_t JFile::putData( char* buffer, long length, long blocksize )
00179 {
00180   size_t p  = 0;
00181   int    rc = 0;
00182   int    c  = 0;
00183   int    z  = 0;
00184   printf("\nlength=%u|%u{", length, blocksize );
00185   for( p = 0; (long)p < length; p++ )
00186   {
00187     c  = (int)buffer[p];
00188     if ( c == 0 ) z++;
00189   }
00190 
00191   printf("(%u)", z );
00192   fflush( stdout );
00193   if ( z >= 3 ) return 0;
00194 
00195   for( p = 0; (long)p < length; p++ )
00196   {
00197     c  = (int)buffer[p];
00198     printf( "[%u]", (char)c );
00199     rc = fputc( c, F() );
00200     MC_OnError( errno, Error, "while saving data from a JFile." )
00201   }
00202   printf("}\n");
00203   fflush( stdout);
00204 
00205 //  p++;
00206 #ifdef DEBUG
00207   printf( "PutData[%s|%d=%d,%d,%d]", buffer, length, p, c, rc );
00208 #endif
00209   if ( (long)p == length ) addCurrent( length );
00210   return p;
00211 }
00212 
00213 
00214 /*
00215 Write a string to an output stream
00216 The fputs() function writes the character string specified by buf 
00217 to the output stream specified by fp. 
00218 The terminating NULL character is not written.
00219 
00220 A nonnegative value for success, or EOF if an error occurs (errno is set).
00221 */
00222 void JFile::putStr( const char* s ) {
00223   register int c = ::fputs( s, F() );
00224   if ( c < 0 ) {
00225     MC_OnError( errno, Error, "while saving a string inside a JFile." )
00226   }
00227 }
00228 
00229 /*
00230 Read a string of characters from a stream
00231 The fgets() function reads a string of characters from the stream 
00232 specified by fp, and stores them in the array specified by buf. 
00233 
00234 It stops reading characters when: 
00235 
00236 the end-of-file is reached 
00237 Or: 
00238 
00239 a newline ('\n') character is read 
00240 Or: 
00241 
00242 n-1 characters have been read. 
00243 The newline character isn't discarded. 
00244 A null character is placed immediately
00245 after the last character read into the array.
00246 
00247 ----------------------------------------------------------------------------
00248  Don't assume the presence of a newline character in every string
00249  that is read with fgets(). A newline character isn't be present
00250  when more than n-1 characters occur before the newline.
00251  Also, a newline character might not appear as the last character
00252  in a file when the end-of-file is reached.
00253 ----------------------------------------------------------------------------
00254 The same pointer as buf for success,
00255 or NULL if the stream is at the end-of-file
00256 or an error occurs (errno is set).
00257 */
00258 char* JFile::getStr( char* s, int n ) {
00259   register char* rc = ::fgets( s, n, F() );
00260   if ( rc == NULL ) {
00261     MC_OnError( errno, Error, "while retrieving a string from a JFile." )  
00262   }
00263   return s;
00264 }
00265 
00266 /*
00267 Write formatted output to a file
00268 
00269 The vfprintf() function writes output to the file pointed to by fp, 
00270 under control of the argument format. 
00271 
00272 The vfprintf() function is equivalent to the fprintf() function, 
00273 with the variable argument list replaced with arg, 
00274 which has been initialized by the va_start() macro.
00275 
00276 Returns:
00277 The number of characters written, 
00278 or a negative value if an output error occurred (errno is set).
00279 */
00280 int JFile::print( const char* format, ... )
00281 {
00282    register int cnt;
00283    va_list argptr;
00284    va_start(argptr, format);
00285    cnt = ::vfprintf(F(), format, argptr );
00286    va_end(argptr);
00287 
00288    if ( cnt < 0 ) {
00289      MC_OnError( errno, Error, "while printing a formated string inside a JFile." )  
00290    }
00291    return cnt;
00292 }
00293 
00294 /*
00295 Read elements from a file
00296 The fread() function reads num elements of size bytes each 
00297 from the stream specified by fp into the buffer specified by buf.
00298 
00299 Returns:
00300 The number of complete elements successfully read; 
00301 this value may be less than the requested number of elements. 
00302 
00303 Use the feof() and ferror() functions to determine 
00304 whether the end of the file was encountered or if an input/output error has occurred.
00305 */
00306 
00307 size_t JFile::readChar( char* c ) {
00308   return read( c, 1, 1 );
00309 }
00310 
00311 size_t JFile::read( void* ptr, size_t size, size_t n ) 
00312 {
00313   register size_t rc = ::fread( ptr, size, n, F() );
00314 
00315   if (  ferror( F() )  ) {
00316     printf( "Error while reading elements from a JFile: %s\n", fpath );
00317     MC_OnError( errno, Error, "while retrieving  elements from a JFile." )
00318     exit( 1 );
00319   }
00320         
00321   if (  feof( F() )    ) {
00322     printf( "End of file reached.\n" );
00323     return (size_t)(-1);
00324   }
00325 
00326   return rc;
00327 }
00328 
00329 /*
00330 Write elements to a file
00331 The fwrite() function writes num elements of size bytes each
00332 to the stream specified by fp.
00333 
00334 Returns:
00335 The number of complete elements successfully written; 
00336 if an error occurs, this will be less than num.
00337 */
00338 size_t JFile::write( const void* ptr, size_t size, size_t n ) 
00339 {
00340   register size_t rc = ::fwrite(ptr, size, n, F());
00341   MC_OnError( errno, Error, "while writing elements inside a JFile." )  
00342   return rc;
00343 }
00344 
00345 /*
00346 Test a stream's end-of-file flag
00347 The feof() function tests the end-of-file flag for the stream specified by fp. 
00348 
00349 Because the end-of-file flag is set when an input operation attempts 
00350 to read past the end-of-file, the feof() function detects the end-of-file 
00351 only after an attempt is made to read beyond the end-of-file. 
00352 Thus, if a file contains 10 lines, the feof() won't detect the end-of-file 
00353 after the tenth line is read; it will detect the end-of-file on the next read operation.
00354 
00355 Returns:
00356 0 if the end-of-file flag isn't set, or nonzero if the end-of-file flag is set.
00357 */
00358 int JFile::eof() { 
00359   return feof( F() ); 
00360 }
00361 
00362 /*
00363 Flush the buffers for a stream
00364 
00365 If the stream specified by fp is open for output or update, 
00366 the fflush() function causes any buffered (see setvbuf()) 
00367 but unwritten data to be written to the file descriptor associated 
00368 with the stream (see fileno()). 
00369 
00370 If the file specified by fp is open for input or update, 
00371 the fflush() function undoes the effect of any preceding ungetc operation on the stream. 
00372 
00373 If fp is NULL all open streams are flushed.
00374 
00375 Returns:
00376 0 for success, or EOF if an error occurs (errno is set).
00377 */
00378 int JFile::flush() { 
00379   return ::fflush( F() ); 
00380 }
00381 
00382 /*
00383 Change the current position of a stream
00384 
00385 The fseek() function changes the current position of the stream specified by fp. 
00386 This position defines the character that will be read or written 
00387 by the next I/O operation on the file. fp is a FILE pointer 
00388 returned by fopen() or freopen(). The offset is the position to seek to, 
00389 relative to one of three positions specified by whence. 
00390 
00391 Valid whence values are: 
00392 
00393 SEEK_SET 
00394 The new file position is computed relative to the start of the file. 
00395 The value of offset must not be negative. 
00396 
00397 SEEK_CUR 
00398 The new file position is computed relative to the current file position. 
00399 The value of offset may be positive, negative or zero. 
00400 A SEEK_CUR with a 0 offset is necessary when you want to switch 
00401 from reading to writing on a stream opened for updates. 
00402 
00403 SEEK_END 
00404 The new file position is computed relative to the end of the file. 
00405 The fseek() function clears the end-of-file indicator, 
00406 and undoes any effects of the ungetc() function on the stream. 
00407 
00408 The ftell() function can be used to get the current position of the stream 
00409 before changing it. The position can be restored by using the value 
00410 returned by ftell() in a subsequent call to fseek() 
00411 with the whence parameter set to SEEK_SET.
00412 
00413 Returns:
00414 0 for success, or nonzero if an error occurs.
00415 */
00416 int JFile::seek( long offset, int whence )
00417 {
00418   register int rc = ::fseek(F(), offset, whence);
00419   if ( rc != 0 ) {
00420     MC_OnError( errno, Error, "while seeking inside a JFile." )
00421   }
00422   return rc;
00423 }
00424 
00425 /*
00426 Get the current position of a stream
00427 
00428 The fgetpos() function stores the current position of the stream fp 
00429 in the fpos_t object specified by pos. 
00430 
00431 The value stored in pos can be used by fsetpos() for repositioning 
00432 the file to the position at the time of the fgetpos() call.
00433 
00434 Returns:
00435 0 for success, or nonzero if an error occurs (errno is set).
00436 */
00437 int JFile::getpos( fpos_t* pos ) {
00438   register int rc = ::fgetpos(F(), pos);
00439   if ( rc != 0 ) {
00440     MC_OnError( errno, Error, "while retrieving the current position inside a JFile." )
00441   }
00442   return rc;
00443 }
00444 
00445 /*
00446 Set the current position of a file
00447 
00448 The fsetpos() function sets the current position of the stream specified 
00449 by fp according to the value of the fpos_t object pointed to by pos. 
00450 
00451 The value pointed to by pos must have been set by a call to fgetpos() on the same file.
00452 
00453 Returns:
00454 0 for success, or nonzero if an error occurs (errno is set).
00455 
00456 */
00457 
00458 int JFile::setpos( const fpos_t* pos ) {
00459   register int rc = ::fsetpos(F(), pos);
00460   if ( rc != 0 ) {
00461     MC_OnError( errno, Error, "while setting the current position inside a JFile." )
00462   }
00463   return rc;
00464 }
00465 
00466 void JFile::setBegin( long offset ) {
00467   register int rc = ::fseek(F(), offset, SEEK_SET );
00468   if ( rc != 0 ) {
00469     MC_OnError( errno, Error, "while seeking inside a JFile." )
00470   }
00471 }
00472 
00473 #if 0
00474 void JFile::addCurrent( long offset ) {
00475   current += offset;
00476   printf("OFFSET=%u\n", current );
00477   fflush( stdout );
00478   register int rc = ::fseek(F(), offset, SEEK_CUR );
00479   if ( rc != 0 ) {
00480     MC_OnError( errno, Error, "while seeking inside a JFile." )
00481   }
00482 }
00483 #endif
00484 
00485 void JFile::addCurrent( long offset ) {
00486   current += offset;
00487   printf("OFFSET=%u\n", current );
00488   fflush( stdout );
00489   register int rc = ::fseek(F(), current, SEEK_SET );
00490   if ( rc != 0 ) {
00491     MC_OnError( errno, Error, "while seeking inside a JFile." )
00492   }
00493 }
00494 
00495 void JFile::setEnd( long offset ) {
00496   register int rc = ::fseek(F(), offset, SEEK_END );
00497   if ( rc != 0 ) {
00498     MC_OnError( errno, Error, "while seeking inside a JFile." )
00499   }
00500 }
00501 
00502 /*
00503 Return the current position of a stream
00504 
00505 The ftell() function returns the current position of the stream specified by fp. 
00506 This position defines the character that will be read or written 
00507 by the next I/O operation on the file. The value returned by ftell() 
00508 can be used in a subsequent call to fseek() to restore the file position 
00509 to a previous value. 
00510 
00511 Returns:
00512 The current position of the file or -1L if an error occurred (errno is set).
00513 */
00514 long JFile::tell() {
00515   register long rc = ::ftell( F() );
00516   if ( rc < 0 ) {
00517     MC_OnError( errno, Error, "while getting the current position inside a JFile." )
00518   }
00519 
00520   return rc;
00521 }
00522 
00523 /*
00524 Rewind a file stream to the beginning of the file
00525 
00526 The rewind() function rewinds the file stream specified by fp to the beginning 
00527 of the file. It's equivalent to calling fseek() like this: 
00528 
00529 fseek( fp, 0L, SEEK_SET );
00530 except that the error indicator for the stream is cleared.
00531 */
00532 void JFile::rewind() {
00533   ::rewind( F() );
00534   MC_OnError( errno, Error, "while rewinding a JFile." )
00535 }
00536 
00537 void JFile::setbuf( char* buf ) {
00538   ::setbuf(F(), buf);
00539   MC_OnError( errno, Error, "while setting a JFile buffer." )
00540 }
00541 
00542 int JFile::setvbuf( char* buf, int type, size_t sz ) {
00543   int rc = ::setvbuf(F(), buf, type, sz);
00544   MC_OnError( errno, Error, "while setting a formated JFile buffer." )
00545   return rc;
00546 }
00547 
00548 /*
00549 Test a stream's error flag
00550 The ferror() function tests the error flag for the stream specified by fp.
00551 
00552 Returns:
00553 0 if the error flag isn't set, or nonzero if the error flag is set.
00554 */
00555 int JFile::error() {
00556   register int rc = ferror( F() );
00557   MC_OnError( errno, Error, "while checking for a JFile error." )
00558   return rc;
00559 }
00560 
00561 void JFile::clearError() {
00562   clearerr( F() );
00563   MC_OnError( errno, Error, "while clearing a JFile error." )
00564 }
00565 
00566 /*
00567 fich= fopen (nom_du_fichier, "rb");
00568 fseek (fich, 0, SEEK_END);
00569 taille_fichier= ftell (fich)
00570 ;fclose (fich);
00571 */
00572 
00573 size_t JFile::getFileSize()
00574 {
00575    struct stat value;
00576 
00577    int result = ::stat( fpath, &value );
00578 
00579    //fstat( F(), &value );
00580 
00581    // Validate data
00582    assert( result != 0 );
00583 
00584    MC_OnError( errno, Error, "while getting a JFile size." )
00585  
00586    // File Size     
00587    return ( value.st_size );
00588 }
00589 
00590 #endif

Generated on Sun Oct 14 18:46:32 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001