encode-dstr.hpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #pragma once
  2. #include <util/util.hpp>
  3. static inline void encode_dstr(struct dstr *str)
  4. {
  5. dstr_replace(str, "#", "#22");
  6. dstr_replace(str, ":", "#3A");
  7. }
  8. static inline void decode_dstr(struct dstr *str)
  9. {
  10. dstr_replace(str, "#3A", ":");
  11. dstr_replace(str, "#22", "#");
  12. }
  13. static inline void EncodeDeviceId(struct dstr *encodedStr,
  14. const wchar_t *name_str,
  15. const wchar_t *path_str)
  16. {
  17. DStr name;
  18. DStr path;
  19. dstr_from_wcs(name, name_str);
  20. dstr_from_wcs(path, path_str);
  21. encode_dstr(name);
  22. encode_dstr(path);
  23. dstr_copy_dstr(encodedStr, name);
  24. dstr_cat(encodedStr, ":");
  25. dstr_cat_dstr(encodedStr, path);
  26. }
  27. static inline bool DecodeDeviceDStr(DStr &name, DStr &path,
  28. const char *device_id)
  29. {
  30. const char *path_str;
  31. if (!device_id || !*device_id)
  32. return false;
  33. path_str = strchr(device_id, ':');
  34. if (!path_str)
  35. return false;
  36. dstr_copy(path, path_str + 1);
  37. dstr_copy(name, device_id);
  38. size_t len = path_str - device_id;
  39. name->array[len] = 0;
  40. name->len = len;
  41. decode_dstr(name);
  42. decode_dstr(path);
  43. return true;
  44. }
  45. static inline bool DecodeDeviceId(DShow::DeviceId &out, const char *device_id)
  46. {
  47. DStr name, path;
  48. if (!DecodeDeviceDStr(name, path, device_id))
  49. return false;
  50. BPtr<wchar_t> wname = dstr_to_wcs(name);
  51. out.name = wname;
  52. if (!dstr_is_empty(path)) {
  53. BPtr<wchar_t> wpath = dstr_to_wcs(path);
  54. out.path = wpath;
  55. }
  56. return true;
  57. }