Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ checkdiff = test/diff/diff_bug-2nd-prime-bad.sh \
test/diff/diff_kat6-31.sh \
test/diff/diff_kat7-qq.sh \
test/diff/diff_kat8-qq-truncate.sh \
test/diff/diff_line-endings.sh \
test/diff/diff_linear0-qq.sh \
test/diff/diff_linear1-qq.sh \
test/diff/diff_linear2-qq.sh \
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ AC_CONFIG_LINKS([
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_line-endings.sh:test/diff/diff_line-endings.sh
test/diff/diff_source.sh:test/diff/diff_source.sh
test/diff/diff_source-gb-lm.sh:test/diff/diff_source-gb-lm.sh
test/diff/diff_source-gb-red.sh:test/diff/diff_source-gb-red.sh
Expand Down
67 changes: 67 additions & 0 deletions src/msolve/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,64 @@ static void getoptions(
files->bin_out_file = bin_out_fname;
}

/**
* Checks whether the first line of file fn ends with "\r\n" (DOS line ending)
*
* \return 1 if it does, 0 otherwise (including if fn cannot be read)
*/
static int has_crlf_line_ending(const char *fn) {
FILE *fh = fopen(fn, "rb");
if (fh == NULL) {
return 0;
}
int c, prev = EOF;
while ((c = fgetc(fh)) != EOF && c != '\n') {
prev = c;
}
fclose(fh);
return c == '\n' && prev == '\r';
}

/**
* Rewrites file fn in place, replacing each "\n" which is not already
* preceded by "\r" with "\r\n"
*
* \return 0 on success, 1 on failure
*/
static int convert_to_crlf_line_endings(const char *fn) {
FILE *fh = fopen(fn, "rb");
if (fh == NULL) {
return 1;
}
FILE *tmp = tmpfile();
if (tmp == NULL) {
fclose(fh);
return 1;
}
int c;
while ((c = fgetc(fh)) != EOF) {
fputc(c, tmp);
}
fclose(fh);
rewind(tmp);

fh = fopen(fn, "wb");
if (fh == NULL) {
fclose(tmp);
return 1;
}
int prev = EOF;
while ((c = fgetc(tmp)) != EOF) {
if (c == '\n' && prev != '\r') {
fputc('\r', fh);
}
fputc(c, fh);
prev = c;
}
fclose(tmp);
return fclose(fh) != 0;
}


int main(int argc, char **argv){

Expand Down Expand Up @@ -564,6 +622,11 @@ int main(int argc, char **argv){
interval *real_roots = NULL;
real_point_t *real_pts = NULL;

/* output files are written with "\n" line endings; if the input file
* uses "\r\n", the output file is converted afterwards to match it */
int crlf_output = files->in_file != NULL && files->out_file != NULL
&& has_crlf_line_ending(files->in_file);

/* main msolve functionality */
int ret = core_msolve(la_option, use_signatures, nr_threads, info_level,
initial_hts, max_pairs, elim_block_len, update_ht,
Expand All @@ -576,6 +639,10 @@ int main(int argc, char **argv){
&param, mpz_paramp, &nb_real_roots, &real_roots,
&real_pts);

if (crlf_output && convert_to_crlf_line_endings(files->out_file)) {
fprintf(ERRSTREAM, "Cannot convert line endings of output file\n");
}

/* free parametrization */
if(param != NULL && gens->field_char){
free_fglm_param(param);
Expand Down
48 changes: 48 additions & 0 deletions test/diff/diff_line-endings.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env bash

source test/diff/diff_source.sh

# the output file should use the same line endings as the input file:
# for each pair of inputs differing only by their line endings, the output
# for the dos input must be the output for the unix input with "\r\n"

prefix=input_files/line_endings

check_line_endings() {
local dos=$1
local unix=$2
local excode=$3

$(pwd)/msolve -f $prefix/$dos.ms -o test/diff/$dos.res \
--random-seed $seed $4
if [ $? -gt 0 ]; then
print_exit $excode
fi

$(pwd)/msolve -f $prefix/$unix.ms -o test/diff/$unix.res \
--random-seed $seed $4
if [ $? -gt 0 ]; then
print_exit $(($excode+1))
fi

if grep -q $'\r' test/diff/$unix.res; then
print_exit $(($excode+2))
fi

awk '{ printf "%s\r\n", $0 }' test/diff/$unix.res | \
cmp -s - test/diff/$dos.res
if [ $? -gt 0 ]; then
print_exit $(($excode+3))
fi

rm test/diff/$dos.res test/diff/$unix.res
}

check_line_endings in1_dos in1_unix 1
check_line_endings in2_dos_noeol in2_unix 5
check_line_endings in3_dos in3_unix 9
check_line_endings in4_dos in4_unix 13
check_line_endings in1_dos in1_unix 17 "-g 2"
check_line_endings in1_dos in1_unix 21 "-P 1"

normal_exit
Loading