include/trace-tools/wifi/timestamp.hxx

00001 /*
00002  * trace-tools - A library and a set of tools to manipulate wireless traces.
00003  * Copyright (C) 2007  Universite Pierre et Marie Curie - Paris 6
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00018  * MA  02110-1301  USA
00019  *
00020  * Author: Thomas Claveirole <thomas.claveirole@lip6.fr>
00021  */
00022 #ifndef WIFI_TIMESTAMP_HXX_
00023 # define WIFI_TIMESTAMP_HXX_
00024 
00025 # include <algorithm>
00026 # include <stack>
00027 
00028 # include <boost/array.hpp>
00029 
00030 # include "timestamp.hh"
00031 
00032 namespace wifi
00033 {
00034 
00035   inline
00036   timestamp::timestamp()
00037   {
00038 #ifndef NDEBUG
00039     std::fill(digits_.begin(), digits_.end(), 0);
00040 #endif // ! NDEBUG
00041   }
00042 
00043   inline
00044   timestamp::timestamp(uint32_t hi, uint32_t lo)
00045   {
00046     digits_[0] = (lo & 0x000000FF) >> 0;
00047     digits_[1] = (lo & 0x0000FF00) >> 8;
00048     digits_[2] = (lo & 0x00FF0000) >> 16;
00049     digits_[3] = (lo & 0xFF000000) >> 24;
00050     digits_[4] = (hi & 0x000000FF) >> 0;
00051     digits_[5] = (hi & 0x0000FF00) >> 8;
00052     digits_[6] = (hi & 0x00FF0000) >> 16;
00053     digits_[7] = (hi & 0xFF000000) >> 24;
00054   }
00055 
00056   inline
00057   bool
00058   timestamp::operator == (const timestamp& rhs) const
00059   {
00060     return digits_ == rhs.digits_;
00061   }
00062 
00063   inline
00064   bool
00065   timestamp::operator != (const timestamp& rhs) const
00066   {
00067     return not (*this == rhs);
00068   }
00069 
00070   inline
00071   bool
00072   timestamp::operator < (const timestamp& rhs) const
00073   {
00074     return
00075       std::lexicographical_compare(digits_.rbegin(), digits_.rend(),
00076                                    rhs.digits_.rbegin(), rhs.digits_.rend());
00077   }
00078 
00079   inline
00080   std::ostream&
00081   timestamp::print(std::ostream& os) const
00082   {
00083     // FIXME: We could optimize here using uint64_t or performing
00084     //        a 32-bit-digits division.  However we do not want to
00085     //        bloat the code with endiannes issues.
00086 
00087     digits_t            tmp;
00088     std::stack<char>    out;
00089 
00090     std::copy(digits_.begin(), digits_.end(), tmp.rbegin());
00091     while (true)
00092       {
00093         digits_t::iterator      i = tmp.begin();
00094         unsigned                carry = 0;
00095 
00096         // Skip leading 0's.
00097         while (i != tmp.end() and not *i)
00098           ++i;
00099 
00100         // Exit loop if tmp = 0.
00101         if (i == tmp.end())
00102           break;
00103 
00104         // Divide by 10.
00105         while (i != tmp.end())
00106           {
00107             carry = (carry << (sizeof (*i) * 8)) + *i;
00108             *i = carry / 10;
00109             carry = carry % 10;
00110             ++i;
00111           }
00112         out.push(carry + '0');
00113       }
00114 
00115     if (out.empty())
00116       return os << "0";
00117     while (not out.empty())
00118       {
00119         os << out.top();
00120         out.pop();
00121       }
00122     return os;
00123   }
00124 
00125   inline
00126   std::ostream&
00127   operator << (std::ostream& os, const timestamp& ts)
00128   {
00129     return ts.print(os);
00130   }
00131 
00132 } // End of namespace wifi.
00133 
00134 #endif // ! WIFI_TIMESTAMP_HXX_

Generated on Wed Sep 12 16:02:47 2007 for trace-tools by  doxygen 1.5.3