]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/lib/ZReadAscii.c
53037cb86428bae165232a6b8f6488b612cd6866
[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$
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$";
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 (len >= 1 && *ptr == ' ') {
50             ptr++;
51             len--;
52         }
53         if (len >= 2 && ptr[0] == '0' && ptr[1] == 'x') {
54             ptr += 2;
55             len -= 2;
56         }
57         if (len < 2)
58             return ZERR_BADFIELD;
59         c1 = Z_cnvt_xtoi(ptr[0]);
60         if (c1 < 0)
61                 return ZERR_BADFIELD;
62         c2 = Z_cnvt_xtoi(ptr[1]);
63         if (c2 < 0)
64                 return ZERR_BADFIELD;
65         hexbyte = (c1 << 4) | c2;
66         field[i] = hexbyte;
67         ptr += 2;
68         len -= 2;
69     }
70
71     return *ptr ? ZERR_BADFIELD : ZERR_NONE;
72 }
73
74 Code_t ZReadAscii32(ptr, len, value_ptr)
75     char *ptr;
76     int len;
77     unsigned long *value_ptr;
78 {
79     unsigned char buf[4];
80     Code_t retval;
81
82     retval = ZReadAscii(ptr, len, buf, 4);
83     if (retval != ZERR_NONE)
84         return retval;
85     *value_ptr = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
86     return ZERR_NONE;
87 }
88
89 Code_t ZReadAscii16(ptr, len, value_ptr)
90     char *ptr;
91     int len;
92     unsigned short *value_ptr;
93 {
94     unsigned char buf[2];
95     Code_t retval;
96
97     retval = ZReadAscii(ptr, len, buf, 2);
98     if (retval != ZERR_NONE)
99         return retval;
100     *value_ptr = (buf[0] << 8) | buf[1];
101     return ZERR_NONE;
102 }
103