atlas  0.6
basic_io_def.h
Go to the documentation of this file.
1 /*
2  This is basic_io_def.h
3 
4  Copyright (C) 2004,2005 Fokko du Cloux
5  part of the Atlas of Lie Groups and Representations
6 
7  For license information see the LICENSE file
8 */
9 
10 #include <iostream>
11 
12 /*****************************************************************************
13 
14  Chapter I -- Template functions defined in basic_io.h
15 
16 ******************************************************************************/
17 
18 namespace atlas {
19 
20 
21 namespace basic_io {
22 
23 /*
24  This is a function for sequential output. It is assumed that I is an
25  iterator type, pointing to a type for which the << operator is defined.
26  Then we output the elements of the range [first,last[ as a list,
27  with prefix pre, postfix post, and separator sep.
28 */
29 template<typename I>
30  std::ostream& seqPrint(std::ostream& strm, const I& first, const I& last,
31  const char* sep, const char* pre, const char* post)
32 {
33  strm << pre;
34  bool firstElt = true;
35 
36  for (I i = first; i != last; ++i)
37  {
38  if (firstElt)
39  firstElt = false;
40  else
41  strm << sep;
42  strm << *i;
43  }
44 
45  strm << post;
46 
47  return strm;
48 }
49 
50 template <unsigned int n>
51 inline unsigned long long read_bytes(std::istream& in)
52 {
53  return static_cast<unsigned char>(in.get())+(read_bytes<n-1>(in)<<8);
54 }
55 
56 template<>
57 inline unsigned long long read_bytes<1>(std::istream& in)
58 {
59  return static_cast<unsigned char>(in.get());
60 }
61 
62 // binary output
63 template <unsigned int n>
64 inline void write_bytes(unsigned long long val, std::ostream& out)
65 {
66  out.put(char(val&0xFF)); write_bytes<n-1>(val>>8,out);
67 }
68 
69 template <>
70 inline void write_bytes<1>(unsigned long long val, std::ostream& out)
71 {
72  out.put(char(val));
73 }
74 
75 } // |namespace basic_io|
76 
77 } // |namespace atlas|
unsigned long long read_bytes(std::istream &in)
Definition: basic_io_def.h:51
unsigned long long read_bytes< 1 >(std::istream &in)
Definition: basic_io_def.h:57
std::ostream & seqPrint(std::ostream &, const I &, const I &, const char *sep=",", const char *pre="", const char *post="")
Definition: basic_io_def.h:30
#define out(c)
Definition: cweave.c:205
unsigned long n
Definition: axis.cpp:77
Definition: Atlas.h:38
void write_bytes< 1 >(unsigned long long val, std::ostream &out)
Definition: basic_io_def.h:70
void write_bytes(unsigned int n, unsigned long long val, std::ostream &out)
Definition: basic_io.cpp:183