Skip to content

Instantly share code, notes, and snippets.

@ajdawson
Created October 10, 2014 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajdawson/df1914f42b7a7a321a9a to your computer and use it in GitHub Desktop.
Save ajdawson/df1914f42b7a7a321a9a to your computer and use it in GitHub Desktop.
Test program for UDUNITS2
#include <stdio.h>
#include <udunits2.h>
int main(int argc, char *argv[])
{
ut_system* unitSystem = ut_read_xml(NULL);
ut_unit *unit0, *unit1, *unit2;
char unit0_str[128], unit1_str[128], unit2_str[128];
unsigned format_opts = UT_ASCII;
/* Create two units: degrees Celsius and dimensionless. */
unit0 = ut_parse(unitSystem, "Celsius", UT_ASCII);
unit1 = ut_parse(unitSystem, "1", UT_ASCII);
if ((unit0 == NULL) || (unit1 == NULL)) {
fprintf(stderr, "error: failed to parse unit string\n");
return 1;
}
/* Multiply the units together. */
unit2 = ut_multiply(unit0, unit1);
/* Turn all the units into strings. */
ut_format(unit0, unit0_str, sizeof(unit0_str), format_opts);
ut_format(unit1, unit1_str, sizeof(unit1_str), format_opts);
ut_format(unit2, unit2_str, sizeof(unit2_str), format_opts);
/* Print the results. */
printf("%s x %s = %s\n", unit0_str, unit1_str, unit2_str);
ut_free(unit0);
ut_free(unit1);
ut_free(unit2);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment