home *** CD-ROM | disk | FTP | other *** search
/ PC Media 4 / PC MEDIA CD04.iso / share / prog / gcoope10 / stdstrm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-22  |  3.5 KB  |  179 lines

  1.  
  2. /*
  3.  
  4.     StdStream object class definition for GCOOPE 1.0
  5.  
  6.     This class is for stream interface to standard ANSI-C streams
  7.  
  8.       released as PUBLIC DOMAIN 4/25/94 modified for GCOOPE 7/21/94
  9.  
  10. */
  11.  
  12.  
  13. #define CLASS StdStream
  14.  
  15. #include "gcoope10.h"
  16. #include "stream.h"
  17. #include <stdarg.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20.  
  21. object CLASS;
  22. extern object String;
  23.  
  24. typedef struct {
  25.     FILE *        stream;
  26.     unsigned    flags;
  27.      } instVars;
  28.  
  29.  
  30. USEGEN(getPos);
  31. USEGEN(setPos);
  32. USEGEN(Putc);
  33. USEGEN(Getc);
  34. USEGEN(UnGet);
  35. USEGEN(Puts);
  36. USEGEN(Gets);
  37. USEGEN(Write);
  38. USEGEN(Read);
  39. USEGEN(SetBuf);
  40. USEGEN(Flush);
  41. USEGEN(clrErr);
  42. USEGEN(Stat);
  43. USEGEN(addressOf);
  44. USEGEN(asString);
  45.  
  46. cmethod object m4New(object instance, FILE * fStream)
  47. {
  48. instVars * ivptr;
  49.  
  50. if(NULL==(ivptr=makeInst(&instance))) return 0;
  51. ivptr->flags=0;
  52. ivptr->stream=fStream;
  53. return instance;
  54. }
  55.  
  56.  
  57. imethod object m4SetBuf(object instance,char * buffer, int type,
  58.                             word size)
  59. {
  60. return setvbuf(((instVars *) getIVptr(instance))->stream,buffer,type,size);
  61. }
  62.  
  63.  
  64. imethod object m4Flush(object instance)
  65. {
  66. return fflush(((instVars *) getIVptr(instance))->stream);
  67. }
  68.  
  69.  
  70.  
  71. imethod long m4getPos(object instance)
  72. {
  73. return ftell(((instVars *) getIVptr(instance))->stream);
  74. }
  75.  
  76.  
  77.  
  78. imethod object m4setPos(object instance, long newPos, int whence)
  79. {
  80. instVars * ivptr;
  81.  
  82. if(NULL==(ivptr=getIVptr(instance))) return FUNCFAIL;
  83. if(newPos==0 && whence==SEEK_SET)
  84.     {
  85.     rewind(ivptr->stream);
  86.     return FUNCOKAY;
  87.     }
  88. return fseek(ivptr->stream,newPos,whence);
  89. }
  90.  
  91.  
  92. imethod object m4clrErr(object instance)
  93. {
  94. clearerr(((instVars *) getIVptr(instance))->stream);
  95. return FUNCOKAY;
  96. }
  97.  
  98.  
  99. imethod object m4Stat(object instance)
  100. {
  101. instVars * ivptr;
  102.  
  103. if(NULL==(ivptr=getIVptr(instance))) return EOF;
  104. ivptr->flags|=(feof(ivptr->stream))?S_EOS:0;
  105. ivptr->flags|=(ferror(ivptr->stream))?S_ERR:0;
  106. return (object) (ivptr->flags);
  107. }
  108.  
  109.  
  110. imethod int m4Putc(object instance, char c)
  111. {
  112. return putc(c,((instVars *) getIVptr(instance))->stream);
  113. }
  114.  
  115.  
  116. imethod int m4Getc(object instance)
  117. {
  118. return getc(((instVars *) getIVptr(instance))->stream);
  119. }
  120.  
  121.  
  122.  
  123. imethod int m4UnGet(object instance, char c)
  124. {
  125. return ungetc(c,((instVars *) getIVptr(instance))->stream);
  126. }
  127.  
  128.  
  129.  
  130. imethod object m4Puts(object instance, object dataObj)
  131. {
  132. return (fputs(((VDPTRRV) g)(GEN(addressOf))(g(GEN(asString))(dataObj)),
  133.     ((instVars *) getIVptr(instance))->stream)==EOF)?FUNCFAIL:FUNCOKAY;
  134. }
  135.  
  136.  
  137. imethod object m4Gets(object instance, int max)
  138. {
  139. char * d;
  140. object rv;
  141.  
  142. d=s_malloc(max);
  143. rv=(fgets(d,max,((instVars *) getIVptr(instance))->stream)==NULL)?END:
  144.     g(New)(String,d);
  145. s_free(d);
  146. return rv;
  147. }
  148.  
  149.  
  150. imethod int m4Write(object instance, const char * ptr, word size,
  151.                                 word n)
  152. {
  153. return fwrite(ptr,size,n,((instVars *) getIVptr(instance))->stream);
  154. }
  155.  
  156.  
  157. imethod int m4Read(object instance, char * ptr, word size, word n)
  158. {
  159. return fread(ptr,size,n,((instVars *) getIVptr(instance))->stream);
  160. }
  161.  
  162.  
  163. imethod void * m4addressOf(object instance)
  164. {
  165. return ((instVars *) getIVptr(instance))->stream;
  166. }
  167.  
  168.  
  169. CLASS_INSTALL
  170. {
  171. if(END==(CLASS=g(New)(Class, 0, sizeof(instVars),END))
  172.     || addGMthd(CLASS, New, (method) m4New)
  173.     || ADDGM(getPos) || ADDGM(setPos) || ADDGM(Putc) || ADDGM(Getc)
  174.     || ADDGM(UnGet) || ADDGM(Puts) || ADDGM(Gets) || ADDGM(Write)
  175.     || ADDGM(Read) || ADDGM(SetBuf) || ADDGM(Flush) || ADDGM(clrErr)
  176.     || ADDGM(Stat) || ADDGM(addressOf)) return FUNCFAIL;
  177. else return FUNCOKAY;
  178. }
  179.