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,int(sizeof(typename)),int(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 #if (defined(NO_SNPRINTF_AVAIL) || defined(NO_TEMPNAM_AVAIL))
00165 #include <stdarg.h>
00172 EXTERNAL_C_LINKAGE int my_snprintf( char *str, size_t n, const char *format, ...)
00173 {
00174 size_t chars;
00175 va_list args;
00176 va_start (args, format);
00177 chars=vsprintf (str, format, args);
00178 va_end (args);
00179 if (chars+1>n) {
00180 printf("Warning -- Data may be overwritten due to missing snprintf() function.\n");
00181 return -1;
00182 }
00183 return chars;
00184 }
00185 #endif
00186
00187 #ifdef NO_TEMPNAM_AVAIL
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 EXTERNAL_C_LINKAGE char *my_tempnam(const char *dir, const char *pfx)
00199 {
00200 (void)dir;
00201 static int timescalled=0;
00202 const char* default_prefix="temp_";
00203 char* str = (char *)malloc(32);
00204 my_snprintf(str,32,"%5s%06d",(pfx? pfx : default_prefix),timescalled);
00205
00206 timescalled++;
00207 return str;
00208 }
00209 #endif
00210