2000-04-12 00:41:58 -05:00
|
|
|
/*******************************************************************\
|
|
|
|
* EuroUtils.c -- utilities for EURO currency *
|
|
|
|
* *
|
|
|
|
* Copyright (C) 2000 Herbert Thoma *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License as *
|
|
|
|
* published by the Free Software Foundation; either version 2 of *
|
|
|
|
* the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License*
|
|
|
|
* along with this program; if not, write to the Free Software *
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
|
|
|
|
* *
|
|
|
|
\********************************************************************/
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2000-10-23 04:41:51 -05:00
|
|
|
#include "gnc-commodity.h"
|
2000-04-12 00:41:58 -05:00
|
|
|
|
2000-09-13 17:33:15 -05:00
|
|
|
#include "EuroUtils.h"
|
|
|
|
|
2000-04-12 00:41:58 -05:00
|
|
|
/* local structs */
|
2000-10-23 04:41:51 -05:00
|
|
|
typedef struct _gnc_euro_rate_struct {
|
2000-04-12 00:41:58 -05:00
|
|
|
const char *currency;
|
|
|
|
double rate;
|
2000-10-23 04:41:51 -05:00
|
|
|
} gnc_euro_rate_struct;
|
|
|
|
|
2000-04-12 00:41:58 -05:00
|
|
|
|
|
|
|
/* This array MUST be sorted ! */
|
|
|
|
static gnc_euro_rate_struct _gnc_euro_rate_[] =
|
|
|
|
{
|
2000-04-12 16:53:07 -05:00
|
|
|
{ "ATS", 13.7603 }, /* austrian schilling */
|
|
|
|
{ "BEF", 40.3399 }, /* belgian franc */
|
2000-04-12 00:41:58 -05:00
|
|
|
{ "BFR", 40.3399 }, /* belgian franc */
|
|
|
|
{ "DEM", 1.95583 }, /* german mark */
|
|
|
|
{ "DM", 1.95583 }, /* german mark */
|
|
|
|
{ "ESC", 200.482 }, /* portugese escudo */
|
2000-04-12 16:53:07 -05:00
|
|
|
{ "ESP", 166.386 }, /* spanish peseta */
|
2000-04-12 00:41:58 -05:00
|
|
|
{ "EUR", 1.00000 }, /* euro */
|
|
|
|
{ "EURO", 1.00000 }, /* euro */
|
|
|
|
{ "FF", 6.55957 }, /* french franc */
|
2000-04-12 16:53:07 -05:00
|
|
|
{ "FIM", 5.94573 }, /* finnmark */
|
2000-04-12 00:41:58 -05:00
|
|
|
{ "FMK", 5.94573 }, /* finnmark */
|
2000-04-12 16:53:07 -05:00
|
|
|
{ "FRF", 6.55957 }, /* french franc */
|
2000-04-12 00:41:58 -05:00
|
|
|
{ "HFL", 2.20371 }, /* netherland gulden */
|
2000-04-12 16:53:07 -05:00
|
|
|
{ "IEP", .787564 }, /* irish pound */
|
2000-04-12 00:41:58 -05:00
|
|
|
{ "IRP", .787564 }, /* irish pound */
|
2000-04-12 16:53:07 -05:00
|
|
|
{ "ITL", 1936.27 }, /* italian lira */
|
2000-04-12 00:41:58 -05:00
|
|
|
{ "LFR", 40.3399 }, /* luxembourg franc */
|
|
|
|
{ "LIT", 1936.27 }, /* italian lira */
|
2000-04-12 16:53:07 -05:00
|
|
|
{ "LUF", 40.3399 }, /* luxembourg franc */
|
|
|
|
{ "NLG", 2.20371 }, /* netherland gulden */
|
2000-04-12 00:41:58 -05:00
|
|
|
{ "PTA", 166.386 }, /* spanish peseta */
|
2000-04-12 16:53:07 -05:00
|
|
|
{ "PTE", 200.482 }, /* portugese escudo */
|
|
|
|
{ "S", 13.7603 }, /* austrian schilling */
|
|
|
|
{ "SCH", 13.7603 } /* austrian schilling */
|
2000-04-12 00:41:58 -05:00
|
|
|
};
|
|
|
|
|
2000-10-23 04:41:51 -05:00
|
|
|
static int
|
|
|
|
_gnc_euro_rate_compare_(const void * key,
|
|
|
|
const void * value)
|
2000-04-12 00:41:58 -05:00
|
|
|
{
|
2000-10-23 04:41:51 -05:00
|
|
|
const gnc_commodity * curr = key;
|
|
|
|
const gnc_euro_rate_struct * euro = value;
|
|
|
|
if(!key || !value) return -1;
|
|
|
|
|
|
|
|
return strcasecmp(gnc_commodity_get_mnemonic(curr), euro->currency);
|
|
|
|
|
2000-04-12 00:41:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------ */
|
|
|
|
|
2000-10-27 15:30:30 -05:00
|
|
|
gboolean
|
2000-10-23 04:41:51 -05:00
|
|
|
gnc_is_euro_currency(const gnc_commodity * currency) {
|
2000-04-12 00:41:58 -05:00
|
|
|
|
2000-10-23 04:41:51 -05:00
|
|
|
gnc_euro_rate_struct *result;
|
2000-10-27 15:30:30 -05:00
|
|
|
const char *namespace;
|
|
|
|
|
|
|
|
if (currency == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
namespace = gnc_commodity_get_namespace (currency);
|
|
|
|
if (namespace == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (strcmp (namespace, GNC_COMMODITY_NS_ISO) != 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
result = bsearch(currency,
|
|
|
|
_gnc_euro_rate_,
|
|
|
|
sizeof(_gnc_euro_rate_) / sizeof(gnc_euro_rate_struct),
|
|
|
|
sizeof(gnc_euro_rate_struct),
|
|
|
|
_gnc_euro_rate_compare_);
|
|
|
|
|
|
|
|
if (result == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
2000-04-12 00:41:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------ */
|
|
|
|
|
2000-09-13 17:33:15 -05:00
|
|
|
double
|
2000-10-23 04:41:51 -05:00
|
|
|
gnc_convert_to_euro(const gnc_commodity * currency, double value) {
|
2000-10-27 15:30:30 -05:00
|
|
|
|
2000-04-12 00:41:58 -05:00
|
|
|
gnc_euro_rate_struct *result;
|
2000-10-27 15:30:30 -05:00
|
|
|
const char *namespace;
|
2000-04-12 00:41:58 -05:00
|
|
|
|
2000-10-27 15:30:30 -05:00
|
|
|
if (currency == NULL)
|
2000-05-01 22:23:23 -05:00
|
|
|
return 0.0;
|
2000-10-27 15:30:30 -05:00
|
|
|
|
|
|
|
namespace = gnc_commodity_get_namespace (currency);
|
|
|
|
if (namespace == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (strcmp (namespace, GNC_COMMODITY_NS_ISO) != 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
result = bsearch(currency,
|
|
|
|
_gnc_euro_rate_,
|
|
|
|
sizeof(_gnc_euro_rate_) / sizeof(gnc_euro_rate_struct),
|
|
|
|
sizeof(gnc_euro_rate_struct),
|
|
|
|
_gnc_euro_rate_compare_);
|
|
|
|
|
|
|
|
if (result == NULL)
|
|
|
|
return 0.0;
|
|
|
|
|
|
|
|
/* round to 2 decimal places */
|
|
|
|
return (floor(((value / result->rate) * 100.0) + 0.5) / 100.0);
|
2000-04-12 00:41:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------ */
|
|
|
|
|
2000-10-23 04:41:51 -05:00
|
|
|
double
|
|
|
|
gnc_convert_from_euro(const gnc_commodity * currency, double value) {
|
2000-10-27 15:30:30 -05:00
|
|
|
|
2000-10-23 04:41:51 -05:00
|
|
|
gnc_euro_rate_struct * result;
|
2000-10-27 15:30:30 -05:00
|
|
|
const char *namespace;
|
|
|
|
|
|
|
|
if (currency == NULL)
|
|
|
|
return 0.0;
|
|
|
|
|
|
|
|
namespace = gnc_commodity_get_namespace (currency);
|
|
|
|
if (namespace == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (strcmp (namespace, GNC_COMMODITY_NS_ISO) != 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
result = bsearch(currency,
|
|
|
|
_gnc_euro_rate_,
|
|
|
|
sizeof(_gnc_euro_rate_) / sizeof(gnc_euro_rate_struct),
|
|
|
|
sizeof(gnc_euro_rate_struct),
|
|
|
|
_gnc_euro_rate_compare_);
|
|
|
|
|
|
|
|
if (result == NULL)
|
2000-05-01 22:23:23 -05:00
|
|
|
return 0.0;
|
2000-10-27 15:30:30 -05:00
|
|
|
|
|
|
|
return (value * result->rate);
|
2000-04-12 00:41:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/************************** END OF FILE *************************/
|