Jpp  18.6.0-rc.1
the software that should make you happy
 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 <iomanip>
8 #include <vector>
9 
10 #include "JLang/JEquals.hh"
11 
12 /**
13  * \file
14  * MAC address.
15  * \author mdejong
16  */
17 namespace JDATABASE {}
18 namespace JPP { using namespace JDATABASE; }
19 
20 namespace JDATABASE {
21 
22  using JLANG::JEquals;
23 
24 
25  /**
26  * MAC address
27  */
28  struct JMACAddress :
29  public std::vector<unsigned char>,
30  public JEquals<JMACAddress>
31  {
32  enum {
33  NUMBER_OF_BYTES = 6 //!< number of bytes
34  };
35 
36 
37  /**
38  * Get list of allowed separators.
39  *
40  * \return allowed separators
41  */
42  static const char* get_separator()
43  {
44  return ":.-";
45  }
46 
47 
48  /**
49  * Check if given character is an allowed separator.
50  *
51  * \param c character
52  * \return true if separator; else false
53  */
54  static bool is_separator(const int c)
55  {
56  const std::string buffer(JMACAddress::get_separator());
57 
58  for (std::string::const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
59  if ((int) *i == c) {
60  return true;
61  }
62  }
63 
64  return false;
65  }
66 
67 
68  /**
69  * Default constructor.
70  */
72  std::vector<unsigned char>(NUMBER_OF_BYTES, 0)
73  {}
74 
75 
76  /**
77  * Equal method.
78  *
79  * \param address MAC address
80  * \return true if this MAC address and given MAC address are equal; else false
81  */
82  inline bool equal(const JMACAddress& address) const
83  {
84  for (int i = 0; i != NUMBER_OF_BYTES; ++i) {
85  if ((*this)[i] != address[i]) {
86  return false;
87  }
88  }
89 
90  return true;
91  }
92 
93 
94  /**
95  * Read MAC address from input stream.
96  *
97  * \param in input stream
98  * \param object MAC address
99  * \return input stream
100  */
101  friend inline std::istream& operator>>(std::istream& in, JMACAddress& object)
102  {
103  using namespace std;
104 
105  object = JMACAddress();
106 
107  int c;
108 
109  if (in >> hex >> c) {
110 
111  object[0] = (unsigned char) c;
112 
113  for (int i = 1; i != NUMBER_OF_BYTES; ++i) {
114  if (is_separator(in.get()) && in >> hex >> c)
115  object[i] = (unsigned char) c;
116  else
117  in.setstate(ios::failbit);
118  }
119  }
120 
121  return in >> dec;
122  }
123 
124 
125 
126  /**
127  * Write MAC address to output stream.
128  *
129  * \param out output stream
130  * \param object MAC address
131  * \return output stream
132  */
133  friend inline std::ostream& operator<<(std::ostream& out, const JMACAddress& object)
134  {
135  using namespace std;
136 
137  out << setw(2) << setfill('0') << hex << (int) object[0];
138 
139  for (int i = 1; i != NUMBER_OF_BYTES; ++i) {
140  out << setw(1) << JMACAddress::get_separator()[0]
141  << setw(2) << setfill('0') << hex << (int) object[i];
142  }
143 
144  return out << setfill(' ') << dec;
145  }
146 
148  };
149 }
150 
151 #endif
friend std::istream & operator>>(std::istream &in, JMACAddress &object)
Read MAC address from input stream.
Definition: JMACAddress.hh:101
JMACAddress()
Default constructor.
Definition: JMACAddress.hh:71
Template definition of auxiliary base class for comparison of data structures.
Definition: JEquals.hh:24
$WORKDIR ev_configure_dqsimulator txt echo process $DQ_SIMULATOR $i $SOURCE_HOST[$index] csh c(setenv ROOTSYS $ROOTSYS &&source $JPP_DIR/setenv.csh $JPP_DIR &&($DQ_SIMULATOR\-u\$NAME\$\-H\$SERVER\$\-M\$LOGGER\$\-d $DEBUG</dev/null > &/dev/null &))'
then fatal The output file must have the wildcard in the e g root fi eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:48
ClassDefNV(JMACAddress, 1)
static bool is_separator(const int c)
Check if given character is an allowed separator.
Definition: JMACAddress.hh:54
bool equal(const JMACAddress &address) const
Equal method.
Definition: JMACAddress.hh:82
static const char * get_separator()
Get list of allowed separators.
Definition: JMACAddress.hh:42
friend std::ostream & operator<<(std::ostream &out, const JMACAddress &object)
Write MAC address to output stream.
Definition: JMACAddress.hh:133