00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
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 #include <stdio.h>
00043 #include <stdlib.h>
00044 #include <string.h>
00045 #include <errno.h>
00046
00047 #include "uti.h"
00048 #include "xalloc.h"
00049
00050 #ifdef __CC_GNUC__
00051 #include <unistd.h>
00052 #endif
00053
00054
00055
00056
00057
00058
00059 void xfile_error ( const char * fn )
00060 {
00061 die_beep("%s error: %s",fn,strerror(errno));
00062 }
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 int _eof_error = XFILE_EOFE_NEVER;
00080
00081
00082
00083 #define eof_status(f) \
00084 ((_eof_error==XFILE_EOFE_ALWAYS)|| \
00085 ((_eof_error==XFILE_EOFE_RETRY)&&(feof(f))));
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 int set_eof_error( int eof_error )
00099 {
00100 _eof_error = (((eof_error!=XFILE_EOFE_NEVER)&&
00101 (eof_error!=XFILE_EOFE_ALWAYS)) ?
00102 XFILE_EOFE_RETRY : eof_error);
00103
00104 return _eof_error;
00105 }
00106
00107
00108
00109
00110 int get_eof_error( void )
00111 {
00112 return _eof_error;
00113 }
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125 int xfclose( FILE * fp )
00126 {
00127 int retval;
00128
00129 if ((retval=fclose(fp))==EOF)
00130 xfile_error ("fclose");
00131
00132 return(retval);
00133 }
00134
00135
00136
00137
00138 #ifdef XFILE_FCLOSEALL
00139
00140 int xfcloseall( void )
00141 {
00142 int retval;
00143
00144 if ((retval=fcloseall())==EOF)
00145 xfile_error ("fcloseall");
00146
00147 return(retval);
00148 }
00149
00150 #endif
00151
00152
00153 int xfflush( FILE * fp )
00154 {
00155 int retval;
00156
00157 if ((retval=fflush(fp))==EOF)
00158 xfile_error ("fflush");
00159
00160 return(retval);
00161 }
00162
00163
00164
00165 int xfgetc( FILE * fp )
00166 {
00167 int retval;
00168 int eof_e = eof_status(fp);
00169
00170 if ((retval=fgetc(fp))==EOF)
00171 if ( (!feof(fp)) || (eof_e) )
00172 xfile_error ("fgetc");
00173
00174 return(retval);
00175 }
00176
00177
00178
00179 int xfgetpos( FILE * fp, fpos_t * pos )
00180 {
00181 int retval;
00182
00183 if ((retval=fgetpos(fp,pos))!=0)
00184 xfile_error ("fgetpos");
00185
00186 return(retval);
00187 }
00188
00189
00190
00191 char * xfgets( char * s, int n, FILE * fp )
00192 {
00193 char * retval;
00194 int eof_e = eof_status(fp);
00195
00196 if ((retval=fgets(s,n,fp))==NULL)
00197 if ( (!feof(fp)) || (eof_e) )
00198 xfile_error ("fgets");
00199
00200 return(retval);
00201 }
00202
00203
00204
00205
00206 #ifdef XFILE_FLUSHALL
00207
00208 int xflushall( void )
00209 {
00210 return(flushall());
00211 }
00212
00213 #endif
00214
00215
00216 FILE * xfopen( const char * filename, const char * mode )
00217 {
00218 FILE * retval;
00219
00220 if ((retval=fopen(filename,mode))==NULL) {
00221 char *tmp=(char*)xmalloc(strlen(filename)+10);
00222 strcpy(tmp,"fopen("); strcat(tmp,filename); strcat(tmp,")");
00223 xfile_error (tmp);
00224 xfree(tmp);
00225 }
00226
00227 return(retval);
00228 }
00229
00230
00231
00232 int xfputc( int c, FILE * fp )
00233 {
00234 int retval;
00235
00236 if ((retval=fputc(c,fp))==EOF)
00237 xfile_error ("fputc");
00238
00239 return(retval);
00240 }
00241
00242
00243
00244 int xfputs( const char * s, FILE * fp )
00245 {
00246 int retval;
00247
00248 if ((retval=fputs(s,fp))==EOF)
00249 xfile_error ("fputs");
00250
00251 return(retval);
00252 }
00253
00254
00255
00256 size_t xfread( void * ptr, size_t size, size_t n, FILE * fp )
00257 {
00258 size_t retval;
00259
00260 if ((retval=fread(ptr,size,n,fp))<n)
00261 xfile_error ("fread");
00262
00263 return(retval);
00264 }
00265
00266
00267
00268 int xfseek( FILE * fp, long int offset, int whence )
00269 {
00270 int retval;
00271
00272 if ((retval=fseek(fp,offset,whence))!=0)
00273 xfile_error ("fseek");
00274
00275 return(retval);
00276 }
00277
00278
00279
00280 int xfsetpos( FILE * fp, const fpos_t * pos )
00281 {
00282 int retval;
00283
00284 if ((retval=fsetpos(fp,pos))!=0)
00285 xfile_error ("fsetpos");
00286
00287 return(retval);
00288 }
00289
00290
00291
00292 long xftell( FILE * fp )
00293 {
00294 long retval;
00295
00296 if ((retval=ftell(fp))==-1L)
00297 xfile_error ("ftell");
00298
00299 return(retval);
00300 }
00301
00302
00303
00304 size_t xfwrite( const void * ptr, size_t size, size_t n, FILE * fp )
00305 {
00306 size_t retval;
00307
00308 if ((retval=fwrite(ptr,size,n,fp))<n)
00309 xfile_error ("fwrite");
00310
00311 return(retval);
00312 }
00313
00314
00315
00316 int xrename( const char * oldname, const char * newname )
00317 {
00318 int retval;
00319
00320 if ((retval=rename(oldname,newname))==-1)
00321 xfile_error ("rename");
00322
00323 return(retval);
00324 }
00325
00326
00327
00328 int xremove( const char * filename )
00329 {
00330 int retval;
00331
00332 if ((retval=remove(filename))==-1)
00333 xfile_error ("remove");
00334
00335 return(retval);
00336 }
00337
00338
00339
00340
00341 #ifdef XFILE_UNLINK
00342
00343 int xunlink( const char * filename )
00344 {
00345 int retval;
00346
00347 if ((retval=unlink(filename))==-1)
00348 xfile_error ("unlink");
00349
00350 return(retval);
00351 }
00352
00353 #endif
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367 char * fgetln( char * s, int n, FILE *fp, int* toolong )
00368 {
00369 char * code;
00370 size_t l;
00371
00372 code = fgets(s,n,fp);
00373 if (code!=NULL) {
00374 l = strlen(s);
00375 if ((l)&&(s[l-1]=='\n')) {
00376 s[--l]=0;
00377 if (toolong) *toolong=0;
00378 }
00379 else if (toolong && (l>=(size_t)(n-1))) *toolong=1;
00380 #ifdef __OS_UNIX__
00381 if ((l)&&(s[l-1]=='\r')) s[--l]=0;
00382 #endif
00383 }
00384
00385 return code;
00386 }
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401 PRIVATE char *lncss( char * s, int remove_comment, int remove_white )
00402 {
00403 char *s1, *s2;
00404 int instr = 0;
00405 int incom = 0;
00406 int quote = 0;
00407
00408 if (remove_white>4) {
00409 remove_white -= 4;
00410 quote = 1;
00411 }
00412
00413 if (s!=NULL) {
00414 s1 = s2 = s;
00415 do {
00416 if (incom) {
00417 if (((remove_white==1)||(remove_white==2)) &&
00418 (strchr(" \t",*s1)!=NULL)) continue;
00419 }
00420 else if (instr) {
00421 if (((remove_white==1)||(remove_white==3)) &&
00422 (strchr(" \t",*s1)!=NULL)) continue;
00423 else if (*s1==FILE_STRCODE) {
00424 if (quote) {
00425 if (s1[1]==FILE_STRCODE) s1++;
00426 else { instr=0; continue; }
00427 }
00428 else instr=0;
00429 }
00430 }
00431 else {
00432 if (remove_white && (strchr(" \t",*s1)!=NULL)) continue;
00433 else if (*s1==FILE_STRCODE) { instr=1; if (quote) continue; }
00434 else if (strchr(FILE_COMMENT,*s1)!=NULL) {
00435 if (remove_comment) *s1='\0'; else incom=1;
00436 }
00437 }
00438 *(s2++)= *s1;
00439 } while (*(s1++));
00440 *s2 = '\0';
00441 }
00442 return s;
00443 }
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485 char * fgetln_filt( char * s, int n, FILE *fp,
00486 int remove_comment, int remove_white, int remove_wlines, int *toolong )
00487 {
00488 char *code;
00489
00490 do {
00491 code = fgetln(s,n,fp,toolong);
00492 if (code==NULL) break;
00493 lncss(s,remove_comment,remove_white);
00494 } while (remove_wlines &&(*s=='\0'));
00495
00496 return code;
00497 }
00498
00499
00500
00501
00502 long flen( FILE * fp )
00503 {
00504 long pos, len;
00505
00506 pos=ftell(fp);
00507 len=-1L;
00508
00509 if (pos!=-1L) {
00510 if (fseek(fp,0,SEEK_END)==0)
00511 len = ftell(fp);
00512
00513 if (fseek(fp,pos,SEEK_SET)!=0)
00514 len=-1L;
00515 }
00516
00517 return len;
00518 }
00519
00520
00521
00522
00523
00524
00525 #ifdef __CC_GNUC__
00526 #include <unistd.h>
00527 int ftrunc( FILE * fp )
00528 {
00529 return ftruncate(fileno(fp),ftell(fp));
00530 }
00531
00532 #elif __CC_BORLANDC__
00533 #include <dos.h>
00534 int ftrunc( FILE * fp )
00535 {
00536 union REGS regs;
00537 int ax;
00538
00539 regs.h.ah = 0x40;
00540 regs.x.bx = fileno(fp);
00541 regs.x.cx = 0;
00542 ax = intdos(®s, ®s);
00543
00544 return(regs.x.cflag ? ax : 0);
00545 }
00546 #elif __CC_MSVC__
00547 #include <stdio.h>
00548 #include <io.h>
00549 int ftrunc( FILE * fp )
00550 {
00551 return _chsize(_fileno(fp),ftell(fp));
00552 }
00553
00554 #else
00555 #error Undefined Compiler/Platform
00556 #endif
00557
00558
00559
00560
00561
00562 PRIVATE void *_amalloc( size_t * size )
00563 {
00564 void *p;
00565
00566 while (*size) {
00567 p = malloc(*size);
00568 if (p)
00569 return p;
00570 (*size) = (size_t)(((long)(*size)+1) >> 1);
00571 }
00572 return NULL;
00573 }
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587 int fmoven( FILE* fp, long from, long to, long n )
00588 {
00589 long i;
00590 char *buf;
00591 size_t l, nn;
00592
00593 if ((from==to)||(!n)) return 0;
00594
00595 l = 32767;
00596 buf = (char*)_amalloc(&l);
00597 if (!buf) return -1;
00598
00599 if (from>to) {
00600 i = 0;
00601 while (n) {
00602 nn = (n>(long)l)? l : (size_t)n;
00603 if (fseek(fp,from+i,SEEK_SET)) break;
00604 if (fread(buf,1,nn,fp)<nn) break;
00605 if (fseek(fp,to+i,SEEK_SET)) break;
00606 if (fwrite(buf,1,nn,fp)<nn) break;
00607 n -= nn;
00608 i += nn;
00609 }
00610 }
00611 else {
00612 i = n;
00613 while (n) {
00614 nn = (n>(long)l)? l : (size_t)n;
00615 n -= nn;
00616 i -= nn;
00617 if (fseek(fp,from+i,SEEK_SET)) break;
00618 if (fread(buf,1,nn,fp)<nn) break;
00619 if (fseek(fp,to+i,SEEK_SET)) break;
00620 if (fwrite(buf,1,nn,fp)<nn) break;
00621 }
00622 }
00623 free(buf);
00624 if (n) return -1;
00625
00626 return 0;
00627 }
00628
00629
00630
00631
00632
00633
00634
00635
00636 int finsn( FILE* fp, long pos, long n )
00637 {
00638 long len, xn;
00639 char *buf;
00640 size_t l, nn;
00641
00642 if (fseek(fp,0,SEEK_END)) return -1;
00643 if ((len=ftell(fp))==-1L) return -1;
00644 if ((pos>len)||(pos<0)) return -1;
00645
00646 xn = n;
00647 l = 32767;
00648 buf = (char*)_amalloc(&l);
00649 if (!buf) return -1;
00650 while (n) {
00651 nn = (n>(long)l)? l : (size_t)n;
00652 if (fwrite(buf,1,nn,fp)<nn) break;
00653 n -= nn;
00654 }
00655 free(buf);
00656 if (n) return -1;
00657
00658 return fmoven(fp, pos, pos+xn, len-pos);
00659 }
00660
00661
00662
00663
00664
00665
00666
00667 int fdeln( FILE* fp, long pos, long n )
00668 {
00669 long len;
00670
00671 if (fseek(fp,0,SEEK_END)) return -1;
00672 if ((len=ftell(fp))==-1L) return -1;
00673 if ((pos>len)||(pos<0)) return -1;
00674 if (pos+n > len)
00675 n = len-pos;
00676 if (fmoven(fp,pos+n,pos,len-(pos+n))) return -1;
00677 if (fseek(fp,len-n,SEEK_SET)) return -1;
00678
00679 return ftrunc(fp);
00680 }
00681
00682
00683
00684
00685 char * xfgetln( char * s, int n, FILE *fp, int *toolong )
00686 {
00687 char * retval;
00688 int eof_e = eof_status(fp);
00689
00690 if ((retval=fgetln(s,n,fp,toolong))==NULL)
00691 if ( (!feof(fp)) || (eof_e) )
00692 xfile_error ("fgetln");
00693
00694 return(retval);
00695 }
00696
00697
00698
00699
00700 char * xfgetln_filt( char * s, int n, FILE *fp,
00701 int remove_comment, int remove_white, int remove_wlines, int *toolong )
00702 {
00703 char * retval;
00704 int eof_e = eof_status(fp);
00705
00706 if ((retval=fgetln_filt(s,n,fp,remove_comment,remove_white,remove_wlines,toolong))==NULL)
00707 if ( (!feof(fp)) || (eof_e) )
00708 xfile_error ("fgetln_filt");
00709
00710 return(retval);
00711 }
00712
00713
00714
00715
00716 long xflen( FILE *fp )
00717 {
00718 long l;
00719
00720 l = flen(fp);
00721
00722 if (l==-1L)
00723 xfile_error ("flen");
00724
00725 return l;
00726 }
00727
00728
00729
00730
00731 int xftrunc( FILE * fp )
00732 {
00733 int retval;
00734
00735 if ((retval=ftrunc(fp))!=0)
00736 xfile_error ("ftrunc");
00737
00738 return(retval);
00739 }
00740
00741
00742
00743
00744 int xfmoven( FILE* fp, long from, long to, long n )
00745 {
00746 int retval;
00747
00748 if ((retval=fmoven(fp,from,to,n))!=0)
00749 xfile_error ("fmoven");
00750
00751 return(retval);
00752 }
00753
00754
00755
00756
00757 int xfinsn( FILE* fp, long pos, long n )
00758 {
00759 int retval;
00760
00761 if ((retval=finsn(fp,pos,n))!=0)
00762 xfile_error ("finsn");
00763
00764 return(retval);
00765 }
00766
00767
00768
00769
00770 int xfdeln( FILE* fp, long pos, long n )
00771 {
00772 int retval;
00773
00774 if ((retval=fdeln(fp,pos,n))!=0)
00775 xfile_error ("fdeln");
00776
00777 return(retval);
00778 }
00779
00780