units.cpp revision 73f5f519746323f02f682c9ed8ce51a5f67cbce9
#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;
}
}
/** 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;
}
/**
* 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];
}
/** Loads units from a text file.
loadText loads and merges the contents of the given file into the UnitTable,
possibly overwriting existing unit definitions.
@param filename: file to be loaded*/
{
// 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 locale to 'C' and keep old locale
char *old_locale;
double factor;
int nchars = 0;
// locale 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) {
}
}
}
/** 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 :