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

Last change on this file since 3712 was 3690, checked in by mies, 15 years ago

Merged 20090512-mies-connectors changes r3472:r3689 into trunk.

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