generated from KoharuDaisukii/StudytimeCheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StudytimeCheck.c
1458 lines (1310 loc) · 35.4 KB
/
StudytimeCheck.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> // strlen
#include <ctype.h> // toupper, isalpha, isdigit
#include <sys/stat.h> // mkdir
#include <sys/types.h> // mkdir, lseek
#include <sys/wait.h> // wait
#include <fcntl.h> // open
#include <dirent.h> // struct dirent
#include <unistd.h> // mkdir, chdir, write, lseek, dup(1,2)
#include <curses.h>
#include <time.h> // time, struct tm
#define USERS_INFO_DIR "users"
#define USERS_INFO_FILE "users.txt"
#define NO_GROUP "no_group"
#define JANUARY 0
#define DECEMBER 11
#define SECONDS_PER_DAY 86400
#define MAX 11
#define ARROW_DOWN 2
#define ARROW_UP 3
#define ARROW_LEFT 4
#define ARROW_RIGHT 5
typedef struct Studyuser
{
char user_ID[11];
char group_ID[11];
time_t signup;// 가입 일자
time_t lastlogin;// 최종 접속 시간
// 또 뭐 넣지
} Studyuser;
typedef struct timelog // 공부 시간 기록을 저장하는 구조체
{
// Studyuser info;
char subject[30];
time_t start_time; // 공부 시작 시간
time_t finish_time; // 공부 종료 시간
double studytime; // 공부 시간
} timelog;
DIR* login(); // login 성공: UID directory 포인터 return
void main_screen();
void menu2();
void menu2_screen(WINDOW* win);
void menu2_1(WINDOW* win); // 이름을 어칼까
void day_stats(WINDOW* win, int year, int month, int day);
void menu2_2(WINDOW* win);
void week_stats(WINDOW* win, time_t today);
void menu2_3(WINDOW* win);
void month_stats(WINDOW* win, struct tm statmonth_tm);
// MENU 3 관련 함수
void menu3();
void menu3_screen(WINDOW* win);
void menu3_join(WINDOW* win);
void menu3_leave(WINDOW* win);
void menu3_rank(WINDOW* win);
void menu4(DIR*);
void menu4_screen(WINDOW* win);
void menu4_profile(WINDOW *win);
void menu4_help(WINDOW *win);
void menu4_deleteAccount(WINDOW* win, DIR*);
int rmdir_r(DIR* path);
int user_dead = 0; // 유저가 탈퇴했는지 인지하는 변수
int usersFd; // USERS_INFO_FILE file descriptor, 이건 users.txt 파일 내부에서 로그인한 유저의 정보를 계속 가리킬 예정
char UID[11]; // 전역 변수로 쓰는 게 편할 것 같음
int main(int argc, char* argv[])
{
if (argc == 1) // 아이디 입력 안 함
{
fprintf(stderr, "아이디를 입력해주세요. Usage: ./StudytimeCheck YOUR_ID\n");
exit(1);
}
else if (strlen(argv[1]) >= 11) // 아이디 10글자 이하로 입력 안 함
{
fprintf(stderr, "아이디는 10글자 이하로 입력해주세요.\n");
exit(2);
}
else if (argc >= 3) // 쓸데없는 것 입력함
{
fprintf(stderr, "ID만 입력해주세요. Usage: ./StudytimeCheck YOUR_ID\n");
exit(3);
}
strcpy(UID, argv[1]); //
DIR* UID_dirptr = login(UID); // 로그인해서 UID 이름의 폴더를 엶
initscr();
noecho();
curs_set(0);
char menu;
while (1)
{
main_screen(UID);
menu = getch();
// if(menu == '1') menu1();
if (menu == '2') menu2();
if (menu == '3') menu3();
if (menu == '4') menu4(UID_dirptr);
if (menu == '5') break;
if (user_dead == 1) break;
}
// delwin(main_screen);
endwin();
closedir(UID_dirptr); // 로그아웃
close(usersFd);
printf("%s님, Bye Bye!\n", UID);
return 0;
}
DIR* login()
{
DIR* dir_ptr;
if (chdir(USERS_INFO_DIR) == -1) // 유저 정보가 담긴 디렉토리로 이동
{
if (mkdir(USERS_INFO_DIR, 0755) == -1 || chdir(USERS_INFO_DIR) == -1) // 없으면 만듦
{
perror(USERS_INFO_DIR);
exit(4);
}
}
int tempfd1, tempfd2;
int user_exist = 0; //
if ((tempfd1 = open(USERS_INFO_FILE, O_RDWR | O_CREAT | O_EXCL, 0777)) == -1) // 파일 만들기 시도
{
if ((tempfd2 = open(USERS_INFO_FILE, O_RDWR)) == -1) // 이미 있네
{
perror("open fd2");
exit(9);
}
if ((usersFd = dup(tempfd2)) == -1) // usersFd에 tempfd2 복제
{
perror("dup2-1");
exit(9);
}
close(tempfd2);
}
else // 파일 없었을 때(이 시점에서는 존재함)
{
if ((usersFd = dup(tempfd1)) == -1) // usersFd에 tempfd2 복제
{
perror("dup2-2");
exit(9);
}
close(tempfd1);
}
Studyuser s_user;
while (read(usersFd, &s_user, sizeof(Studyuser)) >= sizeof(Studyuser))
{
if (strcmp(s_user.user_ID, UID) == 0) // 해당하는 UID의 유저가 있는지 탐색
{
user_exist = 1; // 있네
break; // file pointer는 찾은 유저의 기록 바로 다음을 가리킴
}
}
if (user_exist && chdir(s_user.group_ID) == -1) // 유저 정보가 담긴 디렉토리로 이동
{
perror(s_user.group_ID);
exit(4);
}
if(user_exist == 0)
{
if(chdir(NO_GROUP) == -1)
{
if (mkdir(NO_GROUP, 0755) == -1 || chdir(NO_GROUP) == -1) // 없으면 만듦
{
perror(NO_GROUP);
exit(4);
}
}
}
// 한글 ID 입력하면 컷하는 기능도 필요할 듯
if ((dir_ptr = opendir(UID)) == NULL || user_exist == 0) // UID 폴더 존재하는 지 확인, 그룹 생각 안 하고 일단 함
{
printf("ID가 존재하지 않습니다. 해당 ID로 가입하시겠습니까? (Y/N): ");
char yesno = toupper(getchar());
while (yesno != 'Y' && yesno != 'N') // y도 아니고 n도 아니면 반복
{
printf("Y 또는 N만 입력해라: ");
yesno = toupper(getchar());
}
if (yesno == 'Y') // Y 입력
{
if (mkdir(UID, 0755) == -1) // UID 이름의 디렉토리 만듦 = 가입
{
perror("mkdir error");
exit(6);
}
if ((dir_ptr = opendir(UID)) == NULL) // UID 디렉토리 열기
{
perror("안 열려요");
exit(7);
}
// user_info 파일에 유저 정보 추가
Studyuser newuser;
strcpy(newuser.user_ID, UID);
strcpy(newuser.group_ID, NO_GROUP);
newuser.signup = time(NULL);
newuser.lastlogin = time(NULL);
lseek(usersFd, 0, SEEK_END);
write(usersFd, &newuser, sizeof(Studyuser));
// 추가 완료
printf("가입 완료되었습니다. %s님 환영합니다.\n", UID);
sleep(2); // 로딩하는 척
return dir_ptr;
}
else
{
printf("\'가입하지 않기\'를 선택하셨습니다. 프로그램이 종료됩니다.\n");
sleep(2);
exit(0);
}
}
// 로그인 시간 갱신
s_user.lastlogin = time(NULL);
lseek(usersFd, -sizeof(Studyuser), SEEK_CUR);
write(usersFd, &s_user, sizeof(Studyuser));
// 추가 완료
return dir_ptr;
}
void main_screen()
{
int x = 3;
int y = 7;
//S
move(y, x);
printw("*");
move(y, x + 1);
printw("*");
move(y, x + 2);
printw("*");
move(y, x + 3);
printw("*");
move(y, x + 4);
printw("*");
move(y + 1, x);
printw("*");
move(y + 2, x);
printw("*");
move(y + 2, x + 1);
printw("*");
move(y + 2, x + 2);
printw("*");
move(y + 2, x + 3);
printw("*");
move(y + 2, x + 4);
printw("*");
move(y + 3, x + 4);
printw("*");
move(y + 4, x + 4);
printw("*");
move(y + 4, x + 3);
printw("*");
move(y + 4, x + 2);
printw("*");
move(y + 4, x + 1);
printw("*");
move(y + 4, x);
printw("*");
//T
move(y, x + 7);
printw("*");
move(y, x + 8);
printw("*");
move(y, x + 9);
printw("*");
move(y, x + 10);
printw("*");
move(y, x + 11);
printw("*");
move(y + 1, x + 9);
printw("*");
move(y + 2, x + 9);
printw("*");
move(y + 3, x + 9);
printw("*");
move(y + 4, x + 9);
printw("*");
//U
move(y, x + 14);
printw("*");
move(y + 1, x + 14);
printw("*");
move(y + 2, x + 14);
printw("*");
move(y + 3, x + 14);
printw("*");
move(y + 4, x + 14);
printw("*");
move(y, x + 18);
printw("*");
move(y + 1, x + 18);
printw("*");
move(y + 2, x + 18);
printw("*");
move(y + 3, x + 18);
printw("*");
move(y + 4, x + 18);
printw("*");
move(y + 4, x + 15);
printw("*");
move(y + 4, x + 16);
printw("*");
move(y + 4, x + 17);
printw("*");
//D
move(y, x + 21);
printw("*");
move(y + 1, x + 21);
printw("*");
move(y + 2, x + 21);
printw("*");
move(y + 3, x + 21);
printw("*");
move(y + 4, x + 21);
printw("*");
move(y, x + 22);
printw("*");
move(y, x + 23);
printw("*");
move(y, x + 24);
printw("*");
move(y + 4, x + 22);
printw("*");
move(y + 4, x + 23);
printw("*");
move(y + 4, x + 24);
printw("*");
move(y + 1, x + 25);
printw("*");
move(y + 2, x + 25);
printw("*");
move(y + 3, x + 25);
printw("*");
//Y
move(y, x + 27);
printw("*");
move(y + 1, x + 28);
printw("*");
move(y + 2, x + 29);
printw("*");
move(y + 2, x + 30);
printw("*");
move(y + 2, x + 31);
printw("*");
move(y + 1, x + 32);
printw("*");
move(y, x + 33);
printw("*");
move(y + 3, x + 30);
printw("*");
move(y + 4, x + 30);
printw("*");
//2번째 줄
//T
move(y + 6, x + 4);
printw("*");
move(y + 6, x + 5);
printw("*");
move(y + 6, x + 6);
printw("*");
move(y + 6, x + 7);
printw("*");
move(y + 6, x + 8);
printw("*");
move(y + 7, x + 6);
printw("*");
move(y + 8, x + 6);
printw("*");
move(y + 9, x + 6);
printw("*");
move(y + 10, x + 6);
printw("*");
//I
move(y + 6, x + 12);
printw("*");
move(y + 6, x + 11);
printw("*");
move(y + 6, x + 13);
printw("*");
move(y + 7, x + 12);
printw("*");
move(y + 8, x + 12);
printw("*");
move(y + 9, x + 12);
printw("*");
move(y + 10, x + 12);
printw("*");
move(y + 10, x + 11);
printw("*");
move(y + 10, x + 13);
printw("*");
//M
move(x, y + 40);
printw("Welcome! %s!", UID);
move(x + 1, y + 40);
printw("1. Studytime Measuring");
move(x + 2, y + 40);
printw("2. Display Stats");
move(x + 3, y + 40);
printw("3. Group");
move(x + 4, y + 40);
printw("4. Settings");
move(x + 5, y + 40);
printw("5. Exit");
refresh();
}
void menu2()
{
WINDOW* win = newwin(34, 60, 1, 1);
char c;
while (1)
{
menu2_screen(win);
c = wgetch(win);
if (c == 'q') break;
if (c == '1') menu2_1(win);
if (c == '2') menu2_2(win);
if (c == '3') menu2_3(win);
if (user_dead == 1) break;
}
wclear(win);
wrefresh(win);
delwin(win);
}
void menu2_screen(WINDOW* win)
{
wclear(win);
box(win, '|', '-');
mvwprintw(win, 3, 2, "Display stats ");
mvwprintw(win, 6, 2, "1. Today's Studytime ");
mvwprintw(win, 8, 2, "2. This week's Studytime ");
mvwprintw(win, 10, 2, "3. This month's Studytime ");
mvwprintw(win, 30, 2, "\'q\' to quit");
wrefresh(win);
}
void menu2_1(WINDOW* win)
{
time_t statdate = time(NULL);
time_t today = statdate;
struct tm* statdate_tm = localtime(&statdate);
int today_year = statdate_tm->tm_year + 1900;
int today_month = statdate_tm->tm_mon + 1;
int today_day = statdate_tm->tm_mday;
// 통계를 보여줄 년월일 변수
int stat_year = statdate_tm->tm_year + 1900;
int stat_month = statdate_tm->tm_mon + 1;
int stat_day = statdate_tm->tm_mday;
keypad(win, TRUE); // 방향키 받을 거임
char c;
while (1)
{
mvwprintw(win, 3, 2, "Display stats (Arrow keys for control)");
if (stat_year == today_year && stat_month == today_month && stat_day == today_day)
mvwprintw(win, 5, 2, "Studytime during Today(%04d-%02d-%02d)", stat_year, stat_month, stat_day);
else
mvwprintw(win, 5, 2, "Studytime of %04d-%02d-%02d ", stat_year, stat_month, stat_day);
mvwprintw(win, 6, 2, " ");
mvwprintw(win, 10, 2, " ");
day_stats(win, stat_year, stat_month, stat_day);
wrefresh(win);
c = wgetch(win);
if (c == 'q') break;
if (c == ARROW_DOWN || c == ARROW_LEFT)
{
statdate -= SECONDS_PER_DAY;
statdate_tm = localtime(&statdate);
stat_year = statdate_tm->tm_year + 1900;
stat_month = statdate_tm->tm_mon + 1;
stat_day = statdate_tm->tm_mday;
}
if (statdate < today && (c == ARROW_UP || c == ARROW_RIGHT))
{
statdate += SECONDS_PER_DAY;
statdate_tm = localtime(&statdate);
stat_year = statdate_tm->tm_year + 1900;
stat_month = statdate_tm->tm_mon + 1;
stat_day = statdate_tm->tm_mday;
}
}
keypad(win, FALSE);
wclear(win);
}
void day_stats(WINDOW* win, int year, int month, int day)
{
char statfile[256];
sprintf(statfile, "%04d%02d%02d.txt", year, month, day);
char UID_dir[256];
sprintf(UID_dir, "%s/%s", ".", UID);
mvwprintw(win, 6, 2, "%s", UID_dir);
if (chdir(UID_dir) == -1)
{
perror("chdir");
exit(21);
}
int fd1;
if ((fd1 = creat(statfile, 0777)) == -1)
{
perror("open");
exit(22);
}
srand(time(NULL));
timelog templog = {0, };
strcpy(templog.subject, "C Language");
templog.start_time = 0;
templog.finish_time = rand() % 10800 + 2;
templog.studytime = (double)templog.finish_time - templog.start_time;
write(fd1, &templog, sizeof(timelog));
timelog templog2 = {0, };
strcpy(templog2.subject, "System Programming");
templog2.start_time = 0;
templog2.finish_time = rand() % 10800 + 2;
templog2.studytime = (double)templog2.finish_time - templog2.start_time;
write(fd1, &templog2, sizeof(timelog));
close(fd1);
int fd2;
double total = 0.0;
if ((fd2 = open(statfile, O_RDONLY)) == -1)
{
perror("open");
exit(22);
}
timelog templog3;
int j = 0;
mvwprintw(win, 9, 2, "|----------------------------------------------------|");
mvwprintw(win, 12, 2, "|----------------------------------------------------|");
while (read(fd2, &templog3, sizeof(timelog)) >= sizeof(timelog))
{
total += templog3.studytime;
mvwprintw(win, 8 + 3 * j, 2, "%s studytime: %.0f seconds ", templog3.subject, templog3.studytime);
for (int i = 0; i < templog3.studytime / 210; i++)
mvwprintw(win, 9 + 3 * j, 3 + i, "%%");
j++;
}
mvwprintw(win, 6, 2, "Total studytime: %.0f seconds", total);
if (chdir("..") == -1)
{
perror("chdir");
exit(21);
}
close(fd2);
}
void menu2_2(WINDOW* win)
{
time_t endweek = time(NULL);
time_t today = endweek;
time_t startweek = endweek - (SECONDS_PER_DAY * 6);
// localtime은 static data구나
struct tm endweek_tm; localtime_r(&endweek, &endweek_tm);
struct tm startweek_tm; localtime_r(&startweek, &startweek_tm);
// int today_year = endweek_tm->tm_year + 1900;
// int today_month = endweek_tm->tm_mon + 1;
// int today_day = endweek_tm->tm_mday;
keypad(win, TRUE); // 방향키 받을 거임
char c;
while (1)
{
mvwprintw(win, 3, 2, "Display stats (Arrow keys for control)");
mvwprintw(win, 5, 2, "Studytime of %04d-%02d-%02d ~ %04d-%02d-%02d", startweek_tm.tm_year + 1900, startweek_tm.tm_mon + 1, startweek_tm.tm_mday, endweek_tm.tm_year + 1900, endweek_tm.tm_mon + 1, endweek_tm.tm_mday);
mvwprintw(win, 6, 2, " ");
mvwprintw(win, 10, 2, " ");
week_stats(win, endweek);
wrefresh(win);
c = wgetch(win);
if (c == 'q') break;
if (c == ARROW_DOWN || c == ARROW_LEFT)
{
endweek -= SECONDS_PER_DAY;
startweek -= SECONDS_PER_DAY;
}
if (endweek < today && (c == ARROW_UP || c == ARROW_RIGHT))
{
endweek += SECONDS_PER_DAY;
startweek += SECONDS_PER_DAY;
}
localtime_r(&endweek, &endweek_tm);
localtime_r(&startweek, &startweek_tm);
}
keypad(win, FALSE);
wclear(win);
}
void week_stats(WINDOW* win, time_t today)
{
char UID_dir[256];
sprintf(UID_dir, "%s/%s", ".", UID);
// mvwprintw(win, 6, 2, "%s", UID_dir);
if (chdir(UID_dir) == -1)
{
perror("chdir");
exit(21);
}
timelog weeklog[7];
int year, month, day;
double total = 0.0;
for(int week_i=6; week_i>=0; week_i--)
{
char statfile[15];
struct tm today_tm; localtime_r(&today, &today_tm);
year = today_tm.tm_year + 1900;
month = today_tm.tm_mon + 1;
day = today_tm.tm_mday;
sprintf(statfile, "%04d%02d%02d.txt", year, month, day);
weeklog[week_i].studytime = 0.0;
int fd;
if ((fd = open(statfile, O_RDONLY)) == -1)
{
mvwprintw(win, 8+week_i*3, 2, "%04d-%02d-%02d: %5.0f seconds", year, month, day, 0);
mvwprintw(win, 9+week_i*3, 2, "|----------------------------------------------------|");
today -= SECONDS_PER_DAY;
continue;
}
timelog templog;
while (read(fd, &templog, sizeof(timelog)) >= sizeof(timelog))
{
weeklog[week_i].studytime += templog.studytime;
total += templog.studytime;
}
close(fd);
mvwprintw(win, 8+week_i*3, 2, "%04d-%02d-%02d: %5.0f seconds", year, month, day, weeklog[week_i].studytime);
mvwprintw(win, 9+week_i*3, 2, "|----------------------------------------------------|");
for (int i = 0; i < weeklog[week_i].studytime / 1000; i++)
mvwprintw(win, 9+week_i*3, 3+i, "%%");
today -= SECONDS_PER_DAY;
}
mvwprintw(win, 6, 2, "Total studytime: %.0f seconds", total);
if (chdir("..") == -1)
{
perror("chdir");
exit(21);
}
}
void menu2_3(WINDOW* win)
{
time_t statmonth = time(NULL);
time_t today = statmonth;
// localtime은 static data구나
struct tm statmonth_tm;
// int today_year = endweek_tm->tm_year + 1900;
// int today_month = endweek_tm->tm_mon + 1;
// int today_day = endweek_tm->tm_mday;
keypad(win, TRUE); // 방향키 받을 거임
char c;
while (1)
{
localtime_r(&statmonth, &statmonth_tm);
mvwprintw(win, 3, 2, "Display stats (Arrow keys for control)");
mvwprintw(win, 5, 20, " ");
switch(statmonth_tm.tm_mon + 1)
{
case 1:
mvwprintw(win, 5, 2, "Studytime of January, %d", statmonth_tm.tm_year + 1900);
break;
case 2:
mvwprintw(win, 5, 2, "Studytime of February, %d", statmonth_tm.tm_year + 1900);
break;
case 3:
mvwprintw(win, 5, 2, "Studytime of March, %d", statmonth_tm.tm_year + 1900);
break;
case 4:
mvwprintw(win, 5, 2, "Studytime of April, %d", statmonth_tm.tm_year + 1900);
break;
case 5:
mvwprintw(win, 5, 2, "Studytime of May, %d", statmonth_tm.tm_year + 1900);
break;
case 6:
mvwprintw(win, 5, 2, "Studytime of June, %d", statmonth_tm.tm_year + 1900);
break;
case 7:
mvwprintw(win, 5, 2, "Studytime of July, %d", statmonth_tm.tm_year + 1900);
break;
case 8:
mvwprintw(win, 5, 2, "Studytime of August, %d", statmonth_tm.tm_year + 1900);
break;
case 9:
mvwprintw(win, 5, 2, "Studytime of September, %d", statmonth_tm.tm_year + 1900);
break;
case 10:
mvwprintw(win, 5, 2, "Studytime of October, %d", statmonth_tm.tm_year + 1900);
break;
case 11:
mvwprintw(win, 5, 2, "Studytime of November, %d", statmonth_tm.tm_year + 1900);
break;
case 12:
mvwprintw(win, 5, 2, "Studytime of December, %d", statmonth_tm.tm_year + 1900);
break;
}
mvwprintw(win, 6, 2, " ");
mvwprintw(win, 10, 2, " ");
month_stats(win, statmonth_tm);
wrefresh(win);
c = wgetch(win);
if (c == 'q') break;
if (c == ARROW_DOWN || c == ARROW_LEFT)
{
if(statmonth_tm.tm_mon > JANUARY) // 2월~12월
statmonth_tm.tm_mon--;
else // 1월
{
statmonth_tm.tm_year--;
statmonth_tm.tm_mon = DECEMBER; // 12월
}
}
if (statmonth < today && (c == ARROW_UP || c == ARROW_RIGHT))
{
if(statmonth_tm.tm_mon < DECEMBER) // 1월~11월
statmonth_tm.tm_mon++;
else // 12월
{
statmonth_tm.tm_year++;
statmonth_tm.tm_mon = JANUARY;
}
}
statmonth = mktime(&statmonth_tm);
}
keypad(win, FALSE);
wclear(win);
}
void month_stats(WINDOW* win, struct tm statmonth_tm)
{
char UID_dir[256];
sprintf(UID_dir, "%s/%s", ".", UID);
// mvwprintw(win, 6, 2, "%s", UID_dir);
if (chdir(UID_dir) == -1)
{
perror("chdir");
exit(21);
}
// 구조체 배열은 static 한 건가?
timelog subjectlog[7] = {0, }; // 과목별 로그
int year, month, day;
year = statmonth_tm.tm_year + 1900;
month = statmonth_tm.tm_mon + 1;
double total = 0.0;
int day_i, subject_count = 0;
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day_i = 31;
break;
case 2:
day_i = 29;
break;
default:
day_i = 30;
}
for(; day_i>=1; day_i--)
{
char statfile[15];
day = day_i;
sprintf(statfile, "%04d%02d%02d.txt", year, month, day);
memset(subjectlog[subject_count].subject, 0, sizeof(char)*30);
subjectlog[subject_count].studytime = 0.0;
int fd;
if ((fd = open(statfile, O_RDONLY)) == -1)
continue;
timelog templog;
while (read(fd, &templog, sizeof(timelog)) >= sizeof(timelog))
{
int subject_i;
total += templog.studytime;
for(subject_i=0; subject_i<subject_count; subject_i++)
{
if(strcmp(subjectlog[subject_i].subject, templog.subject) == 0)
{
subjectlog[subject_i].studytime += templog.studytime;
break;
}
}
if(subject_i != subject_count)
continue;
strcpy(subjectlog[subject_count].subject, templog.subject);
subjectlog[subject_count++].studytime += templog.studytime;
}
close(fd);
}
mvwprintw(win, 6, 2, "Total studytime: %.0f seconds ", total);
for(int subject_i = 0; subject_i<subject_count; subject_i++)
{
mvwprintw(win, 8+subject_i*3, 2, "%d. %s", subject_i+1, subjectlog[subject_i].subject);
mvwprintw(win, 9+subject_i*3, 2, "|------------------------------------- %7.0f seconds", subjectlog[subject_i].subject, subjectlog[subject_i].studytime);
}
for (int subject_i = 0; subject_i < subject_count; subject_i++)
for(int j = 0; j < subjectlog[subject_i].studytime / 5000; j++)
mvwprintw(win, 9+3*subject_i, 2+j, "%%");
if (chdir("..") == -1)
{
perror("chdir");
exit(21);
}
}
void menu3() {
//initscr();
noecho();
//wclear(win);
// make new window
WINDOW* win = newwin(34, 60, 1, 1);
char menu;
while (1) {
menu3_screen(win);
menu = getch();
if (menu == '1') menu3_join(win);
if (menu == '2') menu3_leave(win);
//if(menu=='3') menu3_rank(win);
if (menu == '4') {
break;
}
}
wclear(win);
wrefresh(win);
delwin(win);
return;
}
void menu3_join(WINDOW* win) {
wclear(win);
box(win, '|', '-');
wrefresh(win);
echo();
int user_flag = 0;
char* groupid = malloc(sizeof(char*) * MAX);
char* userid = malloc(sizeof(char*) * MAX);
mvwprintw(win, 3, 2, "Insert your USER ID : ");
mvwgetstr(win, 3, 24, userid);
//If the user has a group id, should we tell users to leave the group and rejoin to another group?
mvwprintw(win, 6, 2, "Enter the GROUP ID that you want to join : ");
mvwgetstr(win, 6, 45, groupid);
// ./users/no_group에 있던 userid 디렉토리 있는지 검사
int user_dir = 0;
char* no_dir = "./users/no_group";
DIR* noDir = opendir(no_dir);
if (noDir != NULL) {
struct dirent* no_entry;
while ((no_entry = readdir(noDir)) != NULL) {
if (no_entry->d_type == DT_DIR && strcmp(no_entry->d_name, userid) == 0) {
user_dir = 1; // found user dir
}
else { // user dir not found
// userid 디렉토리가 없다면, userid 확인하라고 에러 메세지 띄움
mvwprintw(win, 8, 2, "Check your USER ID or you already have a GROUP !");
}
}
}
// userid 디렉토리가 있다면, users 디렉토리에 groupid 디렉토리가 있는지 검사
int group_dir = 0;
char* g_dir = malloc(sizeof(char*) * 40); //to save groupid path
strcpy(g_dir, no_dir);
strcat(g_dir, "/");
strcat(g_dir, userid); // making groupid path
if (user_dir == 1) { // no_group에 user 디렉토리가 있다면
DIR* gDir = opendir(g_dir);
if (gDir != NULL) {
struct dirent* g_entry;
while ((g_entry = readdir(gDir)) != NULL) {
if (g_entry->d_type = DT_DIR && strcmp(g_entry->d_name, groupid) == 0) {
group_dir = 1;
}
}
}
}
// groupid 디렉토리가 없다면 , group 디렉토리를 생성하고 userid 디렉토리 옮김
// -> group_dir == 0 && user_dir == 1
if (group_dir == 0 && user_dir == 1) {
}
// groupid 디렉토리가 있다면 , 그 groupid 디렉토리 안으로 userid 디렉토리 옮김
// -> group_dir == 1 && user_dir == 1
else if (group_dir == 1 && user_dir == 1) {
}
int ufd = usersFd;
int read_st = 0;
Studyuser j_user;
lseek(ufd, 0, SEEK_SET);
while (read(ufd, &j_user, sizeof(Studyuser))) {
if (strcmp(j_user.user_ID, userid) == 0) {
user_flag = 1; // userid is found
strcpy(j_user.group_ID, groupid); // GROUP ID is copy to j_user
lseek(ufd, -sizeof(Studyuser), SEEK_CUR); // moving cursor to start point
write(ufd, &j_user, sizeof(Studyuser)); // replacing
wrefresh(win);
}
}
if (user_flag == 1) {
mvwprintw(win, 8, 2, "You join in to <%s> !", groupid);
mvwprintw(win, 9, 2, "If you want to go back, press 'q' !");
wrefresh(win);
}
else {
mvwprintw(win, 8, 2, "There is NO USER ID !!!!");
wrefresh(win);
}
char menu;
while ((menu = getch()) != 'q');
free(groupid);
free(userid);
wclear(win);
wrefresh(win);
return;
}
void menu3_leave(WINDOW* win) {
wclear(win);
box(win, '|', '-');
wrefresh(win);