1 |
/********************************************************************* |
2 |
* Copyright 1992, University Corporation for Atmospheric Research |
3 |
* See netcdf/README file for copying and redistribution conditions. |
4 |
* |
5 |
* Purpose: implementation of classes of typed arrays for netCDF |
6 |
* |
7 |
* $Header: /upc/share/CVS/netcdf-3/cxx/ncvalues.cpp,v 1.11 2006/12/29 16:38:56 ed Exp $ |
8 |
*********************************************************************/ |
9 |
|
10 |
#include <config.h> |
11 |
#include <iostream> |
12 |
#include <string> |
13 |
|
14 |
#include "ncvalues.h" |
15 |
|
16 |
NcValues::NcValues( void ) : the_type(ncNoType), the_number(0) |
17 |
{} |
18 |
|
19 |
NcValues::NcValues(NcType type, long num) |
20 |
: the_type(type), the_number(num) |
21 |
{} |
22 |
|
23 |
NcValues::~NcValues( void ) |
24 |
{} |
25 |
|
26 |
long NcValues::num( void ) |
27 |
{ |
28 |
return the_number; |
29 |
} |
30 |
|
31 |
std::ostream& operator<< (std::ostream& os, const NcValues& vals) |
32 |
{ |
33 |
return vals.print(os); |
34 |
} |
35 |
|
36 |
implement(NcValues,ncbyte) |
37 |
implement(NcValues,char) |
38 |
implement(NcValues,short) |
39 |
implement(NcValues,int) |
40 |
implement(NcValues,nclong) |
41 |
implement(NcValues,long) |
42 |
implement(NcValues,float) |
43 |
implement(NcValues,double) |
44 |
|
45 |
Ncbytes_for_one_implement(ncbyte) |
46 |
Ncbytes_for_one_implement(char) |
47 |
Ncbytes_for_one_implement(short) |
48 |
Ncbytes_for_one_implement(int) |
49 |
Ncbytes_for_one_implement(nclong) |
50 |
Ncbytes_for_one_implement(long) |
51 |
Ncbytes_for_one_implement(float) |
52 |
Ncbytes_for_one_implement(double) |
53 |
|
54 |
as_ncbyte_implement(short) |
55 |
as_ncbyte_implement(int) |
56 |
as_ncbyte_implement(nclong) |
57 |
as_ncbyte_implement(long) |
58 |
as_ncbyte_implement(float) |
59 |
as_ncbyte_implement(double) |
60 |
|
61 |
inline ncbyte NcValues_char::as_ncbyte( long n ) const |
62 |
{ |
63 |
return the_values[n]; |
64 |
} |
65 |
|
66 |
inline ncbyte NcValues_ncbyte::as_ncbyte( long n ) const |
67 |
{ |
68 |
return the_values[n]; |
69 |
} |
70 |
|
71 |
as_char_implement(short) |
72 |
as_char_implement(int) |
73 |
as_char_implement(nclong) |
74 |
as_char_implement(long) |
75 |
as_char_implement(float) |
76 |
as_char_implement(double) |
77 |
|
78 |
inline char NcValues_ncbyte::as_char( long n ) const |
79 |
{ |
80 |
return the_values[n] > CHAR_MAX ? ncBad_char : (char) the_values[n]; |
81 |
} |
82 |
|
83 |
inline char NcValues_char::as_char( long n ) const |
84 |
{ |
85 |
return the_values[n]; |
86 |
} |
87 |
|
88 |
as_short_implement(int) |
89 |
as_short_implement(nclong) |
90 |
as_short_implement(long) |
91 |
as_short_implement(float) |
92 |
as_short_implement(double) |
93 |
|
94 |
inline short NcValues_ncbyte::as_short( long n ) const |
95 |
{ |
96 |
return the_values[n]; |
97 |
} |
98 |
|
99 |
inline short NcValues_char::as_short( long n ) const |
100 |
{ |
101 |
return the_values[n]; |
102 |
} |
103 |
|
104 |
inline short NcValues_short::as_short( long n ) const |
105 |
{ |
106 |
return the_values[n]; |
107 |
} |
108 |
|
109 |
|
110 |
as_int_implement(float) |
111 |
as_int_implement(double) |
112 |
|
113 |
inline int NcValues_ncbyte::as_int( long n ) const |
114 |
{ |
115 |
return the_values[n]; |
116 |
} |
117 |
|
118 |
inline int NcValues_char::as_int( long n ) const |
119 |
{ |
120 |
return the_values[n]; |
121 |
} |
122 |
|
123 |
inline int NcValues_short::as_int( long n ) const |
124 |
{ |
125 |
return the_values[n]; |
126 |
} |
127 |
|
128 |
inline int NcValues_int::as_int( long n ) const |
129 |
{ |
130 |
return the_values[n]; |
131 |
} |
132 |
|
133 |
inline int NcValues_nclong::as_int( long n ) const |
134 |
{ |
135 |
return the_values[n]; |
136 |
} |
137 |
|
138 |
inline int NcValues_long::as_int( long n ) const |
139 |
{ |
140 |
return the_values[n]; |
141 |
} |
142 |
|
143 |
as_nclong_implement(float) |
144 |
as_nclong_implement(double) |
145 |
|
146 |
inline nclong NcValues_ncbyte::as_nclong( long n ) const |
147 |
{ |
148 |
return the_values[n]; |
149 |
} |
150 |
|
151 |
inline nclong NcValues_char::as_nclong( long n ) const |
152 |
{ |
153 |
return the_values[n]; |
154 |
} |
155 |
|
156 |
inline nclong NcValues_short::as_nclong( long n ) const |
157 |
{ |
158 |
return the_values[n]; |
159 |
} |
160 |
|
161 |
inline nclong NcValues_int::as_nclong( long n ) const |
162 |
{ |
163 |
return the_values[n]; |
164 |
} |
165 |
|
166 |
inline nclong NcValues_nclong::as_nclong( long n ) const |
167 |
{ |
168 |
return the_values[n]; |
169 |
} |
170 |
|
171 |
inline nclong NcValues_long::as_nclong( long n ) const |
172 |
{ |
173 |
return the_values[n]; |
174 |
} |
175 |
|
176 |
as_long_implement(float) |
177 |
as_long_implement(double) |
178 |
|
179 |
inline long NcValues_ncbyte::as_long( long n ) const |
180 |
{ |
181 |
return the_values[n]; |
182 |
} |
183 |
|
184 |
inline long NcValues_char::as_long( long n ) const |
185 |
{ |
186 |
return the_values[n]; |
187 |
} |
188 |
|
189 |
inline long NcValues_short::as_long( long n ) const |
190 |
{ |
191 |
return the_values[n]; |
192 |
} |
193 |
|
194 |
inline long NcValues_int::as_long( long n ) const |
195 |
{ |
196 |
return the_values[n]; |
197 |
} |
198 |
|
199 |
inline long NcValues_nclong::as_long( long n ) const |
200 |
{ |
201 |
return the_values[n]; |
202 |
} |
203 |
|
204 |
inline long NcValues_long::as_long( long n ) const |
205 |
{ |
206 |
return the_values[n]; |
207 |
} |
208 |
|
209 |
as_float_implement(ncbyte) |
210 |
as_float_implement(char) |
211 |
as_float_implement(short) |
212 |
as_float_implement(int) |
213 |
as_float_implement(nclong) |
214 |
as_float_implement(long) |
215 |
as_float_implement(float) |
216 |
as_float_implement(double) |
217 |
|
218 |
as_double_implement(ncbyte) |
219 |
as_double_implement(char) |
220 |
as_double_implement(short) |
221 |
as_double_implement(int) |
222 |
as_double_implement(nclong) |
223 |
as_double_implement(long) |
224 |
as_double_implement(float) |
225 |
as_double_implement(double) |
226 |
|
227 |
as_string_implement(short) |
228 |
as_string_implement(int) |
229 |
as_string_implement(nclong) |
230 |
as_string_implement(long) |
231 |
as_string_implement(float) |
232 |
as_string_implement(double) |
233 |
|
234 |
inline char* NcValues_ncbyte::as_string( long n ) const |
235 |
{ |
236 |
char* s = new char[the_number + 1]; |
237 |
s[the_number] = '\0'; |
238 |
strncpy(s, (const char*)the_values + n, (int)the_number); |
239 |
return s; |
240 |
} |
241 |
|
242 |
inline char* NcValues_char::as_string( long n ) const |
243 |
{ |
244 |
char* s = new char[the_number + 1]; |
245 |
s[the_number] = '\0'; |
246 |
strncpy(s, (const char*)the_values + n, (int)the_number); |
247 |
return s; |
248 |
} |
249 |
|
250 |
std::ostream& NcValues_short::print(std::ostream& os) const |
251 |
{ |
252 |
for(int i = 0; i < the_number - 1; i++) |
253 |
os << the_values[i] << ", "; |
254 |
if (the_number > 0) |
255 |
os << the_values[the_number-1] ; |
256 |
return os; |
257 |
} |
258 |
|
259 |
std::ostream& NcValues_int::print(std::ostream& os) const |
260 |
{ |
261 |
for(int i = 0; i < the_number - 1; i++) |
262 |
os << the_values[i] << ", "; |
263 |
if (the_number > 0) |
264 |
os << the_values[the_number-1] ; |
265 |
return os; |
266 |
} |
267 |
|
268 |
std::ostream& NcValues_nclong::print(std::ostream& os) const |
269 |
{ |
270 |
for(int i = 0; i < the_number - 1; i++) |
271 |
os << the_values[i] << ", "; |
272 |
if (the_number > 0) |
273 |
os << the_values[the_number-1] ; |
274 |
return os; |
275 |
} |
276 |
|
277 |
std::ostream& NcValues_long::print(std::ostream& os) const |
278 |
{ |
279 |
for(int i = 0; i < the_number - 1; i++) |
280 |
os << the_values[i] << ", "; |
281 |
if (the_number > 0) |
282 |
os << the_values[the_number-1] ; |
283 |
return os; |
284 |
} |
285 |
|
286 |
std::ostream& NcValues_ncbyte::print(std::ostream& os) const |
287 |
{ |
288 |
for(int i = 0; i < the_number - 1; i++) |
289 |
os << the_values[i] << ", "; |
290 |
if (the_number > 0) |
291 |
os << the_values[the_number-1] ; |
292 |
return os; |
293 |
} |
294 |
|
295 |
std::ostream& NcValues_char::print(std::ostream& os) const |
296 |
{ |
297 |
os << '"'; |
298 |
long len = the_number; |
299 |
while (the_values[--len] == '\0') // don't output trailing null bytes |
300 |
; |
301 |
for(int i = 0; i <= len; i++) |
302 |
os << the_values[i] ; |
303 |
os << '"'; |
304 |
|
305 |
return os; |
306 |
} |
307 |
|
308 |
std::ostream& NcValues_float::print(std::ostream& os) const |
309 |
{ |
310 |
std::streamsize save=os.precision(); |
311 |
os.precision(7); |
312 |
for(int i = 0; i < the_number - 1; i++) |
313 |
os << the_values[i] << ", "; |
314 |
if (the_number > 0) |
315 |
os << the_values[the_number-1] ; |
316 |
os.precision(save); |
317 |
return os; |
318 |
} |
319 |
|
320 |
std::ostream& NcValues_double::print(std::ostream& os) const |
321 |
{ |
322 |
std::streamsize save=os.precision(); |
323 |
os.precision(15); |
324 |
for(int i = 0; i < the_number - 1; i++) |
325 |
os << the_values[i] << ", "; |
326 |
if (the_number > 0) |
327 |
os << the_values[the_number-1]; |
328 |
os.precision(save); |
329 |
return os; |
330 |
} |