-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.c
66 lines (53 loc) · 1.01 KB
/
source.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
/*
@author: Huzaifa Naseer
*/
#include "source.h"
#include "shell.h"
#include <errno.h>
void unget_char(struct source_s *src) {
if (src->curpos < 0) {
return;
}
src->curpos--;
}
char next_char(struct source_s *src) {
if (!src || !src->buffer) {
errno = ENODATA;
return ERRCHAR;
}
char c1 = 0;
if (src->curpos == INIT_SRC_POS) {
src->curpos = -1;
} else {
c1 = src->buffer[src->curpos];
}
if (++src->curpos >= src->bufsize) {
src->curpos = src->bufsize;
return EOF;
}
return src->buffer[src->curpos];
}
char peek_char(struct source_s *src) {
if (!src || !src->buffer) {
errno = ENODATA;
return ERRCHAR;
}
long pos = src->curpos;
if (pos == INIT_SRC_POS) {
pos++;
}
pos++;
if (pos >= src->bufsize) {
return EOF;
}
return src->buffer[pos];
}
void skip_white_spaces(struct source_s *src) {
char c;
if (!src || !src->buffer) {
return;
}
while (((c == peek_char(src)) != EOF) && (c == ' ' || c == '\t')) {
next_char(src);
}
}