home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / internet / netlite / NET / c / MISC < prev    next >
Text File  |  1993-03-04  |  3KB  |  120 lines

  1. /* Miscellaneous machine independent utilities */
  2.  
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <stdlib.h>
  6. #include "global.h"
  7. #include "misc.h"
  8.  
  9. /* Convert hex-ascii to integer */
  10. int htoi(char *s)
  11. {
  12.         int i = 0;
  13.         char c;
  14.  
  15.         while((c = *s++) != '\0'){
  16.                 if(c == 'x')
  17.                         continue;       /* allow 0x notation */
  18.                 if('0' <= c && c <= '9')
  19.                         i = (i * 16) + (c - '0');
  20.                 else if('a' <= c && c <= 'f')
  21.                         i = (i * 16) + (c - 'a' + 10);
  22.                 else if('A' <= c && c <= 'F')
  23.                         i = (i * 16) + (c - 'A' + 10);
  24.                 else
  25.                         break;
  26.         }
  27.         return i;
  28. }
  29. /* replace terminating end of line marker(s) with null */
  30. void rip(register char *s)
  31. {
  32.         register char *cp;
  33.  
  34.         if((cp = strchr(s,'\r')) != NULLCHAR)
  35.                 *cp = '\0';
  36.         if((cp = strchr(s,'\n')) != NULLCHAR)
  37.                 *cp = '\0';
  38. }
  39.  
  40. /* Case-insensitive string comparison */
  41. int strnicmp(register char *a, register char *b, register int n)
  42. {
  43.         char a1, b1;
  44.  
  45.         while (n-- != 0 && (a1 = *a++) != '\0' && (b1 = *b++) != '\0')
  46.         {
  47.                 if (a1 == b1)
  48.                         continue;       /* No need to convert */
  49.                 a1 = tolower(a1);
  50.                 b1 = tolower(b1);
  51.                 if (a1 == b1)
  52.                         continue;       /* Now they match */
  53.                 if (a1 > b1)
  54.                         return(1);
  55.                 if (a1 < b1)
  56.                         return(-1);
  57.         }
  58.  
  59.         return(0);
  60. }
  61. /* Copy a string into a dynamically allocated piece of memory */
  62. char *strdup(char *s)
  63. {
  64.         char *p;
  65.  
  66.         if ((p = malloc(strlen(s) + 1)) == NULL)
  67.                 return(NULL);
  68.  
  69.         strcpy(p, s);
  70.  
  71.         return(p);
  72. }
  73. /* Put a long in host order into a char array in network order */
  74. char *put32(register char *cp, int32 x)
  75. {
  76.         *cp++ = uchar(x >> 24);
  77.         *cp++ = uchar(x >> 16);
  78.         *cp++ = uchar(x >> 8);
  79.         *cp++ = uchar(x);
  80.  
  81.         return(cp);
  82. }
  83.  
  84. /* Put a short in host order into a char array in network order */
  85. char *put16(register char *cp, int16 x)
  86. {
  87.         *cp++ = uchar(x >> 8);
  88.         *cp++ = uchar(x);
  89.  
  90.         return(cp);
  91. }
  92.  
  93. /* Machine independant, alignment insensitive network-to-host short conversion */
  94. int16 get16(register char *cp)
  95. {
  96.         register int16 x;
  97.  
  98.         x = uchar(*cp++);
  99.         x <<= 8;
  100.         x |= uchar(*cp);
  101.  
  102.         return(x);
  103. }
  104.  
  105. /* Machine independant, alignment insensitive network-to-host short conversion */
  106. int32 get32(register char *cp)
  107. {
  108.         register int32 x;
  109.  
  110.         x = uchar(*cp++);
  111.         x <<= 8;
  112.         x |= uchar(*cp++);
  113.         x <<= 8;
  114.         x |= uchar(*cp++);
  115.         x <<= 8;
  116.         x |= uchar(*cp);
  117.  
  118.         return(x);
  119. }
  120.