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