My Project
|
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... | |
Various utilities.
This should be the only file with linux/windows dedicated code.
char * file_add_ext | ( | const char * | base, |
const char * | ext, | ||
char * | file | ||
) |
Add an extension to a string.
base | Base name. |
ext | Extension (.dat, .ext, .bin, etc.) |
file | Output file name. |
char * format_scientific | ( | double | v, |
const char * | unit, | ||
char * | f | ||
) |
Format a value with a unit.
v | Value to print. |
unit | Unit. |
f | Output string. |
int get_cpu_number | ( | void | ) |
Get the number of cpus or cores on the machine.
char * parse_board | ( | const char * | string, |
Board * | board, | ||
int * | player | ||
) |
Parse a board.
string | String to parse |
board | Board. |
player | Player to move color. |
char * parse_boolean | ( | const char * | string, |
bool * | result | ||
) |
Parse a boolean.
string | String to parse |
result | Boolean output value. |
char * parse_command | ( | const char * | string, |
char * | cmd, | ||
char * | param, | ||
const unsigned int | size | ||
) |
Parse a command.
string | String to parse. |
cmd | Output command string (first word of the input string). |
param | Output parameters (remaining of the line). |
size | cmd string capacity. |
char * parse_field | ( | const char * | string, |
char * | word, | ||
unsigned int | n, | ||
char | separator | ||
) |
Parse a field.
string | String to parse |
word | String receiving a copy of the parsed word. |
n | word string capacity. |
separator | field separator. NOTE: It is assumed that w is big enough to contains the word copy. |
char * parse_find | ( | const char * | string, |
const int | c | ||
) |
Find a char.
string | String to parse. |
c | Seeked char. |
Parse a sequence of moves.
string | String to parse. |
board_init | Initial board from where to play the moves. |
line | A move sequence. |
char * parse_int | ( | const char * | string, |
int * | result | ||
) |
Parse an integer.
string | String to parse |
result | Integer output value. |
char * parse_line | ( | const char * | string, |
char * | line, | ||
unsigned int | n | ||
) |
Parse a line.
string | string to parse |
line | string receiving a copy of the parsed line. |
n | line 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. |
Parse a move.
string | String to parse |
board | Board where to play the move. |
move | A move. |
char * parse_real | ( | const char * | string, |
double * | result | ||
) |
Parse a real number (as a double floating point).
string | String to parse. |
result | Real number (double) output value. |
char * parse_skip_spaces | ( | const char * | string | ) |
Skip spaces.
string | String to parse. |
char * parse_skip_word | ( | const char * | string | ) |
Skip word.
string | String to parse. |
char * parse_time | ( | const char * | string, |
long long * | t | ||
) |
parse time as "D:HH:MM:SS.C".
string | Time as a string. |
t | Output time in milliseconds. |
char * parse_word | ( | const char * | string, |
char * | word, | ||
unsigned int | n | ||
) |
Parse a word.
string | String to parse |
word | String receiving a copy of the parsed word. |
n | word string capacity. NOTE: It is assumed that w is big enough to contains the word copy. |
void path_get_dir | ( | const char * | path, |
char * | dir | ||
) |
Extract the directory of a file path.
path | Full path. |
dir | directory part of the path. |
void print_scientific | ( | double | v, |
const char * | unit, | ||
FILE * | f | ||
) |
Print a value with a unit.
v | Value to print. |
unit | Unit. |
f | Output stream. |
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.
random | Pseudo-Random generator state. |
void random_seed | ( | Random * | random, |
const unsigned long long | seed | ||
) |
Pseudo-random number seed.
random | Pseudo-Random generator state. |
seed | a 64-bits integer used as seed. |
void relax | ( | int | t | ) |
sleep for t ms.
t | time in ms. |
char * string_duplicate | ( | const char * | s | ) |
Duplicate a string.
s | string to duplicate. |
char * string_read_line | ( | FILE * | f | ) |
Read a line.
Read a line from an input string. The needed memory is allocated.
f | Input stream. |
bool string_to_boolean | ( | const char * | s | ) |
Convert a string into a boolean.
Parse a string into a boolean (false/true).
s | String. |
int string_to_coordinate | ( | const char * | s | ) |
Convert the two first chars of a string into a coordinate.
s | String. |
int string_to_int | ( | const char * | s, |
const int | default_value | ||
) |
Convert a string into an integer.
Parse a string into an int.
s | String. |
default_value | default value if parsing failed. |
void string_to_lowercase | ( | char * | s | ) |
Change all char of a string to lowercase.
s | string. |
double string_to_real | ( | const char * | s, |
const double | default_value | ||
) |
Convert a string into a real number.
Parse a string into a double.
s | String. |
default_value | default value if parsing failed. |
long long string_to_time | ( | const char * | string | ) |
Read time as "D:HH:MM:SS.C".
string | Time as a string. |
void string_to_uppercase | ( | char * | s | ) |
Change all char of a string to uppercase.
s | Input string. |
char * string_to_word | ( | char * | s | ) |
remove spaces from a string.
s | string. |
void thread_create2 | ( | Thread * | thread, |
void *(*)(void *) | function, | ||
void * | data | ||
) |
Create a thread.
thread | Thread. |
function | Function to run in parallel. |
data | Data for the function. |
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.
thread | Thread. |
Thread thread_self | ( | void | ) |
Current thread.
void thread_set_cpu | ( | Thread | thread, |
int | i | ||
) |
Choose a single core or cpu to run on, under linux systems, to avoid context changes.
void time_print | ( | long long | t, |
bool | justified, | ||
FILE * | f | ||
) |
Print time as "D:HH:MM:SS.CC".
t | Time in milliseconds. |
justified | add spaces or not before the text. |
f | Stream. |
long long time_read | ( | FILE * | f | ) |
read time as "D:HH:MM:SS.C".
f | Input stream. |
void time_stamp | ( | FILE * | f | ) |
Print local time.
f | Output stream. |
long long(* time_clock) (void) | ( | void | ) | = real_clock |
Time clock.
Can be set as a real_clock or a cpu_clock.