1 /*$Id: io_.h,v 1.2 2010-07-09 12:14:23 felix Exp $ -*- C++ -*-
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)
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
22 *------------------------------------------------------------------
23 * shared data for all io functions
24 * other io related stuff, like files and formatting
26 //testing=script 2006.07.17
31 using std::stringstream
;
33 /*--------------------------------------------------------------------------*/
35 const unsigned MAXHANDLE
= (unsigned)(CHAR_BIT
*sizeof(int)-1);
36 /*--------------------------------------------------------------------------*/
37 class INTERFACE OMSTREAM
{
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 */
50 :_mask(m
),_fltdig(7),_fltwid(0),_format(0),_cipher(false),_pack(false) {}
53 explicit OMSTREAM(FILE* f
= 0)
54 :_mask(0),_fltdig(7),_fltwid(0),_format(0),_cipher(false), _pack(false),
56 {_mask
= (f
) ? 1<<fileno(f
) : 0;}
57 OMSTREAM
* outset(CS
& cmd
);
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;}
79 {_fltdig
=7;_fltwid
=0;_format
=0; _cipher
=_pack
=false; return *this;}
81 OMSTREAM
& tab(unsigned);
82 OMSTREAM
& tab(int p
) {return tab(static_cast<unsigned>(p
));}
83 OMSTREAM
& form(const char*,...);
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.
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
108 OMSTREAM
& OMSTREAM::operator<< <>(const char *str
)
111 return ostream_const_char_p(str
);
113 /*--------------------------------------------------------------------------*/
114 /* mputc: multiple putc
117 * crunch spaces, if selected
118 * encripts, if selected
119 * keeps track of character count
122 inline OMSTREAM
& OMSTREAM::operator<< <>(char chr
)
124 return ostream_char(chr
);
126 /*--------------------------------------------------------------------------*/
129 inline OMSTREAM
& OMSTREAM::operator<< <>(double x
)
131 return (*this)<<ftos(x
,_fltwid
,_fltdig
,_format
);
135 inline OMSTREAM
& OMSTREAM::operator<< <>(COMPLEX c
)
139 *this << "-" << -c
.imag();
142 *this << "+" << c
.imag();
144 return *this << "* i";
148 inline OMSTREAM
& OMSTREAM::operator<< <>(bool x
) {return form("%d", x
);}
150 inline OMSTREAM
& OMSTREAM::operator<< <>(int x
) {return form("%d", x
);}
152 inline OMSTREAM
& OMSTREAM::operator<< <>(long int x
) {return form("%li", x
);}
154 inline OMSTREAM
& OMSTREAM::operator<< <>(unsigned x
) {return form("%u", x
);}
156 inline OMSTREAM
& OMSTREAM::operator<< <>(const std::string
& s
) {untested(); return (operator<<(s
.c_str()));}
158 // last resort, make sure to override this carefully
160 inline OMSTREAM
& OMSTREAM::operator<< (T t
){
163 return *this << a
.str().c_str();
165 /*--------------------------------------------------------------------------*/
167 const char* octal(int x
);
168 /*--------------------------------------------------------------------------*/
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 /*--------------------------------------------------------------------------*/
190 // vim:ts=8:sw=2:noet: