per test
[rattatechess.git] / utils.h
blobfce6704dc4834ce553a43f57f259bb7d3b2744d4
1 /***************************************************************************
2 utils.h - some utility funtions
3 -------------------
4 begin : lun ott 24 2005
5 copyright : (C) 2005-2007 by Maurizio Monge
6 email : monge@linuz.sns.it
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18 #ifndef __UTILS_H__
19 #define __UTILS_H__
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <inttypes.h>
24 #include <errno.h>
25 #include "platform.h"
26 #include "compiler.h"
27 #include "array.h"
29 #define SWITCH(a, b) ({ typeof(a) tmp = b; b = a; a = tmp; })
30 #define MAX(a,b) ({ typeof(a) _a=(a); typeof(b) _b=(b); _a > _b ? _a : _b; })
31 #define MIN(a,b) ({ typeof(a) _a=(a); typeof(b) _b=(b); _a < _b ? _a : _b; })
32 #define ABS(a) ({ typeof(a) _a=(a); _a > 0 ? _a : -_a; })
34 #ifdef __x86_64__
35 #define U64F "%lu"
36 #else
37 #define U64F "%llu"
38 #endif
40 #define PROF(f,n) {uint64_t tmp = rdtsc(); f; prof_##n += rdtsc()-tmp; }
42 template<typename T, int S>
43 class Buf
45 private:
46 T m_data[S];
48 public:
49 T *data() { return m_data; }
50 Buf() {}
51 Buf(const char* fmt, ...)
53 va_list ap;
54 va_start(ap, fmt);
55 vsnprintf(m_data, S, fmt, ap);
56 va_end(ap);
60 typedef Buf<char, 64> TmpStr;
61 typedef Buf<char, 1024> BigStr;
63 char **tokenize(const char *str, int* numtokens = NULL, bool accept_quotes = true);
64 char **tokenize(const char *str, const char *whitespaces, int* numtokens = NULL, bool accept_quotes = true);
66 char **get_tokenized(int* numtokens = NULL, bool accept_quotes = true);
67 char **get_tokenized(const char *whitespaces, int* numtokens = NULL, bool accept_quotes = true);
68 char **fget_tokenized(FILE* f, int* numtokens = NULL, bool accept_quotes = true);
69 char **fget_tokenized(FILE* f, const char *whitespaces, int* numtokens = NULL, bool accept_quotes = true);
71 #endif //__UTILS_H__