23 FILE * LogFile=NULL;
\r
38 // switch file buffering off
\r
41 setbuf(stdout,NULL);
\r
46 void my_random_init() {
\r
52 int my_random_int(int n) {
\r
58 r = int(floor(my_random_double()*double(n)));
\r
64 // my_random_double()
\r
66 double my_random_double() {
\r
70 r = double(rand()) / (double(RAND_MAX) + 1.0);
\r
71 ASSERT(r>=0.0&&r<1.0);
\r
78 sint64 my_atoll(const char string[]) {
\r
82 sscanf(string,S64_FORMAT,&n);
\r
89 int my_round(double x) {
\r
91 return int(floor(x+0.5));
\r
96 void * my_malloc(size_t size) {
\r
102 address = malloc(size);
\r
103 if (address == NULL) my_fatal("my_malloc(): malloc(): %s\n",strerror(errno));
\r
110 void * my_realloc(void * address, size_t size) {
\r
112 ASSERT(address!=NULL);
\r
115 address = realloc(address,size);
\r
116 if (address == NULL) my_fatal("my_realloc(): realloc(): %s\n",strerror(errno));
\r
123 void my_free(void * address) {
\r
125 ASSERT(address!=NULL);
\r
132 void my_log_open(const char file_name[]) {
\r
134 ASSERT(file_name!=NULL);
\r
136 LogFile = fopen(file_name,"a");
\r
138 //line buffering doesn't work too well in MSVC and/or windows
\r
139 if (LogFile != NULL) setvbuf(LogFile,NULL,_IOLBF,0); // line buffering
\r
145 void my_log_close() {
\r
147 if (LogFile != NULL) fclose(LogFile);
\r
153 void my_log(const char format[], ...) {
\r
157 ASSERT(format!=NULL);
\r
159 if (LogFile != NULL) {
\r
160 fprintf(LogFile,"%.3f ",now_real());
\r
161 va_start(ap,format);
\r
163 vfprintf(LogFile,format,ap);
\r
173 void my_fatal(const char format[], ...) {
\r
177 ASSERT(format!=NULL);
\r
179 va_start(ap,format);
\r
181 vfprintf(stderr,format,ap);
\r
182 if (LogFile != NULL) vfprintf(LogFile,format,ap);
\r
185 if (Error) { // recursive error
\r
186 my_log("POLYGLOT *** RECURSIVE ERROR ***\n");
\r
187 exit(EXIT_FAILURE);
\r
195 // my_file_read_line()
\r
197 bool my_file_read_line(FILE * file, char string[], int size) {
\r
202 ASSERT(file!=NULL);
\r
203 ASSERT(string!=NULL);
\r
206 if (fgets(string,size,file) == NULL) {
\r
210 my_fatal("my_file_read_line(): fgets(): %s\n",strerror(errno));
\r
214 // remove CRs and LFs
\r
219 while ((c=string[src++]) != '\0') {
\r
220 if (c != '\r' && c != '\n') string[dst++] = c;
\r
223 string[dst] = '\0';
\r
228 // my_string_empty()
\r
230 bool my_string_empty(const char string[]) {
\r
232 return string == NULL || string[0] == '\0';
\r
235 // my_string_whitespace()
\r
237 bool my_string_whitespace(const char string[]){
\r
239 while(string[pos]!='\0'){
\r
240 if(string[pos]!=' ' && string[pos]!='\t'){
\r
248 // my_string_equal()
\r
250 bool my_string_equal(const char string_1[], const char string_2[]) {
\r
252 ASSERT(string_1!=NULL);
\r
253 ASSERT(string_2!=NULL);
\r
255 return strcmp(string_1,string_2) == 0;
\r
258 // my_string_case_equal()
\r
260 bool my_string_case_equal(const char string_1[], const char string_2[]) {
\r
264 ASSERT(string_1!=NULL);
\r
265 ASSERT(string_2!=NULL);
\r
272 if (tolower(c1) != tolower(c2)) return false;
\r
273 if (c1 == '\0') return true;
\r
281 char * my_strdup(const char string[]) {
\r
285 ASSERT(string!=NULL);
\r
287 // strdup() is not ANSI C
\r
289 address = (char *) my_malloc(strlen(string)+1);
\r
290 strcpy(address,string);
\r
295 // my_string_clear()
\r
297 void my_string_clear(const char * * variable) {
\r
299 ASSERT(variable!=NULL);
\r
301 if (*variable != NULL) {
\r
302 my_free((void*)(*variable));
\r
309 void my_string_set(const char * * variable, const char string[]) {
\r
311 ASSERT(variable!=NULL);
\r
312 ASSERT(string!=NULL);
\r
314 if (*variable != NULL) my_free((void*)(*variable));
\r
315 *variable = my_strdup(string);
\r
318 // my_timer_reset()
\r
320 void my_timer_reset(my_timer_t * timer) {
\r
322 ASSERT(timer!=NULL);
\r
324 timer->start_real = 0.0;
\r
325 timer->elapsed_real = 0.0;
\r
326 timer->running = false;
\r
329 // my_timer_start()
\r
331 void my_timer_start(my_timer_t * timer) {
\r
332 // timer->start_real = 0.0;
\r
333 timer->elapsed_real = 0.0;
\r
334 // timer->running = false;
\r
335 ASSERT(timer!=NULL);
\r
337 timer->running = true;
\r
338 timer->start_real = now_real();
\r
343 void my_timer_stop(my_timer_t * timer) {
\r
345 ASSERT(timer!=NULL);
\r
347 ASSERT(timer->running);
\r
349 timer->elapsed_real += now_real() - timer->start_real;
\r
350 timer->start_real = 0.0;
\r
351 timer->running = false;
\r
354 // my_timer_elapsed_real()
\r
356 double my_timer_elapsed_real(const my_timer_t * timer) {
\r
360 ASSERT(timer!=NULL);
\r
362 elapsed = timer->elapsed_real;
\r
363 if (timer->running) elapsed += now_real() - timer->start_real;
\r
365 if (elapsed < 0.0) elapsed = 0.0;
\r