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