fcd8042f1c91a6af6ec5b5faf7b7f0f4fc9fa5dd
[senf.git] / senf / Scheduler / ClockService.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief ClockService non-inline non-template implementation */
30
31 #include "ClockService.hh"
32 //#include "ClockService.ih"
33
34 // Custom includes
35 #include <boost/regex.hpp>
36 #include <senf/Utils/Console/Traits.hh>
37
38 //#include "ClockService.mpp"
39 #define prefix_
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41
42 prefix_ void
43 senf::parseClockServiceInterval(console::ParseCommandInfo::TokensRange const & tokens,
44                                 ClockService::clock_type & out)
45 {
46     out = 0;
47     std::string value;
48     {
49         console::CheckedArgumentIteratorWrapper arg (tokens);
50         console::parse( *(arg++), value );
51     }
52     static boost::sregex_iterator::regex_type rx ("[mun]?[dhms]");
53     boost::sregex_iterator i (value.begin(), value.end(), rx);
54     boost::sregex_iterator const i_end;
55     std::string::const_iterator j (value.begin());
56     for (; i != i_end; ++i) {
57         boost::sregex_iterator::value_type::value_type match ((*i)[0]);
58         long double v (boost::lexical_cast<long double>(std::string(j, match.first)));
59         char scale (0);
60         char unit (0);
61         if (match.length() == 2) {
62             scale = *match.first;
63             unit = *boost::next(match.first);
64         } else {
65             SENF_ASSERT( match.length() == 1,
66                          "Internal failure: RegEx match returns weird number of matches" );
67             unit = *match.first;
68         }
69         switch (scale) {
70         case 0: break;
71         case 'n': v /= 1000.0;
72         case 'u': v /= 1000.0;
73         case 'm': v /= 1000.0;
74         }
75         switch (unit) {
76         case 'd': v *= 24.0;
77         case 'h': v *= 60.0;
78         case 'm': v *= 60.0;
79         case 's': v *= 1000000000.0;
80         }
81         out += ClockService::nanoseconds(ClockService::int64_type(v));
82         j = match.second;
83     }
84     if (j != value.end())
85         throw console::SyntaxErrorException();
86 }
87
88 prefix_ void senf::formatClockServiceInterval(ClockService::clock_type interval,
89                                               std::ostream & os)
90 {
91     os << interval << "ns";
92 }
93
94 //-/////////////////////////////////////////////////////////////////////////////////////////////////
95 #undef prefix_
96 //#include "ClockService.mpp"
97
98 \f
99 // Local Variables:
100 // mode: c++
101 // fill-column: 100
102 // comment-column: 40
103 // c-file-style: "senf"
104 // indent-tabs-mode: nil
105 // ispell-local-dictionary: "american"
106 // compile-command: "scons -u test"
107 // End: