Classes | Macros | Typedefs | Functions | Variables
util.h File Reference

Miscellaneous utilities header. More...

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

Go to the source code of this file.

Classes

struct  Random
 
struct  Log
 LogFile. More...
 

Macros

#define MAX(a, b)   ((a) > (b) ? (a) : (b))
 
#define MIN(a, b)   ((a) < (b) ? (a) : (b))
 
#define BOUND(var, min, max, name)
 
#define fatal_error(...)
 Display an error message as "FATAL_ERROR : file name : function name : line number : ..." and abort the program. More...
 
#define error(...)
 Display an error message as "ERROR : filename : funcname : line number : ...". More...
 
#define warn(...)
 Display a warning message as "WARNING : ... ". More...
 
#define info(...)
 Display a message. More...
 
#define cassio_debug(...)
 Display a message in cassio's "fenetre de rapport" . More...
 
#define trace(...)
 trace indicate if a piece of code is visited More...
 
#define debug(...)
 Display a debuging message as "DEBUG : ... ". More...
 
#define log_open(l, file)
 open a log file if allowed. More...
 
#define log_close(l)
 Close an opened log file. More...
 
#define log_print(l, ...)
 Print into the log file. More...
 
#define log_is_open(l)   ((l)->f != NULL)
 Check if the log stream can be used. More...
 
#define log_receive(l, title, ...)
 log a reception More...
 
#define log_send(l, title, ...)
 log a sending More...
 

Typedefs

typedef struct Random Random
 
typedef struct Log Log
 LogFile. More...
 

Functions

long long real_clock (void)
 
long long cpu_clock (void)
 
void time_print (long long, bool, FILE *)
 Print time as "D:HH:MM:SS.CC". More...
 
long long time_read (FILE *)
 read time as "D:HH:MM:SS.C". More...
 
void time_stamp (FILE *)
 Print local time. More...
 
void relax (int)
 sleep for t ms. More...
 
char * format_scientific (double, const char *, char *)
 Format a value with a unit. More...
 
void print_scientific (double, const char *, FILE *)
 Print a value with a unit. More...
 
char * string_duplicate (const char *)
 Duplicate a string. More...
 
long long string_to_time (const char *)
 Read time as "D:HH:MM:SS.C". More...
 
char * string_read_line (FILE *)
 Read a line. More...
 
char * string_copy_line (FILE *)
 
void string_to_lowercase (char *)
 Change all char of a string to lowercase. More...
 
void string_to_uppercase (char *)
 Change all char of a string to uppercase. More...
 
int string_to_coordinate (const char *)
 Convert the two first chars of a string into a coordinate. More...
 
char * string_to_word (char *)
 remove spaces from a string. More...
 
bool string_to_boolean (const char *)
 Convert a string into a boolean. More...
 
int string_to_int (const char *, const int)
 Convert a string into an integer. More...
 
double string_to_real (const char *, const double)
 Convert a string into a real number. More...
 
char * parse_word (const char *, char *, unsigned int)
 Parse a word. More...
 
char * parse_field (const char *, char *, unsigned int, char)
 Parse a field. More...
 
char * parse_line (const char *, char *, unsigned int)
 Parse a line. More...
 
char * parse_board (const char *, struct Board *, int *)
 Parse a board. More...
 
char * parse_move (const char *, const struct Board *, struct Move *)
 
char * parse_game (const char *, const struct Board *, struct Line *)
 
char * parse_boolean (const char *, bool *)
 Parse a boolean. More...
 
char * parse_int (const char *, int *)
 Parse an integer. More...
 
char * parse_real (const char *, double *)
 Parse a real number (as a double floating point). More...
 
char * parse_command (const char *, char *, char *, const unsigned int)
 Parse a command. More...
 
char * parse_find (const char *, const int)
 Find a char. More...
 
char * parse_skip_spaces (const char *)
 Skip spaces. More...
 
char * parse_skip_word (const char *)
 Skip word. More...
 
void path_get_dir (const char *, char *)
 Extract the directory of a file path. More...
 
char * file_add_ext (const char *, const char *, char *)
 Add an extension to a string. More...
 
bool is_stdin_keyboard (void)
 
unsigned long long random_get (Random *)
 Pseudo-random number generator. More...
 
void random_seed (Random *, const unsigned long long)
 Pseudo-random number seed. More...
 
void thread_create2 (Thread *, void *(*f)(void *), void *)
 Create a thread. More...
 
void thread_join (Thread)
 Join a thread. More...
 
