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)
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
21 *------------------------------------------------------------------
22 * shared data for all io functions
23 * other io related stuff, like files and formatting
25 //testing=script 2006.07.17
30 using std::stringstream
;
32 /*--------------------------------------------------------------------------*/
34 const unsigned MAXHANDLE
= (unsigned)(CHAR_BIT
*sizeof(int)-1);
35 /*--------------------------------------------------------------------------*/
36 class INTERFACE OMSTREAM
{
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 */
49 :_mask(m
),_fltdig(7),_fltwid(0),_format(0),_cipher(false),_pack(false) {}
52 explicit OMSTREAM(FILE* f
= 0)
53 :_mask(0),_fltdig(7),_fltwid(0),_format(0),_cipher(false), _pack(false),
55 {_mask
= (f
) ? 1<<fileno(f
) : 0;}
56 OMSTREAM
* outset(CS
& cmd
);
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;}
78 {_fltdig
=7;_fltwid
=0;_format
=0; _cipher
=_pack
=false; return *this;}
80 OMSTREAM
& tab(unsigned);
81 OMSTREAM
& tab(int p
) {return tab(static_cast<unsigned>(p
));}
82 OMSTREAM
& form(const char*,...);
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.
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
107 OMSTREAM
& OMSTREAM::operator<< <>(const char *str
)
110 return ostream_const_char_p(str
);
112 /*--------------------------------------------------------------------------*/
113 /* mputc: multiple putc
116 * crunch spaces, if selected
117 * encripts, if selected
118 * keeps track of character count
121 inline OMSTREAM
& OMSTREAM::operator<< <>(char chr
)
123 return ostream_char(chr
);
125 /*--------------------------------------------------------------------------*/
128 inline OMSTREAM
& OMSTREAM::operator<< <>(double x
)
130 return (*this)<<ftos(x
,_fltwid
,_fltdig
,_format
);
134 inline OMSTREAM
& OMSTREAM::operator<< <>(COMPLEX c
)
138 *this << "-" << -c
.imag();
141 *this << "+" << c
.imag();
143 return *this << "* i";
147 inline OMSTREAM
& OMSTREAM::operator<< <>(bool x
) {return form("%d", x
);}
149 inline OMSTREAM
& OMSTREAM::operator<< <>(int x
) {return form("%d", x
);}
151 inline OMSTREAM
& OMSTREAM::operator<< <>(long int x
) {return form("%li", x
);}
153 inline OMSTREAM
& OMSTREAM::operator<< <>(unsigned x
) {return form("%u", x
);}
155 inline OMSTREAM
& OMSTREAM::operator<< <>(const std::string
& s
) {untested(); return (operator<<(s
.c_str()));}
157 // last resort, make sure to override this carefully
159 inline OMSTREAM
& OMSTREAM::operator<< (T t
){
162 return *this << a
.str().c_str();
164 /*--------------------------------------------------------------------------*/
166 const char* octal(int x
);
167 /*--------------------------------------------------------------------------*/
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 /*--------------------------------------------------------------------------*/
189 // vim:ts=8:sw=2:noet: