00001 /* 00002 ------------------------------------------------------------------- 00003 00004 Copyright (C) 2008, 2009, Andrew W. Steiner 00005 00006 This file is part of O2scl. 00007 00008 O2scl is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 3 of the License, or 00011 (at your option) any later version. 00012 00013 O2scl is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with O2scl. If not, see <http://www.gnu.org/licenses/>. 00020 00021 ------------------------------------------------------------------- 00022 */ 00023 #ifndef O2SCL_CONVERT_UNITS_H 00024 #define O2SCL_CONVERT_UNITS_H 00025 00026 #include <cstdio> 00027 #include <cstdlib> 00028 #include <iostream> 00029 #include <map> 00030 00031 #include <o2scl/err_hnd.h> 00032 #include <o2scl/file_format.h> 00033 #include <o2scl/misc.h> 00034 #include <o2scl/constants.h> 00035 #include <o2scl/string_conv.h> 00036 00037 #ifndef DOXYGENP 00038 namespace o2scl { 00039 #endif 00040 00041 /** 00042 \brief Convert units 00043 00044 Allow the user to convert between two different units after 00045 specifying a conversion factor. This class will also 00046 automatically combine two conversion factors to create a new 00047 unit conversion. 00048 00049 Conversions are performed by the \ref convert() function and 00050 the conversion factors must be specified beforehand using the 00051 \ref insert_cache() function. 00052 00053 If the GNU units command is not in the local path, the user may 00054 modify \ref units_cmd_string to specify the full pathname. One 00055 can also modify \ref units_cmd_string to specify a different 00056 <tt>units.dat</tt> file. 00057 00058 Example: 00059 \code 00060 convert_units cu; 00061 cu.insert_cache("in","cm",2.54); 00062 cout << "12 in is " << cu.convert("in","cm",12.0) << " cm. " << endl; 00063 \endcode 00064 00065 \future Ideally, a real C++ API for the GNU units command 00066 would be better. 00067 */ 00068 class convert_units { 00069 00070 #ifndef DOXYGEN_INTERNAL 00071 00072 protected: 00073 00074 /// The type for caching unit conversions 00075 typedef struct { 00076 /// The input unit 00077 std::string f; 00078 /// The output unit 00079 std::string t; 00080 /// The conversion factor 00081 double c; 00082 } unit_t; 00083 00084 /// The cache where unit conversions are stored 00085 std::map<std::string,unit_t,string_comp> mcache; 00086 00087 /// The iterator type 00088 typedef std::map<std::string,unit_t,string_comp>::iterator miter; 00089 00090 #endif 00091 00092 public: 00093 00094 /// Verbosity (default 0) 00095 int verbose; 00096 00097 /// (default true) 00098 bool use_gnu_units; 00099 00100 /// (default true) 00101 bool err_on_fail; 00102 00103 /// Default 'units' 00104 std::string units_cmd_string; 00105 00106 convert_units() { 00107 verbose=0; 00108 use_gnu_units=true; 00109 units_cmd_string="units"; 00110 err_on_fail=true; 00111 } 00112 00113 virtual ~convert_units() {} 00114 00115 /** \brief Return the value \c val after converting using units \c 00116 from and \c to 00117 */ 00118 virtual double convert(std::string from, std::string to, double val); 00119 00120 /// Manually insert a unit conversion into the cache 00121 int insert_cache(std::string from, std::string to, double conv); 00122 00123 /// Manually remove a unit conversion into the cache 00124 int remove_cache(std::string from, std::string to); 00125 00126 /// Print the present unit cache to std::cout 00127 int print_cache(); 00128 00129 /// Add conversion factors for energy equivalents 00130 int energy_conv(); 00131 00132 }; 00133 00134 #ifndef DOXYGENP 00135 } 00136 #endif 00137 00138 #endif
Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.
Project hosting provided by
,
O2scl Sourceforge Project Page