-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbook db
More file actions
341 lines (299 loc) · 8.3 KB
/
Copy pathbook db
File metadata and controls
341 lines (299 loc) · 8.3 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
struct book_node{
char* author;
char* name_of_book;
int all;
int available;
} ;
struct node{
int isbn;
struct book_node* content;
struct node* next;
};
typedef struct node xnode;
xnode* root = NULL;
void clear_xnode_bk(xnode* node){
free(node->content->author);
free(node->content->name_of_book);
free(node->content);
free(node);
}
char* dynstring(char c) {
int l = 0;
int size = 1;
char *string = (char*)malloc(size * sizeof(char));
char ch = getchar();
while (ch != c) {
string[(l)++] = ch;
if (l >= size) {
size *= 2;
string = (char*)realloc(string, size * sizeof(char));
}
ch = getchar();
}
string[l] = ',';
return string;
}
char* fdynstring(FILE* fp, char ch){
int l = 0;
char buf;
for(;(buf = fgetc(fp)) != ch && buf != EOF; ++l);
if (buf == EOF)
fseek(fp, -1*(l), SEEK_CUR);
else if (buf =='\n')
fseek(fp, -1*(l+2), SEEK_CUR);
else
fseek(fp, -1*(l+1), SEEK_CUR);
char* a = (char*)malloc((l + 1) * sizeof(char));
for (int i = 0; i < l; ++i)
a[i]=fgetc(fp);
a[l] = '\0';
if (buf != EOF)
fgetc(fp);
else
fseek(fp, -1, 2);
return a;
}
char* timeinf(){
const time_t timer = time(NULL);
char* tm =(char*)malloc(21*sizeof(char));
struct tm* timeinf = localtime(&timer);
strftime (tm,21,"%d_%m_%Y_%H.%M.%S.",timeinf);
//printf("%s\n",tm);
return tm;
}
void add(xnode* current,int isbn,struct book_node* content){
if (current->next == NULL ){
current->next = (xnode*)malloc(sizeof(xnode));
current->next->isbn = isbn;
current->next->next = NULL;
current->next->content = content;
} else if(strcmp(isbn,current->next->isbn)<0){
xnode* temp = (xnode *)malloc(sizeof(xnode));
temp->isbn=isbn;
temp->content = content;
temp->next = current->next;
current->next = temp;
} else {
if (strcmp(isbn,current->next->isbn)){
add(current->next, isbn, content);
} else {
current->content = content;
}
}
}
void add_first(xnode* current, int isbn, struct book_node* content){
if (current == NULL){
root = (xnode*)malloc(sizeof(xnode));
root->isbn = isbn;
root->next = NULL;
root->content = content;
} else if (strcmp(isbn,current->isbn) < 0){
xnode* nde = (xnode*)malloc(sizeof(xnode));
nde->isbn = isbn;
nde->next = root;
nde->content = content;
root = nde;
} else if (strcmp(isbn,current->isbn)){
add(current, isbn, content);
} else {
current->content = content;
}
}
void del(xnode* current,int isbn){
if (isbn != current->next->isbn){
del(current->next,isbn);
}
xnode* temp = current->next;
current->next = current->next->next;
free(temp);
}
void del_first(xnode* current,int isbn){
if(current->isbn != isbn){
del(current,isbn);
} else {
xnode* temp = current->next;
free(current);
root = temp;
}
}
void delfull(xnode* current){
if (current == NULL){
return;
}
xnode* next = current->next;
clear_xnode_bk(current);
delfull(next);
}
void delfullfirst(xnode* current){
delfull(current);
root = NULL;
}
void base_out(xnode* current,FILE* fth){
fprintf(fth,"%d;",current->isbn);
fprintf(fth,"%s;",current->content->author);
fprintf(fth,"%s;",current->content->name_of_book);
fprintf(fth,"%d;",current->content->all);
fprintf(fth,"%d\n",current->content->available);
};
void list(xnode* current,FILE* fth){
if (current == NULL){
return;
}
if (fth == NULL){
return; //error
}
base_out(root,stdout);
list(current->next, fth);
}
xnode* find(xnode* current,char* number){
if (current == NULL){
return NULL; //error
}
if (!strcmp(number,current->isbn)){
base_out(current,stdout);
}
return find(current->next,number);
}
xnode* find_edit(xnode* current,char* number){
if (current == NULL){
return NULL; //error
}
if (!strcmp(number,current->isbn)){
return current;
}
return find_edit(current->next,number);
}
xnode* find_by_surname(xnode* current,char* surname){
if (current == NULL){
return NULL; //error
}
if (!(strcmp(surname ,current->content->author))){
base_out(current,stdout);
}
return find_by_surname(current->next, surname);
}
//--book
void give_book(xnode* current, int isbn){
if (current == NULL){
return; //error
}
current = find(current,isbn);
if (current -> content -> available != 0){
--current -> content -> available;
} else
printf("Out of books");// error
}
//++book
void bring_book (xnode* current, int isbn){
if (current == NULL){
return; //error
}
current = find(current,isbn);
++current -> content -> available;
}
void edit_book(){
printf("Welcome to Edit Menu\n");
printf("write:\n");
printf("isbn\n");
printf("name of column\n");
printf("value\n");
int isbn = atoi(dynstring('\n'));
char* cell = dynstring('\n');
char* value = dynstring('\n');
xnode* curr=find_edit(root,isbn);
if (!strcmp("author", cell)){
free(curr->content->author);
curr->content->author = value;
} else if (!strcmp("name of book", cell)) {
free(curr->content->name_of_book);
curr->content->name_of_book = value;
} else if (!strcmp("all count", cell)) {
curr->content->all = atoi(value);
} else if (!strcmp("available", cell)) {
curr->content->available = atoi(value);
} else {
//10/5 chertovikh errorov
}
}
void backup(){
char* tmt = timeinf();
char* book = "book_";
char* flnm = "C:\\Users\\Timur\\Desktop\\Database\\Backups\\";
char* filename = calloc(strlen(tmt) + strlen(book) + 1, 1);
strcat(filename,flnm);
strcat(filename,book);
strcat(filename,tmt);
strcat(filename,"csv");
FILE* fth1 = fopen(filename,"w");
list(root,fth1);
FILE* fth2 = fopen("C:\\Users\\Timur\\Desktop\\Database\\Backups\\BackupsLibrary.txt","a+"); //C:\Users\Timur\Desktop\Database\Backups
fputs(filename,fth2);
fputc('\n',fth2);
fclose(fth1);
fclose(fth2);
printf("Backup saved\n");
}
void backup_out(){
delfullfirst(root);
printf("Choose file of backup\n");
FILE* fth = fopen("C:\\Users\\Timur\\Desktop\\Database\\Backups\\BackupsLibrary.txt","r");
if (fth == NULL){
//error
}
char ch;
while((ch = getc(fth)) != EOF){
putchar(ch);
}
fclose(fth);
char* string = dynstring('\n');
char* way = "C:\\Users\\Timur\\Desktop\\Database\\Backups\\";
char* str_bck = calloc(strlen(way) + strlen(string) + 1, 1);
strcat(str_bck,way);
strcat(str_bck,string);
FILE *fth1, *fth2;
// Open one file for reading
fth1 = fopen(str_bck, "r");
if (fth1 == NULL){
printf("Cannot open file %s \n", str_bck);
//error(0);
}
// Open another file for writing
fth2 = fopen("C:\\Users\\Timur\\Desktop\\Database\\Databases\\Library.csv", "w");
if (fth2 == NULL)
{
printf("Cannot open file %s \n", "Library.csv");
//error();
}
// Read contents from file
while ((ch = fgetc(fth1)) != EOF) { //(ch = fgetc(fth1)) != EOF
fputc(ch, fth2);
}
//fputc(EOF,fth2);
//fseek(fth1,0,0);
printf("\nBackup successful recover\n");
fclose(fth1);
fclose(fth2);
}
void base_to_stack(){
FILE* fth1 = fopen("C:\\Users\\Timur\\Desktop\\Database\\Databases\\Library.csv","r");
while(fgetc(fth1)!= EOF){
fseek(fth1,-1,1);
struct book_node* cont = (struct book_node*)malloc(sizeof(struct book_node));
//char* isbn = fdynstring(fth1,';');
cont->author = fdynstring(fth1,';');
//printf("\n%s\n",cont->surname);
cont->name_of_book = fdynstring(fth1,';');
//printf("\n%s\n",cont->name);
cont->all = fdynstring(fth1,';');
//printf("\n%s\n",cont->patronym);
cont->available = fdynstring(fth1,';');
//printf("\n%s\n",cont->facility);
add_first(root,isbn,cont);
}
}
int main() {
}