campo-sirio/include/currency.cpp
guy d5db644881 currency.cpp Correzioni sulla gestione valuta
isam.cpp        Evita di chiudere files se il prefix e' gia' stato chiuso
mask.cpp        Riconoscimento campi CURRENCY
maskfld.*       Aggiunti campi CURRENCY
prefix.*        Aggiunta funzione prefix_valid() per sapere se il prefix e' stato inizializzato


git-svn-id: svn://10.65.10.50/trunk@6653 c028cbd2-c16b-5b4b-a496-9718f37d4682
1998-05-13 15:07:41 +00:00

222 lines
4.8 KiB
C++
Executable File

#include <currency.h>
#include <recarray.h>
///////////////////////////////////////////////////////////
// DowJones
///////////////////////////////////////////////////////////
class TDowJones : public TFile_cache
{
struct TExchangeData : public TObject
{
real _num;
real _den;
int _dec;
public:
const TExchangeData& operator=(const TExchangeData& d)
{ _num = d._num; _den = d._den; _dec = d._dec; return d; }
TExchangeData() : _num(1.0), _den(1.0), _dec(0) { }
};
char _defval[4];
protected:
virtual TObject* rec2obj(const TRectype& rec) const;
void test_cache();
const TExchangeData& get(const char* key);
public:
const char* get_defval();
real exchange(const real& num, const char* fromval, const char* toval);
int get_dec(const char* val);
TDowJones() : TFile_cache("%VAL") { }
virtual ~TDowJones() { }
} DowJones;
TObject* TDowJones::rec2obj(const TRectype& rec) const
{
TExchangeData* data = new TExchangeData;
data->_den = rec.get_real("I11");
if (data->_den > ZERO)
{
data->_num = rec.get_real("R11");
if (data->_num < 1.0)
data->_num = 1.0;
data->_dec = rec.get_int("I0");
}
else
{
data->_num = rec.get_real("I10");
if (data->_num < 1.0)
data->_num = 1.0;
data->_den = rec.get_real("R10");
data->_num = 1.0;
const TString& codval = rec.get("CODTAB");
data->_dec = codval.empty() || codval == "LIT" ? 0 : 2;
}
if (data->_den <= ZERO)
{
NFCHECK("Cambio nullo: %s = %s",
(const char*)rec.get("CODTAB"),
data->_den.string());
data->_den = 1.0;
data->_num = 1.0;
data->_dec = 0;
}
return data;
}
void TDowJones::test_cache()
{
if (_cache.items() == 0)
{
fill();
*_defval = '\0';
FOR_EACH_ASSOC_OBJECT(_cache, hash, key, obj)
{
const TExchangeData* data = (const TExchangeData*)obj;
if (data->_num == 1.0 && data->_den == 1.0)
{
strcpy(_defval, key);
break;
}
}
if (*_defval == '\0')
{
strcpy(_defval, "LIT");
_cache.add(_defval, new TExchangeData);
}
}
}
const TDowJones::TExchangeData& TDowJones::get(const char* val)
{
test_cache();
if (val == NULL || *val == '\0')
val = _defval;
return (const TExchangeData&)query(val);
}
const char* TDowJones::get_defval()
{
test_cache();
return _defval;
}
real TDowJones::exchange(const real& num, // Importo da convertire
const char* fromval, // Dalla valuta
const char* toval) // Alla valuta
{
real n = num;
if (n != ZERO)
{
const TExchangeData& datafrom = get(fromval);
const TExchangeData& datato = get(toval);
n *= datafrom._den * datato._num;
n /= datafrom._num * datato._den;
n.round(datato._dec);
}
return n;
}
int TDowJones::get_dec(const char* val)
{
const TExchangeData& data = get(val);
return data._dec;
}
///////////////////////////////////////////////////////////
// TCurrency
///////////////////////////////////////////////////////////
void TCurrency::change_value(const char* val)
{
if (_num != ZERO)
_num = DowJones.exchange(_num, _val, val);
strncpy(_val, val, 4);
}
void TCurrency::read(const TRectype& rec, const char* field, const char* val)
{
_num = rec.get_real(field);
if (val == NULL || *val == '\0')
val = "_VALUTA";
if (rec.exist(val))
strcpy(_val, rec.get(val));
else
*_val = '\0';
if (*_val == '\0')
strcpy(_val, DowJones.get_defval());
}
void TCurrency::write(TRectype& rec, const char* field, const char *val, bool forceval) const
{
if (val == NULL || *val == '\0')
val = "_VALUTA";
if (forceval || rec.exist(val))
{
const TString& recval = rec.get(val);
if (recval != _val)
{
if (forceval || recval.empty())
rec.put(val, _val);
else
NFCHECK("Scrittura della valuta %s su di un record con valuta %s",
_val, (const char*)recval);
}
}
rec.put(field, _num);
}
int TCurrency::compare(const TSortable& s) const
{
const TCurrency& cur = (const TCurrency&)s;
if (stricmp(cur._val, _val) == 0)
{
return _num == cur._num ? 0 : (_num > cur._num ? +1 : -1);
}
TCurrency curr(cur);
curr.change_value(_val);
return _num == curr._num ? 0 : (_num > curr._num ? +1 : -1);
}
const TCurrency& TCurrency::operator=(const TCurrency& cur)
{
_num = cur._num;
strcpy(_val, cur._val);
return cur;
}
const char* TCurrency::string(bool dotted) const
{
TString16 picture;
if (dotted)
{
picture << '.';
const int dec = DowJones.get_dec(_val);
if (dec > 0)
picture << dec;
}
return _num.string(picture);
}
TCurrency::TCurrency(const TCurrency& cur)
: _num(cur._num)
{
strcpy(_val, cur._val);
}
TCurrency::TCurrency(const real& num, const char* val)
: _num(num)
{
strncpy(_val, val, 4);
}