1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
/*
* mstolfp - convert an ascii string in milliseconds to an l_fp number
*/
#include <config.h>
#include <stdio.h>
#include <ctype.h>
#include "ntp_fp.h"
#include "ntp_stdlib.h"
int
mstolfp(
const char *str,
l_fp *lfp
)
{
register const char *cp;
register char *bp;
register const char *cpdec;
char buf[100];
/*
* We understand numbers of the form:
*
* [spaces][-][digits][.][digits][spaces|\n|\0]
*
* This is one enormous hack. Since I didn't feel like
* rewriting the decoding routine for milliseconds, what
* is essentially done here is to make a copy of the string
* with the decimal moved over three places so the seconds
* decoding routine can be used.
*/
bp = buf;
cp = str;
while (isspace((unsigned char)*cp))
cp++;
if (*cp == '-') {
*bp++ = '-';
cp++;
}
if (*cp != '.' && !isdigit((unsigned char)*cp))
return 0;
/*
* Search forward for the decimal point or the end of the string.
*/
cpdec = cp;
while (isdigit((unsigned char)*cpdec))
cpdec++;
/*
* Found something. If we have more than three digits copy the
* excess over, else insert a leading 0.
*/
if ((cpdec - cp) > 3) {
do {
*bp++ = (char)*cp++;
} while ((cpdec - cp) > 3);
} else {
*bp++ = '0';
}
/*
* Stick the decimal in. If we've got less than three digits in
* front of the millisecond decimal we insert the appropriate number
* of zeros.
*/
*bp++ = '.';
if ((cpdec - cp) < 3) {
size_t i = 3 - (cpdec - cp);
do {
*bp++ = '0';
} while (--i > 0);
}
/*
* Copy the remainder up to the millisecond decimal. If cpdec
* is pointing at a decimal point, copy in the trailing number too.
*/
while (cp < cpdec)
*bp++ = (char)*cp++;
if (*cp == '.') {
cp++;
while (isdigit((unsigned char)*cp))
*bp++ = (char)*cp++;
}
*bp = '\0';
/*
* Check to make sure the string is properly terminated. If
* so, give the buffer to the decoding routine.
*/
if (*cp != '\0' && !isspace((unsigned char)*cp))
return 0;
return atolfp(buf, lfp);
}
|