Add forgotten files 1.4.70b
[polyglot.git] / util.h
1
2 // util.h
3
4 #ifndef UTIL_H
5 #define UTIL_H
6
7 // includes
8
9 #include <stdio.h>
10 #include <sys/types.h>
11 #include <sys/timeb.h>
12 #include <sys/stat.h>
13
14 // defines
15
16 #ifndef EXIT_SUCCES
17 #define EXIT_SUCCES 0
18 #endif
19
20 #ifndef STDIN_FILENO
21 #define STDIN_FILENO 0
22 #endif
23
24 #ifndef STDOUT_FILENO
25 #define STDOUT_FILENO 1
26 #endif
27
28
29 #undef FALSE
30 #define FALSE 0
31
32 #undef TRUE
33 #define TRUE 1
34
35 #ifdef DEBUG
36 #  undef DEBUG
37 #  define DEBUG TRUE
38 #else
39 #  define DEBUG FALSE
40 #endif
41
42 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
43 #  define S64_FORMAT "%I64d"
44 #  define U64_FORMAT "%016I64X"
45 #else
46 #  define S64_FORMAT "%lld"
47 #  define U64_FORMAT "%016llX"
48 #endif
49
50 // macros
51
52 #ifdef _MSC_VER
53 #  define S64(u) (u##i64)
54 #  define U64(u) (u##ui64)
55 #else
56 #  define S64(u) (u##LL)
57 #  define U64(u) (u##ULL)
58 #endif
59
60 #undef ASSERT
61 #if DEBUG
62 #  define ASSERT(a) { if (!(a)) my_fatal("file \"%s\", line %d, assertion \"" #a "\" failed\n",__FILE__,__LINE__); }
63 #else
64 #  define ASSERT(a)
65 #endif
66
67 #ifdef _WIN32
68 #define snprintf _snprintf
69 #endif
70
71 #define FormatBufferSize 4096
72 #define StringSize       4096
73
74 #ifdef _MSC_VER
75 #define vsnprintf _vsnprintf
76 #endif
77
78 #define CONSTRUCT_ARG_STRING(format,buf)                                 \
79     {                                                                    \
80         va_list arg_list;                                                \
81         int written;                                                     \
82         va_start(arg_list,format);                                       \
83         written=vsnprintf(buf,                                           \
84                           sizeof(buf),                                   \
85                           format,                                        \
86                           arg_list);                                     \
87         va_end(arg_list);                                                \
88         buf[sizeof(buf)-1]='\0';                                         \
89         if(written>=sizeof(buf) || written<0){                           \
90            my_fatal("write_buffer overflow: file \"%s\", line %d\n",     \
91                    __FILE__,__LINE__);                                   \
92         }                                                                \
93     }                                                                    \
94
95 #define TO_BOOL(string) ((my_string_case_equal(string,"false") ||   \
96                           my_string_equal(string,"0"))?FALSE:TRUE)
97
98 #define IS_BOOL(string) (my_string_case_equal(string,"false")||     \
99                          my_string_case_equal(string,"true") ||     \
100                          my_string_case_equal(string,"1")    ||     \
101                          my_string_case_equal(string,"0"))
102 // types
103
104 typedef signed char sint8;
105 typedef unsigned char uint8;
106
107 typedef signed short sint16;
108 typedef unsigned short uint16;
109
110 typedef signed int sint32;
111 typedef unsigned int uint32;
112
113 typedef int bool;
114
115 #ifdef _MSC_VER
116   typedef signed __int64 sint64;
117   typedef unsigned __int64 uint64;
118 #else
119   typedef signed long long int sint64;
120   typedef unsigned long long int uint64;
121 #endif
122
123 typedef struct {
124    double start_real;
125    double elapsed_real;
126    bool running;
127 } my_timer_t;
128
129
130 // functions
131
132 extern void   util_init             ();
133
134 extern void   my_random_init        ();
135 extern int    my_random_int         (int n);
136 extern double my_random_double      ();
137
138 extern sint64 my_atoll              (const char string[]);
139
140 extern int    my_round              (double x);
141
142 extern void * my_malloc             (size_t size);
143 extern void * my_realloc            (void * address, size_t size);
144 extern void   my_free               (void * address);
145
146 extern void   my_log_open           (const char file_name[]);
147 extern void   my_log_close          ();
148
149 extern void   my_log                (const char format[], ...);
150 extern void   my_fatal              (const char format[], ...);
151
152 extern bool   my_file_read_line     (FILE * file, char string[], int size);
153 extern void   my_path_join          (char *join_path, const char *path, const char *file);
154
155 extern int    my_mkdir              (const char *path);
156
157 extern bool   my_string_empty       (const char string[]);
158 extern bool   my_string_whitespace  (const char string[]);
159 extern bool   my_string_equal       (const char string_1[], const char string_2[]);
160 extern bool   my_string_case_equal  (const char string_1[], const char string_2[]);
161 extern const char* my_string_case_contains(const char haystack[], 
162                                            const char needle[]);
163
164
165 extern bool   my_string_to_lower    (char dst[], const char src[]);
166
167 extern char * my_strdup             (const char string[]);
168
169 extern void   my_string_clear       (const char * * variable);
170 extern void   my_string_set         (const char * * variable, const char string[]);
171
172 extern double now_real              ();
173
174 extern void   my_timer_reset        (my_timer_t * timer);
175 extern void   my_timer_start        (my_timer_t * timer);
176 extern void   my_timer_stop         (my_timer_t * timer);
177
178 extern double my_timer_elapsed_real (const my_timer_t * timer);
179
180 extern char * my_error();
181
182 extern void my_dequote              (char *out, 
183                                      const char *in, 
184                                      const char *special);
185 extern void my_quote                (char *out, 
186                                      const char *in, 
187                                      const char *special);
188
189 extern void my_sleep                (int msec);
190
191 #endif // !defined UTIL_H
192
193 // end of util.h