-
Notifications
You must be signed in to change notification settings - Fork 0
/
IOMngr.c
194 lines (171 loc) · 3.74 KB
/
IOMngr.c
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* @file IOMngr.c
* @author Dakota Kallas
* @brief This module will provide the sole access
* point for reading characters from the source program
* @version 0.1
* @date 2022-09-20
*
* @copyright Copyright (c) 2022
*
*/
#include <string.h>
#include <math.h>
#include <ctype.h>
#include "IOMngr.h"
FILE *sourceFile;
FILE *listingFile;
int currentLine = 1;
int currentColumn = -1;
char line[MAXLINE + 1];
/**
* @brief Open the source file whose name is given in sourceName
* If listingName is not NULL open the listing file whose name is given in listingName
* If listingName is NULL, the output goes to stdout
*
* PRE: You can assume sourceName is assigned a legal
* @param sourceName represents the name of the source file
* @param listingName represents the name of the listing file to use
* @return 1 if the file open(s) were successful, otherwise return 0
*
* Check for listingname == NULL first
*/
int openFiles(char *source, char *listingName)
{
sourceFile = NULL;
listingFile = NULL;
sourceFile = fopen(source, "r");
if (sourceFile == NULL)
{
return 0;
}
if (listingName)
{
listingFile = fopen(listingName, "w");
if (listingFile == NULL)
{
return 0;
}
}
currentLine = 1;
currentColumn = -1;
fgets(line, MAXLINE, sourceFile);
return 1;
}
/**
* @brief Close the source file and the listing file if one was created
*/
void closeFiles()
{
if (sourceFile != NULL)
{
fclose(sourceFile);
}
if (listingFile != NULL)
{
fclose(listingFile);
}
}
/**
* @brief Get the Next Source Char object
*
* @return char
*/
char getNextSourceChar()
{
if (sourceFile)
{
// Look to see if you need to look at a new line
if (line[currentColumn] == '\n' || ((int)(strlen(line)) == currentColumn))
{
currentColumn = -1;
if (feof(sourceFile))
{
return EOF;
}
currentLine++;
line[0] = '\0';
fgets(line, MAXLINE, sourceFile);
if (listingFile)
{
fprintf(listingFile, "%d. %s", currentLine, line);
}
}
currentColumn++;
return line[currentColumn];
}
else
{
return EOF;
}
}
/**
* @brief Get the Current Line Num object
*
* @return int
*/
int getCurrentLineNum()
{
return currentLine;
}
/**
* @brief Get the Current Column Num object
*
* @return int
*/
int getCurrentColumnNum()
{
return currentColumn;
}
/**
* @brief Write the message on a separate line
*
* @param message represents the message that's written on the line
*/
void writeMessage(char *message)
{
if (listingFile == NULL)
{
fprintf(stdout, "%s\n", message);
}
else
{
fprintf(listingFile, "%s\n", message);
}
}
/**
* @brief Write a line containing a single ‘^’ character in the indicated column
*
* @param column represents the column number that the indicator needs to be written into
*/
void writeIndicator(int column)
{
FILE *writeTo = stdout;
if (listingFile != NULL)
{
writeTo = listingFile;
fprintf(writeTo, " ");
for (double i = 1; i < floor(log(abs(currentLine))) + 1; i++)
{
fprintf(writeTo, " ");
}
}
else
{
fprintf(writeTo, "%s", line);
}
if (strchr(line, '\n') == NULL)
{
fprintf(writeTo, "\n");
fprintf(writeTo, " ");
for (double i = 1; i < floor(log(abs(currentLine))) + 1; i++)
{
fprintf(writeTo, " ");
}
}
for (int i = 0; i < column; i++)
{
fprintf(writeTo, "%s", " ");
}
fprintf(writeTo, "%s\n", "^");
}