]> git.sesse.net Git - pgn-extract/blob - lines.c
Push through a computer/human flag to the binary output.
[pgn-extract] / lines.c
1 /*
2  *  Program: pgn-extract: a Portable Game Notation (PGN) extractor.
3  *  Copyright (C) 1994-2014 David Barnes
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 1, or (at your option)
7  *  any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  *  David Barnes may be contacted as D.J.Barnes@kent.ac.uk
19  *  http://www.cs.kent.ac.uk/people/staff/djb/
20  *
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include "bool.h"
28 #include "mymalloc.h"
29 #include "lines.h"
30
31     /* Read a single line of input. */
32 #define INIT_LINE_LENGTH 80
33 #define LINE_INCREMENT 20
34
35 /* Define a character that may be used to comment a line in, e.g.
36  * the variations files.
37  * Use the PGN escape mechanism character, for consistency.
38  */
39 #define COMMENT_CHAR '%'
40
41 char *
42 read_line(FILE *fpin)
43 {   char *line = NULL;
44     unsigned len = 0;
45     unsigned max_length;
46     int ch;
47
48      ch = getc(fpin);
49      if(ch != EOF){
50         line = (char *) MallocOrDie(INIT_LINE_LENGTH + 1);
51         max_length = INIT_LINE_LENGTH;
52         while((ch != '\n') && (ch != '\r') && (ch != EOF)){
53             /* Another character to add. */
54             if(len == max_length){
55                  line = (char *)ReallocOrDie((void *)line,
56                                             max_length+LINE_INCREMENT+1);
57                  if(line == NULL){
58                      return NULL;
59                  }
60                  max_length += LINE_INCREMENT;
61             }
62             line[len] = ch;
63             len++;
64             ch = getc(fpin);
65         }
66         line[len] = '\0';
67         if(ch == '\r') {
68             /* Try to avoid double counting lines in dos-format files. */
69             ch = getc(fpin);
70             if(ch != '\n' && ch != EOF) {
71                 ungetc(ch, fpin);
72             }
73         }
74      }
75      return line;
76 }
77
78         /* Return TRUE if line contains a non-space character, but
79          * is not a comment line.
80          */
81 Boolean
82 non_blank_line(const char *line)
83 {   Boolean blank = TRUE;
84
85     if(line != NULL){
86         if(comment_line(line)){
87             /* Comment lines count as blanks. */
88         }
89         else{
90             while(blank && (*line != '\0')){
91                 if(!isspace((int) *line)){
92                     blank = FALSE;
93                 }
94                 else{
95                     line++;
96                 }
97             }
98         }
99     }
100     return !blank;
101 }
102
103 Boolean
104 blank_line(const char *line)
105 {
106     return !non_blank_line(line);
107 }
108
109
110         /* Should the given line be regarded as a comment line? */
111 Boolean
112 comment_line(const char *line)
113 {
114     return *line == COMMENT_CHAR;
115 }