-
Notifications
You must be signed in to change notification settings - Fork 3
/
first.c
3761 lines (3418 loc) · 107 KB
/
first.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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<signal.h>
#include<stdbool.h>
#define MAX_TMP 15
#define MAX_LINKED_NUMBER 5
int savet_ = 0, save_g= 0, save_d= 0, save_y= 0, save_r= 0, save_a= 0;//옵션을 받았다면 1값으로 바뀌어야 한다
int save_n = 0, save_s = 0, save_b = 0, save_m = 0;
int updatet_ = 0, update_g= 0, update_d= 0, update_y= 0, update_r= 0, update_a= 0;//옵션을 받았다면 1값으로 바뀌어야 한다
int update_n = 0, update_s = 0, update_b = 0, update_m = 0;
int update_num=0;
FILE *ofp;
int file_number=0;
struct movie{
int serial_number; // 영화 시리얼 넘버
char *title; // 영화 이름 포인터
char *genre; // 장르 이름 포인터
struct director_in_movie *director_m; // 감독명 및, 해당 디렉터 노드 정보 저장
int year;
int movie_time;
struct actor_in_movie *actor_m ; // 배우명 및, 다음 배우 구조체 노드, 해당 배우 구조체 노드
struct movie *next; // 다음 movie의 구조체
} ;
struct director_in_movie { // 감독명 및, 해당 디렉터 노드 정보 저장
char *name;
struct director *director_node;
} ;
struct actor_in_movie { // 배우명 및, 다음 배우 구조체 노드, 해당 배우 구조체 노드
char *name;
struct actor_in_movie *next;
struct actor *actor_node;
} ;
struct director {
int serial_number;
char *name; // 감독 이름 포인터
char sex; //성별
char *birth; // 생년월일 포인터
struct movie_in_director *movie_d; // 대표작 포인터로 대표작명, 다음 대표작 자기참조 포인터, 해당 대표작 구조체 노드
struct director *next; // 다음 감독 노드
} ;
struct movie_in_director { // 대표작 포인터로 대표작명, 다음 대표작 자기참조 포인터, 해당 대표작 구조체 노드
char *name; // 영화 이름
struct movie_in_director *next; // 다음 영화 노드
struct movie *movie_node; // 해당 영화 구조체 노드
} ;
struct actor {
int serial_number; //시리얼 넘버
char *name; // 이름 포인터
char sex; // 성별 정보
char *birth; // 생일 포인터
struct movie_in_actor *movie_a; // 출연작 영화 구조체 퐁니터
struct actor *next; // 다음 배우 구조체 노드
} ;
struct movie_in_actor {
char *name; // 영화 이름 포인터
struct movie_in_actor *next; //다음 영화 노드
struct movie *movie_node; // 해당 영화 구조체 노드
};
struct main_struct_ptr {
struct movie *movie_ptr;
struct actor *actor_ptr;
struct director *director_ptr;
};
void get_data(struct main_struct_ptr *data_ptr);
void scan_word(FILE *optr, char *tmp_data);
int str_devider (char *str);
void get_movie(struct main_struct_ptr *data_ptr);
void add_movie (FILE *optr, struct movie *movie_ptr);
void scan_serial_number_m(FILE *optr, struct movie *movie_ptr);
void scan_title_m(FILE *optr, struct movie *movie_ptr);
void scan_genre_m(FILE *optr, struct movie *movie_ptr);
void scan_director_m(FILE *optr, struct movie *movie_ptr);
void scan_year_m(FILE *optr, struct movie *movie_ptr);
void scan_movie_time_m(FILE *optr, struct movie *movie_ptr);
void scan_actor_m(FILE *optr, struct movie *movie_ptr);
void get_director(struct main_struct_ptr *data_ptr);
void add_director (FILE *optr, struct director *director_ptr);
void scan_serial_number_d(FILE *optr, struct director *director_ptr);
void scan_name_d(FILE *optr, struct director *director_ptr);
void scan_sex_d(FILE *optr, struct director *director_ptr);
void scan_birth_d(FILE *optr, struct director *director_ptr);
void scan_title_d(FILE *optr, struct director *director_ptr);
void get_actor(struct main_struct_ptr *data_ptr);
void add_actor (FILE *optr, struct actor *actor_ptr);
void scan_serial_number_a(FILE *optr, struct actor *actor_ptr);
void scan_name_a(FILE *optr, struct actor *actor_ptr); // actor_name
void scan_title_a(FILE *optr, struct actor *actor_ptr);
void scan_sex_a(FILE *optr, struct actor *actor_ptr);
void scan_birth_a(FILE *optr, struct actor *actor_ptr);
void scan_title_a(FILE *optr, struct actor *actor_ptr);
void menu(FILE *optr_m,FILE *optr_d,FILE *optr_a,char *input, struct main_struct_ptr *data_ptr );
void Print_m(struct main_struct_ptr *data_ptr);
void Print_d(struct main_struct_ptr *data_ptr);
void Print_a(struct main_struct_ptr *data_ptr);
void Print_m_num(struct main_struct_ptr *data_ptr,char *token);
void Print_d_num(struct main_struct_ptr *data_ptr,char *token);
void Print_a_num(struct main_struct_ptr *data_ptr,char *token);
void Print_m_list(struct main_struct_ptr *data_ptr);
void Print_d_list(struct main_struct_ptr *data_ptr);
void Print_a_list(struct main_struct_ptr *data_ptr);
void Sort_m_g(struct main_struct_ptr *data_ptr);
void Sort_m_d(struct main_struct_ptr *data_ptr);
void Sort_m_y(struct main_struct_ptr *data_ptr);
void Sort_m_r(struct main_struct_ptr *data_ptr);
void Sort_m_a(struct main_struct_ptr *data_ptr);
void Sort_m_t(struct main_struct_ptr *data_ptr);
void Sort_d_m(struct main_struct_ptr *data_ptr);
void Sort_d_n(struct main_struct_ptr *data_ptr);
void Sort_d_s(struct main_struct_ptr *data_ptr);
void Sort_d_b(struct main_struct_ptr *data_ptr);
void Sort_a_n(struct main_struct_ptr *data_ptr);
void Sort_a_s(struct main_struct_ptr *data_ptr);
void Sort_a_b(struct main_struct_ptr *data_ptr);
void Sort_a_m(struct main_struct_ptr *data_ptr);
int update_movie(FILE *optr, struct movie *tmp_movie_ptr);
struct movie* move2place_m (struct movie *movie_ptr, int update_sn);
int scan_serial_number_mud(FILE *optr, struct movie *movie_ptr);
struct movie* move2preplace_m (struct movie *movie_ptr, int update_sn);
struct director *move2preplace_d (struct director *director_ptr, int update_sn);
struct actor * move2preplace_a (struct actor *actor_ptr, int update_sn);
void add_movie_mode (FILE *optr, struct movie *movie_ptr);
void add_director_mode (FILE *optr, struct director *director_ptr);
void add_actor_mode (FILE *optr, struct actor *actor_ptr);
int mov(struct movie *, char*);
int dir(struct director *, char*);
int act(struct actor*, char*);
bool pattern_match(const char *, const char *);
void QorM_m(struct movie *, char*);
void QorM_d(struct director *, char*);
void QorM_a(struct actor *, char*);
void ordersearch(struct main_struct_ptr*);
void sigint_handler(int sig)
{
char *c;
c = (char*)malloc(sizeof(char)* 10);
printf("\n2번 신호가 발생했습니다.\n");
printf("종료하시겠습니까? (y/n) : ");
scanf("%[^\n]%s", c);
if(*c == 'y' || *c == 'Y')
exit(1);
else
printf("프로그램을 종료하지 않습니다.\n");
}
void scan_word(FILE *optr, char *tmp_data){
for (int i = 0 ; i < 100 ; i++ ) {
fscanf(optr, "%c", (tmp_data + i));
if ( *(tmp_data + i) == ':'){
*(tmp_data + i) = '\0';
return;
}
if ( *(tmp_data + i) == '\n'){
*(tmp_data + i) = '\0';
return;
}
if ( *(tmp_data+i) == EOF){
*(tmp_data + i) = '\0';
return;
}
}
}
int str_devider (char *str) {
int count = 0;
for (int i=0; i<5*15; i++){
if (*(str+i) == ',')
break;
if(*(str+i) == '\n')
break;
if(*(str+i) == '\0' ){
return 0;
}
count ++;
}
return count;
}
void get_data(struct main_struct_ptr *data_ptr){
get_movie(data_ptr);
get_director(data_ptr);
get_actor(data_ptr);
}
int check_skip (char *tmp_name) {
if (strcmp(tmp_name, "=") == 0){
free(tmp_name);
return 0;
}
else if (*tmp_name == '='){
return 0;
}
else {
return 1;
}
}
void free_movie(struct movie *delete_movie_ptr){
free(delete_movie_ptr->title);
free(delete_movie_ptr->genre);
free(delete_movie_ptr->director_m->name);
free(delete_movie_ptr->actor_m);
}//
void delete_movie(FILE *optr, struct movie *tmp_movie_ptr){
int delete_sn;
char *trash = malloc (100);
delete_sn = scan_serial_number_mud (optr, tmp_movie_ptr);
struct movie *delete_movie_ptr;
delete_movie_ptr = move2place_m(tmp_movie_ptr,delete_sn);
free_movie(delete_movie_ptr);
for (int i=0; i<6 ; i++){
scan_word(optr, trash);
}
struct movie *pre_delm_ptr;
if (delete_sn == 1){
printf("you can't delete first data (serial number 1)\n");
return;
}
else {
pre_delm_ptr = move2preplace_m(tmp_movie_ptr,(delete_sn));
struct movie *aft_delm_ptr;
aft_delm_ptr = delete_movie_ptr -> next;
(pre_delm_ptr -> next) = aft_delm_ptr;
}
free(delete_movie_ptr);
}
void get_movie(struct main_struct_ptr *data_ptr){
FILE *optr;
optr = fopen ("test1.txt", "r");
(data_ptr -> movie_ptr) = malloc(sizeof(struct movie));
struct movie *tmp_movie_ptr;
tmp_movie_ptr = (data_ptr->movie_ptr);
struct movie *tmp_movie_memory;
tmp_movie_memory = (data_ptr->movie_ptr);
while (1) //(끝까지 읽기 전까지, 맨앞의 명령어만 읽어들임
{
char *mode;
mode = malloc(1*7); //모드 최고 글자수가 6개 + 1이므로
scan_word(optr, mode);
if (strcmp("add", mode) == 0) {
add_movie(optr, (tmp_movie_ptr));
(tmp_movie_ptr -> next) = malloc (sizeof(struct movie));
tmp_movie_ptr = (tmp_movie_ptr -> next);
(tmp_movie_ptr -> next) = NULL;
}
else if (strcmp("update", mode) == 0 ){
// tmp_movie_ptr = (data_ptr->movie_ptr);
update_movie(optr, (tmp_movie_memory));
}
else if (strcmp("delete", mode) == 0 ){
// tmp_movie_ptr = (data_ptr->movie_ptr);
delete_movie(optr, (tmp_movie_memory));
}
else{
break;
}
free(mode);
}
tmp_movie_ptr = data_ptr->movie_ptr;
while (1){
if(tmp_movie_ptr->next->next == NULL){
tmp_movie_ptr->next = NULL;
break;
}
else
tmp_movie_ptr = tmp_movie_ptr->next;
}
fclose(optr);
}
void add_movie (FILE *optr, struct movie *movie_ptr) {
scan_serial_number_m(optr, movie_ptr);
scan_title_m(optr, movie_ptr);
scan_genre_m(optr, movie_ptr);
scan_director_m(optr,movie_ptr);
scan_year_m(optr,movie_ptr);
scan_movie_time_m(optr, movie_ptr);
scan_actor_m(optr, movie_ptr);
}
void scan_serial_number_m(FILE *optr, struct movie *movie_ptr) {
char *tmp_name;
tmp_name = malloc(1*MAX_TMP);
scan_word(optr, tmp_name); //파일에서 타이틀 읽어서 임시메모리에 저
(movie_ptr -> serial_number) = atoi(tmp_name);
free(tmp_name);
}
void scan_title_m(FILE *optr, struct movie *movie_ptr){
char *tmp_name;
tmp_name = malloc(1*MAX_TMP);
scan_word(optr, tmp_name); //파일에서 타이틀 읽어서 임시메모리에 저장
(movie_ptr->title) = malloc ((strlen(tmp_name)+1)); // 메모리에 데이터 저장 전, 임시 공간에 저장된 이름의 크기만큼 동적메모리 할당해줌
strcpy((movie_ptr->title),tmp_name);
free(tmp_name);
}
void scan_genre_m(FILE *optr,struct movie * movie_ptr){
char *tmp_name;
tmp_name = malloc(1*MAX_TMP);
scan_word(optr, tmp_name);
(movie_ptr->genre) = malloc ((strlen(tmp_name)+1));
strcpy((movie_ptr->genre),tmp_name);
free(tmp_name);
}
void scan_director_m(FILE *optr, struct movie *movie_ptr){
char *tmp_name;
tmp_name = malloc(1*MAX_TMP);
scan_word(optr, tmp_name);
(movie_ptr->director_m) = malloc (sizeof(struct director_in_movie)); // (이름 크기 + 구조체 내의 struct director 포인터의 크기) 동적할당
((movie_ptr->director_m)->name) = malloc ((strlen(tmp_name)+1));
strcpy(((movie_ptr->director_m)->name), tmp_name);
free(tmp_name);
}
void scan_year_m(FILE *optr, struct movie *movie_ptr){
char *tmp_name;
tmp_name = malloc(1*MAX_TMP);
scan_word(optr, tmp_name);
(movie_ptr -> year) = atoi(tmp_name);
free(tmp_name);
}
void scan_movie_time_m(FILE *optr, struct movie *movie_ptr){
char *tmp_name;
tmp_name = malloc(1*MAX_TMP);
scan_word(optr, tmp_name);
(movie_ptr -> movie_time) = atoi(tmp_name);
free(tmp_name);
}
void scan_actor_m(FILE *optr, struct movie *movie_ptr){
(movie_ptr->actor_m) = malloc (sizeof(struct actor_in_movie)); // (이름 크기 + 구조체 내의 struct director 포인터의 크기) 동적할당
movie_ptr->actor_m->next = 0;
struct actor_in_movie *tmp_actor_ptr;
tmp_actor_ptr = movie_ptr->actor_m;
char *tmp_str, *tmp_memory;
int flag = 1;
tmp_str = malloc(MAX_LINKED_NUMBER*MAX_TMP);
tmp_memory = tmp_str;
scan_word(optr, tmp_str);
while ((flag = str_devider(tmp_memory)) != 0) {
char *tmp_actor;
tmp_actor = malloc (flag + 2);
for (int i = 0; i< flag ; i++){
*(tmp_actor + i) = *(tmp_memory + i);
}
*(tmp_actor+flag) = '\0';
tmp_memory = tmp_memory + flag + 1 ;
(tmp_actor_ptr->name) = malloc (flag + 1);
strcpy((tmp_actor_ptr->name), tmp_actor);
(tmp_actor_ptr->next) = malloc (sizeof(struct actor_in_movie));
tmp_actor_ptr = tmp_actor_ptr -> next;
tmp_actor_ptr->next = NULL;
free(tmp_actor);
}
int cnt = 0;
for (; cnt< 16 ; cnt++){
if (*(tmp_memory + cnt) == '\0'){
break;
}
}
char *tmp_actor;
tmp_actor = malloc (cnt + 2);
for (int i = 0; i < cnt-1; i++){
*(tmp_actor + i) = *(tmp_memory + i);
}
*(tmp_actor + cnt - 1) = '\0';
(tmp_actor_ptr->name) = malloc (cnt + 1);
strcpy((tmp_actor_ptr->name), tmp_actor);
tmp_actor_ptr->next = NULL;
free(tmp_actor);
free(tmp_str);
}
void scan_actor_mu(FILE *optr, struct movie *update_movie_ptr){
struct actor_in_movie *tmp_actor_ptr;
tmp_actor_ptr = update_movie_ptr->actor_m;
char *tmp_str;
tmp_str = malloc(MAX_LINKED_NUMBER*MAX_TMP);
scan_word(optr, tmp_str);
int flag = 1;
while ((flag = str_devider(tmp_str)) != 0) {
char *tmp_actor;
tmp_actor = malloc (flag + 2);
for (int i = 0; i< flag ; i++){
*(tmp_actor + i) = *(tmp_str + i);
}
*(tmp_actor+flag) = '\0';
tmp_str = tmp_str + flag + 1;
if((check_skip(tmp_actor)) == 0){
tmp_actor_ptr = tmp_actor_ptr -> next;
continue;
}
else {
if((tmp_actor_ptr->name) != NULL)
free((tmp_actor_ptr->name));
(tmp_actor_ptr->name) = malloc (flag + 1);
strcpy((tmp_actor_ptr->name), tmp_actor);
if ((tmp_actor_ptr -> next) == NULL){
(tmp_actor_ptr->next) = malloc (sizeof(struct actor_in_movie));
tmp_actor_ptr = tmp_actor_ptr -> next;
tmp_actor_ptr->name = NULL;
tmp_actor_ptr->next = NULL;
free(tmp_actor);
}
else {
tmp_actor_ptr = tmp_actor_ptr -> next;
free(tmp_actor);
}
}
}
int cnt = 0;
for (; cnt< 16 ; cnt++){
if (*(tmp_str + cnt) == '\0'){
break;
}
}
char *tmp_actor;
tmp_actor = malloc (cnt + 2);
for (int i = 0; i < cnt+2 ; i++){
*(tmp_actor + i) = *(tmp_str + i);
}
if((check_skip(tmp_actor)) == 0){
return;
}
else {
if((tmp_actor_ptr->name) != NULL)
free((tmp_actor_ptr->name));
(tmp_actor_ptr->name) = malloc (cnt + 1);
strcpy((tmp_actor_ptr->name), tmp_actor);
tmp_actor_ptr->next = NULL;
free(tmp_actor);
}
}
int scan_serial_number_mud(FILE *optr, struct movie *movie_ptr) { // update, delete를 위한 scan함수
char *tmp_name;
int return_val;
tmp_name = malloc(1*MAX_TMP);
scan_word(optr, tmp_name); //파일에서 타이틀 읽어서 임시메모리에 저
return_val = atoi(tmp_name);
free(tmp_name);
return return_val;
}
struct movie* move2place_m (struct movie *movie_ptr, int update_sn){
struct movie *tmp_movie_ptr;
tmp_movie_ptr = movie_ptr;
while((tmp_movie_ptr) != NULL) {
if (update_sn == (tmp_movie_ptr -> serial_number)){
return tmp_movie_ptr;
}
else
tmp_movie_ptr = tmp_movie_ptr -> next;
}
printf("There are no member of <%d serial number>\n", update_sn );
return NULL;
}
struct movie* move2preplace_m (struct movie *movie_ptr, int update_sn) {
struct movie *tmp_movie_ptr;
tmp_movie_ptr = movie_ptr;
while((tmp_movie_ptr->next) != NULL) {
if (update_sn == (tmp_movie_ptr -> next -> serial_number)){
return tmp_movie_ptr;
}
else
tmp_movie_ptr = tmp_movie_ptr -> next;
}
printf("There are no member of <%d serial number>\n", update_sn );
return NULL;
}
void scan_title_mu(FILE *optr, struct movie *update_movie_ptr){
char *tmp_name;
tmp_name = malloc(1*MAX_TMP);
scan_word(optr, tmp_name); //파일에서 타이틀 읽어서 임시메모리에 저장
if ((check_skip(tmp_name)) == 0){
return;
}
else {
free((update_movie_ptr)->title);
(update_movie_ptr)->title = malloc(strlen(tmp_name) + 2);
strcpy((update_movie_ptr->title),tmp_name);
free(tmp_name);
}
}
void scan_genre_mu(FILE *optr,struct movie * update_movie_ptr){
char *tmp_name;
tmp_name = malloc(1*MAX_TMP);
scan_word(optr, tmp_name);
if ((check_skip(tmp_name)) == 0){
return;
}
else {
free((update_movie_ptr)->genre);
(update_movie_ptr)->genre = malloc(strlen(tmp_name) + 2);
strcpy((update_movie_ptr->genre),tmp_name);
free(tmp_name);
}
}
void scan_director_mu(FILE *optr, struct movie *update_movie_ptr){
char *tmp_name;
tmp_name = malloc(1*MAX_TMP);
scan_word(optr, tmp_name);
if ((check_skip(tmp_name)) == 0){
return;
}
else {
free((update_movie_ptr)->director_m->name);
(update_movie_ptr)->director_m->name = malloc(strlen(tmp_name) + 2);
strcpy(((update_movie_ptr->director_m)->name), tmp_name);
free(tmp_name);
}
}
void scan_year_mu(FILE *optr, struct movie *update_movie_ptr){
char *tmp_name;
tmp_name = malloc(1*MAX_TMP);
scan_word(optr, tmp_name);
if ((check_skip(tmp_name)) == 0){
return;
}
else {
(update_movie_ptr -> year) = atoi(tmp_name);
free(tmp_name);
}
}
void scan_movie_time_mu(FILE *optr, struct movie *update_movie_ptr){
char *tmp_name;
tmp_name = malloc(1*MAX_TMP);
scan_word(optr, tmp_name);
if ((check_skip(tmp_name)) == 0){
return;
}
else {
(update_movie_ptr -> movie_time) = atoi(tmp_name);
free(tmp_name);
}
}
int update_movie(FILE * optr, struct movie *movie_ptr){
int update_sn; // update serial number
update_sn =scan_serial_number_mud(optr, movie_ptr);
struct movie *update_movie_ptr;
update_movie_ptr = move2place_m(movie_ptr,update_sn);
if (update_movie_ptr==NULL)
return 0;
scan_title_mu(optr,update_movie_ptr);
scan_genre_mu(optr,update_movie_ptr);
scan_director_mu(optr, update_movie_ptr);
scan_year_mu(optr, update_movie_ptr);
scan_movie_time_mu(optr, update_movie_ptr);
scan_actor_mu(optr,update_movie_ptr);
return 1;
}
int scan_serial_number_dud(FILE *optr, struct director *director_ptr) { // update, delete를 위한 scan함수
char *tmp_name;
int return_val;
tmp_name = malloc(1*MAX_TMP);
scan_word(optr, tmp_name); //파일에서 타이틀 읽어서 임시메모리에 저
return_val = atoi(tmp_name);
free(tmp_name);
return return_val;
}
struct director * move2place_d (struct director *director_ptr, int update_sn){
struct director *tmp_director_ptr;
tmp_director_ptr = director_ptr;
while((tmp_director_ptr) != NULL) {
if (update_sn == (tmp_director_ptr -> serial_number)){
return tmp_director_ptr;
}
else
tmp_director_ptr = tmp_director_ptr -> next;
}
printf("There are no member of <%d serial number>\n", update_sn );
return NULL;
}
struct director * move2preplace_d (struct director *director_ptr, int update_sn) {
struct director *tmp_director_ptr;
tmp_director_ptr = director_ptr;
while((tmp_director_ptr->next) != NULL) {
if (update_sn == (tmp_director_ptr -> next -> serial_number)){
return tmp_director_ptr;
}
else
tmp_director_ptr = tmp_director_ptr -> next;
}
printf("There are no member of <%d serial number>\n", update_sn );
return NULL;
}
void scan_name_du(FILE *optr, struct director *update_director_ptr){
char *tmp_name;
tmp_name = malloc(1*MAX_TMP);
scan_word(optr, tmp_name); //파일에서 타이틀 읽어서 임시메모리에 저장
if ((check_skip(tmp_name)) == 0){
return;
}
else {
free((update_director_ptr)->name);
(update_director_ptr)->name = malloc(strlen(tmp_name) + 2);
strcpy((update_director_ptr->name),tmp_name);
free(tmp_name);
}
}
void scan_birth_du(FILE *optr,struct director * update_director_ptr){
char *tmp_name;
tmp_name = malloc(1*MAX_TMP);
scan_word(optr, tmp_name);
if ((check_skip(tmp_name)) == 0){
return;
}
else {
free((update_director_ptr)->birth);
(update_director_ptr)->birth = malloc(strlen(tmp_name) + 2);
strcpy((update_director_ptr->birth),tmp_name);
free(tmp_name);
}
}
void scan_title_du(FILE *optr, struct director *update_director_ptr){
struct movie_in_director *tmp_movie_ptr;
tmp_movie_ptr = update_director_ptr->movie_d;
char *tmp_str;
tmp_str = malloc(MAX_LINKED_NUMBER*MAX_TMP);
scan_word(optr, tmp_str);
int flag = 1;
while ((flag = str_devider(tmp_str)) != 0) {
char *tmp_movie;
tmp_movie = malloc (flag + 2);
for (int i = 0; i< flag ; i++){
*(tmp_movie + i) = *(tmp_str + i);
}
*(tmp_movie+flag) = '\0';
tmp_str = tmp_str + flag + 1;
if((check_skip(tmp_movie)) == 0){
tmp_movie_ptr = tmp_movie_ptr -> next;
continue;
}
else {
if((tmp_movie_ptr->name) != NULL)
free((tmp_movie_ptr->name));
(tmp_movie_ptr->name) = malloc (flag + 1);
strcpy((tmp_movie_ptr->name), tmp_movie);
if ((tmp_movie_ptr -> next) == NULL){
(tmp_movie_ptr->next) = malloc (sizeof(struct movie_in_director));
tmp_movie_ptr = tmp_movie_ptr -> next;
tmp_movie_ptr->name = NULL;
tmp_movie_ptr->next = NULL;
free(tmp_movie);
}
else {
tmp_movie_ptr = tmp_movie_ptr -> next;
free(tmp_movie);
}
}
}
int cnt = 0;
for (; cnt< 16 ; cnt++){
if (*(tmp_str + cnt) == '\0'){
break;
}
}
char *tmp_movie;
tmp_movie = malloc (cnt + 2);
for (int i = 0; i < cnt+2 ; i++){
*(tmp_movie + i) = *(tmp_str + i);
}
if((check_skip(tmp_movie)) == 0){
return;
}
else {
if((tmp_movie_ptr->name) != NULL)
free((tmp_movie_ptr->name));
(tmp_movie_ptr->name) = malloc (cnt + 1);
strcpy((tmp_movie_ptr->name), tmp_movie);
tmp_movie_ptr->next = NULL;
free(tmp_movie);
}
}
void scan_sex_du (FILE *optr, struct director *update_director_ptr){
char *tmp_name;
tmp_name = malloc(1*3);
scan_word(optr, tmp_name); //파일에서 타이틀 읽어서 임시메모리에 저
if ((check_skip(tmp_name)) == 0){
return;
}
else {
(update_director_ptr -> sex) = *tmp_name;
free(tmp_name);
}
}
int update_director(FILE * optr, struct director *director_ptr){
int update_sn; // update serial number
update_sn =scan_serial_number_dud(optr, director_ptr);
struct director *update_director_ptr;
update_director_ptr = move2place_d(director_ptr,update_sn);
if (update_director_ptr==NULL)
return 0;
scan_name_du(optr,update_director_ptr);
scan_sex_du(optr,update_director_ptr);
scan_birth_du(optr,update_director_ptr);
scan_title_du(optr,update_director_ptr);
return 1;
}
void free_director(struct director *delete_director_ptr) {
free(delete_director_ptr->name);
free(delete_director_ptr->birth);
free(delete_director_ptr->movie_d);
}
void delete_director(FILE *optr, struct director *tmp_director_ptr){
int delete_sn;
char *trash = malloc (100);
delete_sn = scan_serial_number_dud (optr, tmp_director_ptr);
struct director *delete_director_ptr;
delete_director_ptr = move2place_d(tmp_director_ptr,delete_sn);
free_director(delete_director_ptr);
for (int i=0; i<4 ; i++){
scan_word(optr,trash );
}
struct director *pre_delm_ptr;
if (delete_sn == 1){
printf("you can't delete first data (serial number 1)\n");
return;
}
else {
pre_delm_ptr = move2preplace_d(tmp_director_ptr,(delete_sn));
struct director *aft_delm_ptr;
aft_delm_ptr = delete_director_ptr -> next;
(pre_delm_ptr -> next) = aft_delm_ptr;
}
free(delete_director_ptr);
}
void get_director(struct main_struct_ptr *data_ptr){
FILE *optr;
optr = fopen ("test2.txt", "r");
(data_ptr -> director_ptr) = malloc(sizeof(struct director));
struct director *tmp_director_ptr;
tmp_director_ptr = (data_ptr->director_ptr);
struct director *tmp_director_memory;
tmp_director_memory = (data_ptr->director_ptr);
while (1) //(끝까지 읽기 전까지, 맨앞의 명령어만 읽어들임
{
char *mode;
mode = malloc(1*8); //모드 최고 글자수가 6개 + 1이므로
scan_word(optr, mode);
if ( *mode == 0)
break;
if (strcmp("add", mode) == 0) {
free(mode);
add_director(optr, (tmp_director_ptr));
(tmp_director_ptr -> next) = malloc (sizeof(struct director));
tmp_director_ptr = (tmp_director_ptr -> next);
(tmp_director_ptr -> next) = NULL;
}
else if (strcmp("update", mode) == 0 ){
update_director(optr, (tmp_director_memory));
}
else if (strcmp("delete", mode) == 0){
delete_director(optr, (tmp_director_memory));
}
else
continue;
}
tmp_director_ptr = data_ptr->director_ptr;
while (1){
if(tmp_director_ptr->next->next == NULL){
tmp_director_ptr->next = NULL;
break;
}
else
tmp_director_ptr = tmp_director_ptr->next;
}
fclose(optr);
}
void add_director (FILE *optr, struct director *director_ptr) {
scan_serial_number_d(optr, director_ptr);
scan_name_d(optr, director_ptr); // title
scan_sex_d(optr, director_ptr); // new one
scan_birth_d(optr, director_ptr); // genre
scan_title_d(optr, director_ptr); //acotr
}
void scan_serial_number_d(FILE *optr, struct director *director_ptr) {
char *tmp_name;
tmp_name = malloc(1*15);
scan_word(optr, tmp_name); //파일에서 타이틀 읽어서 임시메모리에 저
(director_ptr -> serial_number) = atoi(tmp_name);
free(tmp_name);
}
void scan_name_d(FILE *optr, struct director *director_ptr){
char *tmp_name;
tmp_name = malloc(1*15);
scan_word(optr, tmp_name); //파일에서 타이틀 읽어서 임시메모리에 저장
(director_ptr->name) = malloc ((strlen(tmp_name)+1)); // 메모리에 데이터 저장 전, 임시 공간에 저장된 이름의 크기만큼 동적메모리 할당해줌
strcpy((director_ptr->name),tmp_name);
free(tmp_name);
}
void scan_sex_d (FILE *optr, struct director *director_ptr){
char *tmp_name;
tmp_name = malloc(1*3);
scan_word(optr, tmp_name); //파일에서 타이틀 읽어서 임시메모리에 저
(director_ptr -> sex) = *tmp_name;
free(tmp_name);
}
void scan_birth_d(FILE *optr, struct director *director_ptr){
char *tmp_name;
tmp_name = malloc(1*15);
scan_word(optr, tmp_name);
(director_ptr->birth) = malloc ((strlen(tmp_name)+1));
strcpy((director_ptr->birth),tmp_name);
free(tmp_name);
}
void scan_title_d(FILE *optr, struct director *director_ptr){
(director_ptr->movie_d) = malloc (sizeof(struct movie_in_director)); // (이름 크기 + 구조체 내의 struct director 포인터의 크기) 동적할당
director_ptr->movie_d->next = 0;
struct movie_in_director *tmp_movie_ptr;
tmp_movie_ptr = director_ptr->movie_d;
char *tmp_str;
int flag = 1;
tmp_str = malloc(5*15);
scan_word(optr, tmp_str);
while ((flag = str_devider(tmp_str)) != 0) {
if (*tmp_str == 0)
break;
char *tmp_title;
tmp_title = malloc (flag + 1);
for (int i = 0; i< flag ; i++){
*(tmp_title + i) = *(tmp_str + i);
}
*(tmp_title+flag) = '\0';
tmp_str = tmp_str + flag + 1;
(tmp_movie_ptr->name) = malloc (flag + 1);
strcpy((tmp_movie_ptr->name), tmp_title);
(tmp_movie_ptr->next) = malloc (sizeof(struct movie_in_director));
tmp_movie_ptr = tmp_movie_ptr -> next;
tmp_movie_ptr->next = NULL;
free(tmp_title);
}
int cnt = 0;
for (; cnt< 14 ; cnt++){
if (*(tmp_str + cnt) == '\0'){
break;
}
}
char *tmp_title;
tmp_title = malloc (cnt + 2);
for (int i = 0; i < cnt-1 ; i++){
*(tmp_title + i) = *(tmp_str + i);
}
*(tmp_title + cnt - 1) = '\0';
(tmp_movie_ptr->name) = malloc (cnt + 2);
strcpy((tmp_movie_ptr->name), tmp_title);
tmp_movie_ptr->next = NULL;
free(tmp_title);
}
int scan_serial_number_aud(FILE *optr, struct actor *actor_ptr) { // update, delete를 위한 scan함수
char *tmp_name;
int return_val;
tmp_name = malloc(1*MAX_TMP);
scan_word(optr, tmp_name); //파일에서 타이틀 읽어서 임시메모리에 저
return_val = atoi(tmp_name);
free(tmp_name);
return return_val;
}
struct actor * move2place_a (struct actor *actor_ptr, int update_sn){
struct actor *tmp_actor_ptr;
tmp_actor_ptr = actor_ptr;
while((tmp_actor_ptr) != NULL) {
if (update_sn == (tmp_actor_ptr -> serial_number)){
return tmp_actor_ptr;
}
else
tmp_actor_ptr = tmp_actor_ptr -> next;
}
printf("There are no member of <%d serial number>\n", update_sn );
return NULL;
}
struct actor * move2preplace_a (struct actor *actor_ptr, int update_sn) {
struct actor *tmp_actor_ptr;
tmp_actor_ptr = actor_ptr;
while((tmp_actor_ptr->next) != NULL) {
if (update_sn == (tmp_actor_ptr -> next -> serial_number)){
return tmp_actor_ptr;
}
else
tmp_actor_ptr = tmp_actor_ptr -> next;
}
printf("There are no member of <%d serial number>\n", update_sn );
return NULL;
}
void scan_name_au(FILE *optr, struct actor *update_actor_ptr){
char *tmp_name;
tmp_name = malloc(1*MAX_TMP);
scan_word(optr, tmp_name); //파일에서 타이틀 읽어서 임시메모리에 저장
if ((check_skip(tmp_name)) == 0){
return;
}
else {
free((update_actor_ptr)->name);