c3ee34398b5a774aa54f18c9c0dd0329e8da49d3
[senf.git] / senf / Scheduler / ClockService.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief ClockService non-inline non-template implementation */
25
26 #include "ClockService.hh"
27 //#include "ClockService.ih"
28
29 // Custom includes
30 #include <boost/regex.hpp>
31 #include <senf/Utils/Console/Traits.hh>
32
33 //#include "ClockService.mpp"
34 #define prefix_
35 //-/////////////////////////////////////////////////////////////////////////////////////////////////
36
37 prefix_ void
38 senf::parseClockServiceInterval(console::ParseCommandInfo::TokensRange const & tokens,
39                                 ClockService::clock_type & out)
40 {
41     out = 0;
42     std::string value;
43     {
44         senf::console::CheckedArgumentIteratorWrapper arg (tokens);
45         senf::console::parse( *(arg++), value );
46     }
47     static boost::sregex_iterator::regex_type rx ("[mun]?[dhms]");
48     boost::sregex_iterator i (value.begin(), value.end(), rx);
49     boost::sregex_iterator const i_end;
50     std::string::const_iterator j (value.begin());
51     for (; i != i_end; ++i) {
52         boost::sregex_iterator::value_type::value_type match ((*i)[0]);
53         long double v (boost::lexical_cast<long double>(std::string(j, match.first)));
54         char scale (0);
55         char unit (0);
56         if (match.length() == 2) {
57             scale = *match.first;
58             unit = *boost::next(match.first);
59         } else {
60             SENF_ASSERT( match.length() == 1,
61                          "Internal failure: RegEx match returns weird number of matches" );
62             unit = *match.first;
63         }
64         switch (scale) {
65         case 0: break;
66         case 'n': v /= 1000.0;
67         case 'u': v /= 1000.0;
68         case 'm': v /= 1000.0;
69         }
70         switch (unit) {
71         case 'd': v *= 24.0;
72         case 'h': v *= 60.0;
73         case 'm': v *= 60.0;
74         case 's': v *= 1000000000.0;
75         }
76         out += senf::ClockService::nanoseconds(senf::ClockService::int64_type(v));
77         j = match.second;
78     }
79     if (j != value.end())
80         throw senf::console::SyntaxErrorException();
81 }
82
83 prefix_ void senf::formatClockServiceInterval(ClockService::clock_type interval,
84                                               std::ostream & os)
85 {
86     os << interval << "ns";
87 }
88
89 //-/////////////////////////////////////////////////////////////////////////////////////////////////
90 #undef prefix_
91 //#include "ClockService.mpp"
92
93 \f
94 // Local Variables:
95 // mode: c++
96 // fill-column: 100
97 // comment-column: 40
98 // c-file-style: "senf"
99 // indent-tabs-mode: nil
100 // ispell-local-dictionary: "american"
101 // compile-command: "scons -u test"
102 // End: