home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / graphtal / value.c < prev    next >
C/C++ Source or Header  |  1992-10-28  |  7KB  |  319 lines

  1. /*
  2.  * Value.C - methods for class Value,  which stores reals and strings. 
  3.  *
  4.  * Copyright (C) 1992, Christoph Streit (streit@iam.unibe.ch)
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include "Value.h"
  21. #include "Error.h"
  22.  
  23. Value::Value() 
  24. : type(REAL)
  25. {
  26.   realVal = 0.0;
  27. }
  28.       
  29. Value::Value(real val)
  30. : type(REAL)
  31. {
  32.   realVal = val;
  33. }
  34.  
  35. Value::Value(const char* str)
  36. : type(STRING)
  37. {
  38.   stringVal = new rcString(str);
  39. }
  40.       
  41. Value::Value(const rcString& s)
  42. : type(STRING)
  43. {
  44.   stringVal = new rcString(s);
  45. }
  46.  
  47. Value::Value(const Value& v)
  48. : type(v.type)
  49. {
  50.   if (v.type == REAL)
  51.     realVal = v.realVal;
  52.   else
  53.     stringVal = new rcString(*v.stringVal);
  54. }
  55.  
  56. Value::~Value()
  57. {
  58.   if (type == STRING)
  59.     delete stringVal;
  60. }
  61.  
  62. const Value& Value::operator=(const Value& v)
  63. {
  64.   if (this == &v)
  65.     return *this;
  66.  
  67.   if (type == STRING)
  68.     delete stringVal;
  69.  
  70.   type = v.type;
  71.   if (v.type == REAL)
  72.     realVal = v.realVal;
  73.   else
  74.     stringVal = new rcString(*v.stringVal);
  75.  
  76.   return *this;
  77. }
  78.  
  79. Value::operator real() const
  80. {
  81.   if (type == REAL)
  82.     return realVal;
  83.  
  84.   double retval;
  85.   char *ptr;
  86.  
  87.   retval = strtod(stringVal->chars(), &ptr);
  88.   if (*ptr == '\0')
  89.     return retval;
  90.   else 
  91.     Error(ERR_PANIC, "Value::operator real(): Can't convert rcString '" +
  92.                  *stringVal + "' to real");
  93.   return 0;
  94. }
  95.  
  96.  
  97. Value::operator rcString() const
  98. {
  99.   if (type == STRING)
  100.     return *stringVal;
  101.  
  102.   char ptr[100];
  103.   sprintf(ptr, "%g", realVal);
  104.   return rcString(ptr);
  105. }
  106.  
  107. int Value::toReal(real& x) const
  108. {
  109.   if (type == REAL) {
  110.     x = realVal;
  111.     return 1;
  112.   }
  113.  
  114.   double retval;
  115.   char *ptr;
  116.  
  117.   retval = strtod(stringVal->chars(), &ptr);
  118.   if (*ptr == '\0') {
  119.     x = retval;
  120.     return 1;
  121.   }
  122.   else
  123.     return 0;
  124. }
  125.  
  126. rcString Value::toString() const
  127. {
  128.   if (type == STRING)
  129.     return *stringVal;
  130.  
  131.   char ptr[100];
  132.   sprintf(ptr, "%g", realVal);
  133.   return rcString(ptr);
  134. }
  135.  
  136. ArgumentType Value::binary_type(const Value& v)
  137. {
  138.   switch(type) {
  139.     case REAL: 
  140.        if(v.type == REAL) return RR;
  141.        else if (v.type == STRING) return RS;
  142.        break;
  143.     case STRING:
  144.        if(v.type == STRING) return SS;
  145.        else if (v.type == REAL) return SR;
  146.        break;
  147.     default:
  148.        break;
  149.   }
  150.  
  151.   Error(ERR_PANIC, "Value::binary_type: strange value for 'type'");
  152.  
  153.   return UNKNOWN;
  154. }
  155.  
  156. Value Value::operator-() 
  157. {
  158.   switch (type) {
  159.     case REAL: return Value(-realVal);
  160.     default:   return *this;
  161.   }
  162. }
  163.  
  164. Value Value::operator+(const Value& v)
  165. {
  166.   switch (binary_type(v)) {
  167.     case RR: return Value(realVal + v.realVal);
  168.     case SS: return Value(*stringVal + *v.stringVal);
  169.     case RS: return Value(realVal + (real) v);
  170.     case SR: return Value(*stringVal + (rcString) v);
  171.     case UNKNOWN:
  172.     default: return Value();
  173.   }
  174. }
  175.  
  176. Value Value::operator-(const Value& v)
  177. {
  178.   switch (binary_type(v)) {
  179.     case RR: return Value(realVal - v.realVal);
  180.     default: return Value((real)*this - (real)v);
  181.   }
  182. }
  183.  
  184. Value Value::operator*(const Value& v)
  185. {
  186.   switch (binary_type(v)) {
  187.     case RR: return Value(realVal * v.realVal);
  188.     default: return Value((real)*this * (real)v);
  189.   }
  190. }
  191.  
  192. Value Value::operator/(const Value& v)
  193. {
  194.   switch (binary_type(v)) {
  195.     case RR: return Value(realVal / v.realVal);
  196.     default: return Value((real)*this / (real)v);
  197.   }
  198. }
  199.  
  200. Value Value::operator%(const Value& v)
  201. {
  202.   switch (binary_type(v)) {
  203.     case RR: return Value((long)realVal / (long)v.realVal);
  204.     default: return Value((long)real(*this) / (long)real(v));
  205.   }
  206. }
  207.  
  208. Value Value::operator!() 
  209. {
  210.   switch (type) {
  211.     case REAL: return Value(!realVal);
  212.     case STRING: return Value(stringVal->empty());
  213.     default:   return *this;
  214.   }
  215. }
  216.  
  217.  
  218. Value Value::operator&&(const Value& v)
  219. {
  220.   switch (binary_type(v)) {
  221.     case RR: return Value(realVal && v.realVal);
  222.     default: return Value((real)*this && (real)v);
  223.   }
  224. }
  225.  
  226. Value Value::operator||(const Value& v)
  227. {
  228.   switch (binary_type(v)) {
  229.     case RR: return Value(realVal || v.realVal);
  230.     default: return Value((real)*this || (real)v);
  231.   }
  232. }
  233.  
  234. Value Value::operator==(const Value& v)
  235. {
  236.   switch (binary_type(v)) {
  237.     case RR: return Value(realVal == v.realVal);
  238.     case SS: return Value(*stringVal == *v.stringVal);
  239.     case RS: return Value((rcString) *this ==  *v.stringVal);
  240.     case SR: return Value(*stringVal == (rcString) v);
  241.     case UNKNOWN:
  242.     default: return Value();
  243.   }
  244. }
  245.  
  246. Value Value::operator!=(const Value& v)
  247. {
  248.   switch (binary_type(v)) {
  249.     case RR: return Value(realVal != v.realVal);
  250.     case SS: return Value(*stringVal != *v.stringVal);
  251.     case RS: return Value((rcString) *this !=  *v.stringVal);
  252.     case SR: return Value(*stringVal != (rcString) v);
  253.     case UNKNOWN:
  254.     default: return Value();
  255.   }
  256. }
  257.  
  258.  
  259. Value Value::operator<(const Value& v)
  260. {
  261.   switch (binary_type(v)) {
  262.     case RR: return Value(realVal < v.realVal);
  263.     case SS: return Value(*stringVal < *v.stringVal);
  264.     case RS: return Value((rcString) *this <  *v.stringVal);
  265.     case SR: return Value(*stringVal < (rcString) v);
  266.     case UNKNOWN:
  267.     default: return Value();
  268.   }
  269. }
  270.  
  271. Value Value::operator<=(const Value& v)
  272. {
  273.   switch (binary_type(v)) {
  274.     case RR: return Value(realVal <= v.realVal);
  275.     case SS: return Value(*stringVal <= *v.stringVal);
  276.     case RS: return Value((rcString) *this <=  *v.stringVal);
  277.     case SR: return Value(*stringVal <= (rcString) v);
  278.     case UNKNOWN:
  279.     default: return Value();
  280.   }
  281. }
  282.  
  283. Value Value::operator>(const Value& v)
  284. {
  285.   switch (binary_type(v)) {
  286.     case RR: return Value(realVal > v.realVal);
  287.     case SS: return Value(*stringVal > *v.stringVal);
  288.     case RS: return Value((rcString) *this >  *v.stringVal);
  289.     case SR: return Value(*stringVal > (rcString) v);
  290.     case UNKNOWN:
  291.     default: return Value();
  292.   }
  293. }
  294.  
  295. Value Value::operator>=(const Value& v)
  296. {
  297.   switch (binary_type(v))
  298.   {
  299.     case RR: return Value(realVal >= v.realVal);
  300.     case SS: return Value(*stringVal >= *v.stringVal);
  301.     case RS: return Value((rcString) *this >=  *v.stringVal);
  302.     case SR: return Value(*stringVal >= (rcString) v);
  303.     case UNKNOWN:
  304.     default: return Value();
  305.   }
  306. }
  307.  
  308. ostream& operator<<(ostream& os, const Value& v)
  309. {
  310.   if (v.type == REAL)
  311.     os << v.realVal;
  312.   else if (v.type == STRING)
  313.     os << '\"' << *v.stringVal << '\"';
  314.   else
  315.     os << "Not know type of Value!\n";
  316.  
  317.   return os;
  318. }
  319.