Skip to content

Commit

Permalink
Merge pull request #183 from vneiger/handle_input_file_line_endings
Browse files Browse the repository at this point in the history
Handle input file line endings
  • Loading branch information
ederc authored Oct 11, 2024
2 parents d797801 + e0ddbb7 commit 8a19435
Show file tree
Hide file tree
Showing 14 changed files with 235 additions and 68 deletions.
4 changes: 3 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ msolve_SOURCES = src/msolve/main.c
check_PROGRAMS = neogb_io \
fglm_build_matrixn_radical_shape-31 \
fglm_build_matrixn_nonradical_shape-31 \
fglm_build_matrixn_nonradical_radicalshape-31
fglm_build_matrixn_nonradical_radicalshape-31 \
line_endings_support

checkdiff = test/diff/diff_cp_d_3_n_4_p_2.sh \
test/diff/diff_cyclic5-16.sh \
Expand Down Expand Up @@ -67,6 +68,7 @@ neogb_io_SOURCES = test/neogb/io/validate_input_data.c
fglm_build_matrixn_radical_shape_31_SOURCES = test/fglm/build_matrixn_radical_shape-31.c
fglm_build_matrixn_nonradical_shape_31_SOURCES = test/fglm/build_matrixn_nonradical_shape-31.c
fglm_build_matrixn_nonradical_radicalshape_31_SOURCES = test/fglm/build_matrixn_nonradical_radicalshape-31.c
line_endings_support_SOURCES = test/msolve/line_endings_support.c

TESTS = $(check_PROGRAMS) $(checkdiff)

