Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

UnitsExtension.cpp

Go to the documentation of this file.
00001 #include <fstream>
00002 #include "UnitsExtension.h"
00003 
00004 bool UnitsExtension::addUnit(std::string name, double factor) {
00005   if (existsUnit(name))
00006     return false;
00007 
00008   UnitsObject unit(name, factor);
00009   unitVector.push_back(unit);
00010   return true;
00011 }
00012 
00013 bool UnitsExtension::existsUnit(std::string name) {
00014   std::vector <UnitsObject>::iterator itr;
00015   itr = unitVector.begin();
00016   while (itr!=unitVector.end()) {
00017     if ((*itr).getName() == name) {
00018       return true;
00019     } // if
00020     itr++;
00021   } // while
00022   return false;
00023 }
00024 
00025 double UnitsExtension::getFactor(std::string name) {
00026   std::vector <UnitsObject>::iterator itr;
00027   itr = unitVector.begin();
00028   while (itr!=unitVector.end()) {
00029     if ((*itr).getName() == name) {
00030       return (*itr).getFactor();
00031     }
00032     itr++;
00033   }
00034   return 0;
00035 }
00036 
00037 bool UnitsExtension::getUnits(std::string definitionFile) {
00038   // Parse file and build vector-table.
00039   unitVector.clear();
00040 
00041   std::ifstream inputFile(definitionFile.c_str());
00042   if (inputFile.fail())
00043     return false;
00044 
00045   while (!inputFile.fail() && !inputFile.eof()) {
00046     double unitQuantity;
00047     inputFile >> unitQuantity;
00048     if (inputFile.fail()) {
00049       return false;
00050     }
00051 
00052     std::string unitName;
00053     inputFile >> unitName;
00054     if (inputFile.fail()) {
00055       return false;
00056     }
00057 
00058     std::string equalChar;
00059     inputFile >> equalChar;
00060     if (inputFile.fail() || (equalChar != "=")) {
00061       return false;
00062     }
00063 
00064     double factor;
00065     inputFile >> factor;
00066     if (inputFile.fail()) {
00067       return false;
00068     }
00069 
00070     //everything is here, so create Entry...
00071     double quantity = (factor / unitQuantity);   // = real factor according to quantitiy
00072     addUnit(unitName, quantity);
00073   } // while
00074   inputFile.close();
00075   return true;
00076 }
00077 

Generated at Wed Nov 21 12:22:31 2001 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000