231ae5c1162871e5f2c553ba9f9bbb256db2d815
[public/netxms.git] / src / server / core / ccy.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2016 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** File: ccy.cpp
20 **
21 **/
22
23 #include "nxcore.h"
24 #include <math.h>
25
26 /**
27 * Country information
28 */
29 struct COUNTRY_INFO
30 {
31 TCHAR numericCode[4];
32 TCHAR alphaCode[3];
33 TCHAR alpha3Code[4];
34 TCHAR *name;
35 };
36
37 /**
38 * Currency information
39 */
40 struct CURRENCY_INFO
41 {
42 TCHAR numericCode[4];
43 TCHAR alphaCode[4];
44 TCHAR *description;
45 int exponent;
46 double divisor;
47 };
48
49 /**
50 * Country list
51 */
52 static int s_countryListSize = 0;
53 static COUNTRY_INFO *s_countryList = NULL;
54
55 /**
56 * Currency list
57 */
58 static int s_currencyListSize = 0;
59 static CURRENCY_INFO *s_currencyList = NULL;
60
61 /**
62 * Initialize country list
63 */
64 void InitCountryList()
65 {
66 DB_HANDLE hdb = DBConnectionPoolAcquireConnection();
67 DB_RESULT hResult = DBSelect(hdb, _T("SELECT numeric_code,alpha_code,alpha3_code,name FROM country_codes"));
68 if (hResult != NULL)
69 {
70 s_countryListSize = DBGetNumRows(hResult);
71 if (s_countryListSize > 0)
72 {
73 s_countryList = (COUNTRY_INFO *)malloc(sizeof(COUNTRY_INFO) * s_countryListSize);
74 for(int i = 0; i < s_countryListSize; i++)
75 {
76 DBGetField(hResult, i, 0, s_countryList[i].numericCode, 4);
77 DBGetField(hResult, i, 1, s_countryList[i].alphaCode, 3);
78 DBGetField(hResult, i, 2, s_countryList[i].alpha3Code, 4);
79 s_countryList[i].name = DBGetField(hResult, i, 3, NULL, 0);
80 }
81 }
82 DBFreeResult(hResult);
83 DbgPrintf(4, _T("%d country codes loaded"), s_countryListSize);
84 }
85 DBConnectionPoolReleaseConnection(hdb);
86 }
87
88 /**
89 * Get country alpha code from numeric code
90 */
91 const TCHAR NXCORE_EXPORTABLE *CountryAlphaCode(const TCHAR *code)
92 {
93 for(int i = 0; i < s_countryListSize; i++)
94 {
95 if (!_tcscmp(s_countryList[i].numericCode, code) || !_tcsicmp(s_countryList[i].alpha3Code, code))
96 return s_countryList[i].alphaCode;
97 }
98 return NULL;
99 }
100
101 /**
102 * Get country name from code
103 */
104 const TCHAR NXCORE_EXPORTABLE *CountryName(const TCHAR *code)
105 {
106 for(int i = 0; i < s_countryListSize; i++)
107 {
108 if (!_tcscmp(s_countryList[i].numericCode, code) || !_tcsicmp(s_countryList[i].alphaCode, code) || !_tcsicmp(s_countryList[i].alpha3Code, code))
109 return s_countryList[i].name;
110 }
111 return NULL;
112 }
113
114 /**
115 * Initialize currency list
116 */
117 void InitCurrencyList()
118 {
119 DB_HANDLE hdb = DBConnectionPoolAcquireConnection();
120 DB_RESULT hResult = DBSelect(hdb, _T("SELECT numeric_code,alpha_code,description,exponent FROM currency_codes"));
121 if (hResult != NULL)
122 {
123 s_currencyListSize = DBGetNumRows(hResult);
124 if (s_currencyListSize > 0)
125 {
126 s_currencyList = (CURRENCY_INFO *)malloc(sizeof(CURRENCY_INFO) * s_currencyListSize);
127 for(int i = 0; i < s_currencyListSize; i++)
128 {
129 DBGetField(hResult, i, 0, s_currencyList[i].numericCode, 4);
130 DBGetField(hResult, i, 1, s_currencyList[i].alphaCode, 4);
131 s_currencyList[i].description = DBGetField(hResult, i, 2, NULL, 0);
132 s_currencyList[i].exponent = DBGetFieldLong(hResult, i, 3);
133 s_currencyList[i].divisor = pow(10.0, s_currencyList[i].exponent);
134 }
135 }
136 DBFreeResult(hResult);
137 DbgPrintf(4, _T("%d currency codes loaded"), s_currencyListSize);
138 }
139 DBConnectionPoolReleaseConnection(hdb);
140 }
141
142 /**
143 * Resolve currency code
144 */
145 const TCHAR NXCORE_EXPORTABLE *CurrencyAlphaCode(const TCHAR *numericCode)
146 {
147 for(int i = 0; i < s_currencyListSize; i++)
148 {
149 if (!_tcscmp(s_currencyList[i].numericCode, numericCode))
150 return s_currencyList[i].alphaCode;
151 }
152 return NULL;
153 }
154
155 /**
156 * Get currency exponent
157 */
158 int NXCORE_EXPORTABLE CurrencyExponent(const TCHAR *code)
159 {
160 for(int i = 0; i < s_currencyListSize; i++)
161 {
162 if (!_tcsicmp(s_currencyList[i].alphaCode, code) || !_tcscmp(s_currencyList[i].numericCode, code))
163 return s_currencyList[i].exponent;
164 }
165 return NULL;
166 }
167
168 /**
169 * Get currency name
170 */
171 const TCHAR NXCORE_EXPORTABLE *CurrencyName(const TCHAR *code)
172 {
173 for(int i = 0; i < s_currencyListSize; i++)
174 {
175 if (!_tcsicmp(s_currencyList[i].alphaCode, code) || !_tcscmp(s_currencyList[i].numericCode, code))
176 return s_currencyList[i].description;
177 }
178 return NULL;
179 }