void thread_set_cpu (Thread, int)
 Choose a single core or cpu to run on, under linux systems, to avoid context changes. More...
 
Thread thread_self (void)
 Current thread. More...
 
static void atomic_add (volatile unsigned long long *value, long long i)
 
void cpu (void)
 
int get_cpu_number (void)
 Get the number of cpus or cores on the machine. More...
 

Variables

long long(* time_clock )(void)
 Time clock. More...
 

Detailed Description

Miscellaneous utilities header.

Date
1998 - 2017
Author
Richard Delorme
Version
4.4

Macro Definition Documentation

◆ BOUND

#define BOUND (   var,
  min,
  max,
  name 
)
Value:
do {\
if (var < min && min <= max) {\
if (name) fprintf(stderr, "\nWARNING: %s = %lld is out of range. Set to %lld\n", name, (long long)var, (long long)min);\
var = min;\
} else if (var > max) {\
if (name) fprintf(stderr, "\nWARNING: %s = %lld is out of range. Set to %lld\n", name, (long long)var, (long long)max);\
var = max;\
}\
} while (0)

Constrain a variable to a range of values.

◆ cassio_debug

#define cassio_debug (   ...)
Value:
printf("DEBUG: "); \
printf(__VA_ARGS__); \
log_print(engine_log, "DEBUG: "); \
log_print(engine_log, __VA_ARGS__);\
} else (void) 0
Log engine_log[1]
Definition: cassio.c:35
Options options
Definition: options.c:22
bool debug_cassio
Definition: options.h:37

Display a message in cassio's "fenetre de rapport" .

◆ debug

#define debug (   ...)
Value:
do { \
fprintf(stderr, "\nDEBUG : "); \
fprintf(stderr, __VA_ARGS__); \
} while (0)

Display a debuging message as "DEBUG : ... ".

◆ error

#define error (   ...)
Value:
do { \
fprintf(stderr, "\nERROR: %s : %s : %d :", __FILE__, __func__, __LINE__); \
if (errno) fprintf(stderr, " error #%d : %s", errno, strerror(errno)); \
fputc('\n', stderr); \
fprintf(stderr, __VA_ARGS__); \
errno = 0; \
} while (0)

Display an error message as "ERROR : filename : funcname : line number : ...".

◆ fatal_error

#define fatal_error (   ...)
Value:
do { \
fprintf(stderr, "\nFATAL ERROR: %s : %s : %d : ", __FILE__, __func__, __LINE__); \
if (errno) fprintf(stderr, "\terror #%d : %s", errno, strerror(errno)); \
fputc('\n', stderr); \
fprintf(stderr, __VA_ARGS__); \
abort(); \
} while (0)

Display an error message as "FATAL_ERROR : file name : function name : line number : ..." and abort the program.

◆ info

#define info (   ...)
Value:
if (options.info) { \
fprintf(stderr, __VA_ARGS__); \
} else (void) 0
bool info
Definition: options.h:36
Options options
Definition: options.c:22

Display a message.

◆ log_close

#define log_close (   l)
Value:
if ((l)->f) { \
lock_free(l); \
fclose((l)->f); \
(l)->f = NULL; \
} else (void) 0

Close an opened log file.

◆ log_is_open

#define log_is_open (   l)    ((l)->f != NULL)

Check if the log stream can be used.

◆ log_open

#define log_open (   l,
  file 
)
Value:
if ((l)->f == NULL && file != NULL) { \
lock_init(l); \
(l)->f = fopen(file, "w"); \
} else (void) 0

open a log file if allowed.

◆ log_print

#define log_print (   l,
  ... 
)
Value:
if ((l)->f) { \
fprintf((l)->f, __VA_ARGS__); \
fflush((l)->f); \
} else (void) 0

Print into the log file.

◆ log_receive

#define log_receive (   l,
  title,
  ... 
)
Value:
if ((l)->f) { \
fprintf((l)->f, "%s", title); \
time_stamp((l)->f); \
fprintf((l)->f, " <== "); \
fprintf((l)->f, __VA_ARGS__); \
fflush((l)->f); \
} else (void) 0

log a reception

◆ log_send

#define log_send (   l,
  title,
  ... 
)
Value:
if ((l)->f) { \
fprintf((l)->f, "%s", title); \
time_stamp((l)->f); \
fprintf((l)->f, " ==> "); \
fprintf((l)->f, __VA_ARGS__); \
fflush((l)->f); \
} else (void) 0

log a sending

◆ MAX

#define MAX (   a,
 
)    ((a) > (b) ? (a) : (b))

