INI-File Format
This conversion server uses an ini-file format where the:
- key:
<unit>|<precision>
- value:
<unit>,<multiplier>,<offset>,<precision>
There need to be 2 section available. One for Metric and the other Imperial unit conversion.
Example file:
[Metric]
m/s,0=m/s,1,0,0
m/s,2=mm/s,1000,0,-1
m/s,3=mm/s,1000,0,0
m/s,4=mm/s,1000,0,1
m/s,5=mm/s,1000,0,2
rad,0=deg,57.2958,0,0
rad,1=deg,57.2958,0,0
rad,2=deg,57.2958,0,1
rad,3=deg,57.2958,0,2
rad,4=deg,57.2958,0,3
rad,5=deg,57.2958,0,4
rad,6=deg,57.2958,0,5
[Imperial]
m/s,0="/s,39.37007874015748031496062992126,0,0
m/s,1="/s,39.37007874015748031496062992126,0,1
m/s,2="/s,39.37007874015748031496062992126,0,2
m/s,3="/s,39.37007874015748031496062992126,0,3
m/s,4="/s,39.37007874015748031496062992126,0,4
m/s,5="/s,39.37007874015748031496062992126,0,5
rad,0=deg,57.2958,0,0
rad,1=deg,57.2958,0,0
rad,2=deg,57.2958,0,1
rad,3=deg,57.2958,0,2
rad,4=deg,57.2958,0,3
rad,5=deg,57.2958,0,4
rad,6=deg,57.2958,0,5
°C|1=°F,1.8,32,1
Usage
Conversion Server
The instance of sf::UnitConversionServer
is created some where in the application.
++
std::istringstream is(R"(
[Metric]
m/s|1=mm/s,1000,0,-2
m/s|2=mm/s,1000,0,-1
m/s|3=mm/s,1000,0,0
[Imperial]
m/s|0="/s,39.37007874015748031496062992126,0,0
m/s|1="/s,39.37007874015748031496062992126,0,1
m/s|2="/s,39.37007874015748031496062992126,0,2
°C|1=°F,1.8,32,1
)");
Implementation of a unit conversion server using an ini-file.
Definition UnitConversionServer.h:19
@ usImperial
Definition UnitConversionServer.h:86
Conversion Client
Calling global function #sf::getUnitConversion()
is now handled by the singleton sf::UnitConversionServer
instance.
++
double multiplier{0}, offset{0}, value{32.2};
std::string unit;
std::clog <<
stringf(
"%.*lf", multiplier * value + offset, precision) << std::endl;
TARGET_IMPORT bool getUnitConversion(const std::string &option, const std::string &from_unit, int from_precision, double &multiplier, double &offset, std::string &to_unit, int &to_precision)
Returns true if a conversion could be made. Calculation to convert are: From -> To calculation: n...
int precision(double value)
Returns the precision of the passed floating point value. This is the amount of characters after the ...
std::string stringf(const char *fmt,...)
Creates a formatted string and returns it in a string class instance.