1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libmbutil/input.h,v 1.24 2017/01/12 14:44:04 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati	<masarati@aero.polimi.it>
9  * Paolo Mantegazza	<mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
30  */
31 
32 /* Input */
33 
34 #ifndef INPUT_H
35 #define INPUT_H
36 
37 #include <iostream>
38 #include <myassert.h>
39 
40 /* Filtro per la classe istream che conta il numero di righe. */
41 
42 /* InputStream - begin */
43 
44 class InputStream {
45 	friend InputStream& operator >> (InputStream& in, int& i);
46 	friend InputStream& operator >> (InputStream& in, long int& i);
47 	friend InputStream& operator >> (InputStream& in, short int& i);
48 	friend InputStream& operator >> (InputStream& in, unsigned int& i);
49 	friend InputStream& operator >> (InputStream& in, unsigned long int& i);
50 	friend InputStream& operator >> (InputStream& in, unsigned short int& i);
51 	friend InputStream& operator >> (InputStream& in, char& i);
52 	friend InputStream& operator >> (InputStream& in, float& i);
53 	friend InputStream& operator >> (InputStream& in, double& i);
54 
55 private:
56 	std::istream& iStrm;
57 	unsigned long uLineNumber;
58 
59 public:
60 	/* Costruttore - inizializza il filtro con un reference ad un istream */
61 	InputStream(std::istream& in);
62 
63 	/* Distruttore banale */
64 	~InputStream(void);
65 
66 	/* Legge un carattere; se e' un fine-riga, aggiorna il contatore */
67 	inline char get(void);
68 
69 	/* Legge un carattere; se e' un fine-riga, aggiorna il contatore */
70 	inline std::istream& get(char& ch);
71 
72 	/* Esegue il putback di un carattere */
73 	inline InputStream& putback(char ch);
74 
75 	/* Restituisce il valore del contatore */
76 	inline unsigned long int GetLineNumber(void) const;
77 
78 	/* eof */
79 	inline bool eof(void) const;
80 
81 	/* Restituisce l'istream */
82 	inline const std::istream& GetStream(void) const;
83 	inline std::istream& GetStream(void);
84 };
85 
86 /* Overload dell'operatore di lettura */
87 extern InputStream& operator >> (InputStream& in, int& i);
88 extern InputStream& operator >> (InputStream& in, long int& i);
89 extern InputStream& operator >> (InputStream& in, short int& i);
90 extern InputStream& operator >> (InputStream& in, unsigned int& i);
91 extern InputStream& operator >> (InputStream& in, unsigned long int& i);
92 extern InputStream& operator >> (InputStream& in, unsigned short int& i);
93 extern InputStream& operator >> (InputStream& in, char& i);
94 extern InputStream& operator >> (InputStream& in, float& i);
95 extern InputStream& operator >> (InputStream& in, double& i);
96 
97 /* Legge un carattere; se e' un fine-riga, aggiorna il contatore */
98 inline char
get(void)99 InputStream::get(void)
100 {
101 	char ch = iStrm.get();
102 	if (ch == '\n') {
103 		uLineNumber++;
104 	}
105 	return ch;
106 }
107 
108 /* Legge un carattere; se e' un fine-riga, aggiorna il contatore */
109 inline std::istream&
get(char & ch)110 InputStream::get(char& ch)
111 {
112 	std::istream& i = iStrm.get(ch);
113 	if (ch == '\n') {
114 		uLineNumber++;
115 	}
116 	return i;
117 }
118 
119 /* Esegue il putback di un carattere */
120 inline InputStream&
putback(char ch)121 InputStream::putback(char ch)
122 {
123 	iStrm.putback(ch);
124 	if (ch == '\n') {
125 		uLineNumber--;
126 	}
127 	return *this;
128 }
129 
130 /* Restituisce il valore del contatore */
131 inline unsigned long int
GetLineNumber(void)132 InputStream::GetLineNumber(void) const
133 {
134 	return uLineNumber;
135 }
136 
137 /* eof */
138 inline bool
eof(void)139 InputStream::eof(void) const
140 {
141 	return iStrm.eof();
142 }
143 
144 /* Restituisce l'istream */
145 inline const std::istream&
GetStream(void)146 InputStream::GetStream(void) const
147 {
148 	return iStrm;
149 }
150 
151 inline std::istream&
GetStream(void)152 InputStream::GetStream(void)
153 {
154 	return iStrm;
155 }
156 
157 /* InputStream - end */
158 
159 #endif /* INPUT_H */
160 
161