home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xntp3.zip / lib / mstolfp.c < prev    next >
C/C++ Source or Header  |  1989-08-11  |  2KB  |  105 lines

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