Esempio n. 1
0
value ml_ut_format( value u, value encoding, value names, value basic, value max_length ) {
    CAMLparam5( u, encoding, names, basic, max_length );
    CAMLlocal1( ml_buf );

    int opts =
        Int_val( encoding ) |
        (Int_val( basic ) ? UT_DEFINITION : 0) |
        (Int_val( names ) ? UT_NAMES : 0);

    int result;
    char *buf;
    buf = (char *)malloc( sizeof(char) * Int_val( max_length ) );
    if ( buf == NULL ) {
        caml_failwith( "Unable to allocate buffer" );
    }

    result = ut_format( UD_ut_unit_val( u ), buf, Int_val( max_length ), opts );
    if ( result == -1 ) {
        caml_raise_with_arg( *caml_named_value( "ut status exception" ), Val_int( ut_get_status() ) );
    }

    ml_buf = caml_copy_string( buf );

    free( buf );

    CAMLreturn( ml_buf );
}
Esempio n. 2
0
int /* [rcd] Successful conversion returns NCO_NOERR */
nco_cln_prs_tm /* UDUnits2 Extract time stamp from parsed UDUnits string */
(const char *unt_sng, /* I [ptr] units attribute string */
 tm_cln_sct *tm_in) /* O [sct] Time structure to be populated */
{
  const char fnc_nm[]="nco_cln_prs_tm()"; /* [sng] Function name */

  /* 20141230: fxm figure out a better length */
  char bfr[200];

  char *dt_sng;

  int ut_rcd; /* [enm] UDUnits2 status */

  ut_system *ut_sys;
  ut_unit *ut_sct_in; /* UDUnits structure, input units */

  /* When empty, ut_read_xml() uses environment variable UDUNITS2_XML_PATH, if any
     Otherwise it uses default initial location hardcoded when library was built */
  if(nco_dbg_lvl_get() >= nco_dbg_vrb) ut_set_error_message_handler(ut_write_to_stderr); else ut_set_error_message_handler(ut_ignore);
  ut_sys=ut_read_xml(NULL);
  if(ut_sys == NULL){
    (void)fprintf(stdout,"%s: %s failed to initialize UDUnits2 library\n",nco_prg_nm_get(),fnc_nm);
    return NCO_ERR; /* Failure */
  } /* end if err */ 

  /* Units string to convert from */
  ut_sct_in=ut_parse(ut_sys,unt_sng,UT_ASCII); 
  if(ut_sct_in == NULL){ /* Problem with 'units' attribute */
    ut_rcd=ut_get_status(); /* [enm] UDUnits2 status */
    if(ut_rcd == UT_BAD_ARG) (void)fprintf(stderr,"ERROR: empty units attribute string\n");
    if(ut_rcd == UT_SYNTAX)  (void)fprintf(stderr,"ERROR: units attribute \"%s\" has a syntax error\n",unt_sng);
    if(ut_rcd == UT_UNKNOWN) (void)fprintf(stderr,"ERROR: units attribute \"%s\" is not listed in UDUnits2 SI system database\n",unt_sng);

    return NCO_ERR; /* Failure */
  } /* endif coordinate on disk has no units attribute */

  /* Print timestamp to buffer in standard, dependable format */
  ut_format(ut_sct_in,bfr,sizeof(bfr),UT_ASCII|UT_NAMES);

  /* Extract parsed time units from print string (kludgy)
     20141230 change to using ut_decode_time() instead? */
  dt_sng=strstr(bfr,"since");  
  sscanf(dt_sng,"%*s %d-%d-%d %d:%d:%f",&tm_in->year,&tm_in->month,&tm_in->day,&tm_in->hour,&tm_in->min,&tm_in->sec);

  ut_free_system(ut_sys); /* Free memory taken by UDUnits library */
  ut_free(ut_sct_in);

  return NCO_NOERR;
} /* end UDUnits2 nco_cln_prs_tm() */
Esempio n. 3
0
/*!
 * Return an UTF-8 textual representation of this unit according to \a form and \a option.
 * \sa UdUnit::FormatForm, UdUnit::FormatOption
 */
