Wrapper for Clustal Omega.
[jabaws.git] / binaries / src / clustalo / src / squid / types.c
1 /*****************************************************************
2  * SQUID - a library of functions for biological sequence analysis
3  * Copyright (C) 1992-2002 Washington University School of Medicine
4  * 
5  *     This source code is freely distributed under the terms of the
6  *     GNU General Public License. See the files COPYRIGHT and LICENSE
7  *     for details.
8  *****************************************************************/
9
10 /* file: types.c
11  * 
12  * Finicky type checkers for strings. Return 1 (TRUE) if ok, 0 elsewise.
13  * Also, finicky type converters (sre_ntoh32() and friends)
14  *
15  * CVS $Id: types.c,v 1.5 2001/01/08 22:58:12 eddy Exp)
16  */
17
18 #include <string.h>
19 #include <ctype.h>
20 #include "squid.h"
21
22 /* Function: IsInt()
23  * 
24  * Returns TRUE if s points to something that atoi() will parse
25  * completely and convert to an integer.
26  */
27 int
28 IsInt(char *s)
29 {
30   int hex = 0;
31
32   if (s == NULL) {squid_errno = SQERR_PARAMETER; return 0; }
33
34                                 /* skip whitespace */
35   while (isspace((int) (*s))) s++;      
36                                 /* skip leading sign */
37   if (*s == '-' || *s == '+') s++;
38                                 /* skip leading conversion signals */
39   if ((strncmp(s, "0x", 2) == 0 && (int) strlen(s) > 2) ||
40       (strncmp(s, "0X", 2) == 0 && (int) strlen(s) > 2))
41     {
42       s += 2;
43       hex = 1;
44     }
45   else if (*s == '0' && (int) strlen(s) > 1)
46     s++;
47                                 /* examine remainder for garbage chars */
48   if (!hex)
49     while (*s != '\0')
50       {
51         if (!isdigit((int) (*s))) return 0;
52         s++;
53       }
54   else
55     while (*s != '\0')
56       {
57         if (!isxdigit((int) (*s))) return 0;
58         s++;
59       }
60
61   return 1;
62 }
63
64
65 /* Function: IsReal()
66  * 
67  * Purpose:  Returns TRUE if s is a string representation
68  *           of a valid floating point number.
69  */
70 int
71 IsReal(char *s)
72 {
73   int gotdecimal = 0;
74   int gotexp     = 0;
75   int gotreal    = 0;
76
77   if (s == NULL) return 0;
78
79   while (isspace((int) (*s))) s++;         /* skip leading whitespace */
80   if (*s == '-' || *s == '+') s++; /* skip leading sign */
81
82   /* Examine remainder for garbage. Allowed one '.' and
83    * one 'e' or 'E'; if both '.' and e/E occur, '.'
84    * must be first.
85    */
86   while (*s != '\0')
87     {
88       if (isdigit((int) (*s))) 
89         gotreal++;
90       else if (*s == '.')
91         {
92           if (gotdecimal) return 0; /* can't have two */
93           if (gotexp) return 0; /* e/E preceded . */
94           else gotdecimal++;
95         }
96       else if (*s == 'e' || *s == 'E')
97         {
98           if (gotexp) return 0; /* can't have two */
99           else gotexp++;
100         }
101       else if (isspace((int) (*s)))
102         break;
103
104       s++;
105     }
106
107   while (isspace((int) (*s))) s++;         /* skip trailing whitespace */
108   if (*s == '\0' && gotreal) return 1;
109   else return 0;
110 }
111
112
113 /* Function: Byteswap()
114  * 
115  * Purpose:  Swap between big-endian and little-endian.
116  *           For example:
117  *               int foo = 0x12345678;
118  *               byteswap((char *) &foo, sizeof(int));
119  *               printf("%x\n", foo)
120  *           gives 78563412.
121  *           
122  *           I don't fully understand byte-swapping issues.
123  *           However, I have tested this on chars through floats,
124  *           on various machines:
125  *               SGI IRIX 4.0.5, SunOS 4.1.3, DEC Alpha OSF/1, Alliant
126  *
127  * Date: Sun Feb 12 10:26:22 1995              
128  */
129 void
130 Byteswap(char *swap, int nbytes)
131 {
132   int  x;
133   char byte;
134   
135   for (x = 0; x < nbytes / 2; x++)
136     {
137       byte = swap[nbytes - x - 1];
138       swap[nbytes - x - 1] = swap[x];
139       swap[x] = byte;
140     }
141 }
142
143
144
145 /* Functions: sre_ntoh16(), etc.
146  * Date:      SRE, Sun Dec 31 11:26:53 2000 [St. Louis]
147  *
148  * Purpose:   Provide functionality of ntohs(), etc; extended
149  *            to 64-bit unsigned ints, and explicitly provided
150  *            in case a machine doesn't have the ntohs()
151  *            family. 
152  *            
153  *            If we're using the host functions, 
154  *            USE_HOST_BYTESWAP_FUNCTIONS was set to 1 in
155  *            squidconf.h, and we #define'd sre_hton16(x)=hton(x), etc.
156  *            in squid.h. In doing this, we assumed that the
157  *            host functions work on 16- and 32-bit unsigned quantities.
158  *            If for some reason that's not true, set 
159  *            USE_HOST_BYTESWAP_FUNCTIONS to 0.
160  */
161 #ifndef USE_HOST_BYTESWAP_FUNCTIONS
162 sqd_uint16
163 sre_ntoh16(sqd_uint16 netshort)
164 {
165 #ifdef WORDS_BIGENDIAN
166   return netshort;
167 #else
168   Byteswap((char *) &netshort, 2);
169   return netshort;
170 #endif
171 }
172 sqd_uint32
173 sre_ntoh32(sqd_uint32 netlong)
174 {
175 #ifdef WORDS_BIGENDIAN
176   return netlong;
177 #else
178   Byteswap((char *) &netlong, 4);
179   return netlong;
180 #endif
181 }
182 sqd_uint16
183 sre_hton16(sqd_uint16 hostshort)
184 {
185 #ifdef WORDS_BIGENDIAN
186   return hostshort;
187 #else
188   Byteswap((char *) &hostshort, 2);
189   return hostshort;
190 #endif
191 }
192 sqd_uint32
193 sre_hton32(sqd_uint32 hostlong)
194 {
195 #ifdef WORDS_BIGENDIAN
196   return hostlong;
197 #else
198   Byteswap((char *) &hostlong, 4);
199   return hostlong;
200 #endif
201 }
202 #endif /*USE_HOST_BYTESWAP_FUNCTIONS*/
203
204 sqd_uint64
205 sre_ntoh64(sqd_uint64 net_int64)
206 {
207 #ifdef WORDS_BIGENDIAN
208   return net_int64;
209 #else
210   Byteswap((char *) &net_int64, 8);
211   return net_int64;
212 #endif
213 }
214 sqd_uint64
215 sre_hton64(sqd_uint64 host_int64)
216 {
217 #ifdef WORDS_BIGENDIAN
218   return host_int64;
219 #else
220   Byteswap((char *) &host_int64, 8);
221   return host_int64;
222 #endif
223 }
224
225
226
227