Beispiel #1
0
static int
compositeGetExpression(
    const cv_converter* const	conv,
    char* const			buf,
    const size_t		max,
    const char* const		variable)
{
    char	tmpBuf[132];
    int		nchar = cv_get_expression(conv->composite.first, buf, max,
                                      variable);

    if (nchar >= 0) {
        buf[max-1] = 0;

        if (cvNeedsParentheses(buf)) {
            (void)snprintf(tmpBuf, sizeof(tmpBuf), "(%s)", buf);
        }
        else {
            (void)strncpy(tmpBuf, buf, sizeof(tmpBuf));

            tmpBuf[sizeof(tmpBuf)-1] = 0;
        }

        nchar = cv_get_expression(conv->composite.second, buf, max, tmpBuf);
    }

    return nchar;
}
Beispiel #2
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;
}