File: read_line.c

package info (click to toggle)
numdiff 5.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,680 kB
  • sloc: ansic: 10,808; sh: 3,383; makefile: 276
file content (171 lines) | stat: -rw-r--r-- 4,020 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
    Numdiff - compare putatively similar files, 
    ignoring small numeric differences
    Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017  Ivano Primi  <ivprimi@libero.it>

    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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
*/

#ifdef _TEST_READ_LINE_

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define BUFF_SIZE 32

/* Error codes */
#define OK             0
#define LINE_INTERR    1
#define EOF_REACHED    2
#define READING_ERROR  3
#define OUT_OF_MEMORY  4
/* *** */
#define OPEN_FAILED    5
#define WRONG_USAGE    6
#define FILE_IS_BINARY 7

#endif /* _TEST_READ_LINE_ */

char* read_line (FILE* pf, int* errcode)
{
  char buffer[BUFF_SIZE];
  char *ptr, *line = NULL;
  size_t lline = 0;

  register size_t n;
  register int ch;
  int exception_occurred = 0;

  do
    {
      for (n = 0; n < BUFF_SIZE-1 && (ch = getc(pf)) != EOF && ch != '\0' && ch != '\n'; n++)
	buffer[n] = ch;
      if ( !(exception_occurred = ((ch == EOF && n == 0) || ch == '\0')) )
	{
	  /* 
	     If we enter this block, then CH is not the nul character. 
	     Thus either is n == BUFF_SIZE-1, or is CH == EOF or is CH == NEWLINE.
	   */
	  if (n == BUFF_SIZE-1 || ch == EOF)
	    {
	      buffer[n] = '\0';
	      lline += n;
	    }
	  else
	    {
	      buffer[n] = '\n';
	      buffer[n+1] = '\0';
	      lline += (n+1);	      
	    }
	  if (!line)
	    ptr = (char*) calloc (lline + 1, sizeof(char));
	  else
	    ptr = (char*) realloc ((void*)line, (lline + 1) * sizeof(char));
	  if (!ptr)
	    {
	      if ((line))
		free ((void*)line);
	      *errcode = OUT_OF_MEMORY;
	      return NULL;
	    }
	  else
	    {
	      line = ptr;
	      strcat (line, buffer);
	    }
	  if (lline > 0 && line[lline-1] == '\n')
	    break;
	}
    } while (!exception_occurred);
  if ((exception_occurred))
    {
      if ( (ferror(pf)) )
	*errcode = READING_ERROR;
      else if ( ch == '\0' )
	*errcode = FILE_IS_BINARY;
      else if (lline > 0)
	*errcode = LINE_INTERR;
      else
	*errcode = EOF_REACHED; 
    }
  else
    *errcode = OK;
  return line;
} 

#ifdef _TEST_READ_LINE_

int main (int argc, char* argv[])
{
  FILE* fp;

  if (argc != 2)
    {
      fprintf (stderr, "\n***  Usage: %s FILEPATH\n", argv[0]);
      return 1;
    }
  else if ( !(fp = fopen (argv[1], "r")) )
    {
      fprintf (stderr, "\n*** Cannot open \"%s\" for reading:\n", argv[1]);
      perror(NULL);
      return 1;
    }
  else
    {
      char* line = NULL;
      int errcode = OK;

      do
	{
	  line = read_line (fp, &errcode);
	  switch (errcode)
	    {
	    case FILE_IS_BINARY:
	      fputs ("\n***  Cannot read from a binary file\n", stderr);
	      break;	      
	    case LINE_INTERR:
	      fputs (line, stdout);
	      puts ("<LNINTERR>");
	      break;
	    case READING_ERROR:
	      fputs ("\n***  Error while reading from file\n", stderr);
	      break;
	    case OUT_OF_MEMORY:
	      fputs ("\n***  Memory exhausted while reading from file, abort now...\n", stderr);
	      break;
	    case EOF_REACHED:
	      puts ("<EOF>");
	      break;
	    default: /* OK */
	      fputs (line, stdout);
	      break;
	    }
	  if ((line))
	    free ((void*)line);
	} while (errcode == OK);
      if ( (fclose(fp)) )
	{
	  fprintf (stderr, "\n*** Could not close \"%s\":\n", argv[1]);
	  perror(NULL);
	  return 1;
	}
      else
	{
	  return 0;
	}
    }
}

#endif /* _TEST_READ_LINE_ */