-
Notifications
You must be signed in to change notification settings - Fork 32
/
main.c
1605 lines (1489 loc) · 57.1 KB
/
main.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
/* MIT License
*
* Copyright (c) 2022 hkm
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifdef _WIN32
/* windows下 */
#include <windows.h>
#include <io.h>
#define F_OK 0
#define W_OK 2
#define R_OK 4
#else
/* linux下 */
#include <unistd.h>
#endif
#include "CDanmakuFactory.h"
#include "Define/CLIDef.h"
void printHelpInfo();
int getArgNum(int argc, char **argv, const int optionIndex);
double getArgValDouble(int argc, char **argv, const int optIndex, const char *const optName, const double errorReturnValue);
BOOL getArgValBool(int argc, char **argv, const int optIndex, const char *const optName);
COORDIN getArgValCoodr(int argc, char **argv, const int optIndex, const char *const optName, int *argNum);
void toPause(BOOL skip);
BOOL isContinue(BOOL skip);
static CONFIG defaultConfig =
{
{1920, 1080}, /* 分辨率 */
1.00, /* 显示区域 */
1.00, /* 滚动区域 */
12.0, /* 滚动时间 */
5.0, /* 固定时间 */
0, /* 弹幕密度 */
38, /* 字号 */
/* 字体 */
"Microsoft YaHei",
180, /* 不透明度 */
0, /* 描边 */
1, /* 阴影 */
FALSE, /* 是否加粗 */
TRUE, /* 是否保存屏蔽部分 */
FALSE, /* 是否显示用户名 */
TRUE, /* 是否显示消息框 */
{500, 1080}, /* 消息框大小 */
{20, 0}, /* 消息框位置 */
38, /* 消息框内文字大小 */
0.0f, /* 消息框持续时长 */
0.0f, /* 消息框礼物最低价格限制 */
0, /* 屏蔽模式 */
0, /* 统计模式 */
NULL, /* 弹幕黑名单 */
};
int main(int argc, char **argv)
{
FINFO outfile;
FINFO *infile = NULL;
int infileNum = 0;
int argCnt = 1;
int cnt;
BOOL showConfig = FALSE;
BOOL saveConfig = FALSE;
BOOL configFileErr = FALSE;
BOOL ignoreWarnings = FALSE;
CONFIG config;
char tempStr[MAX_TEXT_LENGTH], *tempPtr;
char programPath[MAX_TEXT_LENGTH];
char configFilePath[MAX_TEXT_LENGTH];
outfile.isSet = FALSE;
/* 打印程序版本信息 */
printf("\nDanmakuFactory "VERSION" "EDITION" by hkm ([email protected])"
"\nhttps://github.com/hihkm/DanmakuFactory\n"
);
/* 获取程序运行目录 */
tempStr[0] = '\0';
#ifdef _WIN32
GetModuleFileName(0, tempStr, MAX_TEXT_LENGTH);
#else
readlink("/proc/self/exe", tempStr, MAX_TEXT_LENGTH);
#endif
filenameGetPath(programPath, tempStr, MAX_TEXT_LENGTH);
/* 获取配置文件路径 */
strSafeCopy(configFilePath, programPath, MAX_TEXT_LENGTH);
strSafeCat(configFilePath, MAX_TEXT_LENGTH, CONFIG_FILE_NAME);
if (strstr(configFilePath, CONFIG_FILE_NAME) == NULL)
{
printf("\nWARNING"
"\nOut of buffer.");
printf("\nNOTE"
"\nFail to get config file path, because the path is too long.\n");
configFileErr = TRUE;
toPause(ignoreWarnings);
}
/* 解析参数 */
if (argc <= 1)
{
printHelpInfo();
return 0;
}
else
{
/* 读配置文件 */
config = readConfig(configFilePath, defaultConfig);
/* 遍历参数 */
while (argCnt < argc)
{
if (!strcmp("-h", argv[argCnt]) || !strcmp("--help", argv[argCnt]))
{
printHelpInfo();
return 0;
}
else if (!strcmp("-c", argv[argCnt]) || !strcmp("--config", argv[argCnt]))
{
showConfig = TRUE;
argCnt += 1;
}
else if (!strcmp("--save", argv[argCnt]))
{
saveConfig = TRUE;
showConfig = TRUE;
argCnt += 1;
}
else if (!strcmp("-o", argv[argCnt]) || !strcmp("--output", argv[argCnt]))
{ /* 输出文件名 */
switch (getArgNum(argc, argv, argCnt))
{
case 0:
fprintf(stderr, "\nERROR"
"\nOutput file must be specified.\n");
return 0;
break;
case 1:
strSafeCopy(outfile.fileName, argv[argCnt+1], FILENAME_LEN);
filenameGetFormat(outfile.template, outfile.fileName, FORMAT_LEN);
argCnt += 2;
break;
case 2:
strSafeCopy(outfile.template, argv[argCnt+1], FORMAT_LEN);
strSafeCopy(outfile.fileName, argv[argCnt+2], FILENAME_LEN);
argCnt += 3;
break;
default:
/* 传入未知参数丢给下一轮获取选项时报错 */
argCnt += 3;
break;
}
deQuotMarks(outfile.fileName);
deQuotMarks(outfile.template);
outfile.isSet = TRUE;
/* 合法性检查 */
if (!ISFORMAT(outfile.template))
{
fprintf(stderr, "\nERROR"
"\nUnknow format \"%s\".\n", outfile.template);
return 0;
}
}
else if (!strcmp("-i", argv[argCnt]) || !strcmp("--input", argv[argCnt]))
{ /* 输入文件名 */
int num = getArgNum(argc, argv, argCnt);
for (cnt = 0; cnt < num; cnt++)
{
if ((infile = (FINFO *)realloc(infile, (infileNum+1) * sizeof(FINFO))) == NULL)
{
fprintf(stderr, "\nERROR"
"\nOut of memory.\n");
return 0;
}
strSafeCopy(tempStr, argv[argCnt + cnt + 1], MAX_TEXT_LENGTH);
deQuotMarks(tempStr);
if (ISFORMAT(tempStr))
{
strcpy(infile[infileNum].template, tempStr);
}
else
{
// sprintf_s(infile[infileNum].template, FILENAME_LEN, "%stemplates\\%s.txt", programPath, tempStr);
// (infile[infileNum].template)[FILENAME_LEN-1] = '\0';
strSafeCopy(infile[infileNum].template, programPath, FILENAME_LEN);
strSafeCat(infile[infileNum].template, FILENAME_LEN, "templates\\");
strSafeCat(infile[infileNum].template, FILENAME_LEN, tempStr);
strSafeCat(infile[infileNum].template, FILENAME_LEN, ".txt");
}
if (ISFORMAT(tempStr) || access(infile[infileNum].template, F_OK) == 0)
{
cnt++;
if (cnt >= num)
{
fprintf(stderr, "\nERROR"
"\nTemplate must be followed by a filename.\n");
return 0;
}
strcpy(infile[infileNum].fileName, argv[argCnt + cnt + 1]);
}
else
{
strcpy(infile[infileNum].fileName, argv[argCnt + cnt + 1]);
filenameGetFormat(infile[infileNum].template, infile[infileNum].fileName, FORMAT_LEN);
/* 合法性检查 */
if (!ISFORMAT(infile[infileNum].template))
{
fprintf(stderr, "\nERROR"
"\nUnknow Template %s.\n", infile[infileNum].template);
return 0;
}
}
infile[infileNum].timeShift = 0.0f;
deQuotMarks(infile[infileNum].fileName);
infileNum++;
}
argCnt += num + 1;
}
else if (!strcmp("-r", argv[argCnt]) || !strcmp("--resolution", argv[argCnt]))
{ /* 分辨率 */
int argNum; /* 用空格分隔也是合法的,因此可能有两个参数,需要获取并按实况跳过 */
COORDIN returnValue = getArgValCoodr(argc, argv, argCnt, "Resolution", &argNum);
if (argNum == -1)
{
return 0;
}
config.resolution = returnValue;
argCnt += argNum + 1; /* 参数数量以及选项本体 */
}
else if (!strcmp("-x", argv[argCnt]) || !strcmp("--resx", argv[argCnt]))
{ /* 旧版本兼容 分辨率宽 */
double returnValue = getArgValDouble(argc, argv, argCnt, "Resx", -256.00);
if (fabs(returnValue - (-256.0)) < EPS)
{
return 0;
}
config.resolution.x = (int)returnValue;
argCnt += 2;
}
else if (!strcmp("-y", argv[argCnt]) || !strcmp("--resy", argv[argCnt]))
{ /* 旧版本兼容 分辨率高 */
double returnValue = getArgValDouble(argc, argv, argCnt, "Resy", -256.00);
if (fabs(returnValue - (-256.0)) < EPS)
{
return 0;
}
config.resolution.y = (int)returnValue;
argCnt += 2;
}
else if (!strcmp("-s", argv[argCnt]) || !strcmp("--scrolltime", argv[argCnt]))
{ /* 滚动时间 */
double returnValue = getArgValDouble(argc, argv, argCnt, "ScrollTime", -256.00);
if (fabs(returnValue - (-256.0)) < EPS)
{
return 0;
}
config.scrolltime = (float)returnValue;
argCnt += 2;
}
else if (!strcmp("-f", argv[argCnt]) || !strcmp("--fixtime", argv[argCnt]))
{ /* 固定时间 */
double returnValue = getArgValDouble(argc, argv, argCnt, "FixTime", -256.00);
if (fabs(returnValue - (-256.0)) < EPS)
{
return 0;
}
config.fixtime = (float)returnValue;
argCnt += 2;
}
else if (!strcmp("-d", argv[argCnt]) || !strcmp("--density", argv[argCnt]))
{ /* 弹幕密度 */
double returnValue = getArgValDouble(argc, argv, argCnt, "Density", -256.00);
if (fabs(returnValue - (-256.0)) < EPS)
{
return 0;
}
config.density = (int)returnValue;
argCnt += 2;
}
else if (!strcmp("-S", argv[argCnt]) || !strcmp("--fontsize", argv[argCnt]))
{ /* 字号 */
double returnValue = getArgValDouble(argc, argv, argCnt, "Fontsize", -256.00);
if (fabs(returnValue - (-256.0)) < EPS)
{
return 0;
}
config.fontsize = (int)returnValue;
argCnt += 2;
}
else if (!strcmp("-N", argv[argCnt]) || !strcmp("--fontname", argv[argCnt]))
{ /* 字号 */
switch (getArgNum(argc, argv, argCnt))
{
case 0:
fprintf(stderr, "\nERROR"
"\nFontname must be specified.\n");
return 0;
break;
case 1:
strSafeCopy(config.fontname, argv[argCnt+1], FONTNAME_LEN);
deQuotMarks(config.fontname);
break;
default:
fprintf(stderr, "\nERROR"
"\nInvalid argument \"%s\".\n", argv[argCnt+2]);
return 0;
break;
}
argCnt += 2;
}
else if (!strcmp("-O", argv[argCnt]) || !strcmp("--opacity", argv[argCnt]))
{ /* 不透明度 */
double returnValue = getArgValDouble(argc, argv, argCnt, "Opacity", -256.00);
if (fabs(returnValue - (-256.0)) < EPS)
{
return 0;
}
config.opacity = (int)returnValue;
argCnt += 2;
}
else if (!strcmp("-L", argv[argCnt]) || !strcmp("--outline", argv[argCnt]))
{ /* 描边 */
double returnValue = getArgValDouble(argc, argv, argCnt, "Outline", -256.00);
if (fabs(returnValue - (-256.0)) < EPS)
{
return 0;
}
config.outline = returnValue;
argCnt += 2;
}
else if (!strcmp("-D", argv[argCnt]) || !strcmp("--shadow", argv[argCnt]))
{ /* 阴影 */
double returnValue = getArgValDouble(argc, argv, argCnt, "Shadow", -256.00);
if (fabs(returnValue - (-256.0)) < EPS)
{
return 0;
}
config.shadow = returnValue;
argCnt += 2;
}
else if (!strcmp("-B", argv[argCnt]) || !strcmp("--bold", argv[argCnt]))
{ /* 是否加粗 */
BOOL returnValue = getArgValBool(argc, argv, argCnt, "Bold");
if (returnValue == BOOL_UNDETERMINED)
{
return 0;
}
config.bold = returnValue;
argCnt += 2;
}
else if (!strcmp("--displayarea", argv[argCnt]))
{ /* 显示区域 */
double returnValue = getArgValDouble(argc, argv, argCnt, "DisplayArea", -256.00);
if (fabs(returnValue - (-256.0)) < EPS)
{
return 0;
}
config.displayarea = (float)returnValue;
argCnt += 2;
}
else if (!strcmp("--scrollarea", argv[argCnt]))
{ /* 滚动区域 */
double returnValue = getArgValDouble(argc, argv, argCnt, "ScrollArea", -256.00);
if (fabs(returnValue - (-256.0)) < EPS)
{
return 0;
}
config.scrollarea = (float)returnValue;
argCnt += 2;
}
else if (!strcmp("-b", argv[argCnt]) || !strcmp("--blockmode", argv[argCnt]))
{ /* 屏蔽模式 */
switch (getArgNum(argc, argv, argCnt))
{
case 0:
fprintf(stderr, "\nERROR"
"\n\"Blockmode\" must be specified.\n");
return 0;
break;
case 1:
break;
default:
fprintf(stderr, "\nERROR"
"\nInvalid argument \"%s\".\n", argv[argCnt+2]);
return 0;
break;
}
config.blockmode = 0;
if (!strcmp("null", argv[argCnt+1]))
{
argCnt += 2;
continue;
}
cnt = 0;
char *argPtr = argv[argCnt+1];
while (*argPtr != '\0')
{
tempPtr = tempStr;
while (*argPtr != '\0' && *argPtr != '-' && cnt < MAX_TEXT_LENGTH)
{
*tempPtr = *argPtr;
tempPtr++;
argPtr++;
cnt++;
}
if (*argPtr == '-')
{
argPtr++;
}
*tempPtr = '\0';
deQuotMarks(tempStr);
toLower(NULL, tempStr);
if (!strcmp("l2r", tempStr))
{
config.blockmode += BLK_L2R;
}
else if (!strcmp("r2l", tempStr))
{
config.blockmode += BLK_R2L;
}
else if (!strcmp("top", tempStr))
{
config.blockmode += BLK_TOP;
}
else if (!strcmp("bottom", tempStr))
{
config.blockmode += BLK_BOTTOM;
}
else if (!strcmp("special", tempStr))
{
config.blockmode += BLK_SPECIAL;
}
else if (!strcmp("color", tempStr) || !strcmp("colour", tempStr))
{
config.blockmode += BLK_COLOR;
}
else if (!strcmp("repeat", tempStr))
{
config.blockmode += BLK_REPEAT;
}
else
{
fprintf(stderr, "\nERROR"
"\nInvalid type-name \"%s\".\n", tempStr);
return 0;
}
}
argCnt += 2;
}
else if (!strcmp("--statmode", argv[argCnt]))
{ /* 统计模式 */
switch (getArgNum(argc, argv, argCnt))
{
case 0:
fprintf(stderr, "\nERROR"
"\n\"Statmode\" must be specified.\n");
return 0;
break;
case 1:
break;
default:
fprintf(stderr, "\nERROR"
"\nInvalid argument \"%s\".\n", argv[argCnt+2]);
return 0;
break;
}
config.statmode = 0;
if (!strcmp("null", argv[argCnt+1]))
{
argCnt += 2;
continue;
}
cnt = 0;
char *argPtr = argv[argCnt+1];
while (*argPtr != '\0')
{
tempPtr = tempStr;
while (*argPtr != '\0' && *argPtr != '-' && cnt < MAX_TEXT_LENGTH)
{/* 拷贝 - 之前的字符 */
*tempPtr = *argPtr;
tempPtr++;
argPtr++;
cnt++;
}
if (*argPtr == '-')
{
argPtr++;
}
*tempPtr = '\0';
/* 字符串比对 */
deQuotMarks(tempStr);
toLower(NULL, tempStr);
if (!strcmp("table", tempStr))
{
config.statmode += TABLE;
}
else if (!strcmp("histogram", tempStr))
{
config.statmode += HISTOGRAM;
}
else
{
fprintf(stderr, "\nERROR"
"\nInvalid type-name \"%s\".\n", tempStr);
return 0;
}
}
argCnt += 2;
}
else if(!(strcmp("--blacklist", argv[argCnt])))
{ /* 弹幕黑名单 */
// 读取黑名单文件
char *filename = argv[argCnt+1];
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
fprintf(stderr, "\nERROR"
"\nOpen blacklist file `%s` failed!\n", filename);
return 0;
}
char buf[4096];
char* tokens[4096 + 1];
int i = 0;
while (i < SIZE_NUM(char *, tokens) - 1 && fgets(buf, SIZE_NUM(char, buf), fp) != NULL) {
size_t len = strlen(buf);
if (len >= 1 && buf[len - 1] == '\n') { // 检查最后一个字符是否为换行符
buf[len - 1] = '\0'; // 如果是,移除它
}
if (strlen(buf) == 0) continue;
tokens[i] = strdup(buf); // malloc here.
i++;
}
tokens[i] = NULL;
fclose(fp);
config.blocklist = tokens;
argCnt += 2;
}
else if (!strcmp("-t", argv[argCnt]) || !strcmp("--timeshift", argv[argCnt]))
{ /* 时轴偏移 因不确定文件数量,故先跳过,最后解析 */
argCnt++;
while (argCnt < argc && ISNUMBERIC(argv[argCnt]))
{
argCnt++;
}
}
else if (!strcmp("--saveblocked", argv[argCnt]))
{ /* 是否保存屏蔽部分 */
BOOL returnValue = getArgValBool(argc, argv, argCnt, "SaveBlocked");
if (returnValue == BOOL_UNDETERMINED)
{
return 0;
}
config.saveBlockedPart = returnValue;
argCnt += 2;
}
else if (!(strcmp("--showusernames", argv[argCnt])))
{ /* 是否显示用户名 */
BOOL returnValue = getArgValBool(argc, argv, argCnt, "showUsernames");
if (returnValue == BOOL_UNDETERMINED)
{
return 0;
}
config.showUserNames = returnValue;
argCnt += 2;
}
else if (!(strcmp("--showmsgbox", argv[argCnt])))
{ /* 是否显示消息框 */
BOOL returnValue = getArgValBool(argc, argv, argCnt, "ShowMsgbox");
if (returnValue == BOOL_UNDETERMINED)
{
return 0;
}
config.showMsgBox = returnValue;
argCnt += 2;
}
else if (!(strcmp("--msgboxfontsize", argv[argCnt])))
{ /* 消息框文字大小 */
double returnValue = getArgValDouble(argc, argv, argCnt, "MsgboxFontsize", 0);
config.msgboxFontsize = (int)returnValue;
argCnt += 2;
}
else if (!(strcmp("--msgboxduration", argv[argCnt])))
{ /* 消息框持续时长 */
double returnValue = getArgValDouble(argc, argv, argCnt, "MsgboxDuration", 0);
config.msgboxDuration = (float)returnValue;
argCnt += 2;
}
else if (!(strcmp("--giftminprice", argv[argCnt])))
{ /* 按最低礼物价格屏蔽 */
double returnValue = getArgValDouble(argc, argv, argCnt, "GiftMinPrice", 0);
config.giftMinPrice = (float)returnValue;
argCnt += 2;
}
else if (!(strcmp("--giftmergetolerance", argv[argCnt])))
{ /* 相同用户相同礼物合并时间窗 (已废弃!) */
printf("\nWarning: `--giftmergetolerance` has been deprecated!\n");
argCnt += 2;
}
else if (!strcmp("--msgboxsize", argv[argCnt]))
{ /* 消息框大小 */
int argNum; /* 用空格分隔也是合法的,因此可能有两个参数,需要获取并按实况跳过 */
COORDIN returnValue = getArgValCoodr(argc, argv, argCnt, "MsgboxSize", &argNum);
if (argNum == -1)
{
return 0;
}
config.msgBoxSize = returnValue;
argCnt += argNum + 1; /* 参数数量以及选项本体 */
}
else if (!strcmp("--msgboxpos", argv[argCnt]))
{ /* 消息框位置 */
int argNum; /* 用空格分隔也是合法的,因此可能有两个参数,需要获取并按实况跳过 */
COORDIN returnValue = getArgValCoodr(argc, argv, argCnt, "MsgboxPos", &argNum);
if (argNum == -1)
{
return 0;
}
config.msgBoxPos = returnValue;
argCnt += argNum + 1; /* 参数数量以及选项本体 */
}
else if (!(strcmp("--ignore-warnings", argv[argCnt])))
{ /* 跳过全部警告*/
ignoreWarnings = TRUE;
argCnt++;
}
else if (!(strcmp("--check-version-"VERSION, argv[argCnt])))
{ /* 为GUI提供程序版本确认 如果是错误的版本将会报非法参数错误 */
argCnt++;
}
else
{
fprintf(stderr, "\nERROR"
"\nInvalid argument %s\n", argv[argCnt]);
return 0;
}
}
/* 寻找并解析时间平移量 */
for (argCnt = 0; argCnt < argc; argCnt++)
{
if (!strcmp("-t", argv[argCnt]) || !strcmp("--timeshift", argv[argCnt]))
{
argCnt++;
for (cnt = 0; cnt < infileNum; cnt++)
{
if (argCnt >= argc || !ISNUMBERIC(argv[argCnt]))
{
fprintf(stderr, "\nERROR"
"\nToo few values in option timeshift(-t, --timeshift).");
fprintf(stderr, "\nNOTE"
"\n%d filenames are provided, but only %d timeshift value.\n",
infileNum, cnt);
return 0;
}
strSafeCopy(tempStr, argv[argCnt], MAX_TEXT_LENGTH);
deQuotMarks(tempStr);
infile[cnt].timeShift = atof(tempStr);
argCnt++;
}
/* 有更多的数字意味着语法错误 */
if (argCnt < argc && ISNUMBERIC(argv[argCnt]))
{
fprintf(stderr, "\nERROR"
"\nToo many values in option timeshift(-t, --timeshift).");
fprintf(stderr, "\nNOTE"
"\nOnly %d filename(s) are provided, but more than %d timeshift values.\n",
infileNum, infileNum);
return 0;
}
/* 解析完毕即跳出循环 */
break;
}
}
}
/* 显示配置信息 */
if (showConfig == TRUE)
{
printConfig(config);
}
/* 配置项合法性检查 */
{
/* 分辨率 */
if (config.resolution.x <= 0 || config.resolution.y <= 0)
{
fprintf(stderr, "\nERROR"
"\n\"Resolution\" must be an integer greater than 0.\n");
return 0;
}
/* 滚动时间 */
if (config.scrolltime < EPS)
{
fprintf(stderr, "\nERROR"
"\n\"ScrollTime\" must be a real number greater than 0.00.\n");
return 0;
}
/* 固定时间 */
if (config.fixtime < EPS)
{
fprintf(stderr, "\nERROR"
"\n\"FixTime\" must be a real number greater than 0.00.\n");
return 0;
}
/* 密度 */
if (config.density < -1)
{
fprintf(stderr, "\nERROR"
"\n\"Density\" must be an integer greater than or equal to -1.\n");
return 0;
}
/* 字号 */
if (config.fontsize <= 0)
{
fprintf(stderr, "\nERROR"
"\n\"Fontsize\" must be an integer greater than 0.\n");
return 0;
}
/* 不透明度 */
if (config.opacity <= 0 || config.opacity > 255)
{
fprintf(stderr, "\nERROR"
"\n\"Opacity\" must be an integer greater than 0 and less than or equal to 255.\n");
return 0;
}
/* 描边 */
if (config.outline < 0.0 || config.outline > 4.0)
{
fprintf(stderr, "\nERROR"
"\n\"Outline\" must be an float greater than or equal to 0 and less than or equal to 4.\n");
return 0;
}
/* 阴影 */
if (config.shadow < 0.0|| config.shadow > 4.0)
{
fprintf(stderr, "\nERROR"
"\n\"Shadow\" must be an float greater than or equal to 0 and less than or equal to 4.\n");
return 0;
}
/* 显示区域 */
if (config.displayarea < EPS || config.displayarea > 1.0 + EPS)
{
fprintf(stderr, "\nERROR"
"\n\"DisplayArea\" must be a real number greater than 0.0 and less than or equal to 1.0.\n");
return 0;
}
/* 滚动区域 */
if (config.scrollarea < EPS || config.scrollarea > 1.0 + EPS)
{
fprintf(stderr, "\nERROR"
"\n\"ScrollArea\" must be a real number greater than 0.0 and less than or equal to 1.0.\n");
return 0;
}
/* 字体 */
tempPtr = config.fontname;
while (*tempPtr != '\0')
{/* 验证是否全部为可打印的ascii字符 */
if (*tempPtr < 0x20 || *tempPtr > 0x7e)
{/* ascii 非可打印字符范围 */
printf("\nWARNING"
"\nSome characters of fontname are non-ASCII characters, which may cause the garbled problem.\n");
toPause(ignoreWarnings);
break;
}
tempPtr++;
}
/* 消息框文字大小 */
if (config.msgboxFontsize <= 0)
{
fprintf(stderr, "\nERROR"
"\n\"MsgboxFontsize\" must be an integer greater than 0.\n");
return 0;
}
/* 消息框大小 */
if (config.msgBoxSize.x <= config.msgboxFontsize || config.msgBoxSize.y <= config.msgboxFontsize)
{
fprintf(stderr, "\nERROR"
"\n\"MsgBoxSize\" must be an integer greater than the \"msgboxFontsize\".\n");
return 0;
}
}
/* 保存配置文件 */
if (saveConfig == TRUE && configFileErr == FALSE)
{
if (writeConfig(configFilePath, config) == TRUE)
{
printf("\nConfiguration file had been saved successfully!\n");
}
else
{
printf("\nWARNING"
"\nFailed to write the configuration file!\n");
toPause(ignoreWarnings);
}
}
/* 显示文件信息 */
if (outfile.isSet == FALSE)
{
fprintf(stderr, "\nERROR"
"\nOutput file must be specified.");
fprintf(stderr, "\nNOTE"
"\nUse -o or --output to specify.\n");
return 0;
}
if (infileNum == 0)
{
fprintf(stderr, "\nERROR"
"\nInput file must be specified.");
fprintf(stderr, "\nNOTE"
"\nUse -i or --input to specify.\n");
return 0;
}
printf("\nInput file(s):");
printf("\nNumber|Template |TimeShift|FileName\n");
for (cnt = 0; cnt < infileNum; cnt++)
{
strSafeCopy(tempStr, infile[cnt].template, MAX_TEXT_LENGTH);
if (strlen(infile[cnt].template) > 10)
{
strcpy(tempStr + 10, "...");
}
printf("%6d|%-13s|%8.3fs|%s\n", cnt+1, tempStr, infile[cnt].timeShift, infile[cnt].fileName);
}
printf("\nOutput file:");
printf("\nFormat|FileName");
printf("\n%6s|%s\n", outfile.template, outfile.fileName);
/* 读取文件 */
printf("\nLoading files...\n");
int returnValue;
STATUS status;
DANMAKU *danmakuPool = NULL;
status.totalNum = 0;
for (cnt = 0; cnt < infileNum; cnt++)
{
printf("Loading file \"%s\"\n", infile[cnt].fileName);
/* 检查文件是否存在 */
if (access(infile[cnt].fileName, F_OK) != 0)
{
fprintf(stderr, "\nERROR"
"\nNo such file.\n");
return 0;
}
/* 权限检查 */
if (access(infile[cnt].fileName, R_OK) != 0)
{
fprintf(stderr, "\nERROR"
"\nPermission denied.\n");
return 0;
}
if (!strcmp("xml", infile[cnt].template))
{
returnValue = readXml(infile[cnt].fileName, &danmakuPool, "a", infile[cnt].timeShift, &status);
switch (returnValue)
{
case 0:
break;
case 1:
fprintf(stderr, "\nERROR [code rx%d]"
"\nFailed to open file \"%s\".\n",
returnValue, infile[cnt].fileName
);
return 0;
break;
case 2:
case 3:
case 4:
fprintf(stderr, "\nERROR [code rx%d]"
"\nFailed to read file \"%s\".\n",
returnValue, infile[cnt].fileName
);
return 0;
break;
case 5:
case 6:
case 7:
fprintf(stderr, "\nERROR [code rx%d]"
"\nOut of memory.\n",
returnValue
);
return 0;
break;
case 8:
printf("\nWARNING [code rx%d]"
"\nIncorrect file format, contiune or exit?",
returnValue
);
printf("\nNOTE"
"\nCould not load file \"%s\" as a xml file.\n", infile[cnt].fileName);
if (isContinue(ignoreWarnings) == FALSE)
{
return 0;
}
break;
default:
fprintf(stderr, "\nERROR [code rx%d]"
"\nUndefined Error\n",
returnValue
);
return 0;
break;
}
}
else if (!strcmp("json", infile[cnt].template))
{
returnValue = readJson(infile[cnt].fileName, &danmakuPool, "a", infile[cnt].timeShift, &status);
switch (returnValue)
{
case 0:
break;