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
|
#include <windows.h>
#include "../../General/gen_ml/ml.h"
#include "pmp.h"
#include <strsafe.h>
#include "api__ml_pmp.h"
#include "resource1.h"
extern winampMediaLibraryPlugin plugin;
wchar_t * FixReplacementVars(wchar_t *str, int str_size, Device * dev, songid_t song);
BOOL RecursiveCreateDirectory(wchar_t* buf1);
void doFormatFileName(wchar_t out[MAX_PATH], wchar_t *fmt, int trackno, wchar_t *artist, wchar_t *album, wchar_t *title, wchar_t *genre, wchar_t *year, wchar_t *trackartist);
static void removebadchars(wchar_t *s) {
while (s && *s)
{
if (*s == L'?' || *s == L'/' || *s == L'\\' || *s == L':' || *s == L'*' || *s == L'\"' || *s == L'<' || *s == L'>' || *s == L'|')
*s = L'_';
s = CharNextW(s);
}
}
// Skip_Root: removes drive/host/share name in a path
wchar_t * Skip_Root(wchar_t *path) {
wchar_t *p = CharNext(path);
wchar_t *p2 = CharNext(p);
if (*path && *p == L':' && *p2 == L'\\') return CharNext(p2);
else if (*path == L'\\' && *p == L'\\') {
// skip host and share name
int x = 2;
while (x--) {
while (p2 && *p2 != L'\\') {
if (!p2 || !*p2) return NULL;
p2 = CharNext(p2);
}
p2 = CharNext(p2);
}
return p2;
}
return NULL;
}
// RecursiveCreateDirectory: creates all non-existent folders in a path
BOOL RecursiveCreateDirectory(wchar_t* buf1) {
wchar_t *p=buf1;
int errors = 0;
if (*p) {
p = Skip_Root(buf1);
if (!p) return true ;
wchar_t ch='c';
while (ch) {
while (p && *p != '\\' && *p) p=CharNext(p);
ch=*p;
if (p) *p=0;
int pp = wcslen(buf1)-1;
while(buf1[pp] == '.' ||
buf1[pp] == ' ' ||
(buf1[pp] == '\\' && (buf1[pp-1] == '.' || buf1[pp-1] == ' ' || buf1[pp-1] == '/'))
|| buf1[pp] == '/' && buf1)
{
if(buf1[pp] == '\\')
{
buf1[pp-1] = '_';
pp -= 2;
}else{
buf1[pp] = '_';
pp--;
}
}
HANDLE h;
WIN32_FIND_DATA fd;
// Avoid a "There is no disk in the drive" error box on empty removable drives
UINT prevErrorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
h = FindFirstFile(buf1,&fd);
SetErrorMode(prevErrorMode);
if (h == INVALID_HANDLE_VALUE)
{
if (!CreateDirectory(buf1,NULL)) errors++;
} else {
FindClose(h);
if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) errors++;
}
*p++ = ch;
}
}
return errors != 0;
}
// FixReplacementVars: replaces <Artist>, <Title>, <Album>, and #, ##, ##, with appropriate data
// DOES NOT add a file extention!!
wchar_t * FixReplacementVars(wchar_t *str, int str_size, Device * dev, songid_t song)
{
wchar_t artist[256]=L"",album[256]=L"",albumartist[256]=L"",title[256]=L"",genre[256]=L"",year[10]=L"",dest[MAX_PATH]=L"";
dev->getTrackArtist(song,artist,256);
dev->getTrackAlbum(song,album,256);
dev->getTrackAlbumArtist(song,albumartist,256);
if(!albumartist[0]) lstrcpyn(albumartist,artist,256);
dev->getTrackTitle(song,title,256);
dev->getTrackGenre(song,genre,256);
int y = dev->getTrackYear(song);
if(y>0) StringCchPrintfW(year,10,L"%d",y);
wchar_t unknown[32] = {0}, unknownartist[32] = {0}, unknownalbum[32] = {0};
WASABI_API_LNGSTRINGW_BUF(IDS_UNKNOWN,unknown,32);
WASABI_API_LNGSTRINGW_BUF(IDS_UNKNOWN_ARTIST,unknownartist,32);
WASABI_API_LNGSTRINGW_BUF(IDS_UNKKNOWN_ALBUM,unknownalbum,32);
doFormatFileName(dest,str,
dev->getTrackTrackNum(song),
*albumartist ? albumartist : unknownartist,
*album ? album : unknownalbum,
*title ? title : L"0",
*genre ? genre : unknown,
*year ? year : unknown,
*artist ? artist : unknown
);
bool c = (str[1]==':');
lstrcpyn(str,dest,str_size);
if(c) str[1]=L':';
return str;
}
static void CleanDirectory(wchar_t *str)
{
if (!str)
return ;
int l = wcslen(str);
while (l--)
{
if (str[l] == L' '
|| str[l] == L'.')
str[l] = 0;
else
break;
}
}
void doFormatFileName(wchar_t out[MAX_PATH], wchar_t *fmt, int trackno, wchar_t *artist, wchar_t *album, wchar_t *title, wchar_t *genre, wchar_t *year, wchar_t *trackartist)
{
CleanDirectory(artist);
CleanDirectory(album);
CleanDirectory(title);
CleanDirectory(genre);
CleanDirectory(year);
while (fmt && *fmt)
{
int whichstr = 0;
if (*fmt == L'#' && trackno != 0xdeadbeef)
{
int cnt = 0;
while (fmt && *fmt == L'#')
{
fmt++;
cnt++;
}
if (cnt > 8) cnt = 8;
wchar_t specstr[32] = {0};
StringCchPrintf(specstr, 32, L"%%%02dd", cnt);
wchar_t tracknostr[32] = {0};
StringCchPrintf(tracknostr, 32, specstr, trackno);
StringCchCat(out, MAX_PATH, tracknostr);
}
else if (artist && !_wcsnicmp(fmt, L"<artist>", 8)) whichstr = 1;
else if (album && !_wcsnicmp(fmt, L"<album>", 7)) whichstr = 2;
else if (title && !_wcsnicmp(fmt, L"<title>", 7)) whichstr = 3;
else if (genre && !_wcsnicmp(fmt, L"<genre>", 7)) whichstr = 4;
else if (year && !_wcsnicmp(fmt, L"<year>", 6)) whichstr = 5;
else if (year && !_wcsnicmp(fmt, L"<trackartist>", 13)) whichstr=6;
else
{
wchar_t p[2] = {0};
p[0] = *fmt++;
if (p[0] == L'?' || p[0] == L'*' || p[0] == L'|') p[0] = L'_';
else if (p[0] == L':') p[0] = L'-';
else if (p[0] == L'\"') p[0] = L'\'';
else if (p[0] == L'<') p[0] = L'(';
else if (p[0] == L'>') p[0] = L')';
p[1] = 0;
StringCchCat(out, MAX_PATH, p);
}
if (whichstr > 0)
{
int islow = IsCharLowerW(fmt[1]) && IsCharLowerW(fmt[2]);
int ishi = IsCharUpperW(fmt[1]) && IsCharUpperW(fmt[2]);
wchar_t *src;
if (whichstr == 1) { src = artist; fmt += 8; }
else if (whichstr == 2) { src = album; fmt += 7; }
else if (whichstr == 3) { src = title; fmt += 7; }
else if (whichstr == 4) { src = genre; fmt += 7; }
else if (whichstr == 5) { src = year; fmt += 6; }
else if (whichstr == 6) { src= trackartist; fmt+=13; }
else break;
while (src && *src)
{
wchar_t p[2] = {src[0], 0};
if (ishi) CharUpperBuffW(p, 1);
else if (islow) CharLowerBuffW(p, 1);
if (p[0] == L'?' || p[0] == L'*' || p[0] == L'|') p[0] = L'_';
else if (p[0] == L'/' || p[0] == L'\\' || p[0] == L':') p[0] = L'-';
else if (p[0] == L'\"') p[0] = L'\'';
else if (p[0] == L'<') p[0] = L'(';
else if (p[0] == L'>') p[0] = L')';
src++;
p[1] = 0;
StringCchCat(out, MAX_PATH, p);
}
}
}
}
|