-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.c
113 lines (93 loc) · 3.06 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "history.h"
/*
* Histories 구조체를 생성(할당)하고 HistoryList 도 생성(할당)한다.
*/
Histories* construct_histories(){
Histories* hists = (Histories*)malloc(sizeof(*hists));
hists->list = (HistoryList*)malloc(sizeof(HistoryList));
hists->list->head = (HistoryNode*)malloc(sizeof(HistoryNode));
hists->list->tail = (HistoryNode*)malloc(sizeof(HistoryNode));
hists->list->head->prev = NULL;
hists->list->head->next = hists->list->tail;
hists->list->tail->prev = hists->list->head;
hists->list->tail->next = NULL;
hists->list->head->data = construct_history();
hists->list->tail->data = construct_history();
hists->size = 0;
return hists;
}
/*
* History 구조체를 생성(할당)한다.
*/
History* construct_history(){
History* hist = (History*)malloc(sizeof(*hist));
return hist;
}
/*
* Histories 구조체를 생성하면서 할당했던 모든 메모리를 해제한다.
*/
bool destroy_histories(Histories **histories_state){
HistoryNode *cur;
int i;
cur = ((*histories_state)->list->head);
for(i=0;i<(*histories_state)->size + 1;i++){
cur = (*histories_state)->list->head;
(*histories_state)->list->head = (*histories_state)->list->head->next;
free(cur->data);
free(cur);
}
free((*histories_state)->list->tail->data);
free((*histories_state)->list->tail);
free((*histories_state)->list);
free((*histories_state));
return true;
}
/*
* History 구조체를 생성(할당)하고 value 에 str 문자열을 저장한다.
*/
History* construct_history_with_string(char* str){
History* hist = construct_history();
strncpy(hist->value, str, HISTORY_MAX_LEN);
return hist;
}
/*
* Histories 에 target History 구조체를 저장한다.
*/
bool push_history(Histories* histories_store, History* target){
assert(histories_store);
assert(target);
assert(histories_store->list);
assert(histories_store->list->head);
assert(histories_store->list->tail);
HistoryNode* hist_node = (HistoryNode*)malloc(sizeof(HistoryNode));
hist_node->data = target;
hist_node->prev = histories_store->list->tail->prev;
hist_node->next = histories_store->list->tail;
histories_store->list->tail->prev->next = hist_node;
histories_store->list->tail->prev = hist_node;
histories_store->list->size += 1;
histories_store->size += 1;
return true;
}
/*
* Histories 에 저장된 명령어 히스토리를 출력한다.
*/
void print_history(Histories *histories_store, char *last_command) {
assert(histories_store);
assert(last_command);
assert(histories_store->list);
assert(histories_store->list->head);
assert(histories_store->list->tail);
HistoryNode** cur = &histories_store->list->head;
int i = 0;
for(i=0;i<histories_store->size + 1;i++){
if(i == 0){
cur = &((*cur)->next);
continue;
}
printf("%-4d %s", i, (char*)(*cur)->data);
cur = &((*cur)->next);
}
printf("%-4d %s", i, last_command);
printf("\n");
}