-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathm3uargparse.cpp
214 lines (183 loc) · 6.2 KB
/
m3uargparse.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
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
#include <string.h>
#include <string>
#include <vector>
#include <fstream>
#include <istream>
#ifdef _WIN32
#include <windows.h> // for file name charset conversion
#include <wchar.h> // for UTF-16 file name functions
#endif
#include <utils/StrUtils.h>
#include "stdtype.h"
#include "utils.hpp"
#include "m3uargparse.hpp"
#ifdef _WIN32
#if _MSC_VER >= 1400 || __GNUC__ >= 8
#define USE_OPEN_W
#endif
#endif
#ifdef _MSC_VER
#define stricmp _stricmp
#else
#define stricmp strcasecmp
#endif
static const char* M3UV2_HEAD = "#EXTM3U";
static const char* M3UV2_META = "#EXTINF:";
static const UINT8 UTF8_SIG[] = {0xEF, 0xBB, 0xBF};
//UINT8 ParseSongFiles(const std::vector<char*>& args, std::vector<SongFileList>& songList, std::vector<PlaylistFileList>& playlistList);
//UINT8 ParseSongFiles(const std::vector<const char*>& args, std::vector<SongFileList>& songList, std::vector<PlaylistFileList>& playlistList);
//UINT8 ParseSongFiles(const std::vector<std::string>& args, std::vector<SongFileList>& songList, std::vector<PlaylistFileList>& playlistList);
//UINT8 ParseSongFiles(size_t argc, const char* const* argv, std::vector<SongFileList>& songList, std::vector<PlaylistFileList>& playlistList);
static bool ReadM3UPlaylist(const char* fileName, std::vector<SongFileList>& songList, bool isM3Uu8);
UINT8 ParseSongFiles(const std::vector<char*>& args, std::vector<SongFileList>& songList, std::vector<PlaylistFileList>& playlistList)
{
const char* const* argv = args.empty() ? NULL : &args[0];
return ParseSongFiles(args.size(), argv, songList, playlistList);
}
UINT8 ParseSongFiles(const std::vector<const char*>& args, std::vector<SongFileList>& songList, std::vector<PlaylistFileList>& playlistList)
{
const char* const* argv = args.empty() ? NULL : &args[0];
return ParseSongFiles(args.size(), argv, songList, playlistList);
}
UINT8 ParseSongFiles(const std::vector<std::string>& args, std::vector<SongFileList>& songList, std::vector<PlaylistFileList>& playlistList)
{
std::vector<const char*> argVec(args.size());
size_t curArg;
for (curArg = 0; curArg < args.size(); curArg ++)
argVec[curArg] = args[curArg].c_str();
return ParseSongFiles(argVec, songList, playlistList);
}
UINT8 ParseSongFiles(size_t argc, const char* const* argv, std::vector<SongFileList>& songList, std::vector<PlaylistFileList>& playlistList)
{
size_t curArg;
const char* fileName;
const char* fileExt;
UINT8 resVal;
bool retValB;
songList.clear();
playlistList.clear();
resVal = 0x00;
for (curArg = 0; curArg < argc; curArg ++)
{
fileName = argv[curArg];
fileExt = GetFileExtension(fileName);
if (fileExt == NULL)
fileExt = "";
if (! stricmp(fileExt, "m3u") || ! stricmp(fileExt, "m3u8"))
{
size_t plSong = songList.size();
retValB = ReadM3UPlaylist(fileName, songList, ! stricmp(fileExt, "m3u8"));
if (! retValB)
{
resVal |= 0x01;
u8printf("Unable to load playlist: %s\n", fileName);
continue;
}
PlaylistFileList pfl;
pfl.fileName = fileName;
pfl.songCount = songList.size() - plSong;
for (; plSong < songList.size(); plSong ++)
songList[plSong].playlistID = playlistList.size();
playlistList.push_back(pfl);
}
else
{
SongFileList sfl;
sfl.fileName = fileName;
sfl.playlistID = (size_t)-1;
sfl.playlistSongID = (size_t)-1;
songList.push_back(sfl);
}
}
return resVal;
}
static bool ReadM3UPlaylist(const char* fileName, std::vector<SongFileList>& songList, bool isM3Uu8)
{
std::ifstream hFile;
std::string baseDir;
char fileSig[0x03];
bool isUTF8;
bool isV2Fmt;
size_t METASTR_LEN;
UINT32 lineNo;
std::string tempStr;
size_t songID;
CPCONV* cpcU8;
#if defined(_WIN32) && defined(USE_OPEN_W)
// not using libvgm CPConv here, because using WinAPIs doesn't need separate initialization
std::wstring fileNameW;
fileNameW.resize(MultiByteToWideChar(CP_UTF8, 0, fileName, -1, NULL, 0) - 1);
MultiByteToWideChar(CP_UTF8, 0, fileName, -1, &fileNameW[0], fileNameW.size());
hFile.open(fileNameW.c_str()); // use "const wchar_t*" for MinGW implementations without wstring overload
#elif defined(_WIN32)
// fallback for Windows when no ifstream::open(wchar_t*) is available (e.g. MS VC6)
std::wstring fileNameW;
std::string fileNameA;
fileNameW.resize(MultiByteToWideChar(CP_UTF8, 0, fileName, -1, NULL, 0) - 1);
MultiByteToWideChar(CP_UTF8, 0, fileName, -1, &fileNameW[0], fileNameW.size());
fileNameA.resize(WideCharToMultiByte(CP_ACP, 0, fileNameW.c_str(), -1, NULL, 0, NULL, NULL) - 1);
WideCharToMultiByte(CP_ACP, 0, fileNameW.c_str(), -1, &fileNameA[0], fileNameA.size(), NULL, NULL);
hFile.open(fileNameA.c_str());
#else
hFile.open(fileName);
#endif
if (! hFile.is_open())
return false;
baseDir = std::string(fileName, GetFileTitle(fileName) - fileName);
memset(fileSig, 0x00, 3);
hFile.read(fileSig, 3);
isUTF8 = ! memcmp(fileSig, UTF8_SIG, 3); // check for UTF-8 BOM
if (! isUTF8)
hFile.seekg(0, std::ios_base::beg);
isUTF8 |= isM3Uu8;
cpcU8 = NULL;
if (! isUTF8)
CPConv_Init(&cpcU8, "CP1252", "UTF-8");
isV2Fmt = false;
METASTR_LEN = strlen(M3UV2_META);
lineNo = 0;
songID = 0;
while(hFile.good() && ! hFile.eof())
{
std::getline(hFile, tempStr);
lineNo ++;
while(! tempStr.empty() && iscntrl((unsigned char)tempStr[tempStr.size() - 1]))
tempStr.resize(tempStr.size() - 1); // remove NewLine-Characters
if (tempStr.empty())
continue;
if (lineNo == 1 && tempStr == M3UV2_HEAD)
{
isV2Fmt = true;
continue;
}
if (isV2Fmt && ! tempStr.compare(0, METASTR_LEN, M3UV2_META))
{
// Ignore metadata of m3u v2
lineNo ++;
continue;
}
if (cpcU8 != NULL)
{
size_t tempU8Len = 0;
char* tempU8Str = NULL;
UINT8 retVal = CPConv_StrConvert(cpcU8, &tempU8Len, &tempU8Str, tempStr.length(), tempStr.c_str());
if (retVal < 0x80)
tempStr.assign(tempU8Str, tempU8Str + tempU8Len);
free(tempU8Str);
}
// at this point, we should have UTF-8 file names
SongFileList sfl;
sfl.fileName = CombinePaths(baseDir, tempStr);
sfl.playlistID = (size_t)-1;
sfl.playlistSongID = songID;
#ifndef _WIN32 // on Unix systems, make sure to turn '\' into '/'
StandardizeDirSeparators(sfl.fileName);
#endif
songList.push_back(sfl);
songID ++;
}
if (cpcU8 != NULL)
CPConv_Deinit(cpcU8);
hFile.close();
return true;
}