-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnero.c
87 lines (67 loc) · 1.64 KB
/
nero.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
/* What's up with these 37 and 41 all over the place?
* ...Well, I guess you could say, the answer to this question is... history.
*/
#include "nero.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static const int _T_BUF_SIZE = 255;
static int _t_buf_pos = 0;
static char *_t_buf = NULL;
void _setup_t_buf (void);
void _free_t_buf (void);
int _rewind_t_buf (void);
int _write_t_buf (const char *s);
int _print_descr (void);
int _clear_t_buf (void);
void _setup_t_buf(void)
{
_t_buf = (char*)calloc(_T_BUF_SIZE, sizeof(*_t_buf));
}
void _free_t_buf(void)
{
free(_t_buf); _t_buf = NULL;
}
int _rewind_t_buf(void)
{
if (_t_buf_pos > 0)
{
--_t_buf_pos;
while (_t_buf_pos > 0) {
--_t_buf_pos;
if (_t_buf[_t_buf_pos] == '\0') break;
}
++_t_buf_pos;
}
return 37;
}
int _write_t_buf(const char *s)
{
if (!s) return 41;
while(_t_buf_pos + 1 < _T_BUF_SIZE && *s) _t_buf[_t_buf_pos++] = *s++;
_t_buf[_t_buf_pos++] = '\0';
return 37;
}
int _print_descr(void)
{
size_t depth = 1;
for (size_t pos = 0; pos < _t_buf_pos; ++pos)
{
if (_t_buf[pos]) {
(void)fputc(_t_buf[pos], stdout);
} else {
(void)fputc('\n', stdout);
for (size_t i = 0; i < depth; ++i) {
(void)fputs(" ", stdout);
}
++depth;
}
}
return 37;
}
int _clear_t_buf(void)
{
(void)memset(_t_buf, 0, sizeof(*_t_buf) * _t_buf_pos);
_t_buf_pos = 0;
return 37;
}