Show
Ignore:
Timestamp:
01/17/06 22:51:03 (3 years ago)
Author:
gnue
Message:

- cleaned up a warning
- the symbols for seek are now seek_set, seek_cur and seek_end since end conflicted with end keyword
- got seek to actually work (replaced == with NewtRefEqual, removed quotes for the symbols)
- added getc, putc and write (write writes a binary just like read reads a binary)
- added a sample code for NSOF I/O.

contribute patch by Paul Guyot

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • NEWT0/trunk/ext/protoFILE/protoFILE.c

    r1 r53  
    9898        return NewtThrow(kNErrNotASymbol, whence); 
    9999 
    100     if (whence == NSSYM("set")) 
     100    if (NewtRefEqual(whence, NSSYM(seek_set))) 
    101101        wh = SEEK_SET; 
    102     else if (whence == NSSYM("curr")) 
     102    else if (NewtRefEqual(whence, NSSYM(seek_cur))) 
    103103        wh = SEEK_CUR; 
    104     else if (whence == NSSYM("end")) 
     104    else if (NewtRefEqual(whence, NSSYM(seek_end))) 
    105105        wh = SEEK_END; 
    106106    else 
     
    391391} 
    392392 
     393newtRef MyGetc(newtRefArg rcvr) 
     394{ 
     395    newtRefVar stream; 
     396    FILE* theFile; 
     397    int theResult; 
     398     
     399    stream = NcGetSlot(rcvr, NSSYM(_stream)); 
     400    if (NewtRefIsNIL(stream)) 
     401        return kNewtRefUnbind; 
     402 
     403    theFile = NewtRefToFILE(stream); 
     404    theResult = fgetc(theFile); 
     405    return NewtMakeInteger(theResult); 
     406} 
     407 
     408newtRef MyPutc(newtRefArg rcvr, newtRefArg byte) 
     409{ 
     410    newtRefVar stream; 
     411    FILE* theFile; 
     412    char theByte; 
     413    int theResult; 
     414     
     415    stream = NcGetSlot(rcvr, NSSYM(_stream)); 
     416    if (NewtRefIsNIL(stream)) 
     417        return kNewtRefUnbind; 
     418 
     419    theFile = NewtRefToFILE(stream); 
     420    theByte = NewtRefToInteger(byte); 
     421    theResult = fputc(theByte, theFile); 
     422    return NewtMakeInteger(theResult); 
     423} 
     424 
     425newtRef MyWrite(newtRefArg rcvr, newtRefArg binary) 
     426{ 
     427    newtRefVar stream; 
     428    FILE* theFile; 
     429    void* data; 
     430    int len; 
     431    int theResult; 
     432     
     433    stream = NcGetSlot(rcvr, NSSYM(_stream)); 
     434    if (NewtRefIsNIL(stream)) 
     435        return kNewtRefUnbind; 
     436 
     437    theFile = NewtRefToFILE(stream); 
     438    data = NewtRefToBinary(binary); 
     439    len = NewtBinaryLength(binary); 
     440    theResult = fwrite(data, len, 1, theFile); 
     441 
     442    return NewtMakeInteger(theResult); 
     443} 
    393444 
    394445/*------------------------------------------------------------------------*/ 
     
    423474    NcSetSlot(r, NSSYM(Print),      NewtMakeNativeFunc(MyPrint,     1, "Print(str)")); 
    424475    NcSetSlot(r, NSSYM(Gets),       NewtMakeNativeFunc(MyGets,      0, "Gets()")); 
     476    NcSetSlot(r, NSSYM(Getc),       NewtMakeNativeFunc(MyGetc,      0, "Getc()")); 
     477    NcSetSlot(r, NSSYM(Putc),       NewtMakeNativeFunc(MyPutc,      1, "Putc(byte)")); 
     478    NcSetSlot(r, NSSYM(Write),      NewtMakeNativeFunc(MyWrite,     1, "Write(binary)")); 
    425479 
    426480    NcSetSlot(r, NSSYM(_stream),    kNewtRefNIL);