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 #ifndef MHUtil_h
9 #define MHUtil_h
10
11 #include <time.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stdarg.h>
15 #include <string.h>
16 #include <math.h>
17 #include <ctype.h>
18
19 #ifdef WIN32
20 #ifdef DM
21 #include <fstream.h>
22 #include <sys/timeb.h>
23 #include <dos.h>
24 #include <io.h>
25
26 #define SLEEP(a) usleep(a * 1000000);
27 #define snprintf _snprintf
28 #define vsnprintf _vsnprintf
29 #define strncasecmp strncmpi
30 #define strcasecmp strcmpi
31 #else
32 #include <sys/time.h>
33 #include <sys/timeb.h>
34 #include <io.h>
35 #define SLEEP(a) _sleep(a * 100);
36 #define unlink _unlink
37 #endif
38 #else
39 #include <unistd.h>
40 #include <sys/time.h>
41 #include <sys/timeb.h>
42
43 #define SLEEP(a) usleep(int(a * 1000000));
44 #endif
45
46 #define ALLOCATE(a,b) char* a = new char[b]; *a = '\0';
47 #define STRNCPY(a,b,c) {strncpy (a, b, c);a[c]='\0';}
48 #define STRLEN(sss) (sss && *sss) ? (strlen(sss)) : 0
49 #define CHECK_STRING(sss) (sss && *sss) ? sss : ""
50
51
52
53 /**
54 * Utility methods. All methods are static.
55 */
56 class MHUtil {
57 public:
58 enum ALIGN {
59 LEFT = 1,
60 RIGHT,
61 CENTER,
62 };
63
64 static char** Allocate (int rows, int size);
65 static char* Allocate (char* old, int& size);
66 static char* AppendString (char* s1, const char* s2, int& len1);
67 static char* AppendChar (char* s1, char s2, int& len1);
68 static double BondValue (double interest_value, double interest, double maturity, int years_maturity);
69 static double Change (double a, double b);
70 static double ChangeVal (double a, double b);
71 static void Delete (char** s, int rows);
72 static bool Equal1 (double d1, double d2);
73 static bool Equal2 (double d1, double d2);
74 static bool Equal3 (double d1, double d2);
75 static bool Equal4 (double d1, double d2);
76 static bool Equal5 (double d1, double d2);
77 static int GetFractionSize (double d);
78 static int GetIntegerSize (double d);
79 static double Interest (const char* d1, const char* d2, double nInterest);
80 static double Interest (const char* d1, const char* d2, double nValue, double nInterest);
81 static int IntHash (int key, int size);
82 static double PercentOf (double a, double b);
83 static double Round (int in, int fr, double d);
84 static void Sleep (double sec);
85 static int StringHash (const char* key, int size);
86 static double Time ();
87 };
88
89
90
91
92 /**
93 * Return change of value
94 * @param double - value
95 * @param double - percent
96 */
ChangeVal(double val,double per)97 inline double MHUtil::ChangeVal (double val, double per) {
98 return ((per / 100) + 1) * val;
99
100 }
101
102
103
104 /**
105 * Compare two doubles with 1 decimal
106 * @param double - First double number
107 * @param double - Second double number
108 * @return bool - true if number is inside 0.1 range
109 */
Equal1(double n1,double n2)110 inline bool MHUtil::Equal1 (double n1, double n2) {
111 return (fabs(n1 - n2) < 0.1) ? true : false;
112 }
113
114
115
116 /**
117 * Compare two doubles with 2 decimals
118 * @param double - First double number
119 * @param double - Second double number
120 * @return bool - true if number is inside 0.01 range
121 */
Equal2(double n1,double n2)122 inline bool MHUtil::Equal2 (double n1, double n2) {
123 return (fabs(n1 - n2) < 0.01) ? true : false;
124 }
125
126
127
128 /**
129 * Compare two doubles with 3 decimals
130 * @param double - First double number
131 * @param double - Second double number
132 * @return bool - true if number is inside 0.001 range
133 */
Equal3(double n1,double n2)134 inline bool MHUtil::Equal3 (double n1, double n2) {
135 return (fabs(n1 - n2) < 0.001) ? true : false;
136 }
137
138
139
140 /**
141 * Compare two doubles with 4 decimals
142 * @param double - First double number
143 * @param double - Second double number
144 * @return bool - true if number is inside 0.01 range
145 */
Equal4(double n1,double n2)146 inline bool MHUtil::Equal4 (double n1, double n2) {
147 return (fabs(n1 - n2) < 0.0001) ? true : false;
148 }
149
150
151
152 /**
153 * Compare two doubles with 5 decimals
154 * @param double - First double number
155 * @param double - Second double number
156 * @return bool - true if number is inside 0.00001 range
157 */
Equal5(double n1,double n2)158 inline bool MHUtil::Equal5 (double n1, double n2) {
159 return (fabs(n1 - n2) < 0.00001) ? true : false;
160 }
161
162
163
164 /**
165 * Sleep
166 * @param double - Number of seconds
167 */
Sleep(double sec)168 inline void MHUtil::Sleep (double sec) {
169 SLEEP((unsigned long)sec);
170 }
171
172 #endif
173
174