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