-
Notifications
You must be signed in to change notification settings - Fork 3
/
TagFormatter.cpp
171 lines (152 loc) · 3.62 KB
/
TagFormatter.cpp
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
#include <string>
#include <stdtype.h>
#include "FileInfoStorage.hpp"
#include "utils.hpp"
#include "TagFormatter.hpp"
std::string FormatVGMTag(const char* format, const FileInfoStorage& fis)
{
std::string result;
const char* fmtPtr;
std::vector<std::string> langPostfixes;
const char* fileTitle;
langPostfixes.push_back("");
langPostfixes.push_back("-JPN");
fileTitle = GetFileTitle(fis._fileNameU.c_str());
for (fmtPtr = format; *fmtPtr != '\0'; fmtPtr ++)
{
if (*fmtPtr == '%')
{
fmtPtr ++;
char varType = (char)toupper((unsigned char)*fmtPtr);
std::vector<std::string> tagKeys;
bool AppendFM = false;
switch(varType)
{
case 'T': // Track Title
tagKeys = CombineBaseWithList("TITLE", langPostfixes);
break;
case 'A': // Author
tagKeys = CombineBaseWithList("ARTIST", langPostfixes);
break;
case 'G': // Game Name
tagKeys = CombineBaseWithList("GAME", langPostfixes);
break;
case 'D': // Release Date
tagKeys.push_back("DATE");
break;
case 'S': // System Name
tagKeys = CombineBaseWithList("SYSTEM", langPostfixes);
break;
case 'C': // File Creator
tagKeys.push_back("ENCODED_BY");
break;
default:
result.push_back(*fmtPtr);
continue; // continue for-loop
}
if (tagKeys.size() > 1)
{
char isJap = (char)toupper((unsigned char)fmtPtr[1]);
if (isJap == 'J')
{
fmtPtr ++;
tagKeys[0].swap(tagKeys[1]); // prefer Japanese tags
}
}
const char* tagData = fis.GetTag(tagKeys);
if (tagData == NULL && varType == 'T')
tagData = fileTitle; // fall back to file title when the title tag is missing
if (tagData != NULL)
result += tagData;
if (varType == 'G' && fis._songInfo.usesYM2413)
result += " (FM)";
}
else
{
result.push_back(*fmtPtr);
}
}
return result;
}
std::string TrimWhitespaces(const std::string& text)
{
size_t startPos;
size_t endPos;
for (startPos = 0; startPos < text.length(); startPos++)
{
if (! isspace((unsigned char)text[startPos]))
break;
}
for (endPos = text.length(); endPos > startPos; endPos --)
{
if (! isspace((unsigned char)text[endPos - 1]))
break;
}
return text.substr(startPos, endPos - startPos);
}
std::string EnforceCRLF(const std::string& text)
{
std::string result;
size_t srcPos;
char lastChr = '\0';
result.reserve(text.size());
for (srcPos = 0; srcPos < text.length(); srcPos++)
{
if (text[srcPos] == '\n' && lastChr != '\r')
result.push_back('\r');
lastChr = text[srcPos];
result.push_back(lastChr);
}
return result;
}
// fix Author-Tag (a;b;c -> a, b, c)
std::string FixSeparators(const std::string& text)
{
static const char BADSEPS[] = {';', '/', '\\', '&', '\0'};
//static const wchar_t BADSEPS[] = {L';', L'/', L'\\', L'&', L'\xFF0C', L'\xFF0F', L'\xFF3C', L'\0'};
#define GOODSEP_CHR ','
size_t spcWrt;
bool wroteSep;
size_t srcPos;
std::string result;
result.reserve(text.size());
spcWrt = 0;
wroteSep = false;
for (srcPos = 0; srcPos < text.length(); srcPos++)
{
char srcChr = text[srcPos];
const char* chkChr;
for (chkChr = BADSEPS; *chkChr != '\0'; chkChr++)
{
if (srcChr == *chkChr)
{
srcChr = GOODSEP_CHR;
break;
}
}
if (srcChr == GOODSEP_CHR)
{
// trim spaces left of the seperator
result.resize(result.size() - spcWrt);
spcWrt = 0;
result.push_back(srcChr);
wroteSep = true;
}
else
{
if (srcChr == ' ')
spcWrt ++;
else
spcWrt = 0;
if (wroteSep && ! spcWrt)
{
// insert space after the seperator
result.push_back(' ');
spcWrt ++;
}
result.push_back(srcChr);
wroteSep = false;
}
}
return result;
}