My Project
Functions | Variables
util.c File Reference

Various utilities. More...

#include "bit.h"
#include "board.h"
#include "move.h"
#include "util.h"
#include "options.h"
#include "const.h"
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>
#include <signal.h>
#include <time.h>

Functions

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

Variables

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

Detailed Description

Various utilities.

This should be the only file with linux/windows dedicated code.

Date
1998 - 2017
Author
Richard Delorme
Version
4.4

Function Documentation

◆ 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

◆ 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 *  string,
const Board board_init,
Line line 
)

Parse a sequence of moves.

Parameters
stringString to parse.
board_initInitial board from where to play the moves.
lineA move sequence.
Returns
The remaining of the input string.

◆ 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 *  string,
const Board board,
Move move 
)

Parse a move.

Parameters
stringString to parse
boardBoard where to play the move.
moveA move.
Returns
The remaining of the input string.

◆ 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_time()

char * parse_time ( const char *  string,
long long *  t 
)

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

Parameters
stringTime as a string.
tOutput time in milliseconds.
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.

◆ relax()

void relax ( int  t)

sleep for t ms.

Parameters
ttime in ms.

◆ 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  thread,
int  i 
)

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) ( void  ) = real_clock

Time clock.

Can be set as a real_clock or a cpu_clock.

Returns
time in milliseconds.