Maximum of two values

◆ MIN

#define MIN (   a,
 
)    ((a) < (b) ? (a) : (b))

Minimum of two values

◆ trace

#define trace (   ...)
Value:
do {\
fprintf(stderr, "trace %s : %s : %d : ", __FILE__, __func__, __LINE__); \
fprintf(stderr, __VA_ARGS__); \
} while (0)

trace indicate if a piece of code is visited

◆ warn

#define warn (   ...)
Value:
do { \
fprintf(stderr, "\nWARNING: "); \
fprintf(stderr, __VA_ARGS__); \
} while (0)

Display a warning message as "WARNING : ... ".

Typedef Documentation

◆ Log

typedef struct Log Log

LogFile.

◆ Random

typedef struct Random Random

Function Documentation

◆ atomic_add()

static void atomic_add ( volatile unsigned long long *  value,
long long  i 
)
inlinestatic

atomic addition

◆ cpu()

void cpu ( void  )

◆ cpu_clock()

long long cpu_clock ( void  )

◆ file_add_ext()

char* file_add_ext ( const char *  base,
const char *  ext,
char *  file 
)

Add an extension to a string.

Parameters
baseBase name.
extExtension (.dat, .ext, .bin, etc.)
fileOutput file name.
Returns
The output file name.

◆ format_scientific()

char* format_scientific ( double  v,
const char *  unit,
char *  f 
)

Format a value with a unit.

Parameters
vValue to print.
unitUnit.
fOutput string.

◆ get_cpu_number()

int get_cpu_number ( void  )

Get the number of cpus or cores on the machine.

Returns
Cpu/Core number

◆ is_stdin_keyboard()

bool is_stdin_keyboard ( void  )

◆ parse_board()

char* parse_board ( const char *  string,
Board board,
int *  player 
)

Parse a board.

Parameters
stringString to parse
boardBoard.
playerPlayer to move color.
Returns
The remaining of the input string.

◆ parse_boolean()

char* parse_boolean ( const char *  string,
bool *  result 
)

Parse a boolean.

Parameters
stringString to parse
resultBoolean output value.
Returns
The remaining of the input string.

◆ parse_command()

char* parse_command ( const char *  string,
char *  cmd,
char *  param,
const unsigned int  size 
)

Parse a command.

Parameters
stringString to parse.
cmdOutput command string (first word of the input string).
paramOutput parameters (remaining of the line).
sizecmd string capacity.
Returns
The remaining of the input string (next line).

◆ parse_field()

char* parse_field ( const char *  string,
char *  word,
unsigned int  n,
char  separator 
)

Parse a field.

Parameters
stringString to parse
wordString receiving a copy of the parsed word.
nword string capacity.
separatorfield separator. NOTE: It is assumed that w is big enough to contains the word copy.
Returns
The remaining of the input string.

◆ parse_find()

char* parse_find ( const char *  string,
const int  c 
)

Find a char.

Parameters
stringString to parse.
cSeeked char.
Returns
The input string at the char position or an empty string.

◆ parse_game()

char* parse_game ( const char *  ,
const struct Board ,
struct Line  
)

◆ parse_int()

char* parse_int ( const char *  string,
int *  result 
)

Parse an integer.

Parameters
stringString to parse
resultInteger output value.
Returns
The remaining of the input string.

◆ parse_line()

char* parse_line ( const char *  string,
char *  line,
unsigned int  n 
)

Parse a line.

Parameters
stringstring to parse
linestring receiving a copy of the parsed line.
nline string capacity. NOTE: It is assumed that line is big enough to contains 'n' characters. if the line to be parsed contains more than 'n' characters, only 'n' caracters will be copied, but the whole line will be parsed.
Returns
The remaining of the input string until the next new line char.

◆ parse_move()

char* parse_move ( const char *  ,
const struct Board ,
struct Move  
)

◆ parse_real()

char* parse_real ( const char *  string,
double *  result 
)

Parse a real number (as a double floating point).

Parameters
stringString to parse.
resultReal number (double) output value.
Returns
The remaining of the input string.

◆ parse_skip_spaces()

char* parse_skip_spaces ( const char *  string)

Skip spaces.

Parameters
stringString to parse.
Returns
The remaining of the input string.

◆ parse_skip_word()

char* parse_skip_word ( const char *  string)

Skip word.

Parameters
stringString to parse.
Returns
The remaining of the input string.

◆ parse_word()

char* parse_word ( const char *  string,
char *  word,
unsigned int  n 
)

