/** * @brief * Converts <value> into a Data-is-Strings unsigned integer and sends * it to <stream>. * * @param[in] stream socket fd * @param[in] value value to be converted * * @return int * @retval DIS_SUCCESS success * @retval error code error * */ int diswui_(int stream, unsigned value) { unsigned ndigs; char *cp; assert(stream >= 0); assert(dis_puts != NULL); cp = discui_(&dis_buffer[DIS_BUFSIZ], value, &ndigs); *--cp = '+'; while (ndigs > 1) cp = discui_(cp, ndigs, &ndigs); if ((*dis_puts)(stream, cp, (size_t)(&dis_buffer[DIS_BUFSIZ] - cp)) < 0) return (DIS_PROTO); return (DIS_SUCCESS); }
int diswui_(int stream, unsigned value) { unsigned ndigs; char *cp; char scratch[DIS_BUFSIZ+1]; assert(stream >= 0); assert(dis_puts != NULL); memset(scratch, 0, DIS_BUFSIZ+1); cp = discui_(&scratch[DIS_BUFSIZ], value, &ndigs); *--cp = '+'; while (ndigs > 1) cp = discui_(cp, ndigs, &ndigs); if ((*dis_puts)(stream, cp, strlen(cp)) < 0) return(DIS_PROTO); return (DIS_SUCCESS); }
void disiui_(void) { char *cp; char scratch[DIS_BUFSIZ]; if ((dis_umax != NULL) || (dis_umaxd != 0)) return; memset(scratch, 0, sizeof(scratch)); cp = discui_(&scratch[sizeof(scratch)-1], UINT_MAX, &dis_umaxd); dis_umax = (char *)calloc(1, dis_umaxd + 1); memcpy(dis_umax, cp, dis_umaxd); }
void disiui_(void) { char *cp; char scratch[DIS_BUFSIZ+1]; assert(dis_umax == NULL); assert(dis_umaxd == 0); memset(scratch, 0, DIS_BUFSIZ+1); cp = discui_(&scratch[DIS_BUFSIZ], UINT_MAX, &dis_umaxd); assert(dis_umaxd > 0); dis_umax = (char *)malloc(dis_umaxd); assert(dis_umax != NULL); memcpy(dis_umax, cp, dis_umaxd); }
/** * @brief * Converts <value> into a Data-is-Strings unsigned integer and sends * it to <stream>. * * @param[in] stream socket fd * @param[in] value value to be converted * * @return int * @retval DIS_SUCCESS success * @retval error code error * */ int diswul(int stream, unsigned long value) { int retval; unsigned ndigs; char *cp; assert(stream >= 0); assert(dis_puts != NULL); assert(disw_commit != NULL); cp = discul_(&dis_buffer[DIS_BUFSIZ], value, &ndigs); *--cp = '+'; while (ndigs > 1) cp = discui_(cp, ndigs, &ndigs); retval = (*dis_puts)(stream, cp, (size_t)(&dis_buffer[DIS_BUFSIZ] - cp)) < 0 ? DIS_PROTO : DIS_SUCCESS; return (((*disw_commit)(stream, retval == DIS_SUCCESS) < 0) ? DIS_NOCOMMIT : retval); }
int diswsl( struct tcp_chan *chan, long value) { int retval; unsigned ndigs; unsigned long ulval; char c; char *cp; char scratch[DIS_BUFSIZ]; memset(scratch, 0, sizeof(scratch)); if (value < 0) { ulval = (unsigned long) - (value + 1) + 1; c = '-'; } else { ulval = value; c = '+'; } cp = discul_(&scratch[sizeof(scratch)-1], ulval, &ndigs); *--cp = c; while (ndigs > 1) cp = discui_(cp, ndigs, &ndigs); retval = tcp_puts(chan, cp, strlen(cp)) < 0 ? DIS_PROTO : DIS_SUCCESS; return ((tcp_wcommit(chan, retval == DIS_SUCCESS) < 0) ? DIS_NOCOMMIT : retval); }
int diswl_( struct tcp_chan *chan, dis_long_double_t value, unsigned ndigs) { int c; int expon; int negate; int retval; unsigned pow2; char *cp; char *ocp; dis_long_double_t ldval; char scratch[DIS_BUFSIZ+1]; assert(ndigs > 0 && ndigs <= LDBL_DIG); memset(scratch, 0, DIS_BUFSIZ+1); /* Make zero a special case. If we don't it will blow exponent */ /* calculation. */ if (value == 0.0L) { retval = tcp_puts(chan, "+0+0", 4) < 0 ? DIS_PROTO : DIS_SUCCESS; return ((tcp_wcommit(chan, retval == DIS_SUCCESS) < 0) ? DIS_NOCOMMIT : retval); } /* Extract the sign from the coefficient. */ ldval = (negate = value < 0.0L) ? -value : value; /* Detect and complain about the infinite form. */ if (ldval > LDBL_MAX) return (DIS_HUGEVAL); /* Compute the integer part of the log to the base 10 of ldval. As a */ /* byproduct, reduce the range of ldval to the half-open interval, */ /* [1, 10). */ if (dis_lmx10 == 0) disi10l_(); expon = 0; pow2 = dis_lmx10 + 1; if (ldval < 1.0L) { do { if (ldval < dis_ln10[--pow2]) { ldval *= dis_lp10[pow2]; expon += 1 << pow2; } } while (pow2); ldval *= 10.0; expon = -expon - 1; } else { do { if (ldval >= dis_lp10[--pow2]) { ldval *= dis_ln10[pow2]; expon += 1 << pow2; } } while (pow2); } /* Round the value to the last digit */ ldval += 5.0L * disp10l_(-ndigs); if (ldval >= 10.0L) { expon++; ldval *= 0.1L; } /* Starting in the middle of the buffer, convert coefficient digits, */ /* most significant first. */ ocp = cp = &scratch[DIS_BUFSIZ - ndigs]; do { c = ldval; ldval = (ldval - c) * 10.0L; *ocp++ = c + '0'; } while (--ndigs); /* Eliminate trailing zeros. */ while (*--ocp == '0'); /* The decimal point is at the low order end of the coefficient */ /* integer, so adjust the exponent for the number of digits in the */ /* coefficient. */ ndigs = ++ocp - cp; expon -= ndigs - 1; /* Put the coefficient sign into the buffer, left of the coefficient. */ *--cp = negate ? '-' : '+'; /* Insert the necessary number of counts on the left. */ while (ndigs > 1) cp = discui_(cp, ndigs, &ndigs); /* The complete coefficient integer is done. Put it out. */ retval = tcp_puts(chan, cp, (size_t)(ocp - cp)) < 0 ? DIS_PROTO : DIS_SUCCESS; /* If that worked, follow with the exponent, commit, and return. */ if (retval == DIS_SUCCESS) return (diswsi(chan, expon)); /* If coefficient didn't work, negative commit and return the error. */ return ((tcp_wcommit(chan, FALSE) < 0) ? DIS_NOCOMMIT : retval); }
/** * @brief * Converts <value> into a Data-is-Strings floating point number and sends * it to <stream>. * * @param[in] stream socket fd * @param[in] value value to be converted * * @return int * @retval DIS_SUCCESS success * @retval error code error * */ int diswf(int stream, double value) { int c; int expon; unsigned ndigs; int negate; int retval; unsigned pow2; char *cp; char *ocp; double dval; assert(stream >= 0); assert(dis_puts != NULL); assert(disw_commit != NULL); /* Make zero a special case. If we don't it will blow exponent */ /* calculation. */ if (value == 0.0) { retval = (*dis_puts)(stream, "+0+0", 4) != 4 ? DIS_PROTO : DIS_SUCCESS; return (((*disw_commit)(stream, retval == DIS_SUCCESS) < 0) ? DIS_NOCOMMIT : retval); } /* Extract the sign from the coefficient. */ dval = (negate = value < 0.0) ? -value : value; /* Detect and complain about the infinite form. */ if (dval > FLT_MAX) return (DIS_HUGEVAL); /* Compute the integer part of the log to the base 10 of dval. As a */ /* byproduct, reduce the range of dval to the half-open interval, */ /* [1, 10). */ /* dis_dmx10 would be initialized by prior call to dis_init_tables */ expon = 0; pow2 = dis_dmx10 + 1; if (dval < 1.0) { do { if (dval < dis_dn10[--pow2]) { dval *= dis_dp10[pow2]; expon += 1 << pow2; } } while (pow2); dval *= 10.0; expon = -expon - 1; } else { do { if (dval >= dis_dp10[--pow2]) { dval *= dis_dn10[pow2]; expon += 1 << pow2; } } while (pow2); } /* Round the value to the last digit */ dval += 5.0 * disp10d_(-FLT_DIG); if (dval >= 10.0) { expon++; dval *= 0.1; } /* Starting in the middle of the buffer, convert coefficient digits, */ /* most significant first. */ ocp = cp = &dis_buffer[DIS_BUFSIZ - FLT_DIG]; ndigs = FLT_DIG; do { c = dval; dval = (dval - c) * 10.0; *ocp++ = c + '0'; } while (--ndigs); /* Eliminate trailing zeros. */ while (*--ocp == '0'); /* The decimal point is at the low order end of the coefficient */ /* integer, so adjust the exponent for the number of digits in the */ /* coefficient. */ ndigs = ++ocp - cp; expon -= ndigs - 1; /* Put the coefficient sign into the buffer, left of the coefficient. */ *--cp = negate ? '-' : '+'; /* Insert the necessary number of counts on the left. */ while (ndigs > 1) cp = discui_(cp, ndigs, &ndigs); /* The complete coefficient integer is done. Put it out. */ retval = (*dis_puts)(stream, cp, (size_t)(ocp - cp)) < 0 ? DIS_PROTO : DIS_SUCCESS; /* If that worked, follow with the exponent, commit, and return. */ if (retval == DIS_SUCCESS) return (diswsi(stream, expon)); /* If coefficient didn't work, negative commit and return the error. */ return (((*disw_commit)(stream, FALSE) < 0) ? DIS_NOCOMMIT : retval); }