1
0

utils.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. #include <windows.h>
  2. #include "../../General/gen_ml/ml.h"
  3. #include "../../Library/ml_pmp/pmp.h"
  4. #include "USBDevice.h"
  5. #include <strsafe.h>
  6. #include <shlwapi.h>
  7. BOOL RecursiveCreateDirectory(wchar_t* buf1);
  8. __int64 fileSize(wchar_t * filename);
  9. bool supportedFormat(wchar_t * file, wchar_t * supportedFormats);
  10. DeviceType detectDeviceType(wchar_t drive);
  11. void removebadchars(wchar_t *s);
  12. wchar_t * FixReplacementVars(wchar_t *str, int str_size, Device * dev, songid_t song);
  13. void removebadchars(wchar_t *s) {
  14. while (s && *s)
  15. {
  16. if (*s == L'?' || *s == L'/' || *s == L'\\' || *s == L':' || *s == L'*' || *s == L'\"' || *s == L'<' || *s == L'>' || *s == L'|')
  17. *s = L'_';
  18. s = CharNextW(s);
  19. }
  20. }
  21. DeviceType detectDeviceType(wchar_t drive) {
  22. // Check for device being a PSP
  23. {
  24. wchar_t memstickind[] = {drive, L":\\MEMSTICK.IND"};
  25. FILE * f = _wfopen(memstickind,L"rb");
  26. if(f) {
  27. fclose(f);
  28. WIN32_FIND_DATA FindFileData = {0};
  29. wchar_t pspdir[] = {drive,L":\\PSP"};
  30. HANDLE hFind = FindFirstFile(pspdir, &FindFileData);
  31. if(hFind != INVALID_HANDLE_VALUE) {
  32. FindClose(hFind);
  33. if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  34. return TYPE_PSP;
  35. }
  36. }
  37. }
  38. // Cannot recognise device type
  39. return TYPE_OTHER;
  40. }
  41. __int64 fileSize(wchar_t * filename)
  42. {
  43. WIN32_FIND_DATA f={0};
  44. HANDLE h = FindFirstFileW(filename,&f);
  45. if(h == INVALID_HANDLE_VALUE) return -1;
  46. FindClose(h);
  47. ULARGE_INTEGER i;
  48. i.HighPart = f.nFileSizeHigh;
  49. i.LowPart = f.nFileSizeLow;
  50. return i.QuadPart;
  51. }
  52. // RecursiveCreateDirectory: creates all non-existent folders in a path
  53. BOOL RecursiveCreateDirectory(wchar_t* buf1) {
  54. wchar_t *p=buf1;
  55. int errors = 0;
  56. if (*p) {
  57. p = PathSkipRoot(buf1);
  58. if (!p) return true ;
  59. wchar_t ch='c';
  60. while (ch) {
  61. while (p && *p != '\\' && *p) p = CharNext(p);
  62. ch = (p ? *p : 0);
  63. if (p) *p = 0;
  64. int pp = (int)wcslen(buf1)-1;
  65. while(buf1[pp] == '.' || buf1[pp] == ' ' ||
  66. (buf1[pp] == '\\' && (buf1[pp-1] == '.' || buf1[pp-1] == ' ' || buf1[pp-1] == '/')) ||
  67. buf1[pp] == '/' && buf1)
  68. {
  69. if(buf1[pp] == '\\')
  70. {
  71. buf1[pp-1] = '_';
  72. pp -= 2;
  73. } else {
  74. buf1[pp] = '_';
  75. pp--;
  76. }
  77. }
  78. WIN32_FIND_DATA fd = {0};
  79. // Avoid a "There is no disk in the drive" error box on empty removable drives
  80. UINT prevErrorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
  81. HANDLE h = FindFirstFile(buf1,&fd);
  82. SetErrorMode(prevErrorMode);
  83. if (h == INVALID_HANDLE_VALUE)
  84. {
  85. if (!CreateDirectory(buf1,NULL)) errors++;
  86. } else {
  87. FindClose(h);
  88. if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) errors++;
  89. }
  90. if (p) *p++ = ch;
  91. }
  92. }
  93. return errors != 0;
  94. }
  95. bool supportedFormat(wchar_t * file, wchar_t * supportedFormats) {
  96. wchar_t * ext = wcsrchr(file,'.');
  97. if(!ext) return false;
  98. ext++;
  99. wchar_t * p = supportedFormats;
  100. while(p && *p) {
  101. bool ret=false;
  102. wchar_t * np = wcschr(p,L';');
  103. if(np) *np = 0;
  104. if(!_wcsicmp(ext,p)) ret=true;
  105. if(np) { *np = L';'; p=np+1; }
  106. else return ret;
  107. if(ret) return true;
  108. }
  109. return false;
  110. }
  111. // FixReplacementVars: replaces <Artist>, <Title>, <Album>, and #, ##, ##, with appropriate data
  112. // DOES NOT add a file extention!!
  113. wchar_t * fixReplacementVars(wchar_t *str, int str_size, Device * dev, songid_t song)
  114. {
  115. #define ADD_STR(x) { x(song,outp,str_size-1-(outp-str)); removebadchars(outp); while (outp && *outp) outp++; }
  116. #define ADD_STR_U(x) { x(song,outp,str_size-1-(outp-str)); removebadchars(outp); while (outp && *outp) { *outp=towupper(*outp); outp++; } }
  117. #define ADD_STR_L(x) { x(song,outp,str_size-1-(outp-str)); removebadchars(outp); while (outp && *outp) { *outp=towlower(*outp); outp++; } }
  118. wchar_t tmpsrc[4096] = {0};
  119. lstrcpyn(tmpsrc,str,sizeof(tmpsrc)/sizeof(wchar_t)); //lstrcpyn is nice enough to make sure it's null terminated.
  120. wchar_t *inp = tmpsrc;
  121. wchar_t *outp = str;
  122. int slash = 0;
  123. while (inp && *inp && outp-str < str_size-2)
  124. {
  125. if (*inp == L'<')
  126. {
  127. if (!wcsncmp(inp,L"<TITLE>",7))
  128. {
  129. ADD_STR_U(dev->getTrackTitle);
  130. inp+=7;
  131. }
  132. else if (!wcsncmp(inp,L"<title>",7))
  133. {
  134. ADD_STR_L(dev->getTrackTitle);
  135. inp+=7;
  136. }
  137. else if (!_wcsnicmp(inp,L"<Title>",7))
  138. {
  139. ADD_STR(dev->getTrackTitle);
  140. inp+=7;
  141. }
  142. else if (!wcsncmp(inp,L"<ALBUM>",7))
  143. {
  144. ADD_STR_U(dev->getTrackAlbum);
  145. inp+=7;
  146. }
  147. else if (!wcsncmp(inp,L"<album>",7))
  148. {
  149. ADD_STR_L(dev->getTrackAlbum);
  150. inp+=7;
  151. }
  152. else if (!_wcsnicmp(inp,L"<Album>",7))
  153. {
  154. ADD_STR(dev->getTrackAlbum);
  155. inp+=7;
  156. }
  157. else if (!wcsncmp(inp,L"<GENRE>",7))
  158. {
  159. ADD_STR_U(dev->getTrackGenre);
  160. inp+=7;
  161. }
  162. else if (!wcsncmp(inp,L"<genre>",7))
  163. {
  164. ADD_STR_L(dev->getTrackGenre);
  165. inp+=7;
  166. }
  167. else if (!_wcsnicmp(inp,L"<Genre>",7))
  168. {
  169. ADD_STR(dev->getTrackGenre);
  170. inp+=7;
  171. }
  172. else if (!wcsncmp(inp,L"<ARTIST>",8))
  173. {
  174. ADD_STR_U(dev->getTrackArtist);
  175. inp+=8;
  176. }
  177. else if (!wcsncmp(inp,L"<artist>",8))
  178. {
  179. ADD_STR_L(dev->getTrackArtist);
  180. inp+=8;
  181. }
  182. else if (!_wcsnicmp(inp,L"<Artist>",8))
  183. {
  184. ADD_STR(dev->getTrackArtist);
  185. inp+=8;
  186. }
  187. else if (!wcsncmp(inp,L"<ALBUMARTIST>",13))
  188. {
  189. wchar_t temp[128] = {0};
  190. dev->getTrackAlbumArtist(song, temp, 128);
  191. if (temp[0] == 0)
  192. dev->getTrackArtist(song, temp, 128);
  193. lstrcpyn(outp,temp,str_size-1-(int)(outp-str));
  194. removebadchars(outp);
  195. while (outp && *outp) { *outp=towupper(*outp); outp++; }
  196. inp+=13;
  197. }
  198. else if (!wcsncmp(inp,L"<albumartist>",13))
  199. {
  200. wchar_t temp[128] = {0};
  201. dev->getTrackAlbumArtist(song, temp, 128);
  202. if (temp[0] == 0)
  203. dev->getTrackArtist(song, temp, 128);
  204. lstrcpyn(outp,temp,str_size-1-(int)(outp-str));
  205. removebadchars(outp);
  206. while (outp && *outp) { *outp=towlower(*outp); outp++; }
  207. inp+=13;
  208. }
  209. else if (!_wcsnicmp(inp,L"<Albumartist>",13))
  210. {
  211. wchar_t temp[128] = {0};
  212. dev->getTrackAlbumArtist(song, temp, 128);
  213. if (temp[0] == 0)
  214. dev->getTrackArtist(song, temp, 128);
  215. lstrcpyn(outp,temp,str_size-1-(int)(outp-str));
  216. removebadchars(outp);
  217. while (outp && *outp) outp++;
  218. inp+=13;
  219. }
  220. else if (!_wcsnicmp(inp,L"<year>",6))
  221. {
  222. wchar_t year[64] = {0};
  223. int y = dev->getTrackYear(song);
  224. if (y) StringCchPrintf(year, ARRAYSIZE(year), L"%d", y);
  225. lstrcpyn(outp,year,str_size-1-(int)(outp-str)); while (outp && *outp) outp++;
  226. inp+=6;
  227. }
  228. else if (!_wcsnicmp(inp,L"<disc>",6))
  229. {
  230. wchar_t disc[16] = {0};
  231. int d = dev->getTrackDiscNum(song);
  232. if (d) StringCchPrintf(disc, ARRAYSIZE(disc), L"%d", d);
  233. lstrcpyn(outp,disc,str_size-1-(int)(outp-str)); while (outp && *outp) outp++;
  234. inp+=6;
  235. }
  236. else if (!_wcsnicmp(inp,L"<filename>",10))
  237. {
  238. wchar_t tfn[MAX_PATH], *ext, *fn;
  239. StringCchCopy(tfn,MAX_PATH,((UsbSong*)song)->filename);
  240. ext = wcsrchr(tfn, L'.');
  241. *ext = 0; //kill extension since its added later
  242. fn = wcsrchr(tfn, L'\\');
  243. fn++;
  244. lstrcpyn(outp,fn,str_size-1-(int)(outp-str));
  245. while (outp && *outp) outp++;
  246. inp+=10;
  247. }
  248. else
  249. {
  250. // use this to skip over unknown tags
  251. while (inp && *inp && *inp != L'>') inp++;
  252. if (inp) inp++;
  253. }
  254. }
  255. else if (*inp == L'#')
  256. {
  257. int nd=0;
  258. wchar_t tmp[64] = {0};
  259. while (inp && *inp == L'#') nd++,inp++;
  260. int track = dev->getTrackTrackNum(song);
  261. if (!track)
  262. {
  263. while (inp && *inp == L' ') inp++;
  264. while (inp && *inp == L'\\') inp++;
  265. if (inp && (*inp == L'-' || *inp == L'.' || *inp == L'_')) // separator
  266. {
  267. inp++;
  268. while (inp && *inp == L' ') inp++;
  269. }
  270. }
  271. else
  272. {
  273. if (nd > 1)
  274. {
  275. wchar_t tmp2[32] = {0};
  276. if (nd > 5) nd=5;
  277. StringCchPrintf(tmp2, ARRAYSIZE(tmp2), L"%%%02dd",nd);
  278. StringCchPrintf(tmp, ARRAYSIZE(tmp), tmp2,track);
  279. }
  280. else StringCchPrintf(tmp, ARRAYSIZE(tmp), L"%d",track);
  281. }
  282. lstrcpyn(outp,tmp,str_size-1-(int)(outp-str)); while (outp && *outp) outp++;
  283. }
  284. else
  285. {
  286. if (*inp == L'\\') slash += 1;
  287. *outp++=*inp++;
  288. }
  289. }
  290. if (outp) *outp = 0;
  291. // if we end up with something like U:\\\ then this
  292. // will set it to be just <filename> so it'll not
  293. // end up making a load of bad files e.g. U:\.mp3
  294. int out_len = lstrlen(str);
  295. if (out_len)
  296. {
  297. if (out_len - 2 == slash)
  298. {
  299. outp = str + 3;
  300. wchar_t tfn[MAX_PATH], *ext, *fn;
  301. StringCchCopy(tfn,MAX_PATH,((UsbSong*)song)->filename);
  302. ext = wcsrchr(tfn, L'.');
  303. *ext = 0; //kill extension since its added later
  304. fn = wcsrchr(tfn, L'\\');
  305. fn++;
  306. lstrcpyn(outp,fn,str_size-1-(int)(outp-str));
  307. while (outp && *outp) outp++;
  308. if (outp) *outp = 0;
  309. }
  310. }
  311. inp=str;
  312. outp=str;
  313. wchar_t lastc=0;
  314. while (inp && *inp)
  315. {
  316. wchar_t ch=*inp++;
  317. if (ch == L'\t') ch=L' ';
  318. if (ch == L' ' && (lastc == L' ' || lastc == L'\\' || lastc == L'/')) continue; // ignore space after slash, or another space
  319. if ((ch == L'\\' || ch == L'/') && lastc == L' ') outp--; // if we have a space then slash, back up to write the slash where the space was
  320. *outp++=ch;
  321. lastc=ch;
  322. }
  323. if (outp) *outp=0;
  324. #undef ADD_STR
  325. #undef ADD_STR_L
  326. #undef ADD_STR_U
  327. return str;
  328. }