commit 9dd3e15297267c22ded9fbba064f54d64fad339c
parent 8fb4670e3d76387b8afb3c8ac81ed09741bf6a15
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date: Fri, 14 Apr 2023 20:21:36 +0200
Removed env
Diffstat:
10 files changed, 53 insertions(+), 145 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1,4 +1,2 @@
nissy
-nissy-*
-doc/*.html
-doc/*.pdf
+tables
diff --git a/Makefile b/Makefile
@@ -5,21 +5,22 @@ VERSION = pre-3.0
PREFIX = /usr/local
CPPFLAGS = -DVERSION=\"${VERSION}\"
-CFLAGS = -std=c99 -pthread -pedantic -Wall -Wextra \
- -Wno-unused-parameter -O3 ${CPPFLAGS}
-DBGFLAGS = -std=c99 -pthread -pedantic -Wall -Wextra \
- -Wno-unused-parameter \
- -g ${CPPFLAGS}
+CFLAGS = -std=c99 -pedantic -Wall -Wextra -Wno-unused-parameter -O3 ${CPPFLAGS}
+DBFLAGS = -std=c99 -pedantic -Wall -Wextra -Wno-unused-parameter -g ${CPPFLAGS}
CC = cc
all: nissy
+clean:
+ rm -f nissy
+
nissy: clean
+ mkdir -p tables
${CC} ${CFLAGS} -o nissy src/*.c
debug:
- ${CC} ${DBGFLAGS} -o nissy src/*.c
+ ${CC} ${DBFLAGS} -o nissy src/*.c
-.PHONY: all debug
+.PHONY: all clean debug
diff --git a/src/coord.c b/src/coord.c
@@ -477,13 +477,12 @@ static bool
read_coord_mtable(Coordinate *coord)
{
FILE *f;
- char fname[strlen(tabledir)+256];
+ char fname[256];
Move m;
uint64_t M;
bool r;
- strcpy(fname, tabledir);
- strcat(fname, "/mt_");
+ strcpy(fname, "tables/mt_");
strcat(fname, coord->name);
if ((f = fopen(fname, "rb")) == NULL)
@@ -507,12 +506,11 @@ static bool
read_coord_sd(Coordinate *coord)
{
FILE *f;
- char fname[strlen(tabledir)+256];
+ char fname[256];
uint64_t M, N;
bool r;
- strcpy(fname, tabledir);
- strcat(fname, "/sd_");
+ strcpy(fname, "tables/sd_");
strcat(fname, coord->name);
if ((f = fopen(fname, "rb")) == NULL)
@@ -535,13 +533,12 @@ static bool
read_coord_ttable(Coordinate *coord)
{
FILE *f;
- char fname[strlen(tabledir)+256];
+ char fname[256];
Trans t;
uint64_t M;
bool r;
- strcpy(fname, tabledir);
- strcat(fname, "/tt_");
+ strcpy(fname, "tables/tt_");
strcat(fname, coord->name);
if ((f = fopen(fname, "rb")) == NULL)
@@ -560,13 +557,12 @@ static bool
write_coord_mtable(Coordinate *coord)
{
FILE *f;
- char fname[strlen(tabledir)+256];
+ char fname[256];
Move m;
uint64_t M;
bool r;
- strcpy(fname, tabledir);
- strcat(fname, "/mt_");
+ strcpy(fname, "tables/mt_");
strcat(fname, coord->name);
if ((f = fopen(fname, "wb")) == NULL)
@@ -590,12 +586,11 @@ static bool
write_coord_sd(Coordinate *coord)
{
FILE *f;
- char fname[strlen(tabledir)+256];
+ char fname[256];
uint64_t M, N;
bool r;
- strcpy(fname, tabledir);
- strcat(fname, "/sd_");
+ strcat(fname, "tables/sd_");
strcat(fname, coord->name);
if ((f = fopen(fname, "wb")) == NULL)
@@ -618,13 +613,12 @@ static bool
write_coord_ttable(Coordinate *coord)
{
FILE *f;
- char fname[strlen(tabledir)+256];
+ char fname[256];
Trans t;
uint64_t M;
bool r;
- strcpy(fname, tabledir);
- strcat(fname, "/tt_");
+ strcat(fname, "tables/tt_");
strcat(fname, coord->name);
if ((f = fopen(fname, "wb")) == NULL)
diff --git a/src/coord.h b/src/coord.h
@@ -1,10 +1,12 @@
#ifndef COORD_H
#define COORD_H
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include <inttypes.h>
#include "cube.h"
-#include "env.h"
typedef enum { COMP_COORD, SYM_COORD, SYMCOMP_COORD } CoordType;
typedef struct {
diff --git a/src/cube.c b/src/cube.c
@@ -54,7 +54,8 @@ apply_permutation(int *perm, int *set, int n, int *aux)
for (i = 0; i < n; i++)
aux[i] = set[perm[i]];
- memcpy(set, aux, n * sizeof(int));
+ for (i = 0; i < n; i++)
+ set[i] = aux[i];
}
static void
@@ -84,10 +85,17 @@ compose(Cube *c2, Cube *c1)
void
copy_cube(Cube *src, Cube *dst)
{
- memcpy(dst->ep, src->ep, 12 * sizeof(int));
- memcpy(dst->eo, src->eo, 12 * sizeof(int));
- memcpy(dst->cp, src->cp, 8 * sizeof(int));
- memcpy(dst->co, src->co, 8 * sizeof(int));
+ int i;
+
+ for (i = 0; i < 12; i++) {
+ dst->ep[i] = src->ep[i];
+ dst->eo[i] = src->eo[i];
+ }
+
+ for (i = 0; i < 8; i++) {
+ dst->cp[i] = src->cp[i];
+ dst->co[i] = src->co[i];
+ }
}
void
@@ -128,12 +136,17 @@ is_solved(Cube *cube)
void
make_solved(Cube *cube)
{
- static int sorted[12] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
+ int i;
+
+ for (i = 0; i < 12; i++) {
+ cube->ep[i] = i;
+ cube->eo[i] = 0;
+ }
- memcpy(cube->ep, sorted, 12 * sizeof(int));
- memset(cube->eo, 0, 12 * sizeof(int));
- memcpy(cube->cp, sorted, 8 * sizeof(int));
- memset(cube->co, 0, 8 * sizeof(int));
+ for (i = 0; i < 8; i++) {
+ cube->cp[i] = i;
+ cube->co[i] = 0;
+ }
}
Move
@@ -151,11 +164,6 @@ inverse_move(Move m)
bool
commute(Move m1, Move m2)
{
- if (m1 > B3 || m2 > B3) {
- fprintf(stderr, "Unexpected commute for non-HTM\n");
- return false;
- }
-
if (m1 == NULLMOVE || m2 == NULLMOVE)
return m1 == m2;
@@ -260,11 +268,8 @@ apply_scramble(char *str, Cube *c)
if (str[i] == ' ' || str[i] == '\t' || str[i] == '\n')
continue;
- if ((str[i] == '(' && niss) || (str[i] == ')' && !niss)) {
- fprintf(stderr,
- "Error reading moves: unmatched %c\n", str[i]);
+ if ((str[i] == '(' && niss) || (str[i] == ')' && !niss))
return false;
- }
if (str[i] == '(' || str[i] == ')') {
niss = !niss;
@@ -284,10 +289,8 @@ apply_scramble(char *str, Cube *c)
apply_move(move, niss ? &inverse : &normal);
}
- if (niss) {
- fprintf(stderr, "Error reading moves: unmatched (\n");
+ if (niss)
return false;
- }
invert_cube(&inverse);
compose(c, &inverse);
diff --git a/src/cube.h b/src/cube.h
@@ -1,11 +1,6 @@
#ifndef CUBE_H
#define CUBE_H
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdint.h>
-
#define false 0
#define true 1
#define POW2TO6 64ULL
diff --git a/src/env.c b/src/env.c
@@ -1,64 +0,0 @@
-#define ENV_C
-
-#include "env.h"
-
-typedef int bool;
-#define true 1
-#define false 0
-
-bool initialized_env = false;
-char *tabledir;
-
-void
-mymkdir(char *d, int m)
-{
-#ifdef _WIN32
- mkdir(d);
-#else
- mkdir(d, m);
-#endif
-}
-
-void
-init_env()
-{
- char *nissydata = getenv("NISSYDATA");
- char *localdata = getenv("XDG_DATA_HOME");
- char *home = getenv("HOME");
- bool read, write;
-
- if (initialized_env)
- return;
-
- if (nissydata != NULL) {
- tabledir = malloc((strlen(nissydata) + 20) * sizeof(char));
- strcpy(tabledir, nissydata);
- } else if (localdata != NULL) {
- tabledir = malloc((strlen(localdata) + 20) * sizeof(char));
- strcpy(tabledir, localdata);
- strcat(tabledir, "/nissy");
- } else if (home != NULL) {
- tabledir = malloc((strlen(home) + 20) * sizeof(char));
- strcpy(tabledir, home);
- strcat(tabledir, "/.nissy");
- } else {
- tabledir = malloc(20 * sizeof(char));
- strcpy(tabledir, ".");
- }
-
- mymkdir(tabledir, 0777);
- strcat(tabledir, "/tables");
- mymkdir(tabledir, 0777);
-
- read = !access(tabledir, R_OK);
- write = !access(tabledir, W_OK);
-
- if (!read) {
- fprintf(stderr, "Table files cannot be read.\n");
- } else if (!write) {
- fprintf(stderr, "Data directory not writable: ");
- fprintf(stderr, "tables can be loaded, but not saved.\n");
- }
-
- initialized_env = true;
-}
diff --git a/src/env.h b/src/env.h
@@ -1,14 +0,0 @@
-#ifndef ENV_H
-#define ENV_H
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <sys/stat.h>
-
-void init_env();
-
-extern char *tabledir;
-
-#endif
diff --git a/src/main.c b/src/main.c
@@ -19,7 +19,6 @@ main(int argc, char *argv[])
m = 0;
t = NORMAL;
- init_env();
init_cube();
for (i = 2; i < argc; i++) {
diff --git a/src/pruning.c b/src/pruning.c
@@ -277,15 +277,12 @@ ptableval(PruneData *pd, uint64_t ind)
static bool
read_ptable_file(PruneData *pd)
{
- init_env();
-
FILE *f;
- char fname[strlen(tabledir)+256];
+ char fname[256];
int i;
uint64_t r;
- strcpy(fname, tabledir);
- strcat(fname, "/pt_");
+ strcpy(fname, "tables/pt_");
strcat(fname, pd->coord->name);
if ((f = fopen(fname, "rb")) == NULL)
@@ -304,15 +301,12 @@ read_ptable_file(PruneData *pd)
static bool
write_ptable_file(PruneData *pd)
{
- init_env();
-
FILE *f;
- char fname[strlen(tabledir)+256];
+ char fname[256];
int i;
uint64_t w;
- strcpy(fname, tabledir);
- strcat(fname, "/pt_");
+ strcpy(fname, "tables/pt_");
strcat(fname, pd->coord->name);
if ((f = fopen(fname, "wb")) == NULL)