-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathXmlFile.c
839 lines (759 loc) · 25.9 KB
/
XmlFile.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
/* 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.
*/
#include "CDanmakuFactory.h"
#define LABEL_LEN 10240
static char *xmlUnescape(char *const str);
static void errorExit(FILE *ipF, DANMAKU *head, DANMAKU *ptr);
/* 弹幕类型重定义规范 */
/* +-------------+---------+ */
/* | 类型名称 | 编号 | */
/* +-------------+---------+ */
/* | 右左滚动 | 1 | */
/* +-------------+---------+ */
/* | 左右滚动 | 2 | */
/* +-------------+---------+ */
/* | 上方固定 | 3 | */
/* +-------------+---------+ */
/* | 下方固定 | 4 | */
/* +-------------+---------+ */
/* | B站特殊弹幕 | 5 | */
/* +-------------+---------+ */
/*
** 是否包含子串
** 参数:
** 文件指针/存储字符串的指针/字符串长度
** 返回值:
** TRUE 包含
** FALSE 读取失败/不包含
*/
BOOL findSubstr(FILE *file, const char *substr, int maxlen) {
// 保存当前文件指针位置
long currentPos = ftell(file);
if (currentPos == -1) {
return FALSE; // 获取文件指针位置失败
}
char buffer[1024];
BOOL isFound = FALSE;
while (maxlen - 1 > 0 && fgets(buffer, maxlen < SIZE_NUM(char, buffer) ? maxlen : SIZE_NUM(char, buffer), file) != NULL) {
if (strstr(buffer, substr) != NULL) {
isFound = TRUE;
break;
}
maxlen -= (int)strlen(buffer);
}
// 恢复文件指针位置
fseek(file, currentPos, SEEK_SET);
return isFound;
}
/*
* 读取xml文件加入弹幕池
* 参数:
* 文件名/链表头/读取模式("n"清空新建 / "a"尾部追加)/时轴偏移量
* 返回值:
* 0 正常退出
* 1 打开文件失败
* 2 3 4 读取文件发生错误
* 5 6 7 内存空间申请失败
* 8 文件未能按正确格式读入
*/
int readXml(const char *const ipFile, DANMAKU **head, const char *mode, const float timeShift, STATUS *const status)
{
FILE *ipF;
DANMAKU *tailNode = NULL;
int cnt, ch;
/* 刷新status */
if (status != NULL)
{
status -> function = (void *)readXml;
status -> completedNum = 0;
status -> isDone = FALSE;
}
/* 打开文件 */
if ((ipF = fopen(ipFile, "r")) == NULL)
{
return 1; /* 文件打开失败 */
}
// 检查文件是否为录播姬生成的文件
BOOL isBililiveRecorder = findSubstr(ipF, "<BililiveRecorder", 1024);
/* 判断读入方式 */
if (*head == NULL || *mode == 'n')
{/* 新建模式 */
freeList(*head);
*head = NULL;
}
else if (*mode == 'a')
{/* 追加模式 */
tailNode = *head;
while (tailNode -> next != NULL)
{
tailNode = tailNode -> next;
}
}
/* 读取xml文件 */
float time;
short type;
short fontSize = 0;
int color = 0;
int messageType;
char *text;
char tempText[MAX_TEXT_LENGTH];
char *textPtr;
char label[LABEL_LEN];
char key[MAX_TEXT_LENGTH];
char raw[LABEL_LEN];
DANMAKU *danmakuNode;
USERPART *userNode;
GIFTPART *giftNode;
SPPART *specialNode;
char *labelPtr;
USERPART user;
GIFTPART gift;
BOOL isDanmaku;
BOOL hasUserInfo;
BOOL hasGiftInfo;
while (!feof(ipF))
{
type = 0;
isDanmaku = FALSE;
hasUserInfo = FALSE;
hasGiftInfo = FALSE;
text = NULL;
gift.price = -1;
gift.count = -1;
gift.name[0] = '\0';
gift.duration = 0;
user.level = -1;
user.medalLevel = -1;
user.uid = 0;
user.medalName[0] = '\0';
user.name[0] = '\0';
/* 读取标签内容 */
while (fgetc(ipF) != '<')
{
if (feof(ipF))
{
goto ENDREAD;
}
if (ferror(ipF))
{
errorExit(ipF, *head, tailNode);
return 2; /* 读文件发生错误 */
}
}
labelPtr = label;
while ((ch = fgetc(ipF)) != '>')
{
if (feof(ipF))
{
goto ENDREAD;
}
if (ferror(ipF))
{
errorExit(ipF, *head, tailNode);
return 2; /* 读文件发生错误 */
}
if (labelPtr - label < LABEL_LEN)
{/* label长度限制 */
*labelPtr = ch;
labelPtr++;
}
}
*labelPtr = '\0';
/* 解析标签内容 */
labelPtr = label;
getNextWord(&labelPtr, tempText, MAX_TEXT_LENGTH, ' ', TRUE);
if (strcmp(tempText, "d") == 0)
{/* 普通弹幕 */
messageType = UNKNOW_TYPE_DANMAKU;
}
else if (strcmp(tempText, "gift") == 0)
{/* 录播姬 - 普通礼物 */
messageType = MSG_GIFT;
hasGiftInfo = TRUE;
}
else if (strcmp(tempText, "sc") == 0)
{/* 录播姬 - SuperChat */
messageType = MSG_SUPER_CHAT;
hasGiftInfo = TRUE;
}
else if (strcmp(tempText, "guard") == 0)
{/* 录播姬 - 舰长 */
messageType = MSG_GUARD;
hasGiftInfo = TRUE;
}
else
{/* 无效标签 */
continue;
}
while (*labelPtr != '\0')
{
strGetLeftPart(key, &labelPtr, '=', MAX_TEXT_LENGTH);
trim(key);
if (strcmp(key, "p") == 0)
{
strGetLeftPart(tempText, &labelPtr, ',', MAX_TEXT_LENGTH);
time = atof(deQuotMarks(tempText));
strGetLeftPart(tempText, &labelPtr, ',', MAX_TEXT_LENGTH);
type = (short)atoi(deQuotMarks(tempText));
strGetLeftPart(tempText, &labelPtr, ',', MAX_TEXT_LENGTH);
fontSize = (short)atoi(deQuotMarks(tempText));
strGetLeftPart(tempText, &labelPtr, ',', MAX_TEXT_LENGTH);
color = atoi(deQuotMarks(tempText));
/* 跳过后四个无价值参数 */
strGetLeftPart(NULL, &labelPtr, ',', MAX_TEXT_LENGTH);
strGetLeftPart(NULL, &labelPtr, ',', MAX_TEXT_LENGTH);
strGetLeftPart(NULL, &labelPtr, ',', MAX_TEXT_LENGTH);
strGetLeftPart(NULL, &labelPtr, '\"', MAX_TEXT_LENGTH);
}
else if (strcmp(key, "ts") == 0)
{
getNextWord(&labelPtr, tempText, MAX_TEXT_LENGTH, ' ', TRUE);
time = atof(deQuotMarks(tempText));
}
else if (strcmp(key, "user") == 0)
{
getNextWord(&labelPtr, user.name, USER_NAME_LEN, ' ', TRUE);
deQuotMarks(user.name);
hasUserInfo = TRUE;
}
else if (strcmp(key, "uid") == 0)
{
getNextWord(&labelPtr, tempText, MAX_TEXT_LENGTH, ' ', TRUE);
user.uid = strtoull(deQuotMarks(tempText), NULL, 10);
}
else if (strcmp(key, "giftname") == 0)
{
getNextWord(&labelPtr, gift.name, GIFT_NAME_LEN, ' ', TRUE);
deQuotMarks(gift.name);
}
else if (strcmp(key, "giftcount") == 0 || strcmp(key, "count") == 0)
{
getNextWord(&labelPtr, tempText, MAX_TEXT_LENGTH, ' ', TRUE);
gift.count = atoi(deQuotMarks(tempText));
}
else if (strcmp(key, "price") == 0)
{
getNextWord(&labelPtr, tempText, MAX_TEXT_LENGTH, ' ', TRUE);
// 如果是录播姬的 SC 金额,需要乘以 1000
if(isBililiveRecorder && messageType==MSG_SUPER_CHAT) {
gift.price = atoi(deQuotMarks(tempText)) * 1000;
} else {
gift.price = atoi(deQuotMarks(tempText));
}
}
else if (strcmp(key, "time") == 0)
{
// sc node
getNextWord(&labelPtr, tempText, MAX_TEXT_LENGTH, ' ', TRUE);
gift.duration = GET_MS_FLT(atof(deQuotMarks(tempText)));
}
else if (strcmp(key, "level") == 0)
{
getNextWord(&labelPtr, tempText, MAX_TEXT_LENGTH, ' ', TRUE);
switch (atoi(deQuotMarks(tempText)))
{
case 1:
// 总督
gift.duration = gift.price = 19998000;
break;
case 2:
// 提督
gift.duration = gift.price = 1998000;
break;
case 3:
// 舰长
gift.duration = gift.price = 198000;
break;
default:
// 未知
gift.duration = gift.price = 18000;
break;
}
}
// BililiveRecorder,开启记录raw
else if (strcmp(key, "raw") == 0)
{
if(hasGiftInfo == TRUE)
{
strGetLeftPart(NULL, &labelPtr, '\"', LABEL_LEN);
strGetLeftPart(raw, &labelPtr, '\"', LABEL_LEN);
/* 解析raw部分 */
char rawKey[KEY_LEN];
char rawValue[VALUE_LEN];
char *rawPtr = raw;
char coinTypeValue[VALUE_LEN] = {0};
xmlUnescape(raw);
strGetLeftPart(NULL, &rawPtr, '{', LABEL_LEN);
while (*rawPtr != '\0')
{
// TODO: fix json parse.
strGetLeftPart(rawKey, &rawPtr, ':', KEY_LEN);
strGetLeftPart(rawValue, &rawPtr, ',', VALUE_LEN);
deQuotMarks(rawKey);
deQuotMarks(rawValue);
if (strcmp(rawKey, "gift_name") == 0)
{
strSafeCopy(gift.name, rawValue, GIFT_NAME_LEN);
}
else if (strcmp(rawKey, "coin_type") == 0)
{
strSafeCopy(coinTypeValue, rawValue, GIFT_NAME_LEN);
}
else if (strcmp(rawKey, "uid") == 0)
{
if (user.uid == 0) {
user.uid = strtoull(rawValue, NULL, 10);
}
}
else if (strcmp(rawKey, "price") == 0)
{
if (gift.price == -1)
{
gift.price = atoi(rawValue);
}
}
else if (strcmp(rawKey, "combo_stay_time") == 0)
{
if (gift.duration == 0) {
gift.duration = GET_MS_FLT(atof(rawValue));
}
}
}
if (strcmp(coinTypeValue, "silver") == 0)
{
gift.price = 0;
}
}
}
// blrec的礼物,不包含sc和guard
else if (strcmp(key, "cointype") == 0) {
char coinTypeValue[VALUE_LEN];
getNextWord(&labelPtr, coinTypeValue, GIFT_NAME_LEN, ' ', TRUE);
deQuotMarks(coinTypeValue);
// 银瓜子 —— 免费礼物
if (strcmp(coinTypeValue, "\xe9\x93\xb6\xe7\x93\x9c\xe5\xad\x90") == 0) {
gift.price = 0;
}
}
else
{
getNextWord(&labelPtr, NULL, MAX_TEXT_LENGTH, ' ', TRUE);
}
}
if (messageType == MSG_GIFT && gift.duration == 0) {
gift.duration = 5 * 1000;
}
if (messageType == UNKNOW_TYPE_DANMAKU || messageType == MSG_SUPER_CHAT)
{
/* 读取普通弹幕 或 SC 的文本部分 */
cnt = 0;
while ((ch = fgetc(ipF)) != '<' && cnt < MAX_TEXT_LENGTH - 1)
{
tempText[cnt] = ch;
if (feof(ipF))
{
break;
}
if (ferror(ipF))
{
errorExit(ipF, *head, tailNode);
return 4; /* 读文件发生错误 */
}
cnt++;
}
tempText[cnt] = '\0';
/* 申请文本部分空间 */
if ((text = (char *)malloc((strlen(tempText)+1) * sizeof(char))) == NULL)
{
errorExit(ipF, *head, danmakuNode);
return 6; /* 申请内存空间失败 */
}
strcpy(text, tempText);
}
/* 申请一个节点的空间 */
{
if ((danmakuNode = (DANMAKU *)malloc(sizeof(DANMAKU))) == NULL)
{
errorExit(ipF, *head, tailNode);
return 5; /* 申请内存空间失败 */
}
/* 类型转换 */
if (messageType == UNKNOW_TYPE_DANMAKU)
{/* 将xml定义的类型编号转换为程序统一定义的类型编号 */
switch (type)
{
case 1:
{
type = R2L;
break;
}
case 6:
{
type = L2R;
break;
}
case 5:
{
type = TOP;
break;
}
case 4:
{
type = BOTTOM;
break;
}
case 7:
{
type = SPECIAL;
break;
}
default:
{
type = type;
break;
}
}
}
else
{
type = messageType;
}
/* 计算时轴偏移量 */
time += timeShift;
if (time < EPS) {
time = 0.0f; /* 如果时间加偏移量是负数则置 0 */
}
/* 数据部分赋值 */
xmlUnescape(text);/* 文本内容xml反转义 */
danmakuNode -> text = text;
danmakuNode -> type = type;
danmakuNode -> time = GET_MS_FLT(time);
danmakuNode -> fontSize = fontSize;
danmakuNode -> color = color;
danmakuNode -> special = NULL;
danmakuNode -> gift = NULL;
danmakuNode -> user = NULL;
danmakuNode -> special = NULL;
danmakuNode -> next = NULL;
}
/* 申请用户信息部分空间 */
if (hasUserInfo == TRUE)
{
if ((userNode = (USERPART *)malloc(sizeof(USERPART))) == NULL)
{
errorExit(ipF, *head, tailNode);
return 5; /* 申请内存空间失败 */
}
strSafeCopy(userNode->name, user.name, USER_NAME_LEN);
userNode->uid = user.uid;
danmakuNode->user = userNode;
}
/* 申请礼物信息部分空间 */
if (hasGiftInfo == TRUE)
{
if ((giftNode = (GIFTPART *)malloc(sizeof(GIFTPART))) == NULL)
{
errorExit(ipF, *head, tailNode);
return 5; /* 申请内存空间失败 */
}
strSafeCopy(giftNode->name, gift.name, GIFT_NAME_LEN);
giftNode->count = gift.count;
giftNode->price = gift.price;
giftNode->duration = gift.duration;
danmakuNode->gift = giftNode;
}
/* 特殊弹幕解析 */
if (type == SPECIAL)
{
char textPart[MAX_TEXT_LENGTH];
/* 申请特殊弹幕部分的空间 */
if ((specialNode = (SPPART *)malloc(sizeof(SPPART))) == NULL)
{
errorExit(ipF, *head, danmakuNode);
return 7; /* 申请内存空间失败 */
}
textPtr = text;
/* [0,0.17,"1-1",7,"文本部分内容",0,0,0,0.17,500,0,true,"微软雅黑",1] */
strGetLeftPart(NULL, &textPtr, '[', MAX_TEXT_LENGTH);
specialNode->startX = atof(deQuotMarks(strGetLeftPart(tempText, &textPtr, ',', MAX_TEXT_LENGTH)));
specialNode->startY = atof(deQuotMarks(strGetLeftPart(tempText, &textPtr, ',', MAX_TEXT_LENGTH)));
specialNode->fadeStart = (int)((1-atof(deQuotMarks(strGetLeftPart(tempText,&textPtr,'-',MAX_TEXT_LENGTH)))) * 255);
specialNode->fadeEnd = (int)((1-atof(deQuotMarks(strGetLeftPart(tempText,&textPtr,',',MAX_TEXT_LENGTH)))) * 255);
specialNode->existTime = GET_MS_FLT(atof(deQuotMarks(strGetLeftPart(tempText, &textPtr, ',', MAX_TEXT_LENGTH))));
/* 文本部分 */
strGetLeftPart(NULL, &textPtr, '\"', MAX_TEXT_LENGTH);
strGetLeftPart(tempText, &textPtr, '\"', MAX_TEXT_LENGTH);
strrpl(tempText, textPart, "/n", "\n", MAX_TEXT_LENGTH); // 远古弹幕转义换行符
strGetLeftPart(NULL, &textPtr, ',', MAX_TEXT_LENGTH);
strcpy(text, textPart);
specialNode->frZ = atoi(deQuotMarks(strGetLeftPart(tempText, &textPtr, ',', MAX_TEXT_LENGTH)));
specialNode->frY = atoi(deQuotMarks(strGetLeftPart(tempText, &textPtr, ',', MAX_TEXT_LENGTH)));
specialNode->endX = atof(deQuotMarks(strGetLeftPart(tempText, &textPtr, ',', MAX_TEXT_LENGTH)));
specialNode->endY = atof(deQuotMarks(strGetLeftPart(tempText, &textPtr, ',', MAX_TEXT_LENGTH)));
specialNode->moveTime = atoi(deQuotMarks(strGetLeftPart(tempText, &textPtr, ',', MAX_TEXT_LENGTH)));
specialNode->pauseTime = atoi(deQuotMarks(strGetLeftPart(tempText, &textPtr, ',', MAX_TEXT_LENGTH)));
/* 字体部分 */
strGetLeftPart(NULL, &textPtr, '\"', MAX_TEXT_LENGTH);
strGetLeftPart(specialNode->fontName, &textPtr, '\"', MAX_TEXT_LENGTH);
deQuotMarks(specialNode->fontName);
danmakuNode -> special = specialNode;
}
/* 链表连接 */
if (*head == NULL)
{
*head = tailNode = danmakuNode;
}
else
{
tailNode -> next = danmakuNode;
tailNode = danmakuNode;
}
/* 更新状态 */
if (status != NULL)
{
(status->totalNum)++;
(status->completedNum)++;
}
}/* 结束 while */
ENDREAD:
if (*head == NULL)
{
return 8; /* 文件不能按正确格式读入 */
}
fclose(ipF);
/* 刷新status */
if (status != NULL)
{
status -> isDone = TRUE;
}
return 0;
}
/*
* 写xml文件
* 参数:文件名/弹幕池/状态
* 返回值:
* 0 正常退出
* 1 弹幕池为空
* 2 创建文件失败
* 3 写文件发生错误
*/
int writeXml(char const *const fileName, DANMAKU *danmakuHead, STATUS *const status)
{
/* 刷新status */
if (status != NULL)
{
status -> function = (void *)writeXml;
(status -> completedNum) = 0;
status -> isDone = FALSE;
}
if (danmakuHead == NULL)
{
return 1;
}
FILE *opF;
DANMAKU *ptr = danmakuHead;
char tempText[64];
int typeInXml;
if ((opF = fopen(fileName, "w")) == NULL)
{
return 2;
}
fprintf(opF, "<?xml version=\"1.0\"?>"
"\n<i>"
);
fprintf(opF, "\n <chatserver></chatserver>"
"\n <chatid></chatid>"
"\n <mission></mission>"
"\n <maxlimit></maxlimit>"
"\n <state></state>"
"\n <real_name></real_name>"
"\n <source></source>"
);
while (ptr != NULL)
{
/* +----------+-----------+-----------+ */
/* | 类型名称 | 原类型编号| 新类型编号| */
/* +----------+-----------+-----------+ */
/* | 右左滚动 | 1 | 1 | */
/* +----------+-----------+-----------+ */
/* | 左右滚动 | 6 | 2 | */
/* +----------+-----------+-----------+ */
/* | 上方固定 | 5 | 3 | */
/* +----------+-----------+-----------+ */
/* | 下方固定 | 4 | 4 | */
/* +----------+-----------+-----------+ */
/* | 特殊弹幕 | 7 | 5 | */
/* +----------+-----------+-----------+ */
if (IS_R2L(ptr))
{
typeInXml = 1;
}
else if (IS_L2R(ptr))
{
typeInXml = 6;
}
else if (IS_TOP(ptr))
{
typeInXml = 5;
}
else if (IS_BTM(ptr))
{
typeInXml = 4;
}
else if (IS_SPECIAL(ptr))
{
typeInXml = 7;
}
else
{
ptr = ptr -> next;
continue;
}
fprintf(opF, "\n <d p=\"%s,%d,%d,%d,0,0,NULL,0\">",
intTimeToStr(tempText, ptr->time, 3),
typeInXml, ptr->fontSize, ptr->color
);
if (IS_SPECIAL(ptr) == FALSE)
{
fprintf(opF, "%s", ptr -> text);
}
else
{
fprintf(opF, "[%s,", floatToStr(tempText, ptr->special -> startX, 2));
fprintf(opF, "%s,", floatToStr(tempText, ptr->special -> startY, 2));
fprintf(opF, "\"%s", floatToStr(tempText, 1 - (ptr->special->fadeStart / 255.00), 2));
fprintf(opF, "-%s\",", floatToStr(tempText, 1 - (ptr->special->fadeEnd / 255.00), 2));
fprintf(opF, "%s,", intTimeToStr(tempText, ptr->special->existTime, 1));
fprintf(opF, "\"%s\",", ptr->text);
fprintf(opF, "%d,%d,", ptr->special -> frZ, ptr->special -> frY);
fprintf(opF, "%s,", floatToStr(tempText, ptr->special -> endX, 2));
fprintf(opF, "%s,", floatToStr(tempText, ptr->special -> endY, 2));
fprintf(opF, "%d,%d,", ptr->special -> moveTime, ptr->special -> pauseTime);
fprintf(opF, "true,\"%s\",1]", ptr->special -> fontName);
}
fprintf(opF, "</d>");
ptr = ptr -> next;
if(ferror(opF))
{
fclose(opF);
return 3;
}
/* 刷新status */
if (status != NULL)
{
(status -> completedNum)++;
}
}
fprintf(opF, "\n</i>");
fclose(opF);
/* 刷新status */
if (status != NULL)
{
status -> isDone = TRUE;
}
return 0;
}
/*
* xml转义字符反转义
*
* 对照:
* 原 反转义
* < <
* > >
* & &
* ' '
* " "
* " "
*/
static char *xmlUnescape(char *const str)
{
if (str == NULL)
{
return NULL;
}
char *srcPtr = str; // 源字符串指针
char *dstPtr = str; // 目标字符串指针,也用于就地修改字符串
while (*srcPtr != '\0')
{
if (*srcPtr != '&')
{
// 如果当前字符不是 '&',就原样复制
*dstPtr++ = *srcPtr++;
continue;
}
if (strncmp(srcPtr, "&", 5) == 0)
{
*dstPtr++ = '&';
srcPtr += 5;
}
else if (strncmp(srcPtr, "'", 6) == 0)
{
*dstPtr++ = '\'';
srcPtr += 6;
}
else if (strncmp(srcPtr, ">", 4) == 0)
{
*dstPtr++ = '>';
srcPtr += 4;
}
else if (strncmp(srcPtr, "<", 4) == 0)
{
*dstPtr++ = '<';
srcPtr += 4;
}
else if (strncmp(srcPtr, """, 6) == 0)
{
*dstPtr++ = '\"';
srcPtr += 6;
}
else if (strncmp(srcPtr, """, 5) == 0)
{
*dstPtr++ = '\"';
srcPtr += 5;
}
else
{
// 如果不是已知的转义序列,就原样复制
*dstPtr++ = *srcPtr++;
}
}
// 字符串结束
*dstPtr = '\0';
return str;
}
/*
* 出错后程序退出前的处理
* 参数:
* 文件指针/链表头指针/最后一个未结尾节点指针/
*/
static void errorExit(FILE *ipF, DANMAKU *head, DANMAKU *ptr)
{
fclose(ipF);
if(head != NULL)
{
ptr -> next = NULL;
ptr -> special = NULL;
freeList(head);
}
return;
}