testing
[gnucap-felix.git] / src / io_.h
blob3ab124fe9e3ac76c6e5c7cb794b4017abaecb464
1 /*$Id: io_.h,v 1.2 2010-07-09 12:14:23 felix Exp $ -*- C++ -*-
2 * :vim:ts=8:sw=2:et:
3 * Copyright (C) 2001 Albert Davis
4 * Author: Albert Davis <aldavis@gnu.org>
6 * This file is part of "Gnucap", the Gnu Circuit Analysis Package
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3, or (at your option)
11 * any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
22 *------------------------------------------------------------------
23 * shared data for all io functions
24 * other io related stuff, like files and formatting
26 //testing=script 2006.07.17
27 #ifndef IO_H
28 #define IO_H
29 #include "l_lib.h"
30 #include <complex>
31 using std::stringstream;
32 #define BROKEN_IO
33 /*--------------------------------------------------------------------------*/
34 class CS;
35 const unsigned MAXHANDLE = (unsigned)(CHAR_BIT*sizeof(int)-1);
36 /*--------------------------------------------------------------------------*/
37 class INTERFACE OMSTREAM {
38 private:
39 int _mask;
40 int _fltdig; /* max precision for float/double numbers */
41 int _fltwid; /* fixed(min)width for float/double numbers */
42 int _format; /* how to format io. Basic option. */
43 static unsigned _cpos[MAXHANDLE+1];/* character counter */
44 bool _cipher; /* flag: encrypt output file */
45 bool _pack; /* flag: convert whitespace to tabs on out */
46 FILE* fn;
47 FILE* to_pipe;
49 OMSTREAM(int m)
50 :_mask(m),_fltdig(7),_fltwid(0),_format(0),_cipher(false),_pack(false) {}
51 public:
52 ~OMSTREAM();
53 explicit OMSTREAM(FILE* f = 0)
54 :_mask(0),_fltdig(7),_fltwid(0),_format(0),_cipher(false), _pack(false),
55 fn(0), to_pipe(0)
56 {_mask = (f) ? 1<<fileno(f) : 0;}
57 OMSTREAM* outset(CS& cmd);
58 void outreset();
59 OMSTREAM& operator=(const OMSTREAM& x) {_mask = x._mask; return *this;}
60 OMSTREAM& attach(const OMSTREAM& x) {_mask |= x._mask; return *this;}
61 OMSTREAM& attach(FILE* f) {return attach(OMSTREAM(f));}
62 OMSTREAM& detach(const OMSTREAM& x) {_mask &= ~(x._mask); return *this;}
63 OMSTREAM& detach(FILE* f) {return detach(OMSTREAM(f));}
65 OMSTREAM operator-(const OMSTREAM& y) {OMSTREAM x = *this; return x.detach(y);}
66 OMSTREAM operator+(const OMSTREAM& y) {OMSTREAM x = *this; return x.attach(y);}
68 //OMSTREAM& operator<<=(const OMSTREAM& x) {untested();_mask <<= x._mask; return *this;}
69 bool any()const {return _mask != 0;}
70 int mask()const {return _mask;}
71 bool cipher()const {return _cipher;}
72 bool pack()const {return _pack;}
73 int format()const {return _format;}
74 OMSTREAM& setcipher(bool x=true) {untested(); _cipher=x; return *this;}
75 OMSTREAM& setpack(bool x=true) {itested(); _pack=x; return *this;}
76 OMSTREAM& setfloatwidth(int d,int w=0) {_fltdig=d; _fltwid=w; return *this;}
77 OMSTREAM& setformat(int f) {_format=f; return *this;}
78 OMSTREAM& reset()
79 {_fltdig=7;_fltwid=0;_format=0; _cipher=_pack=false; return *this;}
80 /* out */
81 OMSTREAM& tab(unsigned);
82 OMSTREAM& tab(int p) {return tab(static_cast<unsigned>(p));}
83 OMSTREAM& form(const char*,...);
85 template<class T>
86 OMSTREAM& operator<<(T);
89 //OMSTREAM& operator<< <>(char c);
90 //OMSTREAM& operator<<(const char* s);
91 OMSTREAM& ostream_char(char );
92 OMSTREAM& ostream_const_char_p(const char *);
97 /*--------------------------------------------------------------------------*/
98 /* mputs: multiple puts.
99 * puts to "m" style files.
100 * also....
101 * starts new line, prefixes it with + if it would exceed width
102 * width is handled separately for each file to support different widths
103 * (which are not currently handled by .options)
104 * and it is possible that current contents of lines may be different
106 template<>
107 inline
108 OMSTREAM & OMSTREAM::operator<< <>(const char *str)
110 assert(str);
111 return ostream_const_char_p(str);
113 /*--------------------------------------------------------------------------*/
114 /* mputc: multiple putc
115 * multiple putc
116 * also....
117 * crunch spaces, if selected
118 * encripts, if selected
119 * keeps track of character count
121 template<>
122 inline OMSTREAM & OMSTREAM::operator<< <>(char chr)
124 return ostream_char(chr);
126 /*--------------------------------------------------------------------------*/
128 template<>
129 inline OMSTREAM& OMSTREAM::operator<< <>(double x)
131 return (*this)<<ftos(x,_fltwid,_fltdig,_format);
134 template<>
135 inline OMSTREAM& OMSTREAM::operator<< <>(COMPLEX c)
137 *this << c.real();
138 if(c.imag() <0){
139 *this << "-" << -c.imag();
141 else{
142 *this << "+" << c.imag();
144 return *this << "* i";
147 template<>
148 inline OMSTREAM& OMSTREAM::operator<< <>(bool x) {return form("%d", x);}
149 template<>
150 inline OMSTREAM& OMSTREAM::operator<< <>(int x) {return form("%d", x);}
151 template<>
152 inline OMSTREAM& OMSTREAM::operator<< <>(long int x) {return form("%li", x);}
153 template<>
154 inline OMSTREAM& OMSTREAM::operator<< <>(unsigned x) {return form("%u", x);}
155 template<>
156 inline OMSTREAM& OMSTREAM::operator<< <>(const std::string& s) {untested(); return (operator<<(s.c_str()));}
158 // last resort, make sure to override this carefully
159 template<class T>
160 inline OMSTREAM& OMSTREAM::operator<< (T t){
161 stringstream a;
162 a << t;
163 return *this << a.str().c_str();
165 /*--------------------------------------------------------------------------*/
167 const char* octal(int x);
168 /*--------------------------------------------------------------------------*/
169 class INTERFACE IO {
170 public:
171 static OMSTREAM mstdout;
172 static OMSTREAM error;
173 static OMSTREAM plotout; /* where to send ascii graphics */
174 static bool plotset; /* plot on by default flag */
175 static int formaat; /* how to format io. Basic option. */
176 static bool incipher; /* flag: decrypt input file */
177 static FILE* stream[MAXHANDLE+1]; /* reverse of fileno() */
179 /*--------------------------------------------------------------------------*/
180 /* contrl */ INTERFACE void initio(OMSTREAM&);
181 inline void outreset() {untested();IO::mstdout.outreset();}
182 // INTERFACE OMSTREAM* outset(CS&,OMSTREAM*);
183 /* findf */ std::string findfile(const std::string&,const std::string&,int);
184 /* xopen */ void xclose(FILE**);
185 FILE* xopen(CS&,const char*,const char*);
186 /*--------------------------------------------------------------------------*/
187 /*--------------------------------------------------------------------------*/
188 /*--------------------------------------------------------------------------*/
189 #endif
190 // vim:ts=8:sw=2:noet: