encode-dstr.hpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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, const wchar_t *name_str, const wchar_t *path_str)
  14. {
  15. DStr name;
  16. DStr path;
  17. dstr_from_wcs(name, name_str);
  18. dstr_from_wcs(path, path_str);
  19. encode_dstr(name);
  20. encode_dstr(path);
  21. dstr_copy_dstr(encodedStr, name);
  22. dstr_cat(encodedStr, ":");
  23. dstr_cat_dstr(encodedStr, path);
  24. }
  25. static inline bool DecodeDeviceDStr(DStr &name, DStr &path, const char *device_id)
  26. {
  27. const char *path_str;
  28. if (!device_id || !*device_id)
  29. return false;
  30. path_str = strchr(device_id, ':');
  31. if (!path_str)
  32. return false;
  33. dstr_copy(path, path_str + 1);
  34. dstr_copy(name, device_id);
  35. size_t len = path_str - device_id;
  36. name->array[len] = 0;
  37. name->len = len;
  38. decode_dstr(name);
  39. decode_dstr(path);
  40. return true;
  41. }
  42. static inline bool DecodeDeviceId(DShow::DeviceId &out, const char *device_id)
  43. {
  44. DStr name, path;
  45. if (!DecodeDeviceDStr(name, path, device_id))
  46. return false;
  47. BPtr<wchar_t> wname = dstr_to_wcs(name);
  48. out.name = wname;
  49. if (!dstr_is_empty(path)) {
  50. BPtr<wchar_t> wpath = dstr_to_wcs(path);
  51. out.path = wpath;
  52. }
  53. return true;
  54. }