Parse a word.

Parameters
stringString to parse
wordString receiving a copy of the parsed word.
nword string capacity. NOTE: It is assumed that w is big enough to contains the word copy.
Returns
The remaining of the input string.

◆ path_get_dir()

void path_get_dir ( const char *  path,
char *  dir 
)

Extract the directory of a file path.

Parameters
pathFull path.
dirdirectory part of the path.

◆ print_scientific()

void print_scientific ( double  v,
const char *  unit,
FILE *  f 
)

Print a value with a unit.

Parameters
vValue to print.
unitUnit.
fOutput stream.

◆ random_get()

unsigned long long random_get ( Random random)

Pseudo-random number generator.

A good pseudo-random generator (derived from rand48 or Java's one) to set the hash codes.

Parameters
randomPseudo-Random generator state.
Returns
a 64-bits pseudo-random unsigned int integer.

◆ random_seed()

void random_seed ( Random random,
const unsigned long long  seed 
)

Pseudo-random number seed.

Parameters
randomPseudo-Random generator state.
seeda 64-bits integer used as seed.

◆ real_clock()

long long real_clock ( void  )

◆ relax()

void relax ( int  t)

sleep for t ms.

Parameters
ttime in ms.

◆ string_copy_line()

char* string_copy_line ( FILE *  )

◆ string_duplicate()

char* string_duplicate ( const char *  s)

Duplicate a string.

Parameters
sstring to duplicate.
Returns
a copy of the string.

◆ string_read_line()

char* string_read_line ( FILE *  f)

Read a line.

Read a line from an input string. The needed memory is allocated.

Parameters
fInput stream.
Returns
NULL if the line is empty, the read line otherwise.

◆ string_to_boolean()

bool string_to_boolean ( const char *  s)

Convert a string into a boolean.

Parse a string into a boolean (false/true).

Parameters
sString.
Returns
a word.

◆ string_to_coordinate()

int string_to_coordinate ( const char *  s)

Convert the two first chars of a string into a coordinate.

Parameters
sString.
Returns
coordinate value (A1 to H8 or PASS or NOMOVE).

◆ string_to_int()

int string_to_int ( const char *  s,
const int  default_value 
)

Convert a string into an integer.

Parse a string into an int.

Parameters
sString.
default_valuedefault value if parsing failed.
Returns
an integer.

◆ string_to_lowercase()

void string_to_lowercase ( char *  s)

Change all char of a string to lowercase.

Parameters
sstring.

◆ string_to_real()

double string_to_real ( const char *  s,
const double  default_value 
)

Convert a string into a real number.

Parse a string into a double.

Parameters
sString.
default_valuedefault value if parsing failed.
Returns
a real number.

◆ string_to_time()

long long string_to_time ( const char *  string)

Read time as "D:HH:MM:SS.C".

Parameters
stringTime as a string.
Returns
Time in milliseconds.

◆ string_to_uppercase()

void string_to_uppercase ( char *  s)

Change all char of a string to uppercase.

Parameters
sInput string.

◆ string_to_word()

char* string_to_word ( char *  s)

remove spaces from a string.

Parameters
sstring.
Returns
a word.

◆ thread_create2()

void thread_create2 ( Thread *  thread,
void *(*)(void *)  function,
void *  data 
)

Create a thread.

Parameters
threadThread.
functionFunction to run in parallel.
dataData for the function.

◆ thread_join()

void thread_join ( Thread  thread)

Join a thread.

Wait for the thread termination. this function also frees the resources allocated by a thread that has not been detached.

Parameters
threadThread.

◆ thread_self()

Thread thread_self ( void  )

Current thread.

Returns
current thread.

◆ thread_set_cpu()

void thread_set_cpu ( Thread  ,
int   
)

Choose a single core or cpu to run on, under linux systems, to avoid context changes.

◆ time_print()

void time_print ( long long  t,
bool  justified,
FILE *  f 
)

Print time as "D:HH:MM:SS.CC".

Parameters
tTime in milliseconds.
justifiedadd spaces or not before the text.
fStream.

◆ time_read()

long long time_read ( FILE *  f)

read time as "D:HH:MM:SS.C".

Parameters
fInput stream.
Returns
time in milliseconds.

◆ time_stamp()

void time_stamp ( FILE *  f)

Print local time.

Parameters
fOutput stream.

Variable Documentation

◆ time_clock

long long(* time_clock) (void)

Time clock.

Can be set as a real_clock or a cpu_clock.

Returns
time in milliseconds.