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

JFile Class Reference

#include <JFile.hpp>

List of all members.

Public Methods

 JFile ()
 JFile (const char *path, const char *mode="r", size_t recordsize=1)
virtual ~JFile ()
size_t getFileSize ()
void reopen ()
int getChar ()
void putChar (int c)
void putStr (const char *s)
char * getStr (char *s, int n)
__inline int ungetChar (int c)
char * getFileName ()
void getFileName (char *buffer)
size_t readChar (char *c)
size_t read (void *ptr, size_t size, size_t n=1)
size_t write (const void *ptr, size_t size, size_t n=1)
int eof ()
int flush ()
int seek (long offset, int whence=SEEK_SET)
int getpos (fpos_t *pos)
int setpos (const fpos_t *pos)
long tell ()
void rewind ()
void setbuf (char *buf)
int setvbuf (char *buf, int type, size_t sz)
int error ()
void clearError ()
void setBegin (long offset=0)
void addCurrent (long offset=0)
void setEnd (long offset=0)
int print (const char *format,...)
long getData (char *buffer, size_t length)
size_t putData (char *buffer, long length, long blocksize)
 JFile ()
 JFile (const char *path, const char *mode="r", size_t recordsize=1)
virtual ~JFile ()
size_t getFileSize ()
void reopen ()
int getChar ()
void putChar (int c)
void putStr (const char *s)
char * getStr (char *s, int n)
__inline int ungetChar (int c)
char * getFileName ()
void getFileName (char *buffer)
size_t readChar (char *c)
size_t read (void *ptr, size_t size, size_t n=1)
size_t write (const void *ptr, size_t size, size_t n=1)
int eof ()
int flush ()
int seek (long offset, int whence=SEEK_SET)
int getpos (fpos_t *pos)
int setpos (const fpos_t *pos)
long tell ()
void rewind ()
void setbuf (char *buf)
int setvbuf (char *buf, int type, size_t sz)
int error ()
void clearError ()
void setBegin (long offset=0)
void addCurrent (long offset=0)
void setEnd (long offset=0)
int print (const char *format,...)
long getData (char *buffer, size_t length)
size_t putData (char *buffer, long length, long blocksize)

Private Methods

FILE * F ()
FILE * F ()

Private Attributes

FILE * f
const char * fpath
const char * fmode
size_t fpos
size_t recsize
size_t current
FILE * f
const char * fpath
const char * fmode


Constructor & Destructor Documentation

JFile::JFile  
 

Definition at line 7 of file JFile.cpp.

00008  : f( NULL ), fmode( "rb" ), recsize( 0 ), 
00009    fpath( NULL ), fpos( 0 ), current( 0 ) { }

JFile::JFile const char *    path,
const char *    mode = "r",
size_t    recordsize = 1
 

Definition at line 45 of file JFile.cpp.

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 }

JFile::~JFile   [virtual]
 

Definition at line 65 of file JFile.cpp.

00065               {
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 }

JFile::JFile  
 

JFile::JFile const char *    path,
const char *    mode = "r",
size_t    recordsize = 1
 

virtual JFile::~JFile   [virtual]
 


Member Function Documentation

FILE* JFile::F   [private]
 

FILE * JFile::F   [private]
 

Definition at line 11 of file JFile.cpp.

Referenced by addCurrent(), clearError(), eof(), error(), flush(), getChar(), getData(), getStr(), getpos(), print(), putChar(), putData(), putStr(), read(), rewind(), seek(), setBegin(), setEnd(), setbuf(), setpos(), setvbuf(), tell(), ungetChar(), and write().

00011                {
00012     assert( f != NULL ); // More sophisticated test needed.
00013   return f;
00014 }

void JFile::addCurrent long    offset = 0
 

void JFile::addCurrent long    offset = 0
 

Definition at line 485 of file JFile.cpp.

Referenced by getData(), and putData().

00485                                     {
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 }

void JFile::clearError  
 

void JFile::clearError  
 

Definition at line 561 of file JFile.cpp.

00561                        {
00562   clearerr( F() );
00563   MC_OnError( errno, Error, "while clearing a JFile error." )
00564 }

int JFile::eof  
 

int JFile::eof  
 

Definition at line 358 of file JFile.cpp.

00358                { 
00359   return feof( F() ); 
00360 }

int JFile::error  
 

int JFile::error  
 

Definition at line 555 of file JFile.cpp.

00555                  {
00556   register int rc = ferror( F() );
00557   MC_OnError( errno, Error, "while checking for a JFile error." )
00558   return rc;
00559 }

int JFile::flush  
 

int JFile::flush  
 

Definition at line 378 of file JFile.cpp.

00378                  { 
00379   return ::fflush( F() ); 
00380 }

int JFile::getChar  
 

int JFile::getChar  
 

Definition at line 125 of file JFile.cpp.

00125                    {
00126   int c = fgetc( F() );
00127   MC_OnError( errno, Error, "while retrieving a character from a JFile." )  
00128   return c;
00129 }

long JFile::getData char *    buffer,
size_t    length
 

long JFile::getData char *    buffer,
size_t    length
 

Definition at line 131 of file JFile.cpp.

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 }

void JFile::getFileName char *    buffer
 

char* JFile::getFileName  
 

void JFile::getFileName char *    buffer
 

