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 );
00013 return f;
00014 }
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
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
00054 exit(1);
00055 }
00056 }
00057
00058
00059
00060
00061
00062
00063
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
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
00088 memcpy( buffer, fpath, len );
00089 }
00090
00091
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
00105
00106
00107
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
00120
00121
00122
00123
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
00140
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
00152
00153
00154
00155
00156
00157
00158
00159
00160 inline int JFile::ungetChar( int c ) {
00161
00162 return ::ungetc( c, F() );
00163 }
00164
00165
00166
00167
00168
00169
00170
00171
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
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
00216
00217
00218
00219
00220
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
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
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
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
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
00296
00297
00298
00299
00300
00301
00302
00303
00304
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
00331
00332
00333
00334
00335
00336
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
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358 int JFile::eof() {
00359 return feof( F() );
00360 }
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378 int JFile::flush() {
00379 return ::fflush( F() );
00380 }
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
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
00427
00428
00429
00430
00431
00432
00433
00434
00435
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
00447
00448
00449
00450
00451
00452
00453
00454
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
00504
00505
00506
00507
00508
00509
00510
00511
00512
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
00525
00526
00527
00528
00529
00530
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
00550
00551
00552
00553
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
00568
00569
00570
00571
00572
00573 size_t JFile::getFileSize()
00574 {
00575 struct stat value;
00576
00577 int result = ::stat( fpath, &value );
00578
00579
00580
00581
00582 assert( result != 0 );
00583
00584 MC_OnError( errno, Error, "while getting a JFile size." )
00585
00586
00587 return ( value.st_size );
00588 }
00589
00590 #endif