home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GERLIB_USR08B.LHA / gerlib / examples / add / normal / couta.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  3.3 KB  |  200 lines

  1. /*-- Rev Header - do NOT edit!
  2.  *
  3.  *  Filename : couta.h
  4.  *  Purpose  : (Erst einmal) Ersatz von ostream, e.g. cout
  5.  *
  6.  *  Program  : -
  7.  *  Author   : Gerhard Müller
  8.  *  Copyright: (c) by Gerhard Müller
  9.  *  Creation :
  10.  *
  11.  *  compile  : make
  12.  *
  13.  *  Compile version  : 0.1
  14.  *  Ext. Version     : 0.1
  15.  *
  16.  *  REVISION HISTORY
  17.  *
  18.  *  Date                     Comment
  19.  *  ------------------------ -------------------------------------------------
  20.  *  Fri Sep 17 00:44:16 1993 Seems to work, some part of ios is supported
  21.  *
  22.  *-- REV_END --
  23.  */
  24.  
  25. #ifndef ADD_COUTA_H
  26. #define ADD_COUTA_H
  27.  
  28.     /*
  29.      * C-Includes, C-Definitionen
  30.      *
  31.      */
  32.  
  33. #define class _class
  34. #define template _template
  35.  
  36. extern "C" {
  37. #include <exec/types.h>
  38. #include <dos/dos.h>
  39. #include <clib/alib_protos.h>
  40. #include <clib/alib_stdio_protos.h>
  41. #include <inline/stubs.h>
  42. #ifdef __OPTIMIZE__
  43. #include <inline/exec.h>
  44. #include <inline/dos.h>
  45. #else
  46. #include <clib/exec_protos.h>
  47. #include <clib/dos_protos.h>
  48. #endif
  49. }
  50.  
  51. #undef template
  52. #undef class
  53.  
  54. #include "ios.h"
  55.  
  56. #ifndef EOF
  57. #define EOF -1
  58. #endif
  59.  
  60. class couta;
  61.  
  62. typedef couta& (*__omanip)(couta&);
  63.  
  64. #ifndef ADD_CINA_H
  65. enum seek_dir { beg=OFFSET_BEGINNING, cur=OFFSET_CURRENT, end=OFFSET_END};
  66. #endif
  67.  
  68. class couta : public ios {
  69.  public:
  70.     couta() : ios(Output())
  71.     {
  72.         _flags |= ios::dont_close;
  73.     }
  74.  
  75.     couta(BPTR own_fh) : ios(own_fh)
  76.     {
  77.         _flags |= ios::dont_close;
  78.     }
  79.  
  80.     ~couta() { flush();}
  81.     couta& operator<<(int x);
  82.     couta& operator<<(char *c);
  83.     couta& operator<<(char c);
  84.     couta& operator<<(long x);
  85.  
  86.     inline couta& operator<<(__omanip func) { return (*func)(*this); }
  87. //    inline couta& operator<<(couta& os, __manip func) {(*func)(os); return os;}
  88.  
  89.     inline couta& flush();
  90.  
  91.  
  92.     couta& put(char c)
  93.     {
  94.         if(FPutC(_fh,c) == EOF) set(ios::eofbit|ios::failbit);
  95.         return *this;
  96.     }
  97.  
  98.     couta& write(const char *s, int n)
  99.     {
  100.         ULONG x;
  101.         SetIoErr(0L);
  102.         x=FWrite(_fh,STRPTR(s),n,1);
  103.         if(x != 1) set(ios::failbit);
  104.         return *this;
  105.     }
  106.  
  107.     couta& write(const unsigned char *s, int n)
  108.     {
  109.         return(write((const char *)s,n));
  110.     }
  111.  
  112.     couta& write(const void *s, int n)
  113.     {
  114.         return(write((const char *)s,n));
  115.     }
  116.  
  117.     LONG tellp()
  118.     {
  119.         LONG x = Seek(_fh, 0, OFFSET_CURRENT);
  120.  
  121.         if (x == -1)
  122.           set(ios::failbit);
  123.  
  124.         return x;
  125.     }
  126.  
  127.     couta& seekp(ULONG pos)
  128.     {
  129.         if(Seek(_fh, pos, OFFSET_BEGINNING)==-1) set(ios::failbit);
  130.         return *this;
  131.     }
  132.  
  133.     couta& seekp(ULONG off, seek_dir dir)
  134.     {
  135.         if(Seek(_fh, off, dir)==-1) set(ios::failbit);
  136.         return *this;
  137.     }
  138.  
  139.     BPTR attach(BPTR fh)
  140.     {
  141.         BPTR save=_fh;
  142.         flush();
  143.         _fh=fh;
  144.         if(!fh) set(ios::failbit);
  145.         return save;
  146.     }
  147. };
  148.  
  149.  
  150. inline couta& couta::operator<<(int x)
  151. {
  152.     if(VFPrintf(_fh,(unsigned char *)"%ld",(long int *)&x)==EOF)
  153.         set(ios::failbit);
  154.  
  155.     return *this;
  156.  
  157. }
  158. inline couta& couta::operator<<(char *c)
  159. {
  160.     if(c)    // no harm if NULL-String
  161.         if(FPuts(_fh,(STRPTR)c)==EOF) set(ios::failbit);
  162.  
  163.     return *this;
  164. }
  165. inline couta& couta::operator<<(char c)
  166. {
  167.     return put(c);
  168. }
  169.  
  170. inline couta& couta::operator<<(long x)
  171. {
  172.     if(VFPrintf(_fh,(unsigned char *)"%ld",(long int *)&x)==EOF) set(ios::failbit);
  173.     return *this;
  174. }
  175.  
  176. inline couta& couta::flush()
  177. {
  178.     if(_fh) Flush(_fh);
  179.     return *this;
  180. }
  181.  
  182. inline couta& flush(couta& a)
  183. {
  184.     return a.flush();
  185. }
  186.  
  187. inline couta& endl(couta& outs)
  188. {
  189.     return outs << "\n" << flush;
  190. }
  191.  
  192. inline couta& ends(couta& outs)
  193. {
  194.     return outs << (char *)0 << flush;
  195. }
  196.  
  197. extern couta cout;
  198.  
  199. #endif
  200.