-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.c
149 lines (136 loc) · 3.37 KB
/
table.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "task.h"
#include "lib.h"
#include "table.h"
#define maxhist 20
/*********************************************************
* this table is a data structure we referenced from internet
* Description : It provides functions: push, pop and get elements
* in table. Table functioned as a container like
* queue.
*/
static char buffer[NUM_TERMINAL * maxhist ][1024];
static int buffer_cur[NUM_TERMINAL];
static int buffer_total[NUM_TERMINAL];
/* init_table
* Description: initial the table
* Input: N/A
* Output: N/A
* Return value: N/A
* Side effect: clear the buffer.
*/
void init_table(void)
{
int i,j,k;
for(i = 0; i < NUM_TERMINAL;i++)
{
for(j = 0; j < maxhist;j++)
for(k = 0; k < 1024;k++)
{
buffer[i*maxhist + j ][k] = 0;
}
buffer_cur[i] = 0;
}
}
/* uptable
* Description: increase the table
* Input: -- buf: store the data from the terminal
* -- terminal: current terminal
* Output: N/A
* Return value: the length stored
* Side effect: N/A
*/
int uptable(char * buf, uint32_t terminal)
{
int k;
// if(buffer_cur[terminal] < buffer_total[terminal])
// {
if((buffer_cur[terminal]) == 0)
{
return strlen(buf);
}
else
{
buffer_cur[terminal]--;
for(k = 0; k < 1024;k++)
{
buf[k]=buffer[terminal*maxhist + buffer_cur[terminal]][k];
}
return strlen(buffer[terminal*maxhist + buffer_cur[terminal]]);
}
// }
// else
// {
// for(k = 0; k < strlen(buf);k++)
// {
// if((buf[k] != '\n') && (buf[k] != '\0' ))
// buffer[terminal*maxhist + buffer_total[terminal]][k] = buf[k];
// }
// buffer_total[terminal]++;
// buffer_cur[terminal] = buffer_total[terminal];
// }
}
/* downtable
* Description: decrease the table
* Input: -- buf: store the data from the terminal
* -- terminal: current terminal
* Output: N/A
* Return value: the length stored
* Side effect: N/A
*/
int downtable(char * buf, uint32_t terminal)
{
int k;
if((buffer_cur[terminal] ) == buffer_total[terminal])
{
return strlen(buf);
}
else
{
buffer_cur[terminal]++;
for(k = 0; k < 1024;k++)
{
buf[k] = buffer[terminal*maxhist + buffer_cur[terminal]][k] ;
}
return strlen(buf);
}
}
/* pushtable
* Description: push the table
* Input: -- buf: pop the data stored
* -- terminal: current terminal
* Output: N/A
* Return value: the length stored
* Side effect: if the content reaches maximum, clear the oldest one.
*/
void pushtable(char * buf, uint32_t terminal)
{
int j,k;
if((buffer_total[terminal]) == maxhist)
{
for(j = 0; j <(maxhist - 1);j++)
for(k = 0; k < 1024;k++)
{
buffer[terminal*maxhist + j ][k] = buffer[terminal*maxhist + j + 1][k];
}
for(k = 0; k < 1024;k++)
{
if((buf[k] != '\n') && (buf[k] != '\0' ))
buffer[terminal*maxhist + j][k] = buf[k];
else
buffer[terminal*maxhist + j][k] = 0;
}
buffer_cur[terminal] = buffer_total[terminal];
}
else
{
if((buf[0] == '\n') || (buf[0] == '\0' ))
return;
for(k = 0; k < 1024;k++)
{
if((buf[k] != '\n') && (buf[k] != '\0' ))
buffer[terminal*maxhist + buffer_total[terminal]][k] = buf[k];
}
buffer_total[terminal]++;
buffer_cur[terminal] = buffer_total[terminal];
}
}