FOX/ObjCryst++  1.10.X (development)
VFNStreamFormat.cpp
1 /* ObjCryst++ Object-Oriented Crystallographic Library
2  (c) 2000-2002 Vincent Favre-Nicolin vincefn@users.sourceforge.net
3  2000-2001 University of Geneva (Switzerland)
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 // This file includes some "effectors" used to format
20 //strings (and especially numbers)
21 
22 #include <iomanip>
23 
24 #include "ObjCryst/Quirks/VFNStreamFormat.h"
25 #include "ObjCryst/Quirks/VFNDebug.h"
26 
27 using namespace std;
28 
30 //
31 // FormatInt
32 //
34 
35 FormatInt::FormatInt(const long num,const int width): mValue(num),mWidth(width) {}
36 FormatInt::~FormatInt(){}
37 
38 ostream& operator<< (ostream& os, const FormatInt& fInt)
39 {
40  return os << setiosflags(ios::right) << setw(fInt.mWidth) <<
41  fInt.mValue << " ";
42 }
43 
45 //
46 // FormatFloat
47 //
49 
50 FormatFloat::FormatFloat(const REAL num,const int width,const int precision)
51  :mValue(num),mWidth(width),mPrecision(precision)
52  {
53  }
54 FormatFloat::~FormatFloat(){}
55 
56 ostream& operator<< (ostream& os, const FormatFloat &fFloat)
57 {
58  std::istream::fmtflags old_flags=os.flags();
59  os.setf( std::istream::fixed | std::istream::right | std::istream::showpoint,std::istream::floatfield );
60  std::streamsize old_prec = os.precision(fFloat.mPrecision);
61  std::streamsize old_width = os.width(fFloat.mWidth);
62  os << fFloat.mValue;
63  os.flags( old_flags );
64  os.precision( old_prec );
65  os.width(old_width);
66  return os;
67 }
68 
70 //
71 // FormatString
72 //
74 FormatString::FormatString(const string &str,const unsigned int width):
75  /*mString(str,1,width),*/mWidth(width)
76 {
77  mString=str;
78  //:KLUDGE:
79  if(mString.size() > mWidth) mString.resize(mWidth);
80  else
81  for(unsigned int i=mString.size();i<width;i++) mString += " ";
82  //cout << mString.size()<<endl;
83  //cout << str << " " << str.length()<< " " << str.size()<<endl;
84 }
85 FormatString::~FormatString(){}
86 int FormatString::length() const {return mString.length();}
87 
88 ostream& operator<< (ostream& os, const FormatString& fStr)
89 {
90  return os << fStr.mString ;
91 }
92 
94 //
95 // FormatVertVector
96 //
98 
99 template<class T> FormatVertVector<T>::FormatVertVector( const CrystVector<T> &fVect,
100  const int width,
101  const int precision, const int nb):
102 mWidth(width),mPrecision(precision),mNb(nb)
103 {
104  mvpVectors.push_back(&fVect);
105 }
106 
107 template<class T> FormatVertVector<T>::FormatVertVector( const CrystVector<T> &fVect1,
108  const CrystVector<T> &fVect2,
109  const int width,
110  const int precision, const int nb):
111 mWidth(width),mPrecision(precision),mNb(nb)
112 {
113  mvpVectors.push_back(&fVect1);
114  mvpVectors.push_back(&fVect2);
115 }
116 
117 template<class T> FormatVertVector<T>::FormatVertVector( const CrystVector<T> &fVect1,
118  const CrystVector<T> &fVect2,
119  const CrystVector<T> &fVect3,
120  const int width,
121  const int precision, const int nb):
122 mWidth(width),mPrecision(precision),mNb(nb)
123 {
124  mvpVectors.push_back(&fVect1);
125  mvpVectors.push_back(&fVect2);
126  mvpVectors.push_back(&fVect3);
127 }
128 
129 template<class T> FormatVertVector<T>::FormatVertVector( const CrystVector<T> &fVect1,
130  const CrystVector<T> &fVect2,
131  const CrystVector<T> &fVect3,
132  const CrystVector<T> &fVect4,
133  const int width,
134  const int precision, const int nb):
135 mWidth(width),mPrecision(precision),mNb(nb)
136 {
137  mvpVectors.push_back(&fVect1);
138  mvpVectors.push_back(&fVect2);
139  mvpVectors.push_back(&fVect3);
140  mvpVectors.push_back(&fVect4);
141 }
142 template<class T> FormatVertVector<T>::FormatVertVector( const CrystVector<T> &fVect1,
143  const CrystVector<T> &fVect2,
144  const CrystVector<T> &fVect3,
145  const CrystVector<T> &fVect4,
146  const CrystVector<T> &fVect5,
147  const int width,
148  const int precision, const int nb):
149 mWidth(width),mPrecision(precision),mNb(nb)
150 {
151  mvpVectors.push_back(&fVect1);
152  mvpVectors.push_back(&fVect2);
153  mvpVectors.push_back(&fVect3);
154  mvpVectors.push_back(&fVect4);
155  mvpVectors.push_back(&fVect5);
156 }
157 template<class T> FormatVertVector<T>::FormatVertVector( const CrystVector<T> &fVect1,
158  const CrystVector<T> &fVect2,
159  const CrystVector<T> &fVect3,
160  const CrystVector<T> &fVect4,
161  const CrystVector<T> &fVect5,
162  const CrystVector<T> &fVect6,
163  const int width,
164  const int precision, const int nb):
165 mWidth(width),mPrecision(precision),mNb(nb)
166 {
167  mvpVectors.push_back(&fVect1);
168  mvpVectors.push_back(&fVect2);
169  mvpVectors.push_back(&fVect3);
170  mvpVectors.push_back(&fVect4);
171  mvpVectors.push_back(&fVect5);
172  mvpVectors.push_back(&fVect6);
173 }
174 
175 template<class T> FormatVertVector<T>::FormatVertVector( const CrystVector<T> *pVect,
176  const int nbVect,
177  const int width,
178  const int precision, const int nb):
179 mWidth(width),mPrecision(precision),mNb(nb)
180 {
181  for(int i=0;i<nbVect;i++) mvpVectors.push_back(&(pVect[i]));
182 }
183 
184 template<class T> FormatVertVector<T>::FormatVertVector( const CrystVector<T> &fVect1,
185  const CrystVector<T> *pVect,
186  const int nbVect,
187  const int width,
188  const int precision, const int nb):
189 mWidth(width),mPrecision(precision),mNb(nb)
190 {
191  mvpVectors.push_back(&fVect1);
192  for(int i=1;i<nbVect;i++) mvpVectors.push_back(&(pVect[i-1]));
193 }
194 
195 template<class T> FormatVertVector<T>::FormatVertVector(
196  vector<const CrystVector<T> *>& v,
197  const int width,
198  const int precision, const int nb):
199 mWidth(width),mPrecision(precision),mNb(nb)
200 {
201  mvpVectors=v;
202 }
203 
204 template<class T> FormatVertVector<T>::~FormatVertVector()
205 {
206 }
207 
208 template<class T> ostream& operator<< (ostream &os, const FormatVertVector<T> &fVect)
209 {
210  VFN_DEBUG_MESSAGE("ostream& operator<<(os,FormatVertVector<T>)",3)
211  long i;
212  size_t j;
213  std::istream::fmtflags old_flags=os.flags();
214  os.setf( std::istream::fixed | std::istream::right | std::istream::showpoint);
215  std::streamsize old_prec = os.precision(fVect.mPrecision);
216  std::streamsize old_width = os.width();
217  long nb=fVect.mNb;
218  if(nb==0)nb=(fVect.mvpVectors[0])->numElements();
219  for(i=0;i<nb;i++)
220  {
221  for(j=0;j<fVect.mvpVectors.size();j++)
222  {
223  os <<setw(fVect.mWidth)<< (*fVect.mvpVectors[j])(i) << " ";
224  }
225  os << endl;
226  }
227  os.flags( old_flags );
228  os.precision( old_prec );
229  os<<setw(old_width);
230  VFN_DEBUG_MESSAGE("ostream& operator<<(os,FormatVertVector<T>):End",2)
231  return os;
232 }
233 
235 //
236 // FormatHorizVector
237 //
239 
240 template<class T> FormatHorizVector<T>::FormatHorizVector( const CrystVector<T> &fVect,
241  const int width,
242  const int precision):
243 mWidth(width),mPrecision(precision)
244 {
245  mpVectors=&fVect;
246 }
248 {}
249 
250 template<class T> ostream& operator<< (ostream &os, const FormatHorizVector<T> &fVect)
251 {
252  long i;
253  std::istream::fmtflags old_flags=os.flags();
254  os.setf( std::istream::fixed | std::istream::right | std::istream::showpoint);
255  std::streamsize old_prec = os.precision(fVect.mPrecision);
256  std::streamsize old_width = os.width();
257  for(i=0;i<(*(fVect.mpVectors)).numElements();i++)
258  {
259  os <<setw(fVect.mWidth)<< (*(fVect.mpVectors))(i) << " ";
260  }
261  os<<endl;
262  os.flags( old_flags );
263  os.precision( old_prec );
264  os<<setw(old_width);
265  return os;
266 }
267 
269 //
270 // FormatVertVectorHKLFloats
271 //
274  const CrystVector<T> &h,
275  const CrystVector<T> &k,
276  const CrystVector<T> &l,
277  const int width,
278  const int precision,
279  const int nb):
280 mWidth(width),mPrecision(precision),mNb(nb)
281 {
282  mvpVectors.push_back(&h);
283  mvpVectors.push_back(&k);
284  mvpVectors.push_back(&l);
285 }
287  const CrystVector<T> &h,
288  const CrystVector<T> &k,
289  const CrystVector<T> &l,
290  const CrystVector<T> &m,
291  const int width,
292  const int precision,
293  const int nb):
294 mWidth(width),mPrecision(precision),mNb(nb)
295 {
296  mvpVectors.push_back(&h);
297  mvpVectors.push_back(&k);
298  mvpVectors.push_back(&l);
299  mvpVectors.push_back(&m);
300 }
302  const CrystVector<T> &h,
303  const CrystVector<T> &k,
304  const CrystVector<T> &l,
305  const CrystVector<T> &m,
306  const CrystVector<T> &n,
307  const int width,
308  const int precision,
309  const int nb):
310 mWidth(width),mPrecision(precision),mNb(nb)
311 {
312  mvpVectors.push_back(&h);
313  mvpVectors.push_back(&k);
314  mvpVectors.push_back(&l);
315  mvpVectors.push_back(&m);
316  mvpVectors.push_back(&n);
317 }
319  const CrystVector<T> &h,
320  const CrystVector<T> &k,
321  const CrystVector<T> &l,
322  const CrystVector<T> &m,
323  const CrystVector<T> &n,
324  const CrystVector<T> &o,
325  const int width,
326  const int precision,
327  const int nb):
328 mWidth(width),mPrecision(precision),mNb(nb)
329 {
330  mvpVectors.push_back(&h);
331  mvpVectors.push_back(&k);
332  mvpVectors.push_back(&l);
333  mvpVectors.push_back(&m);
334  mvpVectors.push_back(&n);
335  mvpVectors.push_back(&o);
336 }
338  const CrystVector<T> &h,
339  const CrystVector<T> &k,
340  const CrystVector<T> &l,
341  const CrystVector<T> &m,
342  const CrystVector<T> &n,
343  const CrystVector<T> &o,
344  const CrystVector<T> &p,
345  const int width,
346  const int precision,
347  const int nb):
348 mWidth(width),mPrecision(precision),mNb(nb)
349 {
350  mvpVectors.push_back(&h);
351  mvpVectors.push_back(&k);
352  mvpVectors.push_back(&l);
353  mvpVectors.push_back(&m);
354  mvpVectors.push_back(&n);
355  mvpVectors.push_back(&o);
356  mvpVectors.push_back(&p);
357 }
359  const CrystVector<T> &h,
360  const CrystVector<T> &k,
361  const CrystVector<T> &l,
362  const CrystVector<T> &m,
363  const CrystVector<T> &n,
364  const CrystVector<T> &o,
365  const CrystVector<T> &p,
366  const CrystVector<T> &q,
367  const int width,
368  const int precision,
369  const int nb):
370 mWidth(width),mPrecision(precision),mNb(nb)
371 {
372  mvpVectors.push_back(&h);
373  mvpVectors.push_back(&k);
374  mvpVectors.push_back(&l);
375  mvpVectors.push_back(&m);
376  mvpVectors.push_back(&n);
377  mvpVectors.push_back(&o);
378  mvpVectors.push_back(&p);
379  mvpVectors.push_back(&q);
380 }
382  const CrystVector<T> &h,
383  const CrystVector<T> &k,
384  const CrystVector<T> &l,
385  const CrystVector<T> &m,
386  const CrystVector<T> &n,
387  const CrystVector<T> &o,
388  const CrystVector<T> &p,
389  const CrystVector<T> &q,
390  const CrystVector<T> &r,
391  const int width,
392  const int precision,
393  const int nb):
394 mWidth(width),mPrecision(precision),mNb(nb)
395 {
396  mvpVectors.push_back(&h);
397  mvpVectors.push_back(&k);
398  mvpVectors.push_back(&l);
399  mvpVectors.push_back(&m);
400  mvpVectors.push_back(&n);
401  mvpVectors.push_back(&o);
402  mvpVectors.push_back(&p);
403  mvpVectors.push_back(&q);
404  mvpVectors.push_back(&r);
405 }
407  const CrystVector<T> &h,
408  const CrystVector<T> &k,
409  const CrystVector<T> &l,
410  const CrystVector<T> &m,
411  const CrystVector<T> &n,
412  const CrystVector<T> &o,
413  const CrystVector<T> &p,
414  const CrystVector<T> &q,
415  const CrystVector<T> &r,
416  const CrystVector<T> &s,
417  const int width,
418  const int precision,
419  const int nb):
420 mWidth(width),mPrecision(precision),mNb(nb)
421 {
422  mvpVectors.push_back(&h);
423  mvpVectors.push_back(&k);
424  mvpVectors.push_back(&l);
425  mvpVectors.push_back(&m);
426  mvpVectors.push_back(&n);
427  mvpVectors.push_back(&o);
428  mvpVectors.push_back(&p);
429  mvpVectors.push_back(&q);
430  mvpVectors.push_back(&r);
431  mvpVectors.push_back(&s);
432 }
434  const CrystVector<T> &h,
435  const CrystVector<T> &k,
436  const CrystVector<T> &l,
437  const CrystVector<T> &m,
438  const CrystVector<T> &n,
439  const CrystVector<T> &o,
440  const CrystVector<T> &p,
441  const CrystVector<T> &q,
442  const CrystVector<T> &r,
443  const CrystVector<T> &s,
444  const CrystVector<T> &t,
445  const int width,
446  const int precision,
447  const int nb):
448 mWidth(width),mPrecision(precision),mNb(nb)
449 {
450  mvpVectors.push_back(&h);
451  mvpVectors.push_back(&k);
452  mvpVectors.push_back(&l);
453  mvpVectors.push_back(&m);
454  mvpVectors.push_back(&n);
455  mvpVectors.push_back(&o);
456  mvpVectors.push_back(&p);
457  mvpVectors.push_back(&q);
458  mvpVectors.push_back(&r);
459  mvpVectors.push_back(&s);
460  mvpVectors.push_back(&t);
461 }
463  const CrystVector<T> &h,
464  const CrystVector<T> &k,
465  const CrystVector<T> &l,
466  const CrystVector<T> &m,
467  const CrystVector<T> &n,
468  const CrystVector<T> &o,
469  const CrystVector<T> &p,
470  const CrystVector<T> &q,
471  const CrystVector<T> &r,
472  const CrystVector<T> &s,
473  const CrystVector<T> &t,
474  const CrystVector<T> &u,
475  const int width,
476  const int precision,
477  const int nb):
478 mWidth(width),mPrecision(precision),mNb(nb)
479 {
480  mvpVectors.push_back(&h);
481  mvpVectors.push_back(&k);
482  mvpVectors.push_back(&l);
483  mvpVectors.push_back(&m);
484  mvpVectors.push_back(&n);
485  mvpVectors.push_back(&o);
486  mvpVectors.push_back(&p);
487  mvpVectors.push_back(&q);
488  mvpVectors.push_back(&r);
489  mvpVectors.push_back(&s);
490  mvpVectors.push_back(&t);
491  mvpVectors.push_back(&u);
492 }
494  vector<const CrystVector<T> *>& v,
495  const int width,
496  const int precision, const int nb):
497 mWidth(width),mPrecision(precision),mNb(nb)
498 {
499  mvpVectors=v;
500 }
501 
503 {}
504 
505 template<class T> ostream& operator<< (ostream& os, const FormatVertVectorHKLFloats<T> &fVect)
506 {
507  long i;
508  unsigned int j;
509  std::istream::fmtflags old_flags=os.flags();
510  os.setf( std::istream::fixed | std::istream::right | std::istream::showpoint);
511  std::streamsize old_prec = os.precision(fVect.mPrecision);
512  std::streamsize old_width = os.width();
513  long nb=fVect.mNb;
514  if(nb==0) nb=(fVect.mvpVectors[0])->numElements();
515  for(i=0;i<nb;i++)
516  {
517  for(j=0;j<3;j++)
518  {
519  os <<setw(fVect.mWidth-fVect.mPrecision)<< (int) ((*fVect.mvpVectors[j])(i)) << " ";
520  }
521  for(j=3;j<fVect.mvpVectors.size();j++)
522  {
523  os <<setw(fVect.mWidth)<<(*fVect.mvpVectors[j])(i) << " ";
524  }
525  os << endl;
526  }
527  os.flags( old_flags );
528  os.precision( old_prec );
529  os<<setw(old_width);
530  return os;
531 }
532 
533 
534 //Explicit instantiations
535 template class FormatVertVector<REAL>;
536 template ostream& operator<< (ostream&,const FormatVertVector<REAL>&);
537 template class FormatHorizVector<REAL>;
538 template ostream& operator<< (ostream&,const FormatHorizVector<REAL>&);
539 template class FormatVertVectorHKLFloats<REAL>;
540 template ostream& operator<< (ostream&,const FormatVertVectorHKLFloats<REAL>&);
541 template class FormatVertVector<long>;
542 template ostream& operator<< (ostream&,const FormatVertVector<long>&);
543 template class FormatHorizVector<long>;
544 template ostream& operator<< (ostream&,const FormatHorizVector<long>&);
545 template class FormatVertVectorHKLFloats<long>;
546 template ostream& operator<< (ostream&,const FormatVertVectorHKLFloats<long>&);
output a number as a formatted integer:
ostream & operator<<(ostream &os, const XMLCrystTag &tag)
Output an XMLCrystTag to a stream.
Output vectors as column arrays, with the first 3 columns printed as integers.
output one or several vectors as (a) column(s):
Format vector as horiz array:
Vector library (Blitz++ mimic) for ObjCryst++.
Definition: CrystVector.h:122
output a number as a formatted float:
output a string with a fixed length (adding necessary space or removing excess characters) : ...