-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.cpp
More file actions
44 lines (38 loc) · 728 Bytes
/
scanner.cpp
File metadata and controls
44 lines (38 loc) · 728 Bytes
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
//
// Created by kzheng on 2/1/19.
//
#include "scanner.h"
#include <malloc.h>
#define BUFLEN 80
size_t lineLen = 0;
int readPos = -1;
char line[BUFLEN];
int lineNum = 1;
int colNum = 0;
char ch = ' ';
char lastch = ' ';
char scan(FILE *file){
if(!file) return -1;
if(readPos == lineLen - 1) {
lineLen = fread(line, 1, BUFLEN, file);
if(lineLen == 0) {
lineLen = 1;
line[0] = -1;
}
readPos = -1;
}
readPos++;
ch = line[readPos];
if(lastch == '\n') {
lineNum++;
colNum = 0;
}
if(ch == -1) {
fclose(file);
file = NULL;
}else if(ch != '\n') {
colNum++;
}
lastch = ch;
return ch;
}