Added an icon with c:iconx as default tools.
[AROS-Contrib.git] / regina / misc.c
blob86adbff2ec5b638fbcb043c04e35fbb00e1d3de7
1 #ifndef lint
2 static char *RCSid = "$Id$";
3 #endif
5 /*
6 * The Regina Rexx Interpreter
7 * Copyright (C) 1992-1994 Anders Christensen <anders@pvv.unit.no>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the Free
21 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "rexx.h"
25 #include <ctype.h>
26 /* Do _not_ use stddef, anders! _not_ stddef! Remember that!!!
27 (it breaks on suns running gcc without fixincludes */
28 #include <stdio.h>
29 #include <limits.h>
30 #if defined(HAVE_ASSERT_H)
31 # include <assert.h>
32 #endif
34 #if defined(TIME_WITH_SYS_TIME)
35 # include <sys/time.h>
36 # include <time.h>
37 #else
38 # if defined(HAVE_SYS_TIME_H)
39 # include <sys/time.h>
40 # else
41 # include <time.h>
42 # endif
43 #endif
45 #if defined(_AMIGA) && !defined(__AROS__)
46 const char *Version="$VER: "PARSE_VERSION_STRING" "__AMIGADATE__" $";
47 # if defined(__SASC)
48 static const long __stack = 0x6000;
49 # endif
50 #endif
52 const unsigned char u_to_l[256] =
54 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
55 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
56 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
57 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
58 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
59 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
60 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
61 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
62 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
63 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
64 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
65 0x78, 0x79, 0x7a, 0x7b, 0x5c, 0x5d, 0x5e, 0x5f,
66 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
67 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
68 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
69 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
70 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
71 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
72 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
73 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
74 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
75 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
76 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
77 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
78 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
79 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
80 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
81 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
82 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
83 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
84 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
85 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
86 } ;
90 /* FIXME, FGC: Nice idea but what happens in case of EBCDIC, etc? */
91 streng *upcase( streng *source )
93 int i=0 ;
95 for (i=0;i<Str_len(source);i++)
96 if ((source->value[i]<=LAST_CHAR+CHAR_OFFSET)&&(source->value[i]>=FIRST_CHAR+CHAR_OFFSET))
97 source->value[i] -= CHAR_OFFSET ;
99 return source ;
103 void getsecs( time_t *secs, time_t *usecs )
105 #if defined(HAVE_GETTIMEOFDAY)
106 struct timeval times ;
108 gettimeofday(&times, NULL) ;
109 *secs = times.tv_sec ;
110 *usecs = times.tv_usec ;
112 if (times.tv_usec < 0)
114 *usecs = (times.tv_usec + 1000000) ;
115 *secs = times.tv_sec - 1 ;
117 assert( *secs>=0 && *usecs>=0 ) ;
119 #elif defined(HAVE_FTIME)
120 struct timeb timebuffer;
122 ftime(&timebuffer);
123 *secs = timebuffer.time;
124 *usecs = timebuffer.millitm * 1000;
125 assert( *secs>=0 && *usecs>=0 ) ;
126 #else
127 *secs = time(NULL) ;
128 *usecs = 0 ;
129 assert( *secs>=0 && *usecs>=0 ) ;
130 #endif
134 const char *system_type( void )
136 #if defined(VMS)
137 return "VMS" ;
138 #elif defined(OS2)
139 return "OS/2" ;
140 #elif defined(DOS)
141 # if defined(__EMX__)
142 if (_osmode == DOS_MODE)
143 return("DOS");
144 else
145 return("OS/2");
146 #else
147 return("DOS");
148 # endif
149 #elif defined(WIN32)
150 return "WIN32" ;
151 #elif defined(_AMIGA) || defined(AMIGA)
152 return "AMIGA" ;
153 #elif defined(__AROS__)
154 return "AROS" ;
155 #elif defined(__QNX__)
156 return "QNX" ;
157 #elif defined(__BEOS__)
158 return "BEOS" ;
159 #elif defined(__CYGWIN__)
160 return "WIN32" ;
161 #elif defined(__WINS__)
162 return "EPOC32-WINS" ;
163 #elif defined(__EPOC32__)
164 return "EPOC32-MARM" ;
165 #else
166 return "UNIX" ;
167 #endif
171 #if !defined(HAVE_STRERROR)
173 * Sigh! This must probably be done this way, although it's incredibly
174 * backwards. Some versions of gcc comes with a complete set of ANSI C
175 * include files, which contains the definition of strerror(). However,
176 * that function does not exist in the default libraries of SunOS.
177 * To circumvent that problem, strerror() is #define'd to get_sys_errlist()
178 * in config.h, and here follows the definition of that function.
179 * Originally, strerror() was #defined to sys_errlist[x], but that does
180 * not work if string.h contains a declaration of the (non-existing)
181 * function strerror().
183 * So, this is a mismatch between the include files and the library, and
184 * it should not create problems for Regina. However, the _user_ will not
185 * encounter any problems until he compiles Regina, so we'll have to
186 * clean up after a buggy installation of the C compiler!
188 const char *get_sys_errlist( int num )
190 extern char *sys_errlist[] ;
191 return sys_errlist[num] ;
193 #endif
196 double cpu_time( void )
198 #ifndef CLOCKS_PER_SEC
200 * Lots of systems don't seem to get this ANSI C piece of code correctly
201 * but most of them seems to use one million ... Using a million for
202 * those systems that haven't defined CLOCKS_PER_SEC may give an incorrect
203 * value if clock() does not return microseconds!
205 # define CLOCKS_PER_SEC 1000000
206 #endif
207 return ((double)(clock()))/((double)(CLOCKS_PER_SEC)) ;
210 /* HIGHBIT is an unsigned with the highest bit set */
211 #define HIGHBIT (((unsigned) 1) << ((sizeof(unsigned) * CHAR_BIT) - 1))
213 /* hashvalue computes a value for hashing from a string content. Use
214 * hashvalue_ic for a case insensitive version.
215 * length may less than 0. The string length is computed by strlen() in this
216 * case.
217 * The return value might be modified by the %-operator for real hash
218 * values.
220 unsigned hashvalue(const char *string, int length)
222 unsigned retval = 0, wrap ;
223 const unsigned char *ptr = (const unsigned char *) string; /* unsigned char makes it fast */
224 unsigned char c;
226 if (length < 0)
227 length = strlen(string);
229 while (length--) {
230 c = *ptr++; /* Yes this is slower but believe in your optimizer! */
231 retval ^= c;
232 wrap = (retval & HIGHBIT) ? 1 : 0;
233 retval <<= 1;
234 retval |= wrap;
237 return(retval);
240 /* hashvalue_ic computes a value for hashing from a string content. Use
241 * hashvalue for a case significant version. This is case insensitive.
242 * length may less than 0. The string length is computed by strlen() in this
243 * case.
244 * The return value might be modified by the %-operator for real hash
245 * values.
247 unsigned hashvalue_ic(const char *string, int length)
249 unsigned retval = 0, wrap ;
250 const unsigned char *ptr = (const unsigned char *) string; /* unsigned char makes it fast */
251 unsigned char c;
253 if (length < 0)
254 length = strlen(string);
256 while (length--) {
257 c = RXTOLOW(*ptr);
258 ptr++;
259 retval ^= c;
260 wrap = (retval & HIGHBIT) ? 1 : 0;
261 retval <<= 1;
262 retval |= wrap;
265 return(retval);