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

  1. /*-- Rev Header - do NOT edit!
  2.  *
  3.  *  Filename : cina.h
  4.  *  Purpose  : (Erst einmal) Ersatz von istream, e.g. cin
  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:41:18 1993 Seems to work, some part of ios is supported
  21.  *
  22.  *
  23.  *-- REV_END --
  24.  */
  25.  
  26. #ifndef ADD_CINA_H
  27. #define ADD_CINA_H
  28.  
  29.     /*
  30.      * C-Includes, C-Definitionen
  31.      *
  32.      */
  33.  
  34. #define class _class
  35. #define template _template
  36.  
  37. extern "C" {
  38. #include <exec/types.h>
  39. #include <dos/dos.h>
  40. #include <dos/dosextens.h>
  41. #include <clib/alib_protos.h>
  42. #include <clib/alib_stdio_protos.h>
  43. #include <inline/stubs.h>
  44. #ifdef __OPTIMIZE__
  45. #include <inline/exec.h>
  46. #include <inline/dos.h>
  47. #else
  48. #include <clib/exec_protos.h>
  49. #include <clib/dos_protos.h>
  50. #endif
  51.  
  52. #include <ctype.h>
  53. }
  54.  
  55. #undef template
  56. #undef class
  57.  
  58. #include "ios.h"
  59.  
  60. #ifndef EOF
  61. #define EOF -1
  62. #endif
  63.  
  64. class cina;
  65.  
  66. typedef cina& (*__imanip)(cina&);
  67.  
  68. #ifndef ADD_COUTA_H
  69. enum seek_dir { beg=OFFSET_BEGINNING, cur=OFFSET_CURRENT, end=OFFSET_END};
  70. #endif
  71.  
  72. inline int skip_ws(cina& sb);
  73. inline cina& ws(cina& ins);
  74.  
  75. class cina : public ios {
  76.  public:
  77.     cina() : ios(Input())
  78.     {
  79.         _flags |= ios::dont_close;
  80.     }
  81.  
  82.     cina (BPTR own_fh) : ios(own_fh)
  83.     {
  84.         _flags |= ios::dont_close;
  85.     }
  86.  
  87.     ~cina() { flush();}
  88.  
  89. //    BPTR tie() { return _tie; }
  90. //    BPTR tie(BPTR new_fh) { BPTR return_fh=_tie; _tie=new_fh; return return_fh; }
  91.  
  92.     cina& operator>>(long& return_val);
  93.     inline cina& operator>>(char *c);
  94.     inline cina& operator>>(char &c);
  95.  
  96.     inline cina& operator>>(__imanip func) { return (*func)(*this); }
  97. //    inline cina& operator<<(cina& os, __manip func) {(*func)(os); return os;}
  98.  
  99.     inline cina& flush();
  100.  
  101.  
  102.     cina& get(char& c)
  103.     {
  104.         look_tie();
  105.         int ch;
  106.  
  107.         ch=(int)FGetC(_fh);
  108.  
  109.         if (ch == EOF)
  110.           set(ios::eofbit|ios::failbit);
  111.         else
  112.             c=(char) ch;
  113.  
  114.         return *this;
  115.     }
  116.  
  117.     cina& get(char* p, int n, char d='\n')
  118.     {
  119.         int ch;
  120.         int x;
  121.  
  122.         look_tie();
  123.  
  124.         ch = skip_ws(*this);    // we don't want to have any '\n' at he beginning
  125.  
  126.         for(x=0; x<(n-1); x++)
  127.         {
  128.             if(ch==EOF)    { set(ios::eofbit); break; }
  129.             else
  130.             {
  131.                 if(ch==d)
  132.                 {
  133.                     UnGetC(_fh,ch);
  134.                     x=n;
  135.                 }
  136.                 else
  137.                     *p++=(char) ch;
  138.             }
  139.  
  140.             ch=FGetC(_fh);        // read next char
  141.         }
  142.         *p=0;
  143.         return *this;
  144.     }
  145.  
  146.     cina& putback(char pb) { UnGetC(_fh,pb); return *this; }
  147.  
  148.     cina& get(unsigned char* ptr, int len, char delim = '\n')
  149.     { return get((char*)ptr, len, delim); }
  150.  
  151.     cina& get(unsigned char& c) { return get((char&)c); }
  152.  
  153.     cina& getline(char* ptr, int len, char delim = '\n')
  154.     {
  155.         int ch;
  156.         int x;
  157.  
  158.         look_tie();
  159.  
  160.         ch = skip_ws(*this);    // we don't want to have any '\n' at he beginning
  161.  
  162.         for(x=0; x<(len-1); x++)
  163.         {
  164.             if(ch==EOF) { set(ios::eofbit); break; }
  165.             if(ch==delim) break;
  166.             *ptr++=(char) ch;
  167.  
  168.             ch=FGetC(_fh);
  169.         }
  170.         *ptr=0;
  171.         return *this;
  172.     }
  173.  
  174.     cina& getline(unsigned char* ptr, int len, char delim = '\n')
  175.     { return getline((char*)ptr, len, delim); }
  176.  
  177.     cina& read(char *ptr, int n)
  178.     {
  179.         LONG x;
  180.         look_tie();
  181.         SetIoErr(0L);
  182.         if(x=FRead(_fh,ptr,n,1)==0) set(ios::eofbit|ios::failbit);
  183.         if(x!=n) set(ios::failbit);
  184.         return *this;
  185.     }
  186.     cina& read(unsigned char *ptr, int n) { return read((char*)ptr, n); }
  187.     cina& read(void *ptr, int n) { return read((char*)ptr, n); }
  188.  
  189. //    cina& gets(char **s, char delim = '\n');
  190.     cina& ignore(int n=1, int delim = EOF)
  191.     {
  192.         ULONG x;
  193.         int ch;
  194.  
  195.         look_tie();
  196.  
  197.         for(x=0; x<n; x++)
  198.         {
  199.             ch=FGetC(_fh);
  200.             if (ch == EOF)
  201.             {
  202.                 set(ios::eofbit);
  203.                 break;
  204.             }
  205.             if(ch==delim) break;
  206.         }
  207.         return *this;
  208.     }
  209.  
  210.     LONG tellg()
  211.     {
  212.         LONG x = Seek(_fh, 0, OFFSET_CURRENT);
  213.  
  214.         if (x == -1)
  215.           set(ios::failbit);
  216.  
  217.         return x;
  218.     }
  219.  
  220.     cina& seekg(ULONG pos)
  221.     {
  222.         if(Seek(_fh, pos, OFFSET_BEGINNING)==-1) set(ios::failbit);
  223.         return *this;
  224.     }
  225.  
  226.     cina& seekg(ULONG off, seek_dir dir)
  227.     {
  228.         if(Seek(_fh, off, dir)==-1) set(ios::failbit);
  229.         return *this;
  230.     }
  231.  
  232.     int peek()
  233.     {
  234.         int ch;
  235.  
  236.         ch=FGetC(_fh);
  237.  
  238.         if (ch == EOF)
  239.           set(ios::eofbit);
  240.         else
  241.             UnGetC(_fh,ch);
  242.         return ch;
  243.     }
  244.  
  245.     int skip_ws(cina& sb)
  246.     {
  247.         int ch;
  248.  
  249.         look_tie();
  250.  
  251.         for (;;)
  252.         {
  253.             ch=FGetC(_fh);
  254.  
  255.             if (ch == EOF)
  256.               set(ios::eofbit);
  257.  
  258.             if (ch == EOF || !isspace(ch))
  259.                 return ch;
  260.         }
  261.     }
  262.  
  263.     BPTR attach(BPTR fh)
  264.     {
  265.         BPTR save=_fh;
  266.         flush();
  267.         _fh=fh;
  268.         if(!fh) set(ios::failbit);
  269.         return save;
  270.     }
  271.  
  272.  
  273.  private:
  274.  
  275.     void look_tie()
  276.     {
  277.         if(_tie)
  278.             if(_tie!=_fh)    // IMPORTANT if Input()==Output()
  279.                 Flush(_tie);
  280.     }
  281. };
  282.  
  283.  
  284.  
  285. inline cina& cina::operator>>(char *c)
  286. {
  287.     look_tie();
  288.  
  289.     int ch;
  290.  
  291.     ch = skip_ws(*this);    // we don't want to have any '\n' at he beginning
  292.  
  293.     if(ch!= EOF)
  294.     {
  295.         putback((char )ch);
  296.  
  297.         if(FGets(_fh,(STRPTR)c,80))
  298.         {
  299.             if(c[strlen(c)-1]=='\n')
  300.             {
  301.                 c[strlen(c)-1]=0;
  302.             }
  303.         }
  304.         else
  305.         {
  306.             set(ios::failbit);
  307.             *c=0;
  308.         }
  309.     }
  310.     else
  311.     {
  312.         *c=0;
  313.         set(ios::eofbit);
  314.     }
  315.     return *this;
  316. }
  317.  
  318. inline cina& cina::operator>>(char &c)
  319. {
  320.     return get(c);
  321. }
  322.  
  323. inline cina& cina::flush()
  324. {
  325.     look_tie();
  326.     if(_fh) Flush(_fh);
  327.     return *this;
  328. }
  329.  
  330. inline cina& flush(cina& a)
  331. {
  332.     return a.flush();
  333. }
  334.  
  335. inline cina& WS(cina& str) { return ws(str); }
  336.  
  337.  
  338. inline cina& ws(cina& ins)
  339. {
  340.     int ch = ins.skip_ws(ins);
  341.     if (ch == EOF)
  342.       ins.set(ios::eofbit);
  343.     else
  344.         ins.putback((char )ch);
  345.  
  346.     return ins;
  347. }
  348.  
  349.  
  350. extern cina cin;
  351.  
  352. #endif
  353.