-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.cpp
60 lines (51 loc) · 1.24 KB
/
input.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
//
// Created by Adam Zvada on 17.06.17.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "input.h"
#define MAX_LINE_LENGTH 257
/**
* staticky definované pole znaků kde se ukládají jednotlivé řádky
*/
char line[MAX_LINE_LENGTH];
int lineNumber = 0;
char *linePointer = line;
FILE *inputFile;
int extendedLine = 0;
/**
* inicializace vstupu na standardní vstup nebo na vstup ze souboru
* standardní vstup se zvolí pokud fileName je NULL
*/
int initInput(const char* fileName) {
if (!fileName) {
inputFile = stdin;
} else {
inputFile = fopen(fileName, "rt");
if (!inputFile) {
printf("Vstupni soubor %s nenalezen.\n", fileName);
return 0;
}
}
return 1;
}
/**
* přečte jeden symbol ze vstupu
*/
int getChar() {
if (!*linePointer) {
if (!fgets(line, MAX_LINE_LENGTH, inputFile)) return EOF;
linePointer = line;
lineNumber++;
int lineLength = strlen(line);
if (extendedLine) {
lineNumber--;
printf("+ %s", line);
} else {
printf("%-4d %s", lineNumber, line);
}
extendedLine = line[lineLength - 1] != '\n';
}
return *linePointer++;
}