-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.c
115 lines (107 loc) · 2.16 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
114
115
#include "headers.h"
void fetch_history()
{
long long int i,j,k,flag;
size_t len = 1000;
char * history_datapoint;
FILE *file;
file = fopen("history.txt","r");
if(!file)
{
printf("Error in history.txt");
return;
}
i=0;
for(;;)
{
flag=getline(&history_datapoint,&len,file);
if(flag==-1)
{
break;
}
for(j=strlen(history_datapoint);j>=0;j--)
{
// printf("%c",history_datapoint[j]);
if(history_datapoint[j]!=' ' && history_datapoint[j]!='\n')
{
break;
}
}
// printf("%lld",j);
history_datapoint[j]='\0';
strcpy(history_data[i%20],history_datapoint);
i+=1;
// printf("%s",history_datapoint);
}
hist_len = i;
}
void add_history(char * input_str)
{
long long int i,j,k;
if(input_str[0]!=' ' && input_str[0]!='\n')
{
if(hist_len<20)
{
strcpy(history_data[hist_len],input_str);
hist_len+=1;
}
else
{
for(i=0;i<19;i++)
{
strcpy(history_data[i],history_data[i+1]);
}
strcpy(history_data[i],input_str);
}
}
}
void history(char * input_str)
{
long long int i,j,k,flag=0,start;
int hist_count=10;
char * history_datapoint;
char * token = strtok(input_str," ");
token = strtok(NULL," ");
if(token == NULL)
{
hist_count=10;
}
else
{
hist_count = atoi(token);
}
if(hist_count>20)
{
hist_count=20;
}
if( hist_len-hist_count >= 0)
{
start = hist_len-hist_count;
}
else
{
start = 0;
}
for(j=start;j<20;j++)
{
printf("%s",history_data[j]);
}
}
void save_history()
{
long long int i,j,k,flag;
size_t len = 1000;
char * history_datapoint;
FILE *file;
file = fopen("history.txt","w");
if(!file)
{
printf("Error in history.txt");
return;
}
for(i=0;i<hist_len;i++)
{
fprintf(file,"%s",history_data[i]);
}
fclose(file);
}