Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JMACAddress.hh
Go to the documentation of this file.
1 #ifndef __JDATABASE__JMACADDRESS__
2 #define __JDATABASE__JMACADDRESS__
3 
4 #include <string>
5 #include <istream>
6 #include <ostream>
7 #include <vector>
8 
9 #include "JLang/JEquals.hh"
10 
11 /**
12  * \file
13  * MAC address.
14  * \author mdejong
15  */
16 namespace JDATABASE {}
17 namespace JPP { using namespace JDATABASE; }
18 
19 namespace JDATABASE {
20 
21  using JLANG::JEquals;
22 
23 
24  /**
25  * MAC address
26  */
27  struct JMACAddress :
28  public std::vector<unsigned char>,
29  public JEquals<JMACAddress>
30  {
31  enum {
32  NUMBER_OF_BYTES = 6 //!< number of bytes
33  };
34 
35 
36  /**
37  * Get list of allowed separators.
38  *
39  * \return allowed separators
40  */
41  static const char* get_separator()
42  {
43  return ":.-";
44  }
45 
46 
47  /**
48  * Check if given character is an allowed separator.
49  *
50  * \param c character
51  * \return true if separator; else false
52  */
53  static bool is_separator(const int c)
54  {
55  const std::string buffer(JMACAddress::get_separator());
56 
57  for (std::string::const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
58  if ((int) *i == c) {
59  return true;
60  }
61  }
62 
63  return false;
64  }
65 
66 
67  /**
68  * Default constructor.
69  */
71  std::vector<unsigned char>(NUMBER_OF_BYTES, 0)
72  {}
73 
74 
75  /**
76  * Equal method.
77  *
78  * \param address MAC address
79  * \return true if this MAC address and given MAC address are equal; else false
80  */
81  inline bool equal(const JMACAddress& address) const
82  {
83  for (int i = 0; i != NUMBER_OF_BYTES; ++i) {
84  if ((*this)[i] != address[i]) {
85  return false;
86  }
87  }
88 
89  return true;
90  }
91 
92 
93  /**
94  * Read MAC address from input stream.
95  *
96  * \param in input stream
97  * \param object MAC address
98  * \return input stream
99  */
100  friend inline std::istream& operator>>(std::istream& in, JMACAddress& object)
101  {
102  using namespace std;
103 
104  object = JMACAddress();
105 
106  int c;
107 
108  if (in >> hex >> c) {
109 
110  object[0] = (unsigned char) c;
111 
112  for (int i = 1; i != NUMBER_OF_BYTES; ++i) {
113  if (is_separator(in.get()) && in >> hex >> c)
114  object[i] = (unsigned char) c;
115  else
116  in.setstate(ios::failbit);
117  }
118  }
119 
120  return in >> dec;
121  }
122 
123 
124 
125  /**
126  * Write MAC address to output stream.
127  *
128  * \param out output stream
129  * \param object MAC address
130  * \return output stream
131  */
132  friend inline std::ostream& operator<<(std::ostream& out, const JMACAddress& object)
133  {
134  using namespace std;
135 
136  out << setw(2) << setfill('0') << hex << (int) object[0];
137 
138  for (int i = 1; i != NUMBER_OF_BYTES; ++i) {
139  out << setw(1) << JMACAddress::get_separator()[0]
140  << setw(2) << setfill('0') << hex << (int) object[i];
141  }
142 
143  return out << setfill(' ') << dec;
144  }
145 
147  };
148 }
149 
150 #endif
friend std::istream & operator>>(std::istream &in, JMACAddress &object)
Read MAC address from input stream.
Definition: JMACAddress.hh:100
JMACAddress()
Default constructor.
Definition: JMACAddress.hh:70
Template definition of auxiliary base class for comparison of data structures.
Definition: JEquals.hh:24
ClassDefNV(JMACAddress, 1)
static bool is_separator(const int c)
Check if given character is an allowed separator.
Definition: JMACAddress.hh:53
bool equal(const JMACAddress &address) const
Equal method.
Definition: JMACAddress.hh:81
static const char * get_separator()
Get list of allowed separators.
Definition: JMACAddress.hh:41
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:36
friend std::ostream & operator<<(std::ostream &out, const JMACAddress &object)
Write MAC address to output stream.
Definition: JMACAddress.hh:132