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