00001
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <math.h>
00011 #include <ctype.h>
00012
00013 #include "ind_types.h"
00014
00015
00016
00017
00018
00019
00020 typedef int endtype;
00024
00025
00026
00027
00032 #define CHECK_TYPE(typename,expectedsize) \
00033 if (sizeof(typename) != expectedsize) { \
00034 printf("Warning -- problem with ind_types: sizeof(%3s)=%d, not %d.\n" \
00035 ,#typename,sizeof(typename),expectedsize); \
00036 errors++; \
00037 }
00038
00039
00040 #define WARN_MISSING(typename) if (warn_if_missing) printf("Note: no %s type defined.\n",#typename)
00041
00042
00043 int check_ind_types( int warn_if_missing )
00044 {
00045 int errors=0;
00046 (void)warn_if_missing;
00047
00048 #ifndef NO_I8
00049 CHECK_TYPE(i8,1);
00050 #else
00051 WARN_MISSING(i8);
00052 #endif
00053 #ifndef NO_I16
00054 CHECK_TYPE(i16,2);
00055 #else
00056 WARN_MISSING(i16);
00057 #endif
00058 #ifndef NO_I32
00059 CHECK_TYPE(i32,4);
00060 #else
00061 WARN_MISSING(i32);
00062 #endif
00063 #ifndef NO_I64
00064 CHECK_TYPE(i64,8);
00065 #else
00066 WARN_MISSING(i64);
00067 #endif
00068 #ifndef NO_F32
00069 CHECK_TYPE(f32,4);
00070 #else
00071 WARN_MISSING(f32);
00072 #endif
00073 #ifndef NO_F64
00074 CHECK_TYPE(f64,8);
00075 #else
00076 WARN_MISSING(f64);
00077 #endif
00078
00079 return errors;
00080 }
00081 #undef CHECK_TYPE
00082 #undef WARN_MISSING
00083
00084
00092 int endianness(void)
00093 {
00094 int little_so_far,big_so_far;
00095 unsigned i;
00096
00097 union {
00098 endtype s;
00099 char c[sizeof(endtype)];
00100 } endu;
00101
00102
00103 endu.s = 0;
00104 for (i=0; i < sizeof(endtype); i++)
00105 endu.s += (i+1) << 8*i;
00106
00107 #ifdef DEBUG
00108
00109
00110 printf ("Bytes ordered 0 to %d:",sizeof(endtype)-1);
00111 for (i=sizeof(endtype); i > 0; i--)
00112 printf(" %d",(int)endu.c[i-1]);
00113 printf("\n");
00114 #endif
00115
00116
00117 little_so_far = 1;
00118 for (i=0; i < sizeof(endtype); i++)
00119 if (little_so_far && ((unsigned)endu.c[i] != i+1))
00120 little_so_far = 0;
00121
00122 if (little_so_far) return 0;
00123
00124
00125 big_so_far = 1;
00126 for (i=0; i < sizeof(endtype); i++)
00127 if (big_so_far && ((unsigned)endu.c[i] != sizeof(endtype)-i))
00128 big_so_far = 0;
00129
00130 if (big_so_far) return 1;
00131
00132 return -1;
00133 }
00134
00135
00136
00138 #define swap_endian_proc(type) \
00139 type type ## _swap_endian(type num) \
00140 { \
00141 unsigned i; \
00142 union { \
00143 type num; \
00144 char c[sizeof(type)]; \
00145 } in,out; \
00146 \
00147 in.num = num; \
00148 for (i=0; i < sizeof(type); i++) \
00149 out.c[i] = in.c[sizeof(type)-1-i]; \
00150 return out.num; \
00151 }
00152 swap_endian_proc(i32)
00153 #ifndef NO_I64
00154 swap_endian_proc(i64)
00155 #endif
00156
00157
00158
00159
00160
00161
00162
00163
00164 #ifdef NO_STRNCASECMP_AVAIL
00165 EXTERNAL_C_LINKAGE int strncasecmp(const char *str1, const char *str2, size_t n)
00166 {
00167 for (; n > 0 ; n--, *str1++, str2++) {
00168 if (toupper(*str1) != toupper(*str2))
00169 return (toupper(*str1) - toupper(*str2));
00170
00171 if (*str1 == '\0') return 0;
00172 }
00173 return 0;
00174 }
00175 #endif
00176
00177
00178 #ifdef NO_SNPRINTF_AVAIL
00179 #include <stdarg.h>
00186 EXTERNAL_C_LINKAGE int snprintf( char *str, size_t n, const char *format, ...)
00187 {
00188 size_t chars;
00189 va_list args;
00190 va_start (args, format);
00191 chars=vsprintf (str, format, args);
00192 va_end (args);
00193 if (chars+1>n) {
00194 printf("Warning -- Data may be overwritten due to missing snprintf() function.\n");
00195 return -1;
00196 }
00197 return chars;
00198 }
00199 #endif
00200
00201
00202 #ifdef NO_TEMPNAM_AVAIL
00203 EXTERNAL_C_LINKAGE char *tempnam(const char *dir, const char *pfx)
00204 {
00205 static int timescalled=0;
00206 char* str = (char *)malloc(32);
00207 if (pfx) {
00208 snprintf(str,32,"%5s%06d",pfx,timescalled);
00209 }
00210 else
00211 snprintf(str,32,"temp_%06d",timescalled);
00212
00213 timescalled++;
00214 return str;
00215 }
00216 #endif
00217
00218
00219 #ifdef NO_DRAND48_AVAIL
00220
00226 EXTERNAL_C_LINKAGE double drand48(void)
00227 { return (rand()/(RAND_MAX+1.0)); }
00228 #endif
00229
00230
00231
00232 #ifdef NO_SRAND48_AVAIL
00233 EXTERNAL_C_LINKAGE void srand48(long int seedval)
00234 { srand(seedval); }
00235 #endif
00236
00237
00238 #ifdef NO_HYPOT_AVAIL
00239 EXTERNAL_C_LINKAGE double hypot(double x, double y)
00240 { return sqrt(x*x+y*y); }
00241 #endif
00242
00243