units.cpp revision a740557e431e45e84fc83153fa6794ff913fc94f
#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 Inkscape {
namespace Util {
{
public:
virtual ~UnitsSAXHandler() {}
bool primary;
bool skip;
};
primary(0),
skip(0),
unit()
{
}
#define BUFSIZE (255)
/**
* Returns the suggested precision to use for displaying numbers
* of this unit.
*/
int Unit::defaultDigits() const {
if (factor_digits < 0) {
g_warning("factor_digits < 0 - returning 0");
return 0;
} else {
return factor_digits;
}
}
/**
* Initializes the unit tables and identifies the primary unit types.
*
* The primary unit's conversion factor is required to be 1.00
*/
{
// if we swich to the xml file, don't forget to force locale to 'C'
}
++iter;
}
}
/** Add a new unit to the table */
if (primary) {
}
}
/** Retrieve a given unit based on its string identifier */
} else {
return Unit();
}
}
/** Remove a unit definition from the given unit type table */
// Cannot delete the primary unit type since it's
// used for conversions
return false;
}
return true;
} else {
return false;
}
}
/** Returns true if the given string 'name' is a valid unit in the table */
}
/** Provides an iteratable list of items in the given unit table */
{
}
}
return submap;
}
/** Returns the default unit abbr for the given type */
return _primary_unit[type];
}
/** Merges the contents of the given file into the UnitTable,
possibly overwriting existing unit definitions. This loads
from a text file */
// Open file for reading
if (f == NULL) {
g_warning("Could not open units file '%s': %s\n",
return false;
}
// bypass current locale in order to make
// sscanf read floats with '.' as a separator
// set locate to 'C' and keep old locale
char *old_locale;
double factor;
int nchars = 0;
// locate is set to C, scanning %lf should work _everywhere_
// Skip the line - doesn't appear to be valid
continue;
}
// insert into _unit_map
Unit u;
u.name_plural = plural;
u.description = desc;
u.type = UNIT_TYPE_LINEAR;
u.type = UNIT_TYPE_RADIAL;
} else {
g_warning("Skipping unknown unit type '%s' for %s.\n",
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) {
g_warning("Error closing units file '%s': %s\n",
return false;
}
return true;
}
UnitsSAXHandler handler(this);
if ( result != 0 ) {
// perhaps
g_warning("Problem loading units file '%s': %d\n",
return false;
}
return true;
}
/** Saves the current UnitTable to the given file. */
// open file for writing
if (f == NULL) {
g_warning("Could not open units file '%s': %s\n",
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) {
g_warning("Error closing units file '%s': %s\n",
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) {
}
}
}
} // 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 :