Definition at line 75 of file JFile.cpp.

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 }

char * JFile::getFileName  
 

Definition at line 92 of file JFile.cpp.

Referenced by TFTP_Option::openFile().

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 }

size_t JFile::getFileSize  
 

size_t JFile::getFileSize  
 

Definition at line 573 of file JFile.cpp.

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 }

char* JFile::getStr char *    s,
int    n
 

char * JFile::getStr char *    s,
int    n
 

Definition at line 258 of file JFile.cpp.

00258                                     {
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 }

int JFile::getpos fpos_t *    pos
 

int JFile::getpos fpos_t *    pos
 

Definition at line 437 of file JFile.cpp.

00437                                {
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 }

int JFile::print const char *    format,
...   
 

int JFile::print const char *    format,
...   
 

Definition at line 280 of file JFile.cpp.

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 }

void JFile::putChar int    c
 

void JFile::putChar int    c
 

Definition at line 173 of file JFile.cpp.

00173                            {
00174   register int rc = ::fputc( c, F() );
00175   MC_OnError( errno, Error, "while saving a character inside a JFile." )   
00176 }

size_t JFile::putData char *    buffer,
long    length,
long    blocksize
 

size_t JFile::putData char *    buffer,
long    length,
long    blocksize
 

Definition at line 178 of file JFile.cpp.

Referenced by fromDisk(), and toDisk().

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 }

void JFile::putStr const char *    s
 

void JFile::putStr const char *    s
 

Definition at line 222 of file JFile.cpp.

00222                                   {
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 }

size_t JFile::read void *    ptr,
size_t    size,
size_t    n = 1
 

size_t JFile::read void *    ptr,
size_t    size,
size_t    n = 1
 

Definition at line 311 of file JFile.cpp.

Referenced by readChar().

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 }

size_t JFile::readChar char *    c
 

size_t JFile::readChar char *    c
 

Definition at line 307 of file JFile.cpp.

00307                                 {
00308   return read( c, 1, 1 );
00309 }

void JFile::reopen  
 

void JFile::reopen  
 

Definition at line 110 of file JFile.cpp.

00110                    {
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 }

void JFile::rewind  
 

void JFile::rewind  
 

Definition at line 532 of file JFile.cpp.

00532                    {
00533   ::rewind( F() );
00534   MC_OnError( errno, Error, "while rewinding a JFile." )
00535 }

int JFile::seek long    offset,
int    whence = SEEK_SET
 

int JFile::seek long    offset,
int    whence = SEEK_SET
 

Definition at line 416 of file JFile.cpp.

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 }

void JFile::setBegin long    offset = 0
 

void JFile::setBegin long    offset = 0
 

Definition at line 466 of file JFile.cpp.

00466                                   {
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 }

void JFile::setEnd long    offset = 0
 

void JFile::setEnd long    offset = 0
 

Definition at line 495 of file JFile.cpp.

00495                                 {
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 }

void JFile::setbuf char *    buf
 

void JFile::setbuf char *    buf
 

Definition at line 537 of file JFile.cpp.

00537                               {
00538   ::setbuf(F(), buf);
00539   MC_OnError( errno, Error, "while setting a JFile buffer." )
00540 }

int JFile::setpos const fpos_t *    pos
 

int JFile::setpos const fpos_t *    pos
 

Definition at line 458 of file JFile.cpp.

00458                                      {
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 }

int JFile::setvbuf char *    buf,
int    type,
size_t    sz
 

int JFile::setvbuf char *    buf,
int    type,
size_t    sz
 

Definition at line 542 of file JFile.cpp.

00542                                                    {
00543   int rc = ::setvbuf(F(), buf, type, sz);
00544   MC_OnError( errno, Error, "while setting a formated JFile buffer." )
00545   return rc;
00546 }

long JFile::tell  
 

long JFile::tell  
 

Definition at line 514 of file JFile.cpp.

00514                  {
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 }

__inline int JFile::ungetChar int    c
 

__inline int JFile::ungetChar int    c
 

Definition at line 160 of file JFile.cpp.

00160                                    {
00161   // No errno set in any case.
00162   return ::ungetc( c, F() );  
00163 }

size_t JFile::write const void *    ptr,
size_t    size,
size_t    n = 1
 

size_t JFile::write const void *    ptr,
size_t    size,
size_t    n = 1
 

Definition at line 338 of file JFile.cpp.

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 }


Member Data Documentation

size_t JFile::current [private]
 

Definition at line 79 of file JOFile.hpp.

FILE* JFile::f [private]
 

Definition at line 73 of file JOFile.hpp.

FILE* JFile::f [private]
 

Definition at line 73 of file JFile.hpp.

const char* JFile::fmode [private]
 

Definition at line 76 of file JOFile.hpp.

const char* JFile::fmode [private]
 

Definition at line 76 of file JFile.hpp.

const char* JFile::fpath [private]
 

Definition at line 75 of file JOFile.hpp.

const char* JFile::fpath [private]
 

Definition at line 75 of file JFile.hpp.

size_t JFile::fpos [private]
 

Definition at line 77 of file JOFile.hpp.

size_t JFile::recsize [private]
 

Definition at line 78 of file JOFile.hpp.


The documentation for this class was generated from the following files:
Generated on Sun Oct 14 18:48:49 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001