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

Last change on this file since 7041 was 4890, checked in by Christoph Mayer, 15 years ago

gcc 4 fixes

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 <cstdio>
48#include <iomanip>
49#include <cassert>
50#include <ostream>
51#include <sstream>
52#include <iostream>
53#include <sys/types.h>
54#include <sys/stat.h>
55#include <sys/timeb.h>
56
57#ifdef WIN32
58#define WIN32_LEAN_AND_MEAN
59#include <windows.h>
60#else
61#include <unistd.h>
62#include <stdlib.h>
63#endif
64
65using std::list;
66using std::string;
67using std::setfill;
68using std::setw;
69using std::cout;
70using std::string;
71using std::ostream;
72using std::ostringstream;
73
74namespace ariba {
75namespace utility {
76
77namespace Helper {
78
79 //
80 // string conversion functions
81 //
82
83 /// unsigned long to string
84 string ultos(unsigned long val);
85
86 /// pointer to string-address
87 template<class T>
88 string ptos(T pnt) {
89 std::ostringstream oss;
90 oss << "0x" << std::hex << pnt;
91 return oss.str();
92 }
93
94 /// long to string
95 string ltos(long val);
96
97 /// unsigned long to hex string
98 string ultohexs(unsigned long val, bool hexdelimiter = true);
99
100 /// long to hex string
101 string ltohexs(long val, bool hexdelimiter = true);
102
103 /// string to long
104 long stol(string str);
105
106 /// string to int
107 int stoi(string str);
108
109 /// string to double
110 double stod(string str);
111
112 /// hex string to unsigned int
113 unsigned int hstoui(string str);
114
115 /// double to string
116 string dtos(double val);
117
118 //
119 // string manipulation functions
120 //
121
122 /// trim string
123 string trim(string str);
124
125 /// split a string into substrings. The single strings trimmed from whitespace
126 /// only strings that have a resulting length > 0 after the trim are in the list
127 typedef list<string> STRING_LIST;
128 typedef STRING_LIST::iterator STRING_LIST_ITERATOR;
129 STRING_LIST split(string str, string delimiter);
130
131 /// replace all occurences of find in the string str with repl
132 string replace(string str, string find, string repl);
133
134 //
135 // time functions
136 //
137
138 string getTime(time_t timeval = 0);
139 string getDate(time_t timeval = 0);
140 unsigned long getElapsedMillis();
141 void sleep(unsigned int millis);
142
143 //
144 // constants
145 //
146
147 #ifdef WIN32
148 const string LINE_BREAK = "\r\n";
149 #else
150 const string LINE_BREAK = "\n";
151 #endif
152
153}; // namespace Helper
154
155inline void Helper::sleep(unsigned int millis) {
156#ifdef WIN32
157 Sleep (millis);
158#else
159
160 unsigned long secondsSleep = millis / 1000;
161 unsigned long millisSleep = millis % 1000;
162
163 //
164 // sleep the seconds part of the time
165 // (usleep can not sleep more than 999999 microseconds
166 // which is a little bit too less for a second)
167 //
168
169 if (secondsSleep > 0)
170 sleep(secondsSleep);
171
172 //
173 // sleep the microsecond part of the time
174 //
175
176 if (millisSleep > 0) {
177 assert (millisSleep < 1000);
178 usleep(millisSleep * 1000);
179 }
180#endif
181}
182
183}} // namespace ariba, common
184
185#endif // __HELPER_H
Note: See TracBrowser for help on using the repository browser.