Expand Down
8 changes: 8 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ AC_CONFIG_FILES([
src/msolve/Makefile
])
AC_CONFIG_LINKS([
input_files/line_endings/in1_dos.ms:input_files/line_endings/in1_dos.ms
input_files/line_endings/in2_dos_noeol.ms:input_files/line_endings/in2_dos_noeol.ms
input_files/line_endings/in3_dos.ms:input_files/line_endings/in3_dos.ms
input_files/line_endings/in4_dos.ms:input_files/line_endings/in4_dos.ms
input_files/line_endings/in1_unix.ms:input_files/line_endings/in1_unix.ms
input_files/line_endings/in2_unix.ms:input_files/line_endings/in2_unix.ms
input_files/line_endings/in3_unix.ms:input_files/line_endings/in3_unix.ms
input_files/line_endings/in4_unix.ms:input_files/line_endings/in4_unix.ms
test/diff/diff_source.sh:test/diff/diff_source.sh
test/diff/diff_source-groebner.sh:test/diff/diff_source-groebner.sh
input_files/cp_d_3_n_4_p_2.ms:input_files/cp_d_3_n_4_p_2.ms
Expand Down
4 changes: 2 additions & 2 deletions doc/msolve-tutorial.tex
Original file line number Diff line number Diff line change
Expand Up @@ -404,11 +404,11 @@ \section{Input file format}\label{sec:input}
Hence the structure of input files to \msolve is as follows:
\begin{enumerate}
\item the first line contains the variables of the input system, separated with
a comma;
a comma (no comma at end of line);
\item the second line contains the characteristic of the field over which
computations are performed;
\item the next lines contain polynomials, in expanded form, separated by a comma
(except the last one) and with a line break.
and with a line break (no comma or line break for the last one).
\end{enumerate}


Expand Down
6 changes: 6 additions & 0 deletions input_files/line_endings/in1_dos.ms
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
x0, x1, x2, x3
0
x0^2+x2^2-1,
x1^2+x3^2-1,
x0*x1+x2*x3,
x0*x3+x1*x2-1
6 changes: 6 additions & 0 deletions input_files/line_endings/in1_unix.ms
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
x0, x1, x2, x3
0
x0^2+x2^2-1,
x1^2+x3^2-1,
x0*x1+x2*x3,
x0*x3+x1*x2-1
6 changes: 6 additions & 0 deletions input_files/line_endings/in2_dos_noeol.ms
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
x0, x1, x2, x3
0
-1+x0^2+x2^2,
-1+x1^2+x3^2,
x0*x1+x2*x3,
-1+x0*x3+x1*x2
6 changes: 6 additions & 0 deletions input_files/line_endings/in2_unix.ms
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
x0, x1, x2, x3
0
-1+x0^2+x2^2,
-1+x1^2+x3^2,
x0*x1+x2*x3,
-1+x0*x3+x1*x2
6 changes: 6 additions & 0 deletions input_files/line_endings/in3_dos.ms
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
x0, x2, x1, x3
0
-1+x0^2+x2^2,
-1+x1^2+x3^2,
x0*x1+x2*x3,
-1+x0*x3+x1*x2
6 changes: 6 additions & 0 deletions input_files/line_endings/in3_unix.ms
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
x0, x2, x1, x3
0
-1+x0^2+x2^2,
-1+x1^2+x3^2,
x0*x1+x2*x3,
-1+x0*x3+x1*x2
4 changes: 4 additions & 0 deletions input_files/line_endings/in4_dos.ms
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
x1,x2
0
x1^2-x1,
x2^2-x2
4 changes: 4 additions & 0 deletions input_files/line_endings/in4_unix.ms
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
x1,x2
0
x1^2-x1,
x2^2-x2
123 changes: 59 additions & 64 deletions src/msolve/iofiles.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,80 +361,70 @@ static void print_ff_basis_data(

static int32_t get_nvars(const char *fn)
{
FILE *fh = fopen(fn,"r");
/** load lines and store data */
const int max_line_size = 1073741824;
char *line = (char *)malloc((nelts_t)max_line_size * sizeof(char));

/** get first line (variables) */
const char comma_splicer = ',';

/** get number of variables */
nvars_t nvars = 1; /** number of variables is number of commata + 1 in first line */
if (fgets(line, max_line_size, fh) != NULL) {
char *tmp = strchr(line, comma_splicer);
while (tmp != NULL) {
/** if there is a comma at the end of the line, i.e. strlen(line)-2 (since
* we have "\0" at the end of the string line, then we do not get another
* variable */
if ((uint32_t)(tmp-line) < strlen(line)-2) {
nvars++;
tmp = strchr(tmp+1, comma_splicer);
} else {
break;
}
FILE * fh = fopen(fn, "r");
char * line = NULL;
size_t len;
nvars_t nvars = -1;

/* number of variables is read from first line, it is 1 + (number of commata) */
if (getline(&line, &len, fh) != -1)
{
nvars = 1;
line = strchr(line, ',');
while (line != NULL)
{
nvars++;
// line points to a comma, which must be followed by one or more characters
// --> line+1 is valid
line = strchr(line+1, ',');
}
}
} else {
printf("Bad file format.\n");
nvars = 0;
}
free(line);
fclose(fh);

return nvars;
free(line);
fclose(fh);

return nvars;
}

/**
* Checks is a line of the input file is just empty resp. consists only
* of whitespaces
*
* \param line line
* Checks if a null-terminated string is just empty
* ("only whitespaces" counts as empty)
*
* \return 1 if the line is empty, else 0
*/
static inline int is_line_empty(const char *line)
{
while (*line != '\0') {
if (!isspace(*line))
return 0;
line++;
}
return 1;
while (*line != '\0')
{
if (!isspace(*line))
return 0;
line++;
}
return 1;
}

static int32_t get_ngenerators(char *fn){
int32_t nlines = 0;
char *line = NULL;
size_t len = 0;
FILE *fh = fopen(fn,"r");
/* first line are the variables */
if (getline(&line, &len, fh) == -1) {
fclose(fh);
return -1;
}
/* second line is the characteristic */
if (getline(&line, &len, fh) == -1) {
static int32_t get_ngenerators(char *fn)
{
int32_t ngens = 0;
char *line = NULL;
size_t len;
FILE *fh = fopen(fn,"r");

/* 1st and 2nd lines are ignored; still check all went fine */
if ( getline(&line, &len, fh) == -1
|| getline(&line, &len, fh) == -1)
ngens = -1;

/* go through subsequent lines, not counting empty ones */
else
while(getline(&line, &len, fh) != -1)
if (! is_line_empty(line))
ngens++;

free(line);
fclose(fh);
return -1;
}
while(getdelim(&line, &len, ',', fh) != -1) {
/* check if there are empty lines in the input file */
if (is_line_empty(line) == 0)
nlines++;
}
free(line);
fclose(fh);
return nlines;

return ngens;
}


Expand Down Expand Up @@ -567,7 +557,7 @@ static void get_nterms_and_all_nterms(FILE *fh, char **linep,
size_t len = 0;
while(getdelim(&line, &len, ',', fh) != -1) {
for (k = 0, j = 0; j < len; ++j) {
if (line[j] != '\n' && line[j] != ' ') {
if (line[j] != '\r' && line[j] != '\n' && line[j] != ' ') {
line[k++] = line[j];
}
}
Expand Down Expand Up @@ -829,7 +819,7 @@ static void get_coeffs_and_exponents_ff32(FILE *fh, char **linep, nelts_t all_nt
for(i = 0; i < *nr_gens; i++){
if (getdelim(&line, &len, ',', fh) != -1) {
for (k = 0, j = 0; j < len; ++j) {
if (line[j] != '\n' && line[j] != ' ') {
if (line[j] != '\r' && line[j] != '\n' && line[j] != ' ') {
line[k++] = line[j];
}
}
Expand Down Expand Up @@ -874,7 +864,7 @@ static void get_coeffs_and_exponents_mpz(FILE *fh, char **linep, nelts_t all_nte
for(i = 0; i < *nr_gens; i++){
if (getdelim(&line, &len, ',', fh) != -1) {
for (k = 0, j = 0; j < len; ++j) {
if (line[j] != '\n' && line[j] != ' ') {
if (line[j] != '\r' && line[j] != '\n' && line[j] != ' ') {
line[k++] = line[j];
}
}
Expand Down Expand Up @@ -926,7 +916,12 @@ static inline void get_data_from_file(char *fn, int32_t *nr_vars,
int32_t *field_char,
int32_t *nr_gens, data_gens_ff_t *gens){
*nr_vars = get_nvars(fn);
if (*nr_vars == -1)
printf("Bad file format (first line).\n");

*nr_gens = get_ngenerators(fn);
if (*nr_gens == -1)
printf("Bad file format (generators).\n");

const int max_line_size = 1073741824;
char *line = (char *)malloc((nelts_t)max_line_size * sizeof(char));
Expand Down
3 changes: 2 additions & 1 deletion src/msolve/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ static inline void display_help(char *str){

fprintf(stdout, "Input file format:\n");
fprintf(stdout, "\t - first line: variables separated by a comma\n");
fprintf(stdout, "\t (no comma at end of line)\n");
fprintf(stdout, "\t - second line: characteristic of the field\n");
fprintf(stdout, "\t - next lines provide the polynomials (one per line),\n");
fprintf(stdout, "\t separated by a comma\n");
fprintf(stdout, "\t (no comma after the final polynomial)\n\n");
fprintf(stdout, "\t (no comma after the last polynomial)\n\n");
fprintf(stdout, "Output file format: \n");
fprintf(stdout, "When there is no solution in an algebraic closure of the base field\n[-1]:\n");
fprintf(stdout, "Where there are infinitely many solutions in \nan algebraic closure of the base field: \n[1, nvars, -1,[]]:\n");
Expand Down
Loading

0 comments on commit 8a19435

Please sign in to comment.