File: lines.c

package info (click to toggle)
pgn-extract 16.7-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 936 kB
  • ctags: 722
  • sloc: ansic: 9,243; makefile: 63
file content (108 lines) | stat: -rw-r--r-- 2,922 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 *  Program: pgn-extract: a Portable Game Notation (PGN) extractor.
 *  Copyright (C) 1994-2005 David Barnes
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 1, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  David Barnes may be contacted as D.J.Barnes@kent.ac.uk
 *  http://www.cs.kent.ac.uk/people/staff/djb/
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "bool.h"
#include "mymalloc.h"
#include "lines.h"

    /* Read a single line of input. */
#define INIT_LINE_LENGTH 80
#define LINE_INCREMENT 20

/* Define a character that may be used to comment a line in, e.g.
 * the variations files.
 * Use the PGN escape mechanism character, for consistency.
 */
#define COMMENT_CHAR '%'

char *
read_line(FILE *fpin)
{   char *line = NULL;
    unsigned len = 0;
    unsigned max_length;
    int ch;

     ch = getc(fpin);
     if(ch != EOF){
        line = (char *) MallocOrDie(INIT_LINE_LENGTH + 1);
        max_length = INIT_LINE_LENGTH;
        while((ch != '\n') && (ch != EOF)){
            /* Another character to add. */
            if(len == max_length){
                 line = (char *)ReallocOrDie((void *)line,
                                            max_length+LINE_INCREMENT+1);
                 if(line == NULL){
                     return NULL;
                 }
                 max_length += LINE_INCREMENT;
            }
            line[len] = ch;
            len++;
            ch = getc(fpin);
        }
        line[len] = '\0';
     }
     return line;
}

        /* Return TRUE if line contains a non-space character, but
         * is not a comment line.
         */
Boolean
non_blank_line(const char *line)
{   Boolean blank = TRUE;

    if(line != NULL){
        if(comment_line(line)){
            /* Comment lines count as blanks. */
        }
        else{
            while(blank && (*line != '\0')){
                if(!isspace((int) *line)){
                    blank = FALSE;
                }
                else{
                    line++;
                }
            }
        }
    }
    return !blank;
}

Boolean
blank_line(const char *line)
{
    return !non_blank_line(line);
}


        /* Should the given line be regarded as a comment line? */
Boolean
comment_line(const char *line)
{
    return *line == COMMENT_CHAR;
}