ReadLine.c
Go to the documentation of this file.
1 #include "LKH.h"
2 
3 /*
4  * The ReadLine function reads the next input line from a file. The function
5  * handles the problem that an input line may be terminated by a carriage
6  * return, a newline, both, or EOF.
7  */
8 
9 static char *Buffer;
10 static int MaxBuffer;
11 
12 static int EndOfLine(FILE * InputFile, int c)
13 {
14  int EOL = (c == '\r' || c == '\n');
15  if (c == '\r') {
16  c = fgetc(InputFile);
17  if (c != '\n' && c != EOF)
18  ungetc(c, InputFile);
19  }
20  return EOL;
21 }
22 
23 char *ReadLine(FILE * InputFile)
24 {
25  int i, c;
26 
27  if (Buffer == 0)
28  assert(Buffer = (char *) malloc(MaxBuffer = 80));
29  for (i = 0; (c = fgetc(InputFile)) != EOF && !EndOfLine(InputFile, c);
30  i++) {
31  if (i >= MaxBuffer - 1) {
32  MaxBuffer *= 2;
33  assert(Buffer = (char *) realloc(Buffer, MaxBuffer));
34  }
35  Buffer[i] = (char) c;
36  }
37  Buffer[i] = '\0';
38  if (!LastLine || (int) strlen(LastLine) < i) {
39  free(LastLine);
40  assert(LastLine = (char *) malloc((i + 1) * sizeof(char)));
41  }
42  strcpy(LastLine, Buffer);
43  return c == EOF && i == 0 ? 0 : Buffer;
44 }
static char * Buffer
Definition: ReadLine.c:9
char * LastLine
Definition: LKH.h:214
static int MaxBuffer
Definition: ReadLine.c:10
static int EndOfLine(FILE *InputFile, int c)
Definition: ReadLine.c:12
CostFunction c
Definition: LKH.h:304
char * ReadLine(FILE *InputFile)
Definition: ReadLine.c:23


glkh_solver
Author(s): Francisco Suarez-Ruiz
autogenerated on Mon Jun 10 2019 13:50:26