misc.c -> util.c, add str_token()
move util funtions declarations to util.h
This commit is contained in:
@@ -142,42 +142,6 @@ typedef u8 dir_t;
|
|||||||
#define sq_upwest(up) ((up) - 1)
|
#define sq_upwest(up) ((up) - 1)
|
||||||
#define sq_upeast(up) ((up) + 1)
|
#define sq_upeast(up) ((up) + 1)
|
||||||
|
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
typedef struct mclock {
|
|
||||||
clockid_t clocktype;
|
|
||||||
ulong elapsed_l;
|
|
||||||
double elapsed_f;
|
|
||||||
struct timespec start;
|
|
||||||
} mclock_t;
|
|
||||||
|
|
||||||
#define CLOCK_WALL CLOCK_REALTIME
|
|
||||||
#define CLOCK_SYSTEM CLOCK_MONOTONIC_RAW
|
|
||||||
#define CLOCK_PROCESS CLOCK_PROCESS_CPUTIME_ID
|
|
||||||
#define CLOCK_THREAD CLOCK_THREAD_CPUTIME_ID
|
|
||||||
|
|
||||||
/**
|
|
||||||
* CLOCK_DEFINE - define a clock type.
|
|
||||||
* @name: clock name
|
|
||||||
* @type: clock type
|
|
||||||
*
|
|
||||||
* This macro is equivalent to:
|
|
||||||
* mclock_t name;
|
|
||||||
* clock_init(&name, type);
|
|
||||||
*/
|
|
||||||
#define CLOCK_DEFINE(name, type) struct mclock name = { .clocktype = type }
|
|
||||||
|
|
||||||
void clock_init(mclock_t *clock, clockid_t type);
|
|
||||||
void clock_start(mclock_t *clock);
|
|
||||||
s64 clock_elapsed_μs(mclock_t *clock);
|
|
||||||
s64 clock_elapsed_ms(mclock_t *clock);
|
|
||||||
double clock_elapsed_sec(mclock_t *clock);
|
|
||||||
|
|
||||||
#define RAND_SEED_DEFAULT U64(0xb0d1ccea)
|
|
||||||
|
|
||||||
void rand_init(u64 seed);
|
|
||||||
u64 rand64(void);
|
|
||||||
|
|
||||||
void init_all(void);
|
void init_all(void);
|
||||||
|
|
||||||
#endif /* _CHESSDEFS_H */
|
#endif /* _CHESSDEFS_H */
|
||||||
|
@@ -19,6 +19,7 @@
|
|||||||
#include <bug.h>
|
#include <bug.h>
|
||||||
|
|
||||||
#include "chessdefs.h"
|
#include "chessdefs.h"
|
||||||
|
#include "util.h"
|
||||||
#include "alloc.h"
|
#include "alloc.h"
|
||||||
#include "position.h"
|
#include "position.h"
|
||||||
#include "piece.h"
|
#include "piece.h"
|
||||||
|
@@ -16,7 +16,7 @@
|
|||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
|
|
||||||
#include "chessdefs.h"
|
#include "chessdefs.h"
|
||||||
|
#include "util.h"
|
||||||
#include "bitboard.h"
|
#include "bitboard.h"
|
||||||
#include "hq.h"
|
#include "hq.h"
|
||||||
#include "eval-defs.h"
|
#include "eval-defs.h"
|
||||||
|
18
src/misc.h
18
src/misc.h
@@ -1,18 +0,0 @@
|
|||||||
/* util.h - various util functions.
|
|
||||||
*
|
|
||||||
* Copyright (C) 2024 Bruno Raoult ("br")
|
|
||||||
* Licensed under the GNU General Public License v3.0 or later.
|
|
||||||
* Some rights reserved. See COPYING.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License along with this
|
|
||||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
|
||||||
*
|
|
||||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef _UTIL_H
|
|
||||||
#define _UTIL_H
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* UTIL_H */
|
|
73
src/uci.c
73
src/uci.c
@@ -13,13 +13,14 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
//#include <string.h>
|
||||||
#include <ctype.h>
|
//#include <ctype.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <brlib.h>
|
#include <brlib.h>
|
||||||
|
|
||||||
#include "chessdefs.h"
|
#include "chessdefs.h"
|
||||||
|
#include "util.h"
|
||||||
#include "position.h"
|
#include "position.h"
|
||||||
#include "uci.h"
|
#include "uci.h"
|
||||||
#include "hist.h"
|
#include "hist.h"
|
||||||
@@ -47,6 +48,7 @@ int do_ucinewgame(pos_t *, char *);
|
|||||||
int do_uci(pos_t *, char *);
|
int do_uci(pos_t *, char *);
|
||||||
int do_isready(pos_t *, char *);
|
int do_isready(pos_t *, char *);
|
||||||
int do_quit(pos_t *, char *);
|
int do_quit(pos_t *, char *);
|
||||||
|
int do_setoption(pos_t *, char *);
|
||||||
|
|
||||||
int do_position(pos_t *, char *);
|
int do_position(pos_t *, char *);
|
||||||
|
|
||||||
@@ -59,20 +61,19 @@ int do_hist(pos_t *, char *);
|
|||||||
int do_help(pos_t *, char *);
|
int do_help(pos_t *, char *);
|
||||||
|
|
||||||
struct command commands[] = {
|
struct command commands[] = {
|
||||||
{ "help", do_help, "(not UCI) This help" },
|
|
||||||
{ "?", do_help, "(not UCI) This help" },
|
|
||||||
{ "quit", do_quit, "Quit" },
|
{ "quit", do_quit, "Quit" },
|
||||||
|
|
||||||
{ "uci", do_uci, "" },
|
{ "uci", do_uci, "" },
|
||||||
{ "ucinewgame", do_ucinewgame, "" },
|
{ "ucinewgame", do_ucinewgame, "" },
|
||||||
{ "isready", do_isready, "" },
|
{ "isready", do_isready, "" },
|
||||||
|
{ "setoption name", do_setoption, ""},
|
||||||
{ "position", do_position, "position startpos|fen [moves ...]" },
|
{ "position", do_position, "position startpos|fen [moves ...]" },
|
||||||
|
|
||||||
{ "perft", do_perft, "(not UCI) perft [divide] [alt] depth" },
|
{ "perft", do_perft, "(not UCI) perft [divide] [alt] depth" },
|
||||||
{ "moves", do_moves, "(not UCI) moves ..." },
|
{ "moves", do_moves, "(not UCI) moves ..." },
|
||||||
{ "diagram", do_diagram, "(not UCI) print current position diagram" },
|
{ "diagram", do_diagram, "(not UCI) print current position diagram" },
|
||||||
{ "hist", do_hist, "(not UCI) print history states" },
|
{ "hist", do_hist, "(not UCI) print history states" },
|
||||||
|
{ "help", do_help, "(not UCI) This help" },
|
||||||
|
{ "?", do_help, "(not UCI) This help" },
|
||||||
|
|
||||||
{ NULL, (int(*)()) NULL, NULL }
|
{ NULL, (int(*)()) NULL, NULL }
|
||||||
};
|
};
|
||||||
@@ -245,6 +246,20 @@ int do_isready(__unused pos_t *pos, __unused char *arg)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int do_setoption(__unused pos_t *pos, __unused char *arg)
|
||||||
|
{
|
||||||
|
__unused char *name, *value, *saveptr;
|
||||||
|
|
||||||
|
/* Note: space in var name are unsupported */
|
||||||
|
saveptr = NULL;
|
||||||
|
name = strtok_r(arg, " ", &saveptr);
|
||||||
|
|
||||||
|
value = strstr(arg, "value");
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int do_position(pos_t *pos, char *arg)
|
int do_position(pos_t *pos, char *arg)
|
||||||
{
|
{
|
||||||
char *saveptr, *token, *fen, *moves;
|
char *saveptr, *token, *fen, *moves;
|
||||||
@@ -423,48 +438,6 @@ int do_help(__unused pos_t *pos, __unused char *arg)
|
|||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
|
||||||
* string_trim - cleanup (trim) blank characters in string.
|
|
||||||
* @str: &string to clean
|
|
||||||
*
|
|
||||||
* str is cleaned and packed with the following rules:
|
|
||||||
* - Leading and trailing blank characters are removed.
|
|
||||||
* - consecutive blank characters are replaced by one space.
|
|
||||||
* - non printable characters are removed.
|
|
||||||
*
|
|
||||||
* "blank" means characters as understood by isspace(3): space, form-feed ('\f'),
|
|
||||||
* newline ('\n'), carriage return ('\r'), horizontal tab ('\t'), and vertical
|
|
||||||
* tab ('\v').
|
|
||||||
*
|
|
||||||
* @return: new @str len.
|
|
||||||
*/
|
|
||||||
int string_trim(char *str)
|
|
||||||
{
|
|
||||||
char *to = str, *from = str;
|
|
||||||
int state = 1;
|
|
||||||
|
|
||||||
while (*from) {
|
|
||||||
switch (state) {
|
|
||||||
case 1: /* blanks */
|
|
||||||
while (*from && isspace(*from))
|
|
||||||
from++;
|
|
||||||
state = 0;
|
|
||||||
break;
|
|
||||||
case 0: /* token */
|
|
||||||
while (*from && !isspace(*from)) {
|
|
||||||
if (isprint(*from))
|
|
||||||
*to++ = *from;
|
|
||||||
from++;
|
|
||||||
}
|
|
||||||
*to++ = ' ';
|
|
||||||
state = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (to > str)
|
|
||||||
to--;
|
|
||||||
*to = 0;
|
|
||||||
return to - str;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int done = 0;
|
static int done = 0;
|
||||||
|
|
||||||
@@ -476,12 +449,12 @@ int do_quit(__unused pos_t *pos, __unused char *arg)
|
|||||||
int uci(pos_t *pos)
|
int uci(pos_t *pos)
|
||||||
{
|
{
|
||||||
char *str = NULL, *saveptr, *token, *args;
|
char *str = NULL, *saveptr, *token, *args;
|
||||||
int len;
|
|
||||||
size_t lenstr = 0;
|
size_t lenstr = 0;
|
||||||
struct command *command;
|
struct command *command;
|
||||||
|
|
||||||
while (!done && getline(&str, &lenstr, stdin) >= 0) {
|
while (!done && getline(&str, &lenstr, stdin) >= 0) {
|
||||||
if (!(len = string_trim(str)))
|
str = str_trim(str);
|
||||||
|
if (! *str)
|
||||||
continue;
|
continue;
|
||||||
token = strtok_r(str, " ", &saveptr);
|
token = strtok_r(str, " ", &saveptr);
|
||||||
if (! (command= find_command(token))) {
|
if (! (command= find_command(token))) {
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
/* misc.c - generic/catchall functions.
|
/* util.c - generic/catchall functions.
|
||||||
*
|
*
|
||||||
* Copyright (C) 2024 Bruno Raoult ("br")
|
* Copyright (C) 2024 Bruno Raoult ("br")
|
||||||
* Licensed under the GNU General Public License v3.0 or later.
|
* Licensed under the GNU General Public License v3.0 or later.
|
||||||
@@ -11,12 +11,15 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <time.h>
|
//#include <time.h>
|
||||||
#include <stdlib.h>
|
//#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
#include <bug.h>
|
#include <bug.h>
|
||||||
|
|
||||||
#include "chessdefs.h"
|
#include "chessdefs.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 1 sec = 1000 millisec
|
* 1 sec = 1000 millisec
|
||||||
@@ -146,3 +149,75 @@ u64 rand64(void)
|
|||||||
rand_seed ^= rand_seed >> 27;
|
rand_seed ^= rand_seed >> 27;
|
||||||
return rand_seed * 0x2545f4914f6cdd1dull;
|
return rand_seed * 0x2545f4914f6cdd1dull;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* str_trim - cleanup (trim) blank characters in string.
|
||||||
|
* @str: &string to clean
|
||||||
|
*
|
||||||
|
* str is cleaned and packed with the following rules:
|
||||||
|
* - Leading and trailing blank characters are removed.
|
||||||
|
* - consecutive blank characters are replaced by one space.
|
||||||
|
* - non printable characters are removed.
|
||||||
|
*
|
||||||
|
* "blank" means characters as understood by isspace(3): space, form-feed ('\f'),
|
||||||
|
* newline ('\n'), carriage return ('\r'), horizontal tab ('\t'), and vertical
|
||||||
|
* tab ('\v').
|
||||||
|
*
|
||||||
|
* @return: new @str len.
|
||||||
|
*/
|
||||||
|
char *str_trim(char *str)
|
||||||
|
{
|
||||||
|
char *to = str, *from = str;
|
||||||
|
int state = 1;
|
||||||
|
|
||||||
|
while (*from) {
|
||||||
|
switch (state) {
|
||||||
|
case 1: /* blanks */
|
||||||
|
while (*from && isspace(*from))
|
||||||
|
from++;
|
||||||
|
state = 0;
|
||||||
|
break;
|
||||||
|
case 0: /* token */
|
||||||
|
while (*from && !isspace(*from)) {
|
||||||
|
if (isprint(*from))
|
||||||
|
*to++ = *from;
|
||||||
|
from++;
|
||||||
|
}
|
||||||
|
*to++ = ' ';
|
||||||
|
state = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (to > str)
|
||||||
|
to--;
|
||||||
|
*to = 0;
|
||||||
|
return to;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* str_token - find a token in string.
|
||||||
|
* @str: string to search
|
||||||
|
* @token: token to look for
|
||||||
|
*
|
||||||
|
* Look for token @token in string @str.
|
||||||
|
* A token is a string delimited by space characters. However, it may contain
|
||||||
|
* space characters itself.
|
||||||
|
*
|
||||||
|
* @return: @token address if found, NULL otherwise.
|
||||||
|
*/
|
||||||
|
char *str_token(const char *str, const char *token)
|
||||||
|
{
|
||||||
|
int len = strlen(token);
|
||||||
|
const char *found = str;
|
||||||
|
|
||||||
|
while (found && *found) {
|
||||||
|
printf("trying pos=%ld\n", found - str);
|
||||||
|
found = strstr(found, token);
|
||||||
|
if (found) {
|
||||||
|
if (!found[len] || isspace(found[len]))
|
||||||
|
break;
|
||||||
|
found = strchr(found, ' ') + 1;
|
||||||
|
printf("new pos=%ld\n", found - str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (char *) found;
|
||||||
|
}
|
60
src/util.h
Normal file
60
src/util.h
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
/* util.h - various util functions.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2024 Bruno Raoult ("br")
|
||||||
|
* Licensed under the GNU General Public License v3.0 or later.
|
||||||
|
* Some rights reserved. See COPYING.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along with this
|
||||||
|
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _UTIL_H
|
||||||
|
#define _UTIL_H
|
||||||
|
|
||||||
|
#include <brlib.h>
|
||||||
|
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
typedef struct mclock {
|
||||||
|
clockid_t clocktype;
|
||||||
|
ulong elapsed_l;
|
||||||
|
double elapsed_f;
|
||||||
|
struct timespec start;
|
||||||
|
} mclock_t;
|
||||||
|
|
||||||
|
#define CLOCK_WALL CLOCK_REALTIME
|
||||||
|
#define CLOCK_SYSTEM CLOCK_MONOTONIC_RAW
|
||||||
|
#define CLOCK_PROCESS CLOCK_PROCESS_CPUTIME_ID
|
||||||
|
#define CLOCK_THREAD CLOCK_THREAD_CPUTIME_ID
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CLOCK_DEFINE - define a clock type.
|
||||||
|
* @name: clock name
|
||||||
|
* @type: clock type
|
||||||
|
*
|
||||||
|
* This macro is equivalent to:
|
||||||
|
* mclock_t name;
|
||||||
|
* clock_init(&name, type);
|
||||||
|
*/
|
||||||
|
#define CLOCK_DEFINE(name, type) struct mclock name = { .clocktype = type }
|
||||||
|
|
||||||
|
void clock_init(mclock_t *clock, clockid_t type);
|
||||||
|
void clock_start(mclock_t *clock);
|
||||||
|
s64 clock_elapsed_μs(mclock_t *clock);
|
||||||
|
s64 clock_elapsed_ms(mclock_t *clock);
|
||||||
|
double clock_elapsed_sec(mclock_t *clock);
|
||||||
|
|
||||||
|
#define RAND_SEED_DEFAULT U64(0xb0d1ccea)
|
||||||
|
|
||||||
|
void rand_init(u64 seed);
|
||||||
|
u64 rand64(void);
|
||||||
|
|
||||||
|
|
||||||
|
char *str_trim(char *str);
|
||||||
|
char *str_token(const char *str, const char *token);
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* UTIL_H */
|
@@ -19,6 +19,7 @@
|
|||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
|
||||||
#include "chessdefs.h"
|
#include "chessdefs.h"
|
||||||
|
#include "util.h"
|
||||||
#include "fen.h"
|
#include "fen.h"
|
||||||
#include "position.h"
|
#include "position.h"
|
||||||
#include "move.h"
|
#include "move.h"
|
||||||
|
Reference in New Issue
Block a user