00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #define LIBTEKLTI_EXPORT (0)
00022
00023 #ifndef _cplusplus
00024 #define _cplusplus
00025 #endif
00026 #include "teklti.h"
00027
00028 #include <limits.h>
00029 #include <errno.h>
00030
00031
00032 MKTEKUUID(UTekFileASCIIBare, "7F3FE3E2-1E9E-11DA-93FA-000BDBC434D9")
00033 MKTEKDEBUGSTRING(DBTekFileASCIIBare, "TekFileASCIIBare")
00034
00035
00036 #ifndef USE_386_ASM
00037
00038
00043 TekFileASCIIBare::TekFileASCIIBare()
00044 {
00045 TEKSTD_IMPLEMENT_INTERFACE(UTekFile)
00046 TEKSTD_IMPLEMENT_INTERFACE(UTekFileASCII)
00047 TEKSTD_IMPLEMENT_INTERFACE(UTekFileASCIIBare)
00048 TEKSTD_IMPLEMENT_ENDBASE
00049
00050
00051 this->PrivFileName.uchar_t_ascii = NULL;
00052 this->PrivFileName.uchar_t_asciilen = 0;
00053 this->PrivFileDesc = NULL;
00054 PrivFileMode = TekFileMode_ReadOnly;
00055 PrivTekFileType = TekFileType_ASCII;
00056
00057
00058 this->PrivBuffer.uchar_t_ascii =
00059 (char *)calloc(TEKFILEASCII_STDBUFLEN + 1, sizeof(char));
00060 if ( this->PrivBuffer.uchar_t_ascii == NULL )
00061 throw;
00062 this->PrivBuffer.uchar_t_asciilen = 0;
00063 this->PrivBufferPtr.uchar_t_ascii = NULL;
00064 this->PrivBufferPtr.uchar_t_asciilen = 0;
00065 }
00066
00067
00072 TekFileASCIIBare::~TekFileASCIIBare()
00073 {
00074 if ( this->PrivFileDesc != NULL )
00075 fclose(this->PrivFileDesc);
00076 if ( this->PrivFileName.uchar_t_ascii != NULL )
00077 free(this->PrivFileName.uchar_t_ascii);
00078 #ifdef TEKLTI_ENFORCE_PRIVACY
00079 memset(this->PrivBuffer.uchar_t_ascii, '\0', TEKFILEASCII_STDBUFLEN);
00080 #endif
00081 free(this->PrivBuffer.uchar_t_ascii);
00082 }
00083
00084
00085 const char TekFileASCIIBare_ReadOnly[] = "rb";
00086 const char TekFileASCIIBare_WriteOnly[] = "wb";
00087 const char TekFileASCIIBare_Append[] = "ab";
00088 const char TekFileASCIIBare_ReadWrite[] = "r+b";
00089 const char TekFileASCIIBare_ReadWriteTrunc[] = "w+b";
00090 const char TekFileASCIIBare_ReadAppend[] = "a+b";
00091
00092
00107 TEKERR TekFileASCIIBare::ReadLine(uchar_t** Buffer, unsigned long ReadMax)
00108 {
00109 uchar_t * ubuffer;
00110 unsigned char wemakebuf;
00111 unsigned long l;
00112
00113 #ifndef NO_INTERNAL_NULL_CHECKS
00114 if ( this->PrivFileDesc == NULL )
00115 return TEKERR_SEQUENCE;
00116 #endif
00117
00118 if ( ReadMax > TEKFILEASCII_STDBUFLEN )
00119 ReadMax = TEKFILEASCII_STDBUFLEN;
00120
00121
00122 ubuffer = *Buffer;
00123 wemakebuf = 0;
00124
00125 if ( ubuffer == NULL )
00126 {
00127
00128 ubuffer = (uchar_t *)malloc(sizeof(uchar_t));
00129
00130 if ( ubuffer == NULL )
00131 {
00132 return TEKERR_MEMORY;
00133 } else {
00134 ubuffer->uchar_t_ascii = (char *)
00135 calloc(TEKFILEASCII_STDBUFLEN + 1, sizeof(char));
00136 if ( ubuffer->uchar_t_ascii == NULL )
00137 {
00138
00139 free(ubuffer);
00140 return TEKERR_MEMORY;
00141 }
00142
00143
00144 ubuffer->uchar_t_asciilen =
00145 (TEKFILEASCII_STDBUFLEN + 1) * sizeof(char);
00146
00147
00148 wemakebuf = 1;
00149 }
00150 }
00151 #ifdef TEKLTI_ENFORCE_PRIVACY
00152 else
00153 {
00154
00155 memset(
00156 ubuffer->uchar_t_ascii,
00157 '\0',
00158 (TEKFILEASCII_STDBUFLEN + 1) * sizeof(char)
00159 );
00160 }
00161 #endif
00162
00163
00164 if ( this->PrivBuffer.uchar_t_asciilen > 0 )
00165 {
00166
00167 for ( l=0; l < ReadMax; l++ )
00168 {
00169
00170 if ( this->PrivBuffer.uchar_t_asciilen == 0 )
00171 {
00172 ubuffer->uchar_t_ascii[l] = '\0';
00173
00174 this->PrivBuffer.uchar_t_ascii =
00175 this->PrivBufferPtr.uchar_t_ascii;
00176 break;
00177 }
00178
00179 if ( this->PrivBuffer.uchar_t_ascii[0] == '\n' )
00180 {
00181 ReadFileEOS:
00182 ubuffer->uchar_t_ascii[l] = '\0';
00183
00184 this->PrivBuffer.uchar_t_asciilen = 0;
00185
00186 this->PrivBuffer.uchar_t_ascii =
00187 this->PrivBufferPtr.uchar_t_ascii;
00188 return TEKERR_FILE_EOF;
00189 }
00190 if ( this->PrivBuffer.uchar_t_ascii[0] == '\r' )
00191 goto ReadFileEOS;
00192
00193
00194 ubuffer->uchar_t_ascii[l] = this->PrivBuffer.uchar_t_ascii[0];
00195
00196 this->PrivBuffer.uchar_t_asciilen--;
00197
00198 this->PrivBuffer.uchar_t_ascii++;
00199 }
00200
00201 ubuffer->uchar_t_ascii[ReadMax] = '\0';
00202
00203
00204 if ( this->PrivBuffer.uchar_t_asciilen == 0 )
00205 {
00206
00207 this->PrivBuffer.uchar_t_ascii =
00208 this->PrivBufferPtr.uchar_t_ascii;
00209 }
00210
00211
00212 return TEKERR_OK;
00213 }
00214
00215
00216 if (
00217 fgets(
00218 this->PrivBuffer.uchar_t_ascii,
00219 TEKFILEASCII_STDBUFLEN + 1,
00220 this->PrivFileDesc
00221 ) == NULL
00222 )
00223 {
00224
00225 if ( wemakebuf )
00226 {
00227 free(ubuffer->uchar_t_ascii);
00228 free(ubuffer);
00229 }
00230
00231
00232 if ( feof(this->PrivFileDesc) )
00233 return TEKERR_FILE_EOF;
00234
00235
00236 if ( errno == EPIPE )
00237 return TEKERR_FILE_PIPE;
00238
00239
00240 return TEKERR_FILE_IO;
00241 }
00242
00243
00244 this->PrivBufferPtr.uchar_t_ascii = this->PrivBuffer.uchar_t_ascii;
00245
00246
00247 l = (unsigned long)strlen(this->PrivBuffer.uchar_t_ascii);
00248
00249 if ( ReadMax >= l )
00250 {
00251
00252 this->PrivBuffer.uchar_t_asciilen = 0;
00253
00254 strcpy(ubuffer->uchar_t_ascii, this->PrivBuffer.uchar_t_ascii);
00255 if ( wemakebuf )
00256 *Buffer = ubuffer;
00257 return TEKERR_OK;
00258 }
00259
00260
00261 this->PrivBuffer.uchar_t_asciilen = l - ReadMax;
00262
00263
00264 strncpy(
00265 ubuffer->uchar_t_ascii,
00266 this->PrivBuffer.uchar_t_ascii, ReadMax
00267 );
00268 ubuffer->uchar_t_ascii[ReadMax] = '\0';
00269 this->PrivBuffer.uchar_t_ascii += ReadMax;
00270
00271
00272 if ( wemakebuf )
00273 *Buffer = ubuffer;
00274
00275
00276 return TEKERR_OK;
00277 }
00278
00289 TEKERR TekFileASCIIBare::WriteLine(const uchar_t* Buffer)
00290 {
00291 int err;
00292
00293 #ifndef NO_SIZE_T_CHECKS
00294 if ( Buffer == NULL )
00295 return TEKERR_POINTER;
00296 #endif
00297
00298 #ifndef NO_INTERNAL_NULL_CHECKS
00299 if ( this->PrivFileDesc == NULL )
00300 return TEKERR_SEQUENCE;
00301 #endif
00302
00303
00304 err = fputs(Buffer->uchar_t_ascii, this->PrivFileDesc);
00305 if ( err == EOF )
00306 {
00307 WriteLineEOF:
00308
00309 switch ( errno )
00310 {
00311 case EPIPE:
00312 return TEKERR_FILE_PIPE;
00313 case ENOSPC:
00314 return TEKERR_FILE_DISKFULL;
00315 case EBADF:
00316
00317 if ( this->PrivFileMode == TekFileMode_ReadOnly )
00318 return TEKERR_FILE_READONLY;
00319 case EIO:
00320 default:
00321 return TEKERR_FILE_IO;
00322 }
00323 }
00324
00325
00326 #ifdef TEKLTI_WRITELINE_CRLF
00327 err = fputs("\r\n", this->PrivFileDesc);
00328 #else
00329 err = fputc('\n', this->PrivFileDesc);
00330 #endif
00331
00332 if ( err == EOF )
00333 goto WriteLineEOF;
00334
00335 return TEKERR_OK;
00336 }
00337
00343 TEKERR TekFileASCIIBare::get_FILE(FILE ** FileDescriptor)
00344 {
00345 #ifndef NO_SIZE_T_CHECKS
00346 if ( FileDescriptor == NULL )
00347 return TEKERR_POINTER;
00348 #endif
00349
00350 *FileDescriptor = this->PrivFileDesc;
00351
00352 return TEKERR_OK;
00353 }
00354
00355
00361 TEKERR TekFileASCIIBare::get_Filename(uchar_t ** Name)
00362 {
00363 uchar_t * newval;
00364
00365 #ifndef NO_SIZE_T_CHECKS
00366 if ( Name == NULL )
00367 return TEKERR_POINTER;
00368 #endif
00369
00370 #ifndef NO_INTERNAL_NULL_CHECKS
00371 if ( this->PrivFileName.uchar_t_ascii == NULL )
00372 return TEKERR_UNEXPECTED;
00373 #endif
00374
00375
00376 newval = char2uchar(this->PrivFileName.uchar_t_ascii);
00377 if ( newval == NULL )
00378 return TEKERR_MEMORY;
00379
00380
00381 *Name = newval;
00382
00383
00384 return TEKERR_OK;
00385 }
00386
00387
00398 TEKERR TekFileASCIIBare::put_FILE(FILE * FileDescriptor)
00399 {
00400 this->PrivFileDesc = FileDescriptor;
00401
00402 return TEKERR_OK;
00403 }
00404
00405
00411 TEKERR TekFileASCIIBare::put_Filename(uchar_t * Name)
00412 {
00413 #ifndef NO_SIZE_T_CHECKS
00414 if ( Name == NULL )
00415 return TEKERR_POINTER;
00416 #endif
00417
00418 this->PrivFileName.uchar_t_ascii = strdup(uchar2char(Name));
00419
00420 return TEKERR_OK;
00421 }
00422
00423
00429 TEKERR TekFileASCIIBare::get_Type(TekFileType * FileTypePointer)
00430 {
00431 TekFileType * retval;
00432
00433 retval = FileTypePointer;
00434
00435 #ifndef NO_SIZE_T_CHECKS
00436 if ( FileTypePointer == NULL )
00437 return TEKERR_POINTER;
00438 #endif
00439
00440 retval[0] = this->PrivTekFileType;
00441
00442 return TEKERR_OK;
00443 }
00444
00445
00451 TEKERR TekFileASCIIBare::Close()
00452 {
00453 int err;
00454
00455 #ifndef NO_INTERNAL_NULL_CHECKS
00456 if ( this->PrivFileDesc == NULL )
00457 return TEKERR_SEQUENCE;
00458 #endif
00459
00460 err = fclose(this->PrivFileDesc);
00461
00462 if ( err != 0 )
00463 {
00464 switch ( err )
00465 {
00466 case ENOSPC:
00467 return TEKERR_FILE_DISKFULL;
00468 case EIO:
00469 case EPIPE:
00470 return TEKERR_FILE_IO;
00471 default:
00472 return TEKERR_UNEXPECTED;
00473 }
00474 }
00475
00476 this->PrivFileDesc = NULL;
00477
00478 return TEKERR_OK;
00479 }
00480
00481
00487 TEKERR TekFileASCIIBare::get_Mode(TekFileMode * ModeID)
00488 {
00489 #ifndef NO_SIZE_T_CHECKS
00490 if ( ModeID == NULL )
00491 return TEKERR_POINTER;
00492 #endif
00493
00494 *ModeID = this->PrivFileMode;
00495
00496 return TEKERR_OK;
00497 }
00498
00499
00505 TEKERR TekFileASCIIBare::Flush()
00506 {
00507 int err;
00508
00509 #ifndef NO_INTERNAL_NULL_CHECKS
00510 if ( this->PrivFileDesc == NULL )
00511 return TEKERR_SEQUENCE;
00512 #endif
00513
00514 err = fflush(this->PrivFileDesc);
00515
00516 if ( err != 0 )
00517 {
00518 switch ( err )
00519 {
00520 case ENOSPC:
00521 return TEKERR_FILE_DISKFULL;
00522 case EIO:
00523 case EPIPE:
00524 return TEKERR_FILE_IO;
00525 default:
00526 return TEKERR_UNEXPECTED;
00527 }
00528 }
00529
00530 return TEKERR_OK;
00531 }
00532
00533
00539 TEKERR TekFileASCIIBare::Open()
00540 {
00541 const char * tmpopenvar;
00542
00543
00544 #ifndef NO_INTERNAL_NULL_CHECKS
00545 if ( this->PrivFileDesc != NULL )
00546 return TEKERR_SEQUENCE;
00547 #endif
00548
00549
00550 switch ( this->PrivFileMode )
00551 {
00552 case TekFileMode_WriteOnly:
00553 tmpopenvar = TekFileASCIIBare_WriteOnly;
00554 break;
00555 case TekFileMode_Append:
00556 tmpopenvar = TekFileASCIIBare_Append;
00557 break;
00558 case TekFileMode_ReadWrite:
00559 tmpopenvar = TekFileASCIIBare_ReadWrite;
00560 break;
00561 case TekFileMode_ReadWriteTrunc:
00562 tmpopenvar = TekFileASCIIBare_ReadWriteTrunc;
00563 break;
00564 case TekFileMode_ReadAppend:
00565 tmpopenvar = TekFileASCIIBare_ReadAppend;
00566 break;
00567 default:
00568 tmpopenvar = TekFileASCIIBare_ReadOnly;
00569 break;
00570 }
00571
00572
00573 this->PrivFileDesc = fopen(
00574 this->PrivFileName.uchar_t_ascii,
00575 tmpopenvar
00576 );
00577 if ( this->PrivFileDesc == NULL )
00578 return TEKERR_FAIL;
00579
00580
00581 return TEKERR_OK;
00582 }
00583
00584
00590 TEKERR TekFileASCIIBare::Open64()
00591 {
00592 #ifndef OPEN64_NOT_SUPPORTED
00593 const char * tmpopenvar;
00594
00595
00596 #ifndef NO_INTERNAL_NULL_CHECKS
00597 if ( this->PrivFileDesc != NULL )
00598 return TEKERR_SEQUENCE;
00599 #endif
00600
00601
00602 switch ( this->PrivFileMode )
00603 {
00604 case TekFileMode_ReadOnly:
00605 tmpopenvar = TekFileASCIIBare_ReadOnly;
00606 break;
00607 case TekFileMode_WriteOnly:
00608 tmpopenvar = TekFileASCIIBare_WriteOnly;
00609 break;
00610 case TekFileMode_Append:
00611 tmpopenvar = TekFileASCIIBare_Append;
00612 break;
00613 case TekFileMode_ReadWrite:
00614 tmpopenvar = TekFileASCIIBare_ReadWrite;
00615 break;
00616 case TekFileMode_ReadWriteTrunc:
00617 tmpopenvar = TekFileASCIIBare_ReadWriteTrunc;
00618 break;
00619 case TekFileMode_ReadAppend:
00620 tmpopenvar = TekFileASCIIBare_ReadAppend;
00621 break;
00622 default:
00623 return TEKERR_UNAVAILABLE;
00624 }
00625
00626
00627 this->PrivFileDesc = fopen64(
00628 this->PrivFileName.uchar_t_ascii,
00629 tmpopenvar
00630 );
00631 if ( this->PrivFileDesc == NULL )
00632 return TEKERR_FAIL;
00633
00634
00635 return TEKERR_OK;
00636 #else
00637 return TEKERR_NOTIMPL;
00638 #endif
00639 }
00640
00641
00647 TEKERR TekFileASCIIBare::put_Mode(TekFileMode ModeID)
00648 {
00649 #ifndef NO_INTERNAL_NULL_CHECKS
00650 if ( this->PrivFileDesc != NULL )
00651 return TEKERR_ACCESS;
00652 #endif
00653
00654 this->PrivFileMode = ModeID;
00655 return TEKERR_OK;
00656 }
00657
00658
00664 TEKERR TekFileASCIIBare::Read64(void * Buffer, uint64_t BytesToRead, uint64_t * BytesRead)
00665 {
00666 return TEKERR_NOTIMPL;
00667 }
00668
00669
00675 TEKERR TekFileASCIIBare::Read(void * Buffer, unsigned long BytesToRead, unsigned long * BytesRead)
00676 {
00677 size_t bytesread;
00678
00679 #ifndef NO_SIZE_T_CHECKS
00680 if ( Buffer == NULL )
00681 return TEKERR_POINTER;
00682 #endif
00683
00684 #ifndef NO_INTERNAL_NULL_CHECKS
00685 if ( this->PrivFileDesc == NULL )
00686 return TEKERR_SEQUENCE;
00687 #endif
00688
00689
00690 bytesread = fread(Buffer, sizeof(char), BytesToRead, this->PrivFileDesc);
00691
00692
00693 *BytesRead = bytesread;
00694
00695
00696 if ( bytesread > 0 )
00697 return TEKERR_OK;
00698
00699
00700 if ( feof(this->PrivFileDesc) != 0 )
00701 return TEKERR_FILE_EOF;
00702
00703
00704 if ( errno == EPIPE )
00705 return TEKERR_FILE_PIPE;
00706
00707
00708 return TEKERR_FILE_IO;
00709 }
00710
00711
00717 TEKERR TekFileASCIIBare::Seek64(TekFileSeek SeekWhere, int64_t NewLocation)
00718 {
00719 return TEKERR_NOTIMPL;
00720 }
00721
00722
00728 TEKERR TekFileASCIIBare::Seek(TekFileSeek SeekWhere, signed long NewLocation)
00729 {
00730 int SeekWhence;
00731
00732 #ifndef NO_INTERNAL_NULL_CHECKS
00733 if ( this->PrivFileDesc == NULL )
00734 return TEKERR_SEQUENCE;
00735 #endif
00736
00737 switch ( SeekWhere )
00738 {
00739 case TekFileSeek_Begin:
00740 SeekWhence = SEEK_SET;
00741 break;
00742 case TekFileSeek_Current:
00743 SeekWhence = SEEK_CUR;
00744 break;
00745 case TekFileSeek_End:
00746 SeekWhence = SEEK_END;
00747 break;
00748 default:
00749 return TEKERR_UNAVAILABLE;
00750 }
00751
00752 if ( fseek(this->PrivFileDesc, NewLocation, SeekWhence) )
00753 return TEKERR_FAIL;
00754
00755 return TEKERR_OK;
00756 }
00757
00758
00764 TEKERR TekFileASCIIBare::Write64(void * Buffer, uint64_t BytesToWrite, uint64_t * BytesWrote)
00765 {
00766 return TEKERR_NOTIMPL;
00767 }
00768
00769
00775 TEKERR TekFileASCIIBare::Write(void * Buffer, unsigned long BytesToWrite, unsigned long * BytesWrote)
00776 {
00777 size_t written;
00778
00779 #ifndef NO_SIZE_T_CHECKS
00780 if ( Buffer == NULL )
00781 return TEKERR_POINTER;
00782 #endif
00783
00784 #ifndef NO_INTERNAL_NULL_CHECKS
00785 if ( this->PrivFileDesc == NULL )
00786 return TEKERR_SEQUENCE;
00787 #endif
00788
00789
00790 written = fwrite(Buffer, sizeof(char), BytesToWrite, this->PrivFileDesc);
00791
00792
00793 *BytesWrote = written;
00794
00795
00796 if ( written == BytesToWrite )
00797 return TEKERR_OK;
00798
00799
00800 switch ( errno )
00801 {
00802 case EPIPE:
00803 return TEKERR_FILE_PIPE;
00804 case ENOSPC:
00805 return TEKERR_FILE_DISKFULL;
00806 case EBADF:
00807
00808 if ( this->PrivFileMode == TekFileMode_ReadOnly )
00809 return TEKERR_FILE_READONLY;
00810 case EIO:
00811 default:
00812 return TEKERR_FILE_IO;
00813 }
00814 }
00815
00816
00823 TEKERR TekFileASCIIBare::get_Location(unsigned long * Location)
00824 {
00825 long loc;
00826
00827 #ifndef NO_INTERNAL_NULL_CHECKS
00828 if ( this->PrivFileDesc == NULL )
00829 return TEKERR_SEQUENCE;
00830 #endif
00831
00832
00833 loc = ftell(this->PrivFileDesc);
00834
00835
00836 if ( loc != -1 )
00837 {
00838
00839 *Location = (unsigned long)loc;
00840
00841
00842 return TEKERR_OK;
00843 }
00844
00845
00846 if ( errno != EPIPE )
00847 return TEKERR_FILE_IO;
00848
00849 return TEKERR_FILE_PIPE;
00850 }
00851
00852
00859 TEKERR TekFileASCIIBare::get_Location64(uint64_t * Location)
00860 {
00861 return TEKERR_NOTIMPL;
00862 }
00863
00864
00865 #endif
00866
00867