1
0

replaceVars.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #include <windows.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include "..\..\General\gen_ml/ml.h"
  5. #include <shlwapi.h>
  6. #include <strsafe.h>
  7. static void removebadchars(wchar_t *s, wchar_t *&end, size_t &str_size)
  8. {
  9. const wchar_t *start = s;
  10. while (s && *s)
  11. {
  12. if (*s == L'?' || *s == L'/' || *s == L'\\' || *s == L':' || *s == L'*' || *s == L'\"' || *s == L'<' || *s == L'>' || *s == L'|')
  13. *s = L'_';
  14. s = CharNextW(s);
  15. }
  16. // cut off trailing periods
  17. if (s != start)
  18. {
  19. do
  20. {
  21. s--;
  22. if (*s == '.')
  23. {
  24. *s = '_';
  25. end--;
  26. str_size++;
  27. }
  28. else
  29. break;
  30. }
  31. while (s != start);
  32. }
  33. }
  34. void RecursiveCreateDirectory(wchar_t *str)
  35. {
  36. wchar_t *p = str;
  37. if ((p[0] ==L'\\' || p[0] ==L'/') && (p[1] ==L'\\' || p[1] ==L'/'))
  38. {
  39. p += 2;
  40. while (p &&*p && *p !=L'\\' && *p !=L'/') p++;
  41. if (!p || !*p) return ;
  42. p++;
  43. while (p && *p && *p !=L'\\' && *p !=L'/') p++;
  44. }
  45. else
  46. {
  47. while (p && *p && *p !=L'\\' && *p !=L'/') p++;
  48. }
  49. while (p && *p)
  50. {
  51. while (p && *p && *p !=L'\\' && *p !=L'/') p = CharNextW(p);
  52. if (p && *p)
  53. {
  54. wchar_t lp = *p;
  55. *p = 0;
  56. CreateDirectoryW(str, NULL);
  57. *p++ = lp;
  58. }
  59. }
  60. }
  61. void FixFileLength(wchar_t *str)
  62. {
  63. size_t len = wcslen(str);
  64. if (len >= MAX_PATH) // no good
  65. {
  66. size_t extensionPosition=len;
  67. while (extensionPosition--)
  68. {
  69. if (str[extensionPosition]=='.' || str[extensionPosition] == '/' || str[extensionPosition] == '\\')
  70. break;
  71. }
  72. if (str[extensionPosition]=='.') // found an extension? good, let's relocate it
  73. {
  74. ptrdiff_t diff = len - extensionPosition;
  75. diff++;
  76. while (diff)
  77. {
  78. str[MAX_PATH-diff]=str[len-diff+1];
  79. diff--;
  80. }
  81. }
  82. str[MAX_PATH-1]=0;
  83. }
  84. }
  85. static void ADD_STR(const wchar_t *x, wchar_t *&outp, size_t &str_size)
  86. {
  87. wchar_t *end = outp;
  88. StringCchCopyExW(outp, str_size, (x) ? (x) : L"", &end, &str_size, 0);
  89. removebadchars(outp, end, str_size);
  90. outp = end;
  91. }
  92. static void ADD_STR_U(const wchar_t *x, wchar_t *&outp, size_t &str_size)
  93. {
  94. wchar_t *end = outp;
  95. StringCchCopyExW(outp, str_size, (x) ? (x) : L"", &end, &str_size, 0);
  96. removebadchars(outp, end, str_size);
  97. CharUpperW(outp);
  98. outp = end;
  99. }
  100. static void ADD_STR_L(const wchar_t *x, wchar_t *&outp, size_t &str_size)
  101. {
  102. wchar_t *end = outp;
  103. StringCchCopyExW(outp, str_size, (x)?(x):L"", &end, &str_size,0);
  104. removebadchars(outp, end, str_size);
  105. CharLowerW(outp);
  106. outp=end;
  107. }
  108. // FixReplacementVars: replaces <Artist>, <Title>, <Album>, and #, ##, ##, with appropriate data
  109. wchar_t *FixReplacementVars(wchar_t *str, size_t str_size, itemRecordW * song)
  110. {
  111. wchar_t tmpsrc[4096] = {0};
  112. lstrcpyn(tmpsrc,str,sizeof(tmpsrc)/sizeof(wchar_t)); //lstrcpyn is nice enough to make sure it's null terminated.
  113. wchar_t *inp = tmpsrc;
  114. wchar_t *outp = str;
  115. while (inp && *inp && str_size)
  116. {
  117. if (*inp == L'<')
  118. {
  119. if (!wcsncmp(inp,L"<TITLE>",7))
  120. {
  121. ADD_STR_U(song->title, outp, str_size);
  122. inp+=7;
  123. }
  124. else if (!wcsncmp(inp,L"<title>",7))
  125. {
  126. ADD_STR_L(song->title, outp, str_size);
  127. inp+=7;
  128. }
  129. else if (!_wcsnicmp(inp,L"<Title>",7))
  130. {
  131. ADD_STR(song->title, outp, str_size);
  132. inp+=7;
  133. }
  134. else if (!wcsncmp(inp,L"<ALBUM>",7))
  135. {
  136. ADD_STR_U(song->album, outp, str_size);
  137. inp+=7;
  138. }
  139. else if (!wcsncmp(inp,L"<album>",7))
  140. {
  141. ADD_STR_L(song->album, outp, str_size);
  142. inp+=7;
  143. }
  144. else if (!_wcsnicmp(inp,L"<Album>",7))
  145. {
  146. ADD_STR(song->album, outp, str_size);
  147. inp+=7;
  148. }
  149. else if (!wcsncmp(inp,L"<GENRE>",7))
  150. {
  151. ADD_STR_U(song->genre, outp, str_size);
  152. inp+=7;
  153. }
  154. else if (!wcsncmp(inp,L"<genre>",7))
  155. {
  156. ADD_STR_L(song->genre, outp, str_size);
  157. inp+=7;
  158. }
  159. else if (!_wcsnicmp(inp,L"<Genre>",7))
  160. {
  161. ADD_STR(song->genre, outp, str_size);
  162. inp+=7;
  163. }
  164. else if (!wcsncmp(inp,L"<ARTIST>",8))
  165. {
  166. ADD_STR_U(song->artist, outp, str_size);
  167. inp+=8;
  168. }
  169. else if (!wcsncmp(inp,L"<artist>",8))
  170. {
  171. ADD_STR_L(song->artist, outp, str_size);
  172. inp+=8;
  173. }
  174. else if (!_wcsnicmp(inp,L"<Artist>",8))
  175. {
  176. ADD_STR(song->artist, outp, str_size);
  177. inp+=8;
  178. }
  179. else if (!wcsncmp(inp,L"<ALBUMARTIST>",13))
  180. {
  181. if (song->albumartist && song->albumartist[0])
  182. ADD_STR_U(song->albumartist, outp, str_size);
  183. else
  184. ADD_STR_U(song->artist, outp, str_size);
  185. inp+=13;
  186. }
  187. else if (!wcsncmp(inp,L"<albumartist>",13))
  188. {
  189. if (song->albumartist && song->albumartist[0])
  190. ADD_STR_L(song->albumartist, outp, str_size);
  191. else
  192. ADD_STR_L(song->artist, outp, str_size);
  193. inp+=13;
  194. }
  195. else if (!_wcsnicmp(inp,L"<Albumartist>",13))
  196. {
  197. if (song->albumartist && song->albumartist[0])
  198. ADD_STR(song->albumartist, outp, str_size);
  199. else
  200. ADD_STR(song->artist, outp, str_size);
  201. inp+=13;
  202. }
  203. else if (!_wcsnicmp(inp,L"<year>",6))
  204. {
  205. wchar_t year[64] = {0};
  206. if(song->year==0) year[0]=0;
  207. else StringCchPrintf(year, 64, L"%d", song->year);
  208. ADD_STR(year, outp, str_size);
  209. inp+=6;
  210. }
  211. else if (!_wcsnicmp(inp,L"<disc>",6))
  212. {
  213. wchar_t disc[16] = {0};
  214. if(song->disc==0) disc[0]=0;
  215. else StringCchPrintf(disc, 16, L"%d", song->disc);
  216. ADD_STR(disc, outp, str_size);
  217. inp+=6;
  218. }
  219. else if (!_wcsnicmp(inp,L"<discs>",7))
  220. {
  221. wchar_t discs[32] = {0};
  222. if(song->disc==0) discs[0]=0;
  223. else StringCchPrintf(discs, 32, L"%d", song->discs);
  224. ADD_STR(discs, outp, str_size);
  225. inp+=7;
  226. }
  227. else if(!_wcsnicmp(inp,L"<filename>",10))
  228. {
  229. wchar_t *fn = _wcsdup(PathFindFileName(song->filename));
  230. const wchar_t *insert;
  231. if (NULL != fn &&
  232. FALSE != PathIsFileSpec(fn))
  233. {
  234. PathRemoveExtension(fn);
  235. insert = fn;
  236. }
  237. else
  238. insert = NULL;
  239. if (NULL == insert || L'\0' == *insert)
  240. insert = L"<filename>";
  241. ADD_STR(insert, outp, str_size);
  242. free(fn);
  243. inp+=10;
  244. }
  245. else *outp++=*inp++;
  246. }
  247. else if (*inp == '#')
  248. {
  249. int nd = 0;
  250. wchar_t tmp[64] = {0};
  251. while (inp && *inp =='#') nd++,inp++;
  252. if (!song->track)
  253. {
  254. tmp[0] = 0;
  255. while (inp && *inp == ' ') inp++;
  256. if (inp && (*inp == '-' || *inp == '.' || *inp == '_')) // separator
  257. {
  258. inp++;
  259. while (inp && *inp == ' ') inp++;
  260. }
  261. }
  262. else
  263. {
  264. if (nd > 1)
  265. {
  266. wchar_t tmp2[32] = {0};
  267. if (nd > 5) nd=5;
  268. StringCchPrintf(tmp2, 32, L"%%%02dd",nd);
  269. StringCchPrintf(tmp, 64, tmp2,song->track);
  270. }
  271. else StringCchPrintf(tmp, 64, L"%d", song->track);
  272. }
  273. ADD_STR(tmp, outp, str_size);
  274. }
  275. else *outp++ = *inp++;
  276. }
  277. if (outp) *outp = 0;
  278. inp = str;
  279. outp = str;
  280. wchar_t lastc=0;
  281. while (inp && *inp)
  282. {
  283. wchar_t c=*inp++;
  284. if (c == '\t') c=' ';
  285. if (c == ' ' && (lastc == ' ' || lastc == '\\' || lastc == '/')) continue; // ignore space after slash, or another space
  286. if ((c == '\\' || c == '/') && lastc == ' ') outp--; // if we have a space then slash, back up to write the slash where the space was
  287. *outp++ = c;
  288. lastc = c;
  289. }
  290. if (outp) *outp=0;
  291. #undef ADD_STR
  292. #undef ADD_STR_L
  293. #undef ADD_STR_U
  294. return(str);
  295. }