1
0

utils.cpp 8.4 KB

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