QString UdUnit::format(FormatForm form, FormatOption option) const
{
    static const int size = 256;
    char buffer[size + 1];
    int flags = UT_UTF8;
    if (form == DefinitionForm)
        flags |= UT_DEFINITION;
    if (option == UseUnitName)
        flags |= UT_NAMES;
    int nBytes = ut_format(m_unit, buffer, 256, flags);
    if (nBytes < 0 || nBytes > size)
        return QString();
    else
        return QString::fromUtf8(buffer, nBytes);
}
Esempio n. 4
0
/*
 * Encode a unit-structure into a formatted unit-specification.
 */
int
utPrint(
    const utUnit	*unit,
    char		**buf)
{
    int	status;

    for (;;) {
	int	len = ut_format(unit->unit2, buffer, buflen, UT_ASCII);
	if (len == -1) {
	    status = ut_get_status();

	    if (status == UT_BAD_ARG) {
		status = UT_EINVALID;
	    }
	    else {
		status = UT_EALLOC;
	    }
	    break;
	}
	if (len < buflen) {
	    *buf = buffer;
	    status = 0;
	    break;
	}
	else {
	    int		newLen = buflen * 2;
	    char*	newBuf = malloc(newLen);

	    if (newBuf == NULL) {
		status = UT_EALLOC;
		break;
	    }
	    buffer = newBuf;
	    buflen = newLen;
	}
    }
    return status;
}
Esempio n. 5
0
static int
handleRequest(void)
{
    int		success = 0;

    if (getInputValue()) {
	if (getOutputRequest()) {
	    if (_wantDefinition) {
                char	        buf[256];
                ut_unit*        unit = ut_scale(_haveUnitAmount, _haveUnit);
                int	        nbytes = ut_format(unit, buf, sizeof(buf),
                        _formattingOptions);

                if (nbytes >= sizeof(buf)) {
                    errMsg("Resulting unit specification is too long");
                }
                else if (nbytes >= 0) {
                    buf[nbytes] = 0;

                    (void)printf("    %s\n", buf);
                }

                ut_free(unit);
	    }
	    else if (!ut_are_convertible(_wantUnit, _haveUnit)) {
		errMsg("Units are not convertible");
	    }
	    else {
		cv_converter*	conv = ut_get_converter(_haveUnit, _wantUnit);

		if (conv == NULL) {
		    errMsg("Couldn't get unit converter");
		}
		else {
                    char        haveExp[_POSIX_MAX_INPUT+1];
                    char        exp[_POSIX_MAX_INPUT+1];
                    char        whiteSpace[] = " \t\n\r\f\v\xa0";
		    int	        needsParens =
                        strpbrk(_wantSpec, whiteSpace) != NULL;
                    int         n;

		    (void)printf(
			needsParens
			    ? "    %g %s = %g (%s)\n"
			    : "    %g %s = %g %s\n",
                        _haveUnitAmount,
			_haveUnitSpec,
			cv_convert_double(conv, _haveUnitAmount),
                        _wantSpec);

                    (void)sprintf(haveExp,
                        strpbrk(_haveUnitSpec, whiteSpace) ||
                                strpbrk(_haveUnitSpec, "/")
                            ? "(x/(%s))"
                            : "(x/%s)",
                        _haveUnitSpec);

                    n = cv_get_expression(conv, exp, sizeof(exp), haveExp);

                    if (n >= 0)
                        (void)printf(
                            strpbrk(_wantSpec, whiteSpace) ||
                                    strpbrk(_wantSpec, "/")
                                ?  "    x/(%s) = %*s\n"
                                :  "    x/%s = %*s\n",
                        _wantSpec, n, exp);

                    cv_free(conv);
		}
	    }

	    success = 1;
	}
    }

    return success;
}