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
152 void my_log(const char format[], ...) {
\r
156 ASSERT(format!=NULL);
\r
158 if (LogFile != NULL) {
\r
159 fprintf(LogFile,"%.3f ",now_real());
\r
160 va_start(ap,format);
\r
162 vfprintf(LogFile,format,ap);
\r
172 void my_fatal(const char format[], ...) {
\r
176 ASSERT(format!=NULL);
\r
178 va_start(ap,format);
\r
180 vfprintf(stderr,format,ap);
\r
181 if (LogFile != NULL) vfprintf(LogFile,format,ap);
\r
184 if (Error) { // recursive error
\r
185 my_log("POLYGLOT *** RECURSIVE ERROR ***\n");
\r
186 exit(EXIT_FAILURE);
\r
194 // my_file_read_line()
\r
196 bool my_file_read_line(FILE * file, char string[], int size) {
\r
201 ASSERT(file!=NULL);
\r
202 ASSERT(string!=NULL);
\r
205 if (fgets(string,size,file) == NULL) {
\r
209 my_fatal("my_file_read_line(): fgets(): %s\n",strerror(errno));
\r
213 // remove CRs and LFs
\r
218 while ((c=string[src++]) != '\0') {
\r
219 if (c != '\r' && c != '\n') string[dst++] = c;
\r
222 string[dst] = '\0';
\r
227 // my_string_empty()
\r
229 bool my_string_empty(const char string[]) {
\r
231 return string == NULL || string[0] == '\0';
\r
234 // my_string_whitespace()
\r
236 bool my_string_whitespace(const char string[]){
\r
238 while(string[pos]!='\0'){
\r
239 if(string[pos]!=' ' && string[pos]!='\t'){
\r
247 // my_string_equal()
\r
249 bool my_string_equal(const char string_1[], const char string_2[]) {
\r
251 ASSERT(string_1!=NULL);
\r
252 ASSERT(string_2!=NULL);
\r
254 return strcmp(string_1,string_2) == 0;
\r
257 // my_string_case_equal()
\r
259 bool my_string_case_equal(const char string_1[], const char string_2[]) {
\r
263 ASSERT(string_1!=NULL);
\r
264 ASSERT(string_2!=NULL);
\r
271 if (tolower(c1) != tolower(c2)) return false;
\r
272 if (c1 == '\0') return true;
\r
280 char * my_strdup(const char string[]) {
\r
284 ASSERT(string!=NULL);
\r
286 // strdup() is not ANSI C
\r
288 address = (char *) my_malloc(strlen(string)+1);
\r
289 strcpy(address,string);
\r
294 // my_string_clear()
\r
296 void my_string_clear(const char * * variable) {
\r
298 ASSERT(variable!=NULL);
\r
300 if (*variable != NULL) {
\r
301 my_free((void*)(*variable));
\r
308 void my_string_set(const char * * variable, const char string[]) {
\r
310 ASSERT(variable!=NULL);
\r
311 ASSERT(string!=NULL);
\r
313 if (*variable != NULL) my_free((void*)(*variable));
\r
314 *variable = my_strdup(string);
\r
317 // my_timer_reset()
\r
319 void my_timer_reset(my_timer_t * timer) {
\r
321 ASSERT(timer!=NULL);
\r
323 timer->start_real = 0.0;
\r
324 timer->elapsed_real = 0.0;
\r
325 timer->running = false;
\r
328 // my_timer_start()
\r
330 void my_timer_start(my_timer_t * timer) {
\r
331 // timer->start_real = 0.0;
\r
332 timer->elapsed_real = 0.0;
\r
333 // timer->running = false;
\r
334 ASSERT(timer!=NULL);
\r
336 timer->running = true;
\r
337 timer->start_real = now_real();
\r
342 void my_timer_stop(my_timer_t * timer) {
\r
344 ASSERT(timer!=NULL);
\r
346 ASSERT(timer->running);
\r
348 timer->elapsed_real += now_real() - timer->start_real;
\r
349 timer->start_real = 0.0;
\r
350 timer->running = false;
\r
353 // my_timer_elapsed_real()
\r
355 double my_timer_elapsed_real(const my_timer_t * timer) {
\r
359 ASSERT(timer!=NULL);
\r
361 elapsed = timer->elapsed_real;
\r
362 if (timer->running) elapsed += now_real() - timer->start_real;
\r
364 if (elapsed < 0.0) elapsed = 0.0;
\r