Files correlati : Ricompilazione Demo : [ ] Commento : currency.* Migliorata gestione valute nulle date.h Migliorati operatori di somma date.cpp Eliminato uso inutile della __tmp_date execp.cpp Corretta chiamata a chiusura files expr.h Trasformato Sandro in Alex expr.cpp Sostituiti vari real(0.0) con ZERO git-svn-id: svn://10.65.10.50/trunk@7199 c028cbd2-c16b-5b4b-a496-9718f37d4682
		
			
				
	
	
		
			234 lines
		
	
	
		
			4.9 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			234 lines
		
	
	
		
			4.9 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");
 | 
						|
 | 
						|
    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::set_currency(const char* val)
 | 
						|
{
 | 
						|
  if (val && *val)
 | 
						|
    strncpy(_val, val, 4);
 | 
						|
  else
 | 
						|
    strcpy(_val, DowJones.get_defval());
 | 
						|
}
 | 
						|
 | 
						|
void TCurrency::change_currency(const char* val)
 | 
						|
{
 | 
						|
  if (_num != ZERO)
 | 
						|
    _num = DowJones.exchange(_num, _val, val);
 | 
						|
  set_currency(val);
 | 
						|
}
 | 
						|
 | 
						|
void TCurrency::read(const TRectype& rec, const char* field, const char* val)
 | 
						|
{
 | 
						|
  _num = rec.get_real(field);
 | 
						|
  if (val == NULL || *val == '\0')
 | 
						|
    val = "_VALUTA";
 | 
						|
  TString16 tmpval;
 | 
						|
  if (rec.exist(val))
 | 
						|
    tmpval = rec.get(val);
 | 
						|
  set_currency(tmpval);
 | 
						|
}
 | 
						|
 | 
						|
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_currency(_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() : _num(0.0)
 | 
						|
{ 
 | 
						|
  set_currency(NULL);
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
TCurrency::TCurrency(const TCurrency& cur)
 | 
						|
         : _num(cur._num)
 | 
						|
{
 | 
						|
  strcpy(_val, cur._val);
 | 
						|
}
 | 
						|
 | 
						|
TCurrency::TCurrency(const real& num, const char* val)
 | 
						|
         : _num(num)
 | 
						|
{
 | 
						|
  set_currency(val);
 | 
						|
}
 | 
						|
 |