]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/lib/ZReadAscii.c
Initial revision
[1ts-debian.git] / zephyr / lib / ZReadAscii.c
1 /* This file is part of the Project Athena Zephyr Notification System.
2  * It contains source for the ZReadAscii function.
3  *
4  *      Created by:     Robert French
5  *
6  *      $Id: ZReadAscii.c,v 1.18 1999/01/22 23:19:21 ghudson Exp $
7  *
8  *      Copyright (c) 1987, 1990 by the Massachusetts Institute of Technology.
9  *      For copying and distribution information, see the file
10  *      "mit-copyright.h". 
11  */
12
13 #ifndef lint
14 static char rcsid_ZReadAscii_c[] = "$Id: ZReadAscii.c,v 1.18 1999/01/22 23:19:21 ghudson Exp $";
15 #endif /* lint */
16
17 #include <internal.h>
18 #include <assert.h>
19
20 #if 0
21 static __inline__
22 int
23 Z_cnvt_xtoi (char c)
24 {
25     c -= '0';
26     if (c < 10)
27         return c;
28     c -= 'A'-'9'-1;
29     if (c < 16)
30         return c;
31     return -1;
32 }
33 #endif
34
35 #define Z_cnvt_xtoi(c)  ((temp=(c)-'0'),(temp<10)?temp:((temp-='A'-'9'-1),(temp<16)?temp:-1))
36
37 Code_t ZReadAscii(ptr, len, field, num)
38     char *ptr;
39     int len;
40     unsigned char *field;
41     int num;
42 {
43     int i;
44     unsigned int hexbyte;
45     register int c1, c2;
46     register unsigned int temp;
47
48     for (i=0;i<num;i++) {
49         if (*ptr == ' ') {
50             ptr++;
51             if (--len < 0)
52                 return ZERR_BADFIELD;
53         } 
54         if (ptr[0] == '0' && ptr[1] == 'x') {
55             ptr += 2;
56             len -= 2;
57             if (len < 0)
58                 return ZERR_BADFIELD;
59         } 
60         c1 = Z_cnvt_xtoi(ptr[0]);
61         if (c1 < 0)
62                 return ZERR_BADFIELD;
63         c2 = Z_cnvt_xtoi(ptr[1]);
64         if (c2 < 0)
65                 return ZERR_BADFIELD;
66         hexbyte = (c1 << 4) | c2;
67         field[i] = hexbyte;
68         ptr += 2;
69         len -= 2;
70         if (len < 0)
71             return ZERR_BADFIELD;
72     }
73
74     return *ptr ? ZERR_BADFIELD : ZERR_NONE;
75 }
76
77 Code_t ZReadAscii32(ptr, len, value_ptr)
78     char *ptr;
79     int len;
80     unsigned long *value_ptr;
81 {
82     unsigned char buf[4];
83     Code_t retval;
84
85     retval = ZReadAscii(ptr, len, buf, 4);
86     if (retval != ZERR_NONE)
87         return retval;
88     *value_ptr = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
89     return ZERR_NONE;
90 }
91
92 Code_t ZReadAscii16(ptr, len, value_ptr)
93     char *ptr;
94     int len;
95     unsigned short *value_ptr;
96 {
97     unsigned char buf[2];
98     Code_t retval;
99
100     retval = ZReadAscii(ptr, len, buf, 2);
101     if (retval != ZERR_NONE)
102         return retval;
103     *value_ptr = (buf[0] << 8) | buf[1];
104     return ZERR_NONE;
105 }
106