Subversion Repositories SvarDOS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2019 mateusz.vi 1
/* get_line - this function will read a string from the file.  The
2
   string may be broken across several lines using a "continue_ch" as
3
   the final character on a line. */
4
 
5
/* THIS FUNCTION WILL malloc() MEMORY FOR THE STRING.  THE CALLING
6
   FUNCTION MUST free() THE STRING!! */
7
 
8
/* Copyright (C) 2000 Jim Hall <jhall1@isd.net> */
9
 
10
/*
11
  This library is free software; you can redistribute it and/or
12
  modify it under the terms of the GNU Lesser General Public
13
  License as published by the Free Software Foundation; either
14
  version 2.1 of the License, or (at your option) any later version.
15
 
16
  This library is distributed in the hope that it will be useful,
17
  but WITHOUT ANY WARRANTY; without even the implied warranty of
18
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19
  Lesser General Public License for more details.
20
 
21
  You should have received a copy of the GNU Lesser General Public
22
  License along with this library; if not, write to the Free Software
23
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
*/
25
 
26
#include <stdio.h>
27
#include <stdlib.h>			/* malloc */
28
 
29
char *
30
get_line (FILE *pfile, int continue_ch)
31
{
32
  char *str;				/* string that we will return   */
33
  char *tmp_str;
34
  int str_len;				/* current length of the string */
35
  int str_size;				/* malloc size of the string    */
36
  int ch;
37
  int last_ch;
38
 
39
  /* this function will malloc memory for the string.  the calling
40
     function must free() the string. */
41
 
42
  str_len = 0;
43
  str_size = 10;
44
  str = malloc (sizeof (char) * str_size);
45
 
46
  if (!str)
47
    {
48
      /* failed! */
49
      return (str);
50
    }
51
 
52
  /* now, read the string */
53
 
54
  last_ch = '\0';
55
  while ((ch = fgetc (pfile)) != EOF)
56
    {
57
      /* do we have enough room in the str for this ch? */
58
 
59
      if (str_len >= str_size)
60
	{
61
	  /* reallocate memory */
62
 
63
	  str_size *= 2;
64
	  tmp_str = realloc (str, sizeof (char) * str_size);
65
 
66
	  if (tmp_str)
67
	    {
68
	      /* move the pointer */
69
	      str = tmp_str;
70
	    }
71
 
72
	  else
73
	    {
74
	      /* failure!  return what we have */
75
	      return (str);
76
	    }
77
	}
78
 
79
      /* add the ch to the str */
80
 
81
      if (ch == '\n')
82
	{
83
	  /* is the string terminated? */
84
 
85
	  if (last_ch == continue_ch)
86
	    {
87
	      /* string is continued on next line ... ignore this ch
88
                 and erase last_ch in the string */
89
	      str_len--;
90
	    }
91
 
92
	  else
93
	    {
94
	      /* string is terminated.  return it. */
95
	      str[str_len++] = '\0';
96
	      return (str);
97
	    }
98
	}
99
 
100
      else
101
	{
102
	  str[str_len++] = ch;
103
	  last_ch = ch;
104
	}
105
 
106
    } /* while */
107
 
108
  /* we hit eof without eol.  return what we have. */
109
 
110
  return (NULL);
111
}