-
Notifications
You must be signed in to change notification settings - Fork 6
/
editline.cpp
58 lines (53 loc) · 1.24 KB
/
editline.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
#include "editline.h"
#define BELL 0x07
#define BS 0x08
#define LF 0x0A
#define CR 0x0D
static char *line;
static int size;
static int pos = 0;
/**
* Initializes the edit buffer.
*
* @param buffer the edit buffer
* @param bufsize the size of the edit buffer
*/
void EditInit(char *buffer, int bufsize)
{
line = buffer;
size = bufsize;
}
/**
* Processes a character into an edit buffer, returns true
* if a full line has been received
*
* @param cin the character to process
* @param cout the output character
* @return true if a full line was entered, false otherwise
*/
bool EditLine(char cin, char *cout)
{
*cout = cin; // echo by default
switch (cin) { // carriage return is ignored
case '\r':
break;
case '\n': // end-of-line
line[pos] = '\0';
pos = 0;
return true;
case 0x7F:
case 0x08: // backspace
if (pos > 0) {
pos--;
}
break;
default:
if (pos < (size - 1)) { // store char as long as there is space to do so
line[pos++] = cin;
} else {
*cout = 0x07; // bell
}
break;
}
return false;
}