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

C:/temp/src/j2k/Fred/JOFile.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 < 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 == 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; 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; 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 ( 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 }
00264 
00265 /*
00266 Write formatted output to a file
00267 
00268 The vfprintf() function writes output to the file pointed to by fp, 
00269 under control of the argument format. 
00270 
00271 The vfprintf() function is equivalent to the fprintf() function, 
00272 with the variable argument list replaced with arg, 
00273 which has been initialized by the va_start() macro.
00274 
00275 Returns:
00276 The number of characters written, 
00277 or a negative value if an output error occurred (errno is set).
00278 */
00279 int JFile::print( const char* format, ... )
00280 {
00281    register int cnt;
00282    va_list argptr;
00283    va_start(argptr, format);
00284    cnt = ::vfprintf(F(), format, argptr );
00285    va_end(argptr);
00286 
00287    if ( cnt < 0 ) {
00288      MC_OnError( errno, Error, "while printing a formated string inside a JFile." )  
00289    }
00290    return cnt;
00291 }
00292 
00293 /*
00294 Read elements from a file
00295 The fread() function reads num elements of size bytes each 
00296 from the stream specified by fp into the buffer specified by buf.
00297 
00298 Returns:
00299 The number of complete elements successfully read; 
00300 this value may be less than the requested number of elements. 
00301 
00302 Use the feof() and ferror() functions to determine 
00303 whether the end of the file was encountered or if an input/output error has occurred.
00304 */
00305 
00306 size_t JFile::readChar( char* c ) {
00307   return read( c, 1, 1 );
00308 }
00309 
00310 size_t JFile::read( void* ptr, size_t size, size_t n ) 
00311 {
00312   register size_t rc = ::fread( ptr, size, n, F() );
00313 
00314   if (  ferror( F() )  ) {
00315     printf( "Error while reading elements from a JFile: %s\n", fpath );
00316     MC_OnError( errno, Error, "while retrieving  elements from a JFile." )
00317     exit( 1 );
00318   }
00319         
00320   if (  feof( F() )    ) {
00321     printf( "End of file reached.\n" );
00322     return (size_t)(-1);
00323   }
00324 
00325   return rc;
00326 }
00327 
00328 /*
00329 Write elements to a file
00330 The fwrite() function writes num elements of size bytes each
00331 to the stream specified by fp.
00332 
00333 Returns:
00334 The number of complete elements successfully written; 
00335 if an error occurs, this will be less than num.
00336 */
00337 size_t JFile::write( const void* ptr, size_t size, size_t n ) 
00338 {
00339   register size_t rc = ::fwrite(ptr, size, n, F());
00340   MC_OnError( errno, Error, "while writing elements inside a JFile." )  
00341   return rc;
00342 }
00343 
00344 /*
00345 Test a stream's end-of-file flag
00346 The feof() function tests the end-of-file flag for the stream specified by fp. 
00347 
00348 Because the end-of-file flag is set when an input operation attempts 
00349 to read past the end-of-file, the feof() function detects the end-of-file 
00350 only after an attempt is made to read beyond the end-of-file. 
00351 Thus, if a file contains 10 lines, the feof() won't detect the end-of-file 
00352 after the tenth line is read; it will detect the end-of-file on the next read operation.
00353 
00354 Returns:
00355 0 if the end-of-file flag isn't set, or nonzero if the end-of-file flag is set.
00356 */
00357 int JFile::eof() { 
00358   return feof( F() ); 
00359 }
00360 
00361 /*
00362 Flush the buffers for a stream
00363 
00364 If the stream specified by fp is open for output or update, 
00365 the fflush() function causes any buffered (see setvbuf()) 
00366 but unwritten data to be written to the file descriptor associated 
00367 with the stream (see fileno()). 
00368 
00369 If the file specified by fp is open for input or update, 
00370 the fflush() function undoes the effect of any preceding ungetc operation on the stream. 
00371 
00372 If fp is NULL all open streams are flushed.
00373 
00374 Returns:
00375 0 for success, or EOF if an error occurs (errno is set).
00376 */
00377 int JFile::flush() { 
00378   return ::fflush( F() ); 
00379 }
00380 
00381 /*
00382 Change the current position of a stream
00383 
00384 The fseek() function changes the current position of the stream specified by fp. 
00385 This position defines the character that will be read or written 
00386 by the next I/O operation on the file. fp is a FILE pointer 
00387 returned by fopen() or freopen(). The offset is the position to seek to, 
00388 relative to one of three positions specified by whence. 
00389 
00390 Valid whence values are: 
00391 
00392 SEEK_SET 
00393 The new file position is computed relative to the start of the file. 
00394 The value of offset must not be negative. 
00395 
00396 SEEK_CUR 
00397 The new file position is computed relative to the current file position. 
00398 The value of offset may be positive, negative or zero. 
00399 A SEEK_CUR with a 0 offset is necessary when you want to switch 
00400 from reading to writing on a stream opened for updates. 
00401 
00402 SEEK_END 
00403 The new file position is computed relative to the end of the file. 
00404 The fseek() function clears the end-of-file indicator, 
00405 and undoes any effects of the ungetc() function on the stream. 
00406 
00407 The ftell() function can be used to get the current position of the stream 
00408 before changing it. The position can be restored by using the value 
00409 returned by ftell() in a subsequent call to fseek() 
00410 with the whence parameter set to SEEK_SET.
00411 
00412 Returns:
00413 0 for success, or nonzero if an error occurs.
00414 */
00415 int JFile::seek( long offset, int whence )
00416 {
00417   register int rc = ::fseek(F(), offset, whence);
00418   if ( rc != 0 ) {
00419     MC_OnError( errno, Error, "while seeking inside a JFile." )
00420   }
00421   return rc;
00422 }
00423 
00424 /*
00425 Get the current position of a stream
00426 
00427 The fgetpos() function stores the current position of the stream fp 
00428 in the fpos_t object specified by pos. 
00429 
00430 The value stored in pos can be used by fsetpos() for repositioning 
00431 the file to the position at the time of the fgetpos() call.
00432 
00433 Returns:
00434 0 for success, or nonzero if an error occurs (errno is set).
00435 */
00436 int JFile::getpos( fpos_t* pos ) {
00437   register int rc = ::fgetpos(F(), pos);
00438   if ( rc != 0 ) {
00439     MC_OnError( errno, Error, "while retrieving the current position inside a JFile." )
00440   }
00441   return rc;
00442 }
00443 
00444 /*
00445 Set the current position of a file
00446 
00447 The fsetpos() function sets the current position of the stream specified 
00448 by fp according to the value of the fpos_t object pointed to by pos. 
00449 
00450 The value pointed to by pos must have been set by a call to fgetpos() on the same file.
00451 
00452 Returns:
00453 0 for success, or nonzero if an error occurs (errno is set).
00454 
00455 */
00456 
00457 int JFile::setpos( const fpos_t* pos ) {
00458   register int rc = ::fsetpos(F(), pos);
00459   if ( rc != 0 ) {
00460     MC_OnError( errno, Error, "while setting the current position inside a JFile." )
00461   }
00462   return rc;
00463 }
00464 
00465 void JFile::setBegin( long offset ) {
00466   register int rc = ::fseek(F(), offset, SEEK_SET );
00467   if ( rc != 0 ) {
00468     MC_OnError( errno, Error, "while seeking inside a JFile." )
00469   }
00470 }
00471 
00472 #if 0
00473 void JFile::addCurrent( long offset ) {
00474   current += offset;
00475   printf("OFFSET=%u\n", current );
00476   fflush( stdout );
00477   register int rc = ::fseek(F(), offset, SEEK_CUR );
00478   if ( rc != 0 ) {
00479     MC_OnError( errno, Error, "while seeking inside a JFile." )
00480   }
00481 }
00482 #endif
00483 
00484 void JFile::addCurrent( long offset ) {
00485   current += offset;
00486   printf("OFFSET=%u\n", current );
00487   fflush( stdout );
00488   register int rc = ::fseek(F(), current, SEEK_SET );
00489   if ( rc != 0 ) {
00490     MC_OnError( errno, Error, "while seeking inside a JFile." )
00491   }
00492 }
00493 
00494 void JFile::setEnd( long offset ) {
00495   register int rc = ::fseek(F(), offset, SEEK_END );
00496   if ( rc != 0 ) {
00497     MC_OnError( errno, Error, "while seeking inside a JFile." )
00498   }
00499 }
00500 
00501 /*
00502 Return the current position of a stream
00503 
00504 The ftell() function returns the current position of the stream specified by fp. 
00505 This position defines the character that will be read or written 
00506 by the next I/O operation on the file. The value returned by ftell() 
00507 can be used in a subsequent call to fseek() to restore the file position 
00508 to a previous value. 
00509 
00510 Returns:
00511 The current position of the file or -1L if an error occurred (errno is set).
00512 */
00513 long JFile::tell() {
00514   register long rc = ::ftell( F() );
00515   if ( rc < 0 ) {
00516     MC_OnError( errno, Error, "while getting the current position inside a JFile." )
00517   }
00518 
00519   return rc;
00520 }
00521 
00522 /*
00523 Rewind a file stream to the beginning of the file
00524 
00525 The rewind() function rewinds the file stream specified by fp to the beginning 
00526 of the file. It's equivalent to calling fseek() like this: 
00527 
00528 fseek( fp, 0L, SEEK_SET );
00529 except that the error indicator for the stream is cleared.
00530 */
00531 void JFile::rewind() {
00532   ::rewind( F() );
00533   MC_OnError( errno, Error, "while rewinding a JFile." )
00534 }
00535 
00536 void JFile::setbuf( char* buf ) {
00537   ::setbuf(F(), buf);
00538   MC_OnError( errno, Error, "while setting a JFile buffer." )
00539 }
00540 
00541 int JFile::setvbuf( char* buf, int type, size_t sz ) {
00542   return ::setvbuf(F(), buf, type, sz);
00543   MC_OnError( errno, Error, "while setting a formated JFile buffer." )
00544 }
00545 
00546 /*
00547 Test a stream's error flag
00548 The ferror() function tests the error flag for the stream specified by fp.
00549 
00550 Returns:
00551 0 if the error flag isn't set, or nonzero if the error flag is set.
00552 */
00553 int JFile::error() {
00554   register int rc = ferror( F() );
00555   MC_OnError( errno, Error, "while checking for a JFile error." )
00556   return rc;
00557 }
00558 
00559 void JFile::clearError() {
00560   clearerr( F() );
00561   MC_OnError( errno, Error, "while clearing a JFile error." )
00562 }
00563 
00564 /*
00565 fich= fopen (nom_du_fichier, "rb");
00566 fseek (fich, 0, SEEK_END);
00567 taille_fichier= ftell (fich)
00568 ;fclose (fich);
00569 */
00570 
00571 size_t JFile::getFileSize()
00572 {
00573    struct stat value;
00574 
00575    int result = ::stat( fpath, &value );
00576 
00577    //fstat( F(), &value );
00578 
00579    // Validate data
00580    assert( result != 0 );
00581 
00582    MC_OnError( errno, Error, "while getting a JFile size." )
00583  
00584    // File Size     
00585    return ( value.st_size );
00586 }
00587 
00588 #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