Skip to content

Commit 12ef6c8

Browse files
ambros-gleixnerleoneifler
authored andcommitted
Dynamic line length in file readers
1 parent 555f5d5 commit 12ef6c8

File tree

3 files changed

+68
-17
lines changed

3 files changed

+68
-17
lines changed

CHANGELOG

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ features:
88
same for multiple options the different solutions are appended to the same file
99

1010
interface & parameters:
11+
- allow reading of files with arbitrarily long lines by reallocating buffers dynamically
1112

1213
performance:
1314

src/soplex/spxlpbase_rational.hpp

+33-9
Original file line numberDiff line numberDiff line change
@@ -387,16 +387,18 @@ bool SPxLPBase<Rational>::readLPF(
387387
int colidx;
388388
int sense = 0;
389389

390-
char buf[SOPLEX_LPF_MAX_LINE_LEN];
391-
char tmp[SOPLEX_LPF_MAX_LINE_LEN];
392-
char line[SOPLEX_LPF_MAX_LINE_LEN];
393390
int lineno = 0;
394391
bool unnamed = true;
395392
bool finished = false;
396393
bool other;
397394
bool have_value = true;
398395
int i;
399396
int k;
397+
int buf_size;
398+
int buf_pos;
399+
char* buf;
400+
char* tmp;
401+
char* line;
400402
char* s;
401403
char* pos;
402404
char* pos_old = 0;
@@ -441,23 +443,45 @@ bool SPxLPBase<Rational>::readLPF(
441443
//--------------------------------------------------------------------------
442444
//--- Main Loop
443445
//--------------------------------------------------------------------------
446+
buf_size = SOPLEX_LPF_MAX_LINE_LEN;
447+
spx_alloc(buf, buf_size);
448+
spx_alloc(tmp, buf_size);
449+
spx_alloc(line, buf_size);
450+
444451
for(;;)
445452
{
446-
// 0. Read a line from the file.
447-
if(!p_input.getline(buf, sizeof(buf)))
453+
buf_pos = 0;
454+
455+
while(!p_input.getline(buf + buf_pos, buf_size - buf_pos))
448456
{
449-
if(strlen(buf) == SOPLEX_LPF_MAX_LINE_LEN - 1)
457+
p_input.clear();
458+
459+
if(strlen(buf) == (size_t) buf_size - 1)
450460
{
451-
SPX_MSG_ERROR(std::cerr << "ELPFRD06 Line exceeds " << SOPLEX_LPF_MAX_LINE_LEN - 2
452-
<< " characters" << std::endl;)
461+
buf_pos = buf_size - 1;
462+
buf_size = buf_size + SOPLEX_LPF_MAX_LINE_LEN;
463+
464+
if(buf_size >= INT_MAX)
465+
{
466+
SPX_MSG_ERROR(std::cerr << "ELPFRD16 Line longer than INT_MAX" << std::endl;)
467+
finished = true;
468+
break;
469+
}
470+
471+
spx_realloc(buf, buf_size);
453472
}
454473
else
455474
{
456475
SPX_MSG_ERROR(std::cerr << "ELPFRD07 No 'End' marker found" << std::endl;)
457476
finished = true;
477+
break;
458478
}
479+
}
459480

460-
break;
481+
if((size_t) buf_size > sizeof(tmp))
482+
{
483+
spx_realloc(tmp, buf_size);
484+
spx_realloc(line, buf_size);
461485
}
462486

463487
lineno++;

src/soplex/spxlpbase_real.hpp

+34-8
Original file line numberDiff line numberDiff line change
@@ -830,16 +830,18 @@ bool SPxLPBase<R>::readLPF(
830830
int colidx;
831831
int sense = 0;
832832

833-
char buf[SOPLEX_LPF_MAX_LINE_LEN];
834-
char tmp[SOPLEX_LPF_MAX_LINE_LEN];
835-
char line[SOPLEX_LPF_MAX_LINE_LEN];
836833
int lineno = 0;
837834
bool unnamed = true;
838835
bool finished = false;
839836
bool other;
840837
bool have_value = true;
841838
int i;
842839
int k;
840+
int buf_size;
841+
int buf_pos;
842+
char* buf;
843+
char* tmp;
844+
char* line;
843845
char* s;
844846
char* pos;
845847
char* pos_old = 0;
@@ -884,23 +886,47 @@ bool SPxLPBase<R>::readLPF(
884886
//--------------------------------------------------------------------------
885887
//--- Main Loop
886888
//--------------------------------------------------------------------------
889+
buf_size = SOPLEX_LPF_MAX_LINE_LEN;
890+
spx_alloc(buf, buf_size);
891+
spx_alloc(tmp, buf_size);
892+
spx_alloc(line, buf_size);
893+
buf[0] = '\0';
894+
887895
for(;;)
888896
{
889897
// 0. Read a line from the file.
890-
if(!p_input.getline(buf, sizeof(buf)))
898+
buf_pos = 0;
899+
900+
while(!p_input.getline(buf + buf_pos, buf_size - buf_pos))
891901
{
892-
if(strlen(buf) == SOPLEX_LPF_MAX_LINE_LEN - 1)
902+
p_input.clear();
903+
904+
if(strlen(buf) == (size_t) buf_size - 1)
893905
{
894-
SPX_MSG_ERROR(std::cerr << "ELPFRD06 Line exceeds " << SOPLEX_LPF_MAX_LINE_LEN - 2
895-
<< " characters" << std::endl;)
906+
buf_pos = buf_size - 1;
907+
buf_size = buf_size + SOPLEX_LPF_MAX_LINE_LEN;
908+
909+
if(buf_size >= INT_MAX)
910+
{
911+
SPX_MSG_ERROR(std::cerr << "ELPFRD16 Line longer than INT_MAX" << std::endl;)
912+
finished = true;
913+
break;
914+
}
915+
916+
spx_realloc(buf, buf_size);
896917
}
897918
else
898919
{
899920
SPX_MSG_ERROR(std::cerr << "ELPFRD07 No 'End' marker found" << std::endl;)
900921
finished = true;
922+
break;
901923
}
924+
}
902925

903-
break;
926+
if((size_t) buf_size > sizeof(tmp))
927+
{
928+
spx_realloc(tmp, buf_size);
929+
spx_realloc(line, buf_size);
904930
}
905931

906932
lineno++;

0 commit comments

Comments
 (0)