AutoCharFn.h 1016 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #ifndef NULLSOFT_UTILITY_AUTOCHARFN_H
  2. #define NULLSOFT_UTILITY_AUTOCHARFN_H
  3. /* Winamp defines this, but this little block lets us use this thing outside of Winamp */
  4. #ifndef FILENAME_SIZE
  5. #define FILENAME_SIZE (MAX_PATH*4)
  6. #define REMOVE_FILENAME_SIZE
  7. #endif
  8. #include <windows.h>
  9. #include <shlwapi.h>
  10. class AutoCharFn
  11. {
  12. public:
  13. AutoCharFn(const wchar_t *filename)
  14. {
  15. out[0]=0;
  16. if (!filename)
  17. return;
  18. if (PathIsURLW(filename))
  19. {
  20. WideCharToMultiByte(CP_ACP, 0, filename, -1, out, FILENAME_SIZE, NULL, NULL);
  21. return ;
  22. }
  23. BOOL unconvertable = FALSE;
  24. WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, filename, -1, out, FILENAME_SIZE, NULL, &unconvertable);
  25. if (unconvertable)
  26. {
  27. wchar_t temp[MAX_PATH];
  28. if (GetShortPathNameW(filename, temp, MAX_PATH))
  29. WideCharToMultiByte(CP_ACP, 0, temp, -1, out, FILENAME_SIZE, NULL, NULL);
  30. }
  31. }
  32. operator char *() { return out; }
  33. private:
  34. char out[FILENAME_SIZE];
  35. };
  36. #ifdef REMOVE_FILENAME_SIZE
  37. #undef FILENAME_SIZE
  38. #endif
  39. #endif