1 /**
2 * Copyright Mikael H�gdahl - triyana@users.sourceforge.net
3 *
4 * This source is distributed under the terms of the Q Public License version 1.0,
5 * created by Trolltech (www.trolltech.com).
6 */
7
8 #include "MHUtil.h"
9 #include "MHDate.h"
10
11
12
13 /**
14 * Create a pointer to char
15 * @param int - Rows
16 * @param int - Size fo row
17 * @return char** - Allocated string, remember to delete later
18 */
Allocate(int rows,int size)19 char** MHUtil::Allocate (int rows, int size) {
20 char** s = new char*[rows];
21
22 for (int f = 0; f < rows; f++) {
23 s[f] = new char[size];
24 s[f][0] = '\0';
25 }
26 return s;
27 }
28
29
30
31 /**
32 * Allocate new buffer
33 * @param char* - Old string
34 * @param int - Size
35 * @return char* - New string
36 */
Allocate(char * old,int & size)37 char* MHUtil::Allocate (char* old, int& size) {
38 char* nw = new char[size * 2];
39
40 strcpy (nw, old);
41 delete []old;
42 size = size * 2;
43 return nw;
44 }
45
46
47
48 /**
49 * Append buffer to another buffer
50 * @param char* - String 1
51 * @param const char* - String 2
52 * @param int - Size of destination buffer
53 * @return char* - Destination string
54 */
AppendString(char * s1,const char * s2,int & size)55 char* MHUtil::AppendString (char* s1, const char* s2, int& size) {
56 if (s1 && s2) {
57 int len1 = STRLEN (s1);
58 int len2 = STRLEN (s2);
59 int tot = len1 + len2;
60
61 if ((tot + 1) >= size) {
62 char* tmp = new char [tot * 2];
63
64 strcpy (tmp, s1);
65 strcat (tmp, s2);
66 delete []s1;
67 s1 = tmp;
68 size = tot * 2;
69 }
70 else {
71 strcat (s1, s2);
72 }
73 }
74 return s1;
75 }
76
77
78
79 /**
80 * Append buffer to another buffer
81 * @param char* - String 1
82 * @param const - String 2
83 * @param int - Size of destination buffer
84 * @return char* - Destination string
85 */
AppendChar(char * s1,char s2,int & size)86 char* MHUtil::AppendChar (char* s1, char s2, int& size) {
87 if (s1) {
88 int len1 = STRLEN (s1);
89
90 if ((len1 + 1) >= size) {
91 char* tmp = new char [len1 * 2];
92
93 strcpy (tmp, s1);
94 tmp[len1] = s2;
95 tmp[len1 + 1] = '\0';
96 delete []s1;
97 s1 = tmp;
98 size = len1 * 2;
99 }
100 else {
101 s1[len1] = s2;
102 s1[len1 + 1] = '\0';;
103 }
104 }
105 return s1;
106 }
107
108
109
110 /**
111 * Get current market price of a bond.
112 * @param double - Interest value
113 * @param double - Interest rate, in percent
114 * @param double - Maturity value of bond
115 * @param int - Years to maturity
116 * @return double - Value
117 */
BondValue(double interest_value,double interest,double maturity,int years_maturity)118 double MHUtil::BondValue (double interest_value, double interest, double maturity, int years_maturity) {
119 double value = 0;
120
121 interest /= 100;
122 for (int f = 1; f <= years_maturity; f++) {
123 value += interest_value / (pow (1.0 + interest, f));
124 }
125 value += maturity / (pow (1.0 + interest, years_maturity));
126 return value;
127 }
128
129
130
131 /**
132 * Return change in percent
133 * @param double - Start number
134 * @param double - Stop number
135 * @return double - Change in percent
136 */
Change(double start,double stop)137 double MHUtil::Change (double start, double stop) {
138 double diff = stop - start;
139
140 if (fabs (start) > 0.0000000001)
141 return (diff / start) * 100.0;
142 else
143 return 0;
144 }
145
146
147
148 /**
149 * Delete a pointer to char
150 * @param char** - String
151 * @param int - Rows
152 */
Delete(char ** s,int rows)153 void MHUtil::Delete (char** s, int rows) {
154 for (int f = 0; f < rows; f++)
155 delete s[f];
156 delete []s;
157 }
158
159
160
161 /**
162 * Set fraction width.
163 * @return double - Number to use for calculations of the fraction size
164 * return int - Fraction size
165 */
GetFractionSize(double d)166 int MHUtil::GetFractionSize (double d) {
167 double dd = d;
168 int fr = 0;
169
170 dd = fabs (dd);
171 dd = dd - int(dd);
172 while (dd <= 1.0000000 && dd > 0.000000) {
173 fr++;
174 dd *= 10;
175 }
176 return fr;
177 }
178
179
180
181 /**
182 * Set integer width
183 * @param double - Number to use as a base
184 * return int - Integer size
185 */
GetIntegerSize(double d)186 int MHUtil::GetIntegerSize (double d) {
187 double dd = fabs (d);
188 int is = 0;
189
190 while (dd >= 1.00000000) {
191 dd /= 10;
192 is++;
193 }
194 if (d < 0)
195 is++;
196 return is;
197 }
198
199
200
201 /**
202 * Return interest.
203 * @param const char* - Start date
204 * @param const char* - Stop date
205 * @param double - Interest / year in percent
206 * @return double - Interest between start and stop date
207 */
Interest(const char * d1,const char * d2,double nInterest)208 double MHUtil::Interest (const char* d1, const char* d2, double nInterest) {
209 double years = MHDate::NoYears (d1, d2);
210
211 nInterest /= 100;
212 nInterest += 1.0;
213 nInterest = pow (nInterest, years);
214 nInterest -= 1.0;
215 return (nInterest * 100);
216 }
217
218
219
220 /**
221 * Return interst.
222 * @param const char* - Start date
223 * @param const char* - Stop date
224 * @param double - Amount
225 * @param double - Interest / year in percent
226 * @return double - Interest between start and stop date
227 */
Interest(const char * d1,const char * d2,double nValue,double nInterest)228 double MHUtil::Interest (const char* d1, const char* d2, double nValue, double nInterest) {
229 double interest = MHUtil::Interest (d1, d2, nInterest);
230 return MHUtil::ChangeVal (nValue, interest);
231 }
232
233
234
235 /**
236 * Create hash from number
237 * @param int - Key to hash
238 * @param int - Hash size
239 * @return int - Hash number
240 */
IntHash(int key,int size)241 int MHUtil::IntHash (int key, int size) {
242 key += ~(key << 15);
243 key ^= (key >> 10);
244 key += (key << 3);
245 key ^= (key >> 6);
246 key += ~(key << 11);
247 key ^= (key >> 16);
248 return (key % size);
249 }
250
251
252
253 /**
254 * Return change in percent
255 * @param double - First number
256 * @param double - Second number
257 * @return double - Percent (second/first) 10, 5 = 200
258 */
PercentOf(double a,double b)259 double MHUtil::PercentOf (double a, double b) {
260 if (fabs (b) > 0.0000000001)
261 return (a / b) * 100;
262 else
263 return 0;
264 }
265
266
267
268 /**
269 * Rounds a number.
270 * @param int - Integer size
271 * @param int - Fraction size
272 * @param double - Number to round
273 * @return double - The rounded number
274 */
Round(int in,int fr,double d)275 double MHUtil::Round (int in, int fr, double d) {
276 int f;
277
278 if (in > 0) {
279 for (f = in; f > 0; f--)
280 d /= 10;
281
282 d = int(d);
283 for (f = in; f > 0; f--)
284 d *= 10;
285 }
286 else if (fr > 0) {
287 for (f = fr; f > 1; f--)
288 d *= 10;
289
290 d = int(d);
291 for (f = fr; f > 1; f--)
292 d /= 10;
293 }
294
295 return d;
296 }
297
298
299
300 /**
301 * Create hash number from key.
302 * @param const char* - Key to hash
303 * @param int - Size of hash
304 * @return int - Hash number
305 */
StringHash(const char * key,int size)306 int MHUtil::StringHash (const char* key, int size) {
307 int i;
308 for (i = 0; *key; key++) i = 131 * i + *key;
309 return abs (i % size);
310 }
311
312
313
314 /**
315 * Get time.
316 * @return double - Number of seconds
317 */
Time()318 double MHUtil::Time () {
319 #ifdef WIN32
320 struct _timeb TimeVal;
321
322 _ftime (&TimeVal);
323 return TimeVal.time + (double (TimeVal.millitm) / 1000.0);
324 #else
325 timeb t;
326
327 ftime (&t);
328 return t.time + (double (t.millitm) / 1000.0);
329 #endif
330 }
331