guess.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <string.h>
  2. #include <memory.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <windows.h>
  6. wchar_t *guessTitles(const wchar_t *filename,
  7. int *tracknum,
  8. wchar_t **artist,
  9. wchar_t **album,
  10. wchar_t **title) // should free the result of this function after using artist/album/title
  11. {
  12. *tracknum=0;
  13. *artist=0;
  14. *album=0;
  15. *title=0;
  16. if (!filename[0]) return 0;
  17. wchar_t *_artist=NULL;
  18. wchar_t *_album=NULL;
  19. const wchar_t *f=filename;
  20. while (f && *f) f++;
  21. while (f && (f > filename) && *f != L'/' && *f != L'\\') f--;
  22. if (f == filename) return 0;
  23. int dirlen = f-filename;
  24. wchar_t *fullfn = (wchar_t*)_wcsdup(filename);
  25. fullfn[dirlen]=0;
  26. wchar_t *n=fullfn+dirlen;
  27. while (n >= fullfn && *n != L'/' && *n != L'\\') n--;
  28. n++;
  29. // try to guess artist and album from the dirname
  30. if (!wcsstr(n,L"-")) // assume dir name is album name, artist name unknown
  31. {
  32. *album=n;
  33. _album=n;
  34. }
  35. else
  36. {
  37. *artist=_artist=n;
  38. _album=wcsstr(n,L"-")+1;
  39. wchar_t *t=_album-2;
  40. while (t >= n && *t == L' ') t--;
  41. t[1]=0;
  42. while (_album && *_album == L' ') _album++;
  43. *album=_album;
  44. }
  45. // get filename shizit
  46. wcsncpy(fullfn+dirlen+1,filename+dirlen+1,wcslen(filename) - (dirlen + 1));
  47. n=fullfn+dirlen+1;
  48. while (n && *n) n++;
  49. while (n > fullfn+dirlen+1 && *n != L'.') n--;
  50. if (*n == L'.') *n=0;
  51. n=fullfn+dirlen+1;
  52. while (n && *n == L' ') n++;
  53. // detect XX. filename
  54. if (wcsstr(n,L".") && _wtoi(n) && _wtoi(n) < 130)
  55. {
  56. wchar_t *tmp=n;
  57. while (tmp && *tmp >= L'0' && *tmp <= L'9') tmp++;
  58. while (tmp && *tmp == L' ') tmp++;
  59. if (tmp && *tmp == L'.')
  60. {
  61. *tracknum=_wtoi(n);
  62. n=tmp+1;
  63. while (n && *n == L'.') n++;
  64. while (n && *n == L' ') n++;
  65. }
  66. }
  67. // detect XX- filename
  68. if (!*tracknum && wcsstr(n,L"-") && _wtoi(n) && _wtoi(n) < 130)
  69. {
  70. wchar_t *tmp=n;
  71. while (tmp && *tmp >= L'0' && *tmp <= L'9') tmp++;
  72. while (tmp && *tmp == L' ') tmp++;
  73. if (tmp && *tmp == L'-')
  74. {
  75. *tracknum=_wtoi(n);
  76. n=tmp+1;
  77. while (n && *n == L'-') n++;
  78. while (n && *n == L' ') n++;
  79. }
  80. }
  81. while (wcsstr(n,L"-"))
  82. {
  83. wchar_t *a=n;
  84. n=wcsstr(n,L"-");
  85. {
  86. wchar_t *t=n-1;
  87. while (t >= a && *t == L' ') t--;
  88. t[1]=0;
  89. }
  90. *n=0;
  91. n++;
  92. while (n && *n == L'-') n++;
  93. while (n && *n == L' ') n++;
  94. // a is the next token.
  95. if (!*tracknum && !_wcsnicmp(a,L"Track ",6) && _wtoi(a+6)) *tracknum=_wtoi(a+6);
  96. else if (*artist== _artist)
  97. {
  98. *artist=a;
  99. }
  100. if (*artist != _artist && *tracknum) break;
  101. }
  102. *title=n;
  103. return fullfn;
  104. }