-
Notifications
You must be signed in to change notification settings - Fork 5
/
history.c
73 lines (60 loc) · 1.9 KB
/
history.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
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "history.h"
#include "command_def.h"
#include "list.h"
struct history_manager
{
struct list *history_list; // history들을 관리하는 링크드 리스트
};
struct history
{
char command_str[COMMAND_INPUT_MAX_LEN+1];
};
/* history manager 내의 history_list에 사용될 node 구조체 */
struct history_node
{
struct history history;
struct list_node list_node;
};
/* history manager를 생성하는 factory 함수 */
struct history_manager *history_manager_construct ()
{
struct history_manager *manager = malloc (sizeof(*manager));
manager->history_list = list_construct ();
return manager;
}
/* history manager를 소멸시키는 함수 */
void history_manager_destroy (struct history_manager *manager)
{
struct list_node *node;
while ((node = list_pop_front (manager->history_list)))
{
free (list_entry (node, struct history_node, list_node));
}
list_destroy (manager->history_list);
free (manager);
}
/* history manager에 history를 추가하는 함수 */
void history_insert (struct history_manager *manager, const char *command_str)
{
struct history_node *node = malloc (sizeof(*node));
strncpy (node->history.command_str, command_str, COMMAND_INPUT_MAX_LEN);
list_push_back (manager->history_list, &node->list_node);
}
/* history manager내에 있는 history 들을 출력하는 함수 */
void history_print (struct history_manager *manager, const char *cur_command_str)
{
struct list_node *node;
int no = 0;
for (node = list_begin (manager->history_list);
node != list_end (manager->history_list);
node = list_next (node))
{
struct history_node *history_node = list_entry (node, struct history_node, list_node);
printf ("%-4d %s", ++no, history_node->history.command_str);
}
printf ("%-4d %s", ++no, cur_command_str);
}