units.cpp revision 9f578482ce54902110df95931d68120ec51e6cab
/*
* Inkscape Units
*
* Authors:
* Matthew Petroff <matthew@mpetroff.net>
*
* Copyright (C) 2013 Matthew Petroff
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <cmath>
#include <cerrno>
#include <glib.h>
#include "io/simple-sax.h"
#include "path-prefix.h"
#include "streq.h"
namespace
{
/**
* A std::map that gives the data type value for the string version.
*
* Note that we'd normally not return a reference to an internal version, but
* for this constant case it allows us to check against getTypeMappings().end().
*/
/** @todo consider hiding map behind hasFoo() and getFoo() type functions.*/
{
static bool init = false;
if (!init)
{
init = true;
// Note that code was not yet handling LINEAR_SCALED, TIME, QTY and NONE
}
return typeMap;
}
} // namespace
namespace Inkscape {
namespace Util {
{
public:
virtual ~UnitsSAXHandler() {}
bool primary;
bool skip;
};
primary(0),
skip(0),
unit()
{
}
#define BUFSIZE (255)
factor(1.0),
name(),
name_plural(),
abbr(),
{
}
double factor,
{
}
{
*this = Unit();
}
int Unit::defaultDigits() const {
if (factor_digits < 0) {
g_warning("factor_digits < 0 - returning 0");
factor_digits = 0;
}
return factor_digits;
}
/** Checks if a unit is compatible with the specified unit. */
// Percentages
return true;
}
// Other units with same type
return true;
}
// Different, incompatible types
return false;
}
/** Check if units are equal. */
}
/** Check if units are not equal. */
}
/** Temporary - get SVG unit. */
return 1;
return 2;
return 3;
return 4;
return 5;
return 6;
return 7;
return 8;
return 9;
return 10;
return 0;
}
/** Temporary - get metric. */
return 1;
return 2;
return 3;
return 4;
return 5;
return 6;
return 7;
return 8;
return 0;
}
{
// if we swich to the xml file, don't forget to force locale to 'C'
}
{
}
}
if (primary) {
}
}
} else {
return Unit();
}
}
// Extract value
double value = 0;
}
// Extract unit abbreviation
}
}
bool deleted = false;
// Cannot delete the primary unit type since it's
// used for conversions
deleted = true;
}
}
return deleted;
}
{
}
{
}
}
return submap;
}
{
return _primary_unit[type];
}
{
// Open file for reading
if (f == NULL) {
g_warning("Could not open units file '%s': %s\n",
return false;
}
/** @todo fix this to use C++ means and explicit locale to avoid need to change. */
// bypass current locale in order to make
// sscanf read floats with '.' as a separator
// set locale to 'C' and keep old locale
double factor = 0.0;
int nchars = 0;
// locale is set to C, scanning %lf should work _everywhere_
/** @todo address %15n, which causes a warning: */
{
// Skip the line - doesn't appear to be valid
continue;
}
// insert into _unit_map
{
continue;
}
// if primary is 'Y', list this unit as a primary
}
// set back the saved locale
g_free (old_locale);
// close file
if (fclose(f) != 0) {
return false;
}
return true;
}
UnitsSAXHandler handler(this);
if ( result != 0 ) {
// perhaps
return false;
}
return true;
}
// open file for writing
if (f == NULL) {
return false;
}
// write out header
// foreach item in _unit_map, sorted alphabetically by type and then unit name
// sprintf a line
// name
// name_plural
// abbr
// type
// factor
// PRI - if listed in primary unit table, 'Y', else 'N'
// description
// write line to the file
// close file
if (fclose(f) != 0) {
return false;
}
return true;
}
{
// reset for next use
primary = false;
skip = false;
for ( int i = 0; attrs[i]; i += 2 ) {
{
} else {
skip = true;
}
}
}
}
}
{
// TODO make sure we use the right conversion
if (!skip) {
}
}
}
/** Initialize a quantity. */
unit = u;
quantity = q;
}
/** Checks if a quantity is compatible with the specified unit. */
return unit->compatibleWith(u);
}
/** Return the quantity's value in the specified unit. */
}
/** Convert distances. */
// Incompatible units
return -1;
}
// Compatible units
}
} // namespace Util
} // namespace Inkscape
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :