-
Notifications
You must be signed in to change notification settings - Fork 18
/
vgm_stat.c
871 lines (758 loc) · 18.2 KB
/
vgm_stat.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
// vgm_stat.c - VGM Statistics
//
// TODO: Fix UTF-8 rendering (multibyte characters result in incorrect padding)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h> // for isalnum()
#include <wchar.h>
#include <locale.h> // for setlocale()
#include <zlib.h>
#ifdef WIN32
#include <windows.h> // for Directory Listing
#else
#include <glob.h>
#include <sys/stat.h>
#endif
#include "stdtype.h"
#include "stdbool.h"
#include "VGMFile.h"
#include "common.h"
//#define SHOW_FILE_STATS
#ifdef WIN32
#define DIR_SEP '\\'
#else
#define DIR_SEP '/'
#endif
static bool OpenVGMFile(const char* FileName);
static wchar_t* ReadWStrFromFile(gzFile hFile, UINT32* FilePos, UINT32 EOFPos);
static void ReadDirectory(const char* DirName);
static void ReadPlaylist(const char* FileName);
#ifdef SHOW_FILE_STATS
static void ShowFileStats(const char* FileTitle);
#endif
static void PrintSampleTime(char* buffer, const UINT32 Samples, bool LoopMode);
static void ShowStatistics(void);
static void PrintTrackTime(UINT32 TitleLen, const char* TimeTotal, const char* TimeLoop);
UINT32 GetTitleLines(UINT32* StrAlloc, char** String, const char* TitleStr);
typedef struct track_list
{
char* Title;
UINT32 SmplTotal;
UINT32 SmplLoop;
} TRACK_LIST;
VGM_HEADER VGMHead;
GD3_TAG VGMTag;
UINT32 VGMDataLen;
char FilePath[MAX_PATH];
UINT32 AllTotal;
UINT32 AllLoop;
UINT32 TrackCount;
UINT32 TrkCntDigits;
UINT32 TrackAlloc;
TRACK_LIST* TrackList;
TRACK_LIST* CurTrkEntry;
bool IsPlayList;
bool Utf8Output;
int main(int argc, char* argv[])
{
int argbase;
int ErrVal;
char FileName[MAX_PATH];
char* FileExt;
bool PLMode;
UINT32 TempLng;
setlocale(LC_CTYPE, ""); // set to use system codepage
printf("VGM Statistics\n--------------\n\n");
//printf("ZLib Version: %s\n", zlibVersion());
ErrVal = 0;
Utf8Output = false;
argbase = 1;
while(argbase < argc && argv[argbase][0] == '-')
{
if (! stricmp(argv[argbase], "-utf8"))
{
Utf8Output = true;
argbase ++;
}
else
{
break;
}
}
printf("File Path or PlayList:\t");
if (argc <= 0x01)
{
ReadFilename(FileName, sizeof(FileName));
}
else
{
strcpy(FileName, argv[argbase]);
printf("%s\n", FileName);
}
if (! strlen(FileName))
return 0;
AllTotal = 0;
AllLoop = 0;
PLMode = false;
if (FileName[strlen(FileName) - 1] != DIR_SEP)
{
#ifdef WIN32
if (! (GetFileAttributes(FileName) & FILE_ATTRIBUTE_DIRECTORY))
#else
struct stat st;
if(!stat(FileName, &st) && S_ISREG(st.st_mode))
#endif
{
FileExt = strrchr(FileName, '.');
if (FileExt < strrchr(FileName, DIR_SEP))
FileExt = NULL;
if (FileExt != NULL)
{
FileExt ++;
if (! stricmp(FileExt, "m3u"))
PLMode = true;
}
}
else
{
strcat(FileName, "/");
}
}
TrackCount = 0;
TrackAlloc = 0;
TrackList = NULL;
IsPlayList = PLMode;
if (! PLMode)
ReadDirectory(FileName);
else
ReadPlaylist(FileName);
TempLng = TrackCount;
TrkCntDigits = 0;
do
{
TempLng /= 10;
TrkCntDigits ++;
} while(TempLng);
if (TrkCntDigits < 2)
TrkCntDigits = 2;
#ifdef SHOW_FILE_STATS
ShowFileStats(NULL);
printf("\n\n");
#endif
ShowStatistics();
//EndProgram:
DblClickWait(argv[0]);
return ErrVal;
}
static bool OpenVGMFile(const char* FileName)
{
gzFile hFile;
UINT32 fccHeader;
UINT32 CurPos;
UINT32 TempLng;
char* TempPnt;
INT16 LoopCnt;
hFile = gzopen(FileName, "rb");
if (hFile == NULL)
return false;
gzseek(hFile, 0x00, SEEK_SET);
gzread(hFile, &fccHeader, 0x04);
if (fccHeader != FCC_VGM)
goto OpenErr;
if (gzseek(hFile, 0x00, SEEK_SET) == -1)
{
printf("gzseek Error!!\n");
if (gzrewind(hFile) == -1)
{
printf("gzrewind Error!!\n");
goto OpenErr;
}
}
TempLng = gztell(hFile);
if (TempLng != 0)
{
printf("gztell returns invalid offset: 0x%X\n", TempLng);
goto OpenErr;
}
gzread(hFile, &VGMHead, sizeof(VGM_HEADER));
ZLIB_SEEKBUG_CHECK(VGMHead);
// relative -> absolute addresses
VGMHead.lngEOFOffset += 0x00000004;
if (VGMHead.lngGD3Offset)
VGMHead.lngGD3Offset += 0x00000014;
if (! VGMHead.lngDataOffset)
VGMHead.lngDataOffset = 0x0000000C;
VGMHead.lngDataOffset += 0x00000034;
CurPos = VGMHead.lngDataOffset;
if (VGMHead.lngVersion < 0x00000150)
CurPos = 0x40;
TempLng = sizeof(VGM_HEADER);
if (TempLng > CurPos)
TempLng -= CurPos;
else
TempLng = 0x00;
memset((UINT8*)&VGMHead + CurPos, 0x00, TempLng);
if (! VGMHead.bytLoopModifier)
VGMHead.bytLoopModifier = 0x10;
//printf("\tTrack %u, Data Ofs 0x%X, Version %X\n", TrackCount, VGMHead.lngDataOffset, VGMHead.lngVersion);
//printf("\tSamples Total: %u, Samples Loop: %u\n", VGMHead.lngTotalSamples, VGMHead.lngLoopOffset);
//printf("\tGD3 Offset: 0x%X, EOF Offset: 0x%X\n", VGMHead.lngGD3Offset, VGMHead.lngEOFOffset);
// Allocate Memory for Track List
if (TrackAlloc <= TrackCount)
{
TrackAlloc += 0x100;
TrackList = (TRACK_LIST*)realloc(TrackList, sizeof(TRACK_LIST) * TrackAlloc);
}
CurTrkEntry = &TrackList[TrackCount];
TrackCount ++;
// Read GD3 Tag
if (VGMHead.lngGD3Offset)
{
gzseek(hFile, VGMHead.lngGD3Offset, SEEK_SET);
gzread(hFile, &fccHeader, 0x04);
if (fccHeader != FCC_GD3)
VGMHead.lngGD3Offset = 0x00000000;
//goto OpenErr;
}
if (VGMHead.lngGD3Offset)
{
CurPos = VGMHead.lngGD3Offset;
gzseek(hFile, CurPos, SEEK_SET);
gzread(hFile, &VGMTag, 0x0C);
CurPos += 0x0C;
TempLng = CurPos + VGMTag.lngTagLength;
VGMTag.strTrackNameE = ReadWStrFromFile(hFile, &CurPos, TempLng);
}
else
{
VGMTag.strTrackNameE = L"";
}
TempLng = wcslen(VGMTag.strTrackNameE) * 0x04; // 4 bytes = worst case for UTF-8
if (TempLng)
{
#ifdef WIN32
UINT32 CPMode;
if (Utf8Output)
CPMode = CP_UTF8;
else if (ftell(stdout) == -1) // Outputting to console
CPMode = GetConsoleOutputCP();
else // Outputting to file
CPMode = CP_ACP;
CurTrkEntry->Title = (char*)malloc(TempLng + 0x01);
WideCharToMultiByte(CPMode, 0x00, VGMTag.strTrackNameE, -1, CurTrkEntry->Title, TempLng + 0x01, NULL, NULL);
#else
CurTrkEntry->Title = (char*)malloc(TempLng + 0x01);
wcstombs(CurTrkEntry->Title, VGMTag.strTrackNameE, TempLng);
#endif
CurTrkEntry->Title[TempLng] = '\0';
}
else
{
TempPnt = strrchr(FileName, '\\');
if (TempPnt == NULL)
TempPnt = (char*)FileName;
else
TempPnt ++;
TempLng = strlen(TempPnt);
CurTrkEntry->Title = (char*)malloc(TempLng + 0x01);
strcpy(CurTrkEntry->Title, TempPnt);
TempPnt = strrchr(CurTrkEntry->Title, '.');
if (TempPnt != NULL)
*TempPnt = '\0'; // strip ".vgm"
}
CurTrkEntry->SmplTotal = VGMHead.lngTotalSamples;
CurTrkEntry->SmplLoop = VGMHead.lngLoopSamples;
AllTotal += CurTrkEntry->SmplTotal;
LoopCnt = (2 * VGMHead.bytLoopModifier + 0x08) / 0x10 - VGMHead.bytLoopBase;
if (LoopCnt < 1)
LoopCnt = 1;
AllLoop += CurTrkEntry->SmplLoop * (LoopCnt - 1);
gzclose(hFile);
return true;
OpenErr:
gzclose(hFile);
return false;
}
static wchar_t* ReadWStrFromFile(gzFile hFile, UINT32* FilePos, UINT32 EOFPos)
{
UINT32 CurPos;
wchar_t* TextStr;
UINT32 StrLen;
UINT16 UnicodeChr;
#if WCHAR_MAX >= 0x10000
UINT16 ucs2First = 0;
#endif
CurPos = *FilePos;
TextStr = (wchar_t*)malloc((EOFPos - CurPos) / 0x02 * sizeof(wchar_t));
if (TextStr == NULL)
return NULL;
gzseek(hFile, CurPos, SEEK_SET);
StrLen = 0;
do
{
gzread(hFile, &UnicodeChr, 0x02);
#if WCHAR_MAX < 0x10000
// Windows: uses UCS-2 encoding
TextStr[StrLen] = (wchar_t)UnicodeChr;
StrLen ++;
#else
// Linux: do USC-2 -> UTF-32 conversion
if (! ucs2First && (UnicodeChr >= 0xD800 && UnicodeChr <= 0xDFFF))
{
ucs2First = UnicodeChr;
}
else if (ucs2First)
{
if (UnicodeChr >= 0xDC00 && UnicodeChr <= 0xDFFF)
{
UINT16 x = ((UnicodeChr & 0x03FF) << 0) | ((ucs2First & 0x003F) << 10);
UINT16 w = 1 + ((ucs2First & 0x07C0) >> 6);
TextStr[StrLen] = (wchar_t)((w << 16) | (x << 0));
StrLen ++;
}
else
{
// bad UCS2 surrogate bytes - just copy them
TextStr[StrLen + 0] = (wchar_t)ucs2First;
TextStr[StrLen + 1] = (wchar_t)UnicodeChr;
StrLen += 2;
}
ucs2First = 0;
}
else
{
TextStr[StrLen] = (wchar_t)UnicodeChr;
StrLen ++;
}
#endif
CurPos += 0x02;
if (CurPos >= EOFPos)
break;
} while(UnicodeChr != 0);
TextStr = (wchar_t*)realloc(TextStr, StrLen * sizeof(wchar_t));
*FilePos = CurPos;
return TextStr;
}
static void AddDummyFile(const char* FileName)
{
UINT32 TempLng;
char* TempPnt;
if (TrackAlloc <= TrackCount)
{
TrackAlloc += 0x100;
TrackList = (TRACK_LIST*)realloc(TrackList, sizeof(TRACK_LIST) * TrackAlloc);
}
CurTrkEntry = &TrackList[TrackCount];
TrackCount ++;
TempPnt = strrchr(FileName, '\\');
if (TempPnt == NULL)
TempPnt = (char*)FileName;
else
TempPnt ++;
TempLng = strlen(TempPnt);
CurTrkEntry->Title = (char*)malloc(TempLng + 0x01);
strcpy(CurTrkEntry->Title, TempPnt);
TempPnt = strrchr(CurTrkEntry->Title, '.');
if (TempPnt != NULL)
*TempPnt = '\0'; // strip ".vgm"
CurTrkEntry->SmplTotal = (UINT32)-1;
CurTrkEntry->SmplLoop = (UINT32)-1;
return;
}
static void ReadDirectory(const char* DirName)
{
#ifdef WIN32
HANDLE hFindFile;
WIN32_FIND_DATA FindFileData;
BOOL RetVal;
#else
struct stat st;
int i;
#endif
char FileName[MAX_PATH];
char* TempPnt;
char* FileExt;
strcpy(FilePath, DirName);
TempPnt = strrchr(FilePath, DIR_SEP);
if (TempPnt == NULL)
strcpy(FilePath, "");
TempPnt = strrchr(FilePath, DIR_SEP);
if (TempPnt != NULL)
TempPnt[0x01] = '\0';
strcpy(FileName, FilePath);
strcat(FileName, "*.vg?");
//printf(" Base Path: %s\n", FilePath);
#ifdef WIN32
hFindFile = FindFirstFile(FileName, &FindFileData);
if (hFindFile == INVALID_HANDLE_VALUE)
{
//ShowErrMessage();
printf("Error reading directory!\n");
return;
}
#else
glob_t globbuf;
glob(FileName, 0, NULL, &globbuf);
#endif
strcpy(FileName, FilePath);
TempPnt = FileName + strlen(FileName);
#ifdef SHOW_FILE_STATS
printf("\t\t Sample\t Time\n");
printf("File Title\tTotal\tLoop\tTotal\tLoop\n\n");
#endif
#ifdef WIN32
do
{
if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
goto SkipFile;
FileExt = strrchr(FindFileData.cFileName, '.');
if (FileExt == NULL)
goto SkipFile;
FileExt ++;
if (stricmp(FileExt, "vgm") && stricmp(FileExt, "vgz"))
goto SkipFile;
strcpy(TempPnt, FindFileData.cFileName);
if (! OpenVGMFile(FileName))
printf("%s\tError opening the file!\n", TempPnt);
#ifdef SHOW_FILE_STATS
else
ShowFileStats(TempPnt);
#endif
SkipFile:
RetVal = FindNextFile(hFindFile, &FindFileData);
}
while(RetVal);
#else
for(i = 0; i < globbuf.gl_pathc; i++) {
if(!stat(globbuf.gl_pathv[i], &st)) {
FileExt = strrchr(globbuf.gl_pathv[i], '.');
if (FileExt == NULL)
continue;
FileExt ++;
if(stricmp(FileExt, "vgm") && stricmp(FileExt, "vgz"))
continue;
strcpy(TempPnt, globbuf.gl_pathv[i]);
if (! OpenVGMFile(globbuf.gl_pathv[i]))
printf("%s\tError opening the file!\n", TempPnt);
#ifdef SHOW_FILE_STATS
else
ShowFileStats(TempPnt);
#endif
}
}
#endif
#ifdef WIN32
RetVal = FindClose(hFindFile);
#else
globfree(&globbuf);
#endif
return;
}
static void ReadPlaylist(const char* FileName)
{
const char M3UV2_HEAD[] = "#EXTM3U";
const char M3UV2_META[] = "#EXTINF:";
UINT32 METASTR_LEN;
FILE* hFile;
UINT32 LineNo;
bool IsV2Fmt;
char TempStr[MAX_PATH];
char FileVGM[MAX_PATH];
char* RetStr;
RetStr = strrchr(FileName, '\\');
if (RetStr != NULL)
{
RetStr ++;
strncpy(TempStr, FileName, RetStr - FileName);
TempStr[RetStr - FileName] = '\0';
strcpy(FilePath, TempStr);
}
else
{
strcpy(FilePath, "");
}
//printf(" Base Path: %s\n", FilePath);
hFile = fopen(FileName, "rt");
if (hFile == NULL)
return;
#ifdef SHOW_FILE_STATS
printf("\t\t Sample\t Time\n");
printf("File Title\tTotal\tLoop\tTotal\tLoop\n\n");
#endif
LineNo = 0;
IsV2Fmt = false;
METASTR_LEN = strlen(M3UV2_META);
while(! feof(hFile))
{
RetStr = fgets(TempStr, MAX_PATH, hFile);
if (RetStr == NULL)
break;
//RetStr = strchr(TempStr, 0x0D);
//if (RetStr)
// *RetStr = '\0'; // remove NewLine-Character
RetStr = TempStr + strlen(TempStr) - 0x01;
while(*RetStr < 0x20)
{
*RetStr = '\0'; // remove NewLine-Characters
RetStr --;
}
if (! strlen(TempStr))
continue;
//printf(" Read Line: %s\n", TempStr);
if (! LineNo)
{
if (! strcmp(TempStr, M3UV2_HEAD))
{
IsV2Fmt = true;
LineNo ++;
continue;
}
}
if (IsV2Fmt)
{
if (! strncmp(TempStr, M3UV2_META, METASTR_LEN))
{
// Ignore Metadata of m3u Version 2
LineNo ++;
continue;
}
}
strcpy(FileVGM, FilePath);
strcat(FileVGM, TempStr);
RetStr = strrchr(TempStr, '\\');
if (RetStr == NULL)
RetStr = TempStr;
else
RetStr ++;
//printf(" Full File Path: %s\n", FileVGM);
if (! OpenVGMFile(FileVGM))
{
printf("%s\tError opening the file!\n", RetStr);
AddDummyFile(FileVGM);
}
#ifdef SHOW_FILE_STATS
else
{
ShowFileStats(RetStr);
}
#endif
LineNo ++;
}
fclose(hFile);
return;
}
#ifdef SHOW_FILE_STATS
static void ShowFileStats(const char* FileTitle)
{
UINT32 SmplTotal;
UINT32 SmplLoop;
char TimeTotal[0x10];
char TimeLoop[0x10];
if (FileTitle != NULL)
{
SmplTotal = VGMHead.lngTotalSamples;
SmplLoop = VGMHead.lngLoopSamples;
}
else
{
FileTitle = "\nTotal Length";
SmplTotal = AllTotal;
SmplLoop = AllTotal + AllLoop;
}
PrintSampleTime(TimeTotal, SmplTotal, false);
PrintSampleTime(TimeLoop, SmplLoop, true);
printf("%s\t%u\t%u\t%s\t%s\n", FileTitle, SmplTotal, SmplLoop, TimeTotal, TimeLoop);
return;
}
#endif
static void PrintSampleTime(char* buffer, const UINT32 Samples, bool LoopMode)
{
UINT32 SmplVal;
UINT16 HourVal;
UINT16 MinVal;
UINT16 SecVal;
if (Samples == (UINT32)-1)
{
sprintf(buffer, " -:--");
return;
}
if (! Samples && LoopMode)
{
sprintf(buffer, " - "); // '-' at minute position, others = spaces (due to right-alignment)
return;
}
SmplVal = Samples + 22050; // Round to whole seconds
SmplVal /= 44100;
SecVal = (UINT16)(SmplVal % 60);
SmplVal /= 60;
MinVal = (UINT16)(SmplVal % 60);
SmplVal /= 60;
HourVal = (UINT16)SmplVal;
if (HourVal)
sprintf(buffer, "%u:%02u:%02u", HourVal, MinVal, SecVal);
else
sprintf(buffer, "%u:%02u", MinVal, SecVal);
return;
}
#define MAX_TITLE_CHARS 35
static void ShowStatistics(void)
{
UINT32 CurTL;
char* TitleStr;
UINT32 TitleLen;
char TimeTotal[0x10];
char TimeLoop[0x10];
UINT32 TempStrAlloc;
char* TempStr;
UINT32 CurLine;
UINT32 LineCnt;
TempStrAlloc = 0x00;
TempStr = NULL;
printf("Song Length |Total |Loop\n");
printf("-----------------------------------+------+----\n");
for (CurTL = 0; CurTL < TrackCount; CurTL ++)
{
char* TitleWithNumber;
CurTrkEntry = &TrackList[CurTL];
if (IsPlayList)
{
TitleWithNumber = (char*)malloc(TrkCntDigits + 1 + strlen(CurTrkEntry->Title) + 1);
sprintf(TitleWithNumber, "%0*u %s", TrkCntDigits, 1 + CurTL, CurTrkEntry->Title);
}
else
{
TitleWithNumber = strdup(CurTrkEntry->Title);
}
LineCnt = GetTitleLines(&TempStrAlloc, &TempStr, TitleWithNumber);
TitleStr = (LineCnt) ? TempStr : TitleWithNumber;
if (! LineCnt)
LineCnt ++;
for (CurLine = 0; CurLine < LineCnt; CurLine ++)
{
if (CurLine)
printf("\n");
TitleLen = printf("%.*s", MAX_TITLE_CHARS, TitleStr);
TitleStr += TitleLen + 1;
}
PrintSampleTime(TimeTotal, CurTrkEntry->SmplTotal, false);
PrintSampleTime(TimeLoop, CurTrkEntry->SmplLoop, true);
PrintTrackTime(TitleLen, TimeTotal, TimeLoop);
free(TitleWithNumber);
}
printf("\n");
if (TempStr != NULL)
{
free(TempStr); TempStr = NULL;
}
TitleStr = "Total Length";
TitleLen = printf("%.*s", MAX_TITLE_CHARS, TitleStr);
PrintSampleTime(TimeTotal, AllTotal, false);
PrintSampleTime(TimeLoop, AllTotal + AllLoop, false);
PrintTrackTime(TitleLen, TimeTotal, TimeLoop);
return;
}
static void PrintTrackTime(UINT32 TitleLen, const char* TimeTotal, const char* TimeLoop)
{
char TimeStrAll[0x20];
INT32 TSALen;
INT32 Spaces;
TSALen = sprintf(TimeStrAll, "%5s %6s", TimeTotal, TimeLoop);
// strip trailing spaces
while(TSALen > 0 && TimeStrAll[TSALen - 1] == ' ')
TSALen --;
TimeStrAll[TSALen] = '\0';
Spaces = MAX_TITLE_CHARS - TitleLen;
// TimeStrAll is left-aligned for up to 12 characters.
// If larger, right-align it by removing padding between title and time string.
if (TSALen > 12)
Spaces -= (TSALen - 12);
for (; Spaces < 0; Spaces ++)
putchar('\b');
printf("%*s%s\n", Spaces, "", TimeStrAll);
return;
}
UINT32 GetTitleLines(UINT32* StrAlloc, char** String, const char* TitleStr)
{
UINT32 CurLine;
UINT32 CurPos;
UINT32 SpcPos;
UINT32 NoAlphaPos;
const char* SrcPtr;
char* DstPtr;
CurPos = strlen(TitleStr);
if (CurPos <= MAX_TITLE_CHARS)
return 0; // didn't do anything
if (*StrAlloc < CurPos + 0x10)
{
*StrAlloc = (CurPos + 0x20) & ~0x0F;
*String = (char*)realloc(*String, *StrAlloc);
}
SrcPtr = TitleStr;
DstPtr = *String;
NoAlphaPos = SpcPos = CurPos = 0;
CurLine = 0x01;
if (IsPlayList)
{
for (CurPos = 0; CurPos < TrkCntDigits + 1; CurPos ++)
DstPtr[CurPos] = SrcPtr[CurPos];
}
while(SrcPtr[CurPos] != '\0')
{
if (SrcPtr[CurPos] == ' ')
SpcPos = CurPos;
if (CurPos >= MAX_TITLE_CHARS)
{
if (SpcPos) // Space found
{
// replace last space with line break (which is \0 here)
CurPos = SpcPos;
DstPtr[CurPos] = '\0';
SrcPtr += CurPos + 1;
DstPtr += CurPos + 1;
}
else if (NoAlphaPos) // non-alnum char found
{
// break after last non-alpha-numeric character
CurPos = NoAlphaPos + 1;
DstPtr[CurPos] = '\0';
SrcPtr += CurPos;
DstPtr += CurPos + 1;
}
else // word with more chars than the line has
{
// break right after the previous letter and insert a -
DstPtr[CurPos - 1] = '-';
DstPtr[CurPos] = '\0';
SrcPtr += CurPos - 1;
DstPtr += CurPos + 1;
}
CurPos = (DstPtr - *String) + strlen(SrcPtr);
if (*StrAlloc < CurPos + 0x10)
{
*StrAlloc = (CurPos + 0x20) & ~0x0F;
SpcPos = DstPtr - *String;
*String = (char*)realloc(*String, *StrAlloc);
DstPtr = *String + SpcPos;
}
NoAlphaPos = SpcPos = CurPos = 0;
if (IsPlayList)
{
// to make this format:
// 01 Very
// Long
// Title
for (; CurPos < TrkCntDigits + 1; CurPos ++)
DstPtr[CurPos] = ' ';
SrcPtr -= CurPos;
}
CurLine ++;
}
if (! isalnum(SrcPtr[CurPos]))
NoAlphaPos = CurPos;
DstPtr[CurPos] = SrcPtr[CurPos];
CurPos ++;
}
DstPtr[CurPos] = '\0';
return CurLine;
}