source: source/ariba/utility/misc/Helper.h@ 4853

Last change on this file since 4853 was 4063, checked in by Christoph Mayer, 15 years ago
File size: 4.6 KB
Line 
1// [License]
2// The Ariba-Underlay Copyright
3//
4// Copyright (c) 2008-2009, Institute of Telematics, UniversitÀt Karlsruhe (TH)
5//
6// Institute of Telematics
7// UniversitÀt Karlsruhe (TH)
8// Zirkel 2, 76128 Karlsruhe
9// Germany
10//
11// Redistribution and use in source and binary forms, with or without
12// modification, are permitted provided that the following conditions are
13// met:
14//
15// 1. Redistributions of source code must retain the above copyright
16// notice, this list of conditions and the following disclaimer.
17// 2. Redistributions in binary form must reproduce the above copyright
18// notice, this list of conditions and the following disclaimer in the
19// documentation and/or other materials provided with the distribution.
20//
21// THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND
22// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ARIBA PROJECT OR
25// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32//
33// The views and conclusions contained in the software and documentation
34// are those of the authors and should not be interpreted as representing
35// official policies, either expressed or implied, of the Institute of
36// Telematics.
37// [License]
38
39#ifndef __HELPER_H
40#define __HELPER_H
41
42#include <string>
43#include <list>
44#include <cassert>
45#include <ctime>
46#include <ostream>
47#include <iomanip>
48#include <cassert>
49#include <ostream>
50#include <sstream>
51#include <iostream>
52#include <sys/types.h>
53#include <sys/stat.h>
54#include <sys/timeb.h>
55
56#ifdef WIN32
57#define WIN32_LEAN_AND_MEAN
58#include <windows.h>
59#else
60#include <unistd.h>
61#include <stdlib.h>
62#endif
63
64using std::list;
65using std::string;
66using std::setfill;
67using std::setw;
68using std::cout;
69using std::string;
70using std::ostream;
71using std::ostringstream;
72
73namespace ariba {
74namespace utility {
75
76namespace Helper {
77
78 //
79 // string conversion functions
80 //
81
82 /// unsigned long to string
83 string ultos(unsigned long val);
84
85 /// pointer to string-address
86 template<class T>
87 string ptos(T pnt) {
88 std::ostringstream oss;
89 oss << "0x" << std::hex << pnt;
90 return oss.str();
91 }
92
93 /// long to string
94 string ltos(long val);
95
96 /// unsigned long to hex string
97 string ultohexs(unsigned long val, bool hexdelimiter = true);
98
99 /// long to hex string
100 string ltohexs(long val, bool hexdelimiter = true);
101
102 /// string to long
103 long stol(string str);
104
105 /// string to int
106 int stoi(string str);
107
108 /// string to double
109 double stod(string str);
110
111 /// hex string to unsigned int
112 unsigned int hstoui(string str);
113
114 /// double to string
115 string dtos(double val);
116
117 //
118 // string manipulation functions
119 //
120
121 /// trim string
122 string trim(string str);
123
124 /// split a string into substrings. The single strings trimmed from whitespace
125 /// only strings that have a resulting length > 0 after the trim are in the list
126 typedef list<string> STRING_LIST;
127 typedef STRING_LIST::iterator STRING_LIST_ITERATOR;
128 STRING_LIST split(string str, string delimiter);
129
130 /// replace all occurences of find in the string str with repl
131 string replace(string str, string find, string repl);
132
133 //
134 // time functions
135 //
136
137 string getTime(time_t timeval = 0);
138 string getDate(time_t timeval = 0);
139 unsigned long getElapsedMillis();
140 void sleep(unsigned int millis);
141
142 //
143 // constants
144 //
145
146 #ifdef WIN32
147 const string LINE_BREAK = "\r\n";
148 #else
149 const string LINE_BREAK = "\n";
150 #endif
151
152}; // namespace Helper
153
154inline void Helper::sleep(unsigned int millis) {
155#ifdef WIN32
156 Sleep (millis);
157#else
158
159 unsigned long secondsSleep = millis / 1000;
160 unsigned long millisSleep = millis % 1000;
161
162 //
163 // sleep the seconds part of the time
164 // (usleep can not sleep more than 999999 microseconds
165 // which is a little bit too less for a second)
166 //
167
168 if (secondsSleep > 0)
169 sleep(secondsSleep);
170
171 //
172 // sleep the microsecond part of the time
173 //
174
175 if (millisSleep > 0) {
176 assert (millisSleep < 1000);
177 usleep(millisSleep * 1000);
178 }
179#endif
180}
181
182}} // namespace ariba, common
183
184#endif // __HELPER_H
Note: See TracBrowser for help on using the repository browser.