home *** CD-ROM | disk | FTP | other *** search
- /*-- Rev Header - do NOT edit!
- *
- * Filename : cina.h
- * Purpose : (Erst einmal) Ersatz von istream, e.g. cin
- *
- * Program : -
- * Author : Gerhard Müller
- * Copyright: (c) by Gerhard Müller
- * Creation :
- *
- * compile : make
- *
- * Compile version : 0.1
- * Ext. Version : 0.1
- *
- * REVISION HISTORY
- *
- * Date Comment
- * ------------------------ -------------------------------------------------
- * Fri Sep 17 00:41:18 1993 Seems to work, some part of ios is supported
- *
- *
- *-- REV_END --
- */
-
- #ifndef ADD_CINA_H
- #define ADD_CINA_H
-
- /*
- * C-Includes, C-Definitionen
- *
- */
-
- #define class _class
- #define template _template
-
- extern "C" {
- #include <exec/types.h>
- #include <dos/dos.h>
- #include <dos/dosextens.h>
- #include <clib/alib_protos.h>
- #include <clib/alib_stdio_protos.h>
- #include <inline/stubs.h>
- #ifdef __OPTIMIZE__
- #include <inline/exec.h>
- #include <inline/dos.h>
- #else
- #include <clib/exec_protos.h>
- #include <clib/dos_protos.h>
- #endif
-
- #include <ctype.h>
- }
-
- #undef template
- #undef class
-
- #include "ios.h"
-
- #ifndef EOF
- #define EOF -1
- #endif
-
- class cina;
-
- typedef cina& (*__imanip)(cina&);
-
- #ifndef ADD_COUTA_H
- enum seek_dir { beg=OFFSET_BEGINNING, cur=OFFSET_CURRENT, end=OFFSET_END};
- #endif
-
- inline int skip_ws(cina& sb);
- inline cina& ws(cina& ins);
-
- class cina : public ios {
- public:
- cina() : ios(Input())
- {
- _flags |= ios::dont_close;
- }
-
- cina (BPTR own_fh) : ios(own_fh)
- {
- _flags |= ios::dont_close;
- }
-
- ~cina() { flush();}
-
- // BPTR tie() { return _tie; }
- // BPTR tie(BPTR new_fh) { BPTR return_fh=_tie; _tie=new_fh; return return_fh; }
-
- cina& operator>>(long& return_val);
- inline cina& operator>>(char *c);
- inline cina& operator>>(char &c);
-
- inline cina& operator>>(__imanip func) { return (*func)(*this); }
- // inline cina& operator<<(cina& os, __manip func) {(*func)(os); return os;}
-
- inline cina& flush();
-
-
- cina& get(char& c)
- {
- look_tie();
- int ch;
-
- ch=(int)FGetC(_fh);
-
- if (ch == EOF)
- set(ios::eofbit|ios::failbit);
- else
- c=(char) ch;
-
- return *this;
- }
-
- cina& get(char* p, int n, char d='\n')
- {
- int ch;
- int x;
-
- look_tie();
-
- ch = skip_ws(*this); // we don't want to have any '\n' at he beginning
-
- for(x=0; x<(n-1); x++)
- {
- if(ch==EOF) { set(ios::eofbit); break; }
- else
- {
- if(ch==d)
- {
- UnGetC(_fh,ch);
- x=n;
- }
- else
- *p++=(char) ch;
- }
-
- ch=FGetC(_fh); // read next char
- }
- *p=0;
- return *this;
- }
-
- cina& putback(char pb) { UnGetC(_fh,pb); return *this; }
-
- cina& get(unsigned char* ptr, int len, char delim = '\n')
- { return get((char*)ptr, len, delim); }
-
- cina& get(unsigned char& c) { return get((char&)c); }
-
- cina& getline(char* ptr, int len, char delim = '\n')
- {
- int ch;
- int x;
-
- look_tie();
-
- ch = skip_ws(*this); // we don't want to have any '\n' at he beginning
-
- for(x=0; x<(len-1); x++)
- {
- if(ch==EOF) { set(ios::eofbit); break; }
- if(ch==delim) break;
- *ptr++=(char) ch;
-
- ch=FGetC(_fh);
- }
- *ptr=0;
- return *this;
- }
-
- cina& getline(unsigned char* ptr, int len, char delim = '\n')
- { return getline((char*)ptr, len, delim); }
-
- cina& read(char *ptr, int n)
- {
- LONG x;
- look_tie();
- SetIoErr(0L);
- if(x=FRead(_fh,ptr,n,1)==0) set(ios::eofbit|ios::failbit);
- if(x!=n) set(ios::failbit);
- return *this;
- }
- cina& read(unsigned char *ptr, int n) { return read((char*)ptr, n); }
- cina& read(void *ptr, int n) { return read((char*)ptr, n); }
-
- // cina& gets(char **s, char delim = '\n');
- cina& ignore(int n=1, int delim = EOF)
- {
- ULONG x;
- int ch;
-
- look_tie();
-
- for(x=0; x<n; x++)
- {
- ch=FGetC(_fh);
- if (ch == EOF)
- {
- set(ios::eofbit);
- break;
- }
- if(ch==delim) break;
- }
- return *this;
- }
-
- LONG tellg()
- {
- LONG x = Seek(_fh, 0, OFFSET_CURRENT);
-
- if (x == -1)
- set(ios::failbit);
-
- return x;
- }
-
- cina& seekg(ULONG pos)
- {
- if(Seek(_fh, pos, OFFSET_BEGINNING)==-1) set(ios::failbit);
- return *this;
- }
-
- cina& seekg(ULONG off, seek_dir dir)
- {
- if(Seek(_fh, off, dir)==-1) set(ios::failbit);
- return *this;
- }
-
- int peek()
- {
- int ch;
-
- ch=FGetC(_fh);
-
- if (ch == EOF)
- set(ios::eofbit);
- else
- UnGetC(_fh,ch);
- return ch;
- }
-
- int skip_ws(cina& sb)
- {
- int ch;
-
- look_tie();
-
- for (;;)
- {
- ch=FGetC(_fh);
-
- if (ch == EOF)
- set(ios::eofbit);
-
- if (ch == EOF || !isspace(ch))
- return ch;
- }
- }
-
- BPTR attach(BPTR fh)
- {
- BPTR save=_fh;
- flush();
- _fh=fh;
- if(!fh) set(ios::failbit);
- return save;
- }
-
-
- private:
-
- void look_tie()
- {
- if(_tie)
- if(_tie!=_fh) // IMPORTANT if Input()==Output()
- Flush(_tie);
- }
- };
-
-
-
- inline cina& cina::operator>>(char *c)
- {
- look_tie();
-
- int ch;
-
- ch = skip_ws(*this); // we don't want to have any '\n' at he beginning
-
- if(ch!= EOF)
- {
- putback((char )ch);
-
- if(FGets(_fh,(STRPTR)c,80))
- {
- if(c[strlen(c)-1]=='\n')
- {
- c[strlen(c)-1]=0;
- }
- }
- else
- {
- set(ios::failbit);
- *c=0;
- }
- }
- else
- {
- *c=0;
- set(ios::eofbit);
- }
- return *this;
- }
-
- inline cina& cina::operator>>(char &c)
- {
- return get(c);
- }
-
- inline cina& cina::flush()
- {
- look_tie();
- if(_fh) Flush(_fh);
- return *this;
- }
-
- inline cina& flush(cina& a)
- {
- return a.flush();
- }
-
- inline cina& WS(cina& str) { return ws(str); }
-
-
- inline cina& ws(cina& ins)
- {
- int ch = ins.skip_ws(ins);
- if (ch == EOF)
- ins.set(ios::eofbit);
- else
- ins.putback((char )ch);
-
- return ins;
- }
-
-
- extern cina cin;
-
- #endif
-