-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrfile.cpp
157 lines (133 loc) · 3.71 KB
/
trfile.cpp
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
/*
Copyright (c) 2016 Walter William Karas
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
functions for reading a file and tracking the last
character read, and to print an error message indicating
last character read before error.
*/
#include <stdio.h>
#include <string.h>
#include "trfile.h"
/*
function to open file for tracked reading.
returns 0 for success, -1 for error.
*/
int tr_open
(
/* pointer to descriptor for file */
TR_DESC *t,
/* name of file to open. if null pointer, stdin used */
const char *fn
)
{
if (fn == (const char *) 0)
{
t->file_p = stdin;
/* save file name */
(void) strcpy(t->file_name,"standard input");
}
else
{
/* open file for reading */
t->file_p = fopen(fn,"r");
if (t->file_p == (FILE *) 0)
return(S_TR_OPEN);
/* save file name */
(void) strncpy(t->file_name,fn,TR_MAX_LEN_FILE_NAME);
/* insure proper termination */
(t->file_name)[TR_MAX_LEN_FILE_NAME] = (char) '\0';
}
/* reset counters */
t->line_no = 1;
t->char_no = 0;
/* create empty buffer: will trigger a read */
(t->line)[0] = (char) '\0';
return(S_TR_GOOD);
}
/*
function to get a character from file.
*/
int tr_getc
(
/* pointer to descriptor for file */
TR_DESC *t,
/* variable to put character into */
char *c
)
{
while ((t->line)[t->char_no] == (char) '\0')
{
if (fgets(t->line,(TR_MAX_LEN_LINE + 2),t->file_p)
== (char *) 0)
{
if (feof(t->file_p))
return(S_TR_EOF);
else
return(S_TR_READ);
}
t->char_no = 0;
}
*c = (t->line)[(t->char_no)++];
if (*c == (char) '\n')
(t->line_no)++;
return(S_TR_GOOD);
}
/*
function to print error message along with number
of current line, text of current line, pointer to
last character read.
*/
int tr_print_error
(
/* pointer to descriptor for file */
TR_DESC *t,
/* string containing error message */
const char *e_msg
)
{
int i;
if (fprintf(stderr,"error in line %d of %s:\n %s\n",
t->line_no,t->file_name,e_msg) < 0)
return(S_TR_MESSAGE);
/* make sure line has end-of-line */
(t->line)[strlen(t->line) - 1] = (char) '\n';
/* output line */
if (fputs(t->line,stderr) != 0)
return(S_TR_MESSAGE);
/* output pointer to last character read */
(t->char_no)--;
for (i = 0; i < t->char_no; i++)
if (fputc((char) ' ',stderr) == EOF)
return(S_TR_MESSAGE);
if (fputs("^\n",stderr) != 0)
return(S_TR_MESSAGE);
return(S_TR_GOOD);
}
/*
function to close file
*/
int tr_close
(
/* pointer to descriptor for file */
TR_DESC *t
)
{
if (t->file_p != stdin)
if (fclose(t->file_p) == EOF)
return(S_TR_CLOSE);
return(S_TR_GOOD);
}