filename.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Implementation of Filename for Windows.
  3. */
  4. #include <wchar.h>
  5. #include "putty.h"
  6. Filename *filename_from_str(const char *str)
  7. {
  8. Filename *fn = snew(Filename);
  9. fn->cpath = dupstr(str);
  10. fn->wpath = dup_mb_to_wc(DEFAULT_CODEPAGE, fn->cpath);
  11. fn->utf8path = encode_wide_string_as_utf8(fn->wpath);
  12. return fn;
  13. }
  14. Filename *filename_from_wstr(const wchar_t *str)
  15. {
  16. Filename *fn = snew(Filename);
  17. fn->wpath = dupwcs(str);
  18. fn->cpath = dup_wc_to_mb(DEFAULT_CODEPAGE, fn->wpath, "?");
  19. fn->utf8path = encode_wide_string_as_utf8(fn->wpath);
  20. return fn;
  21. }
  22. Filename *filename_from_utf8(const char *ustr)
  23. {
  24. Filename *fn = snew(Filename);
  25. fn->utf8path = dupstr(ustr);
  26. fn->wpath = decode_utf8_to_wide_string(fn->utf8path);
  27. fn->cpath = dup_wc_to_mb(DEFAULT_CODEPAGE, fn->wpath, "?");
  28. return fn;
  29. }
  30. Filename *filename_copy(const Filename *fn)
  31. {
  32. Filename *newfn = snew(Filename);
  33. newfn->cpath = dupstr(fn->cpath);
  34. newfn->wpath = dupwcs(fn->wpath);
  35. newfn->utf8path = dupstr(fn->utf8path);
  36. return newfn;
  37. }
  38. const char *filename_to_str(const Filename *fn)
  39. {
  40. return fn->cpath; /* FIXME */
  41. }
  42. bool filename_equal(const Filename *f1, const Filename *f2)
  43. {
  44. /* wpath is primary: two filenames refer to the same file if they
  45. * have the same wpath */
  46. return !wcscmp(f1->wpath, f2->wpath);
  47. }
  48. bool filename_is_null(const Filename *fn)
  49. {
  50. return !*fn->wpath;
  51. }
  52. void filename_free(Filename *fn)
  53. {
  54. sfree(fn->wpath);
  55. sfree(fn->cpath);
  56. sfree(fn->utf8path);
  57. sfree(fn);
  58. }
  59. void filename_serialise(BinarySink *bs, const Filename *f)
  60. {
  61. put_asciz(bs, f->utf8path);
  62. }
  63. Filename *filename_deserialise(BinarySource *src)
  64. {
  65. const char *utf8 = get_asciz(src);
  66. return filename_from_utf8(utf8);
  67. }
  68. char filename_char_sanitise(char c)
  69. {
  70. if (strchr("<>:\"/\\|?*", c))
  71. return '.';
  72. return c;
  73. }
  74. FILE *f_open(const Filename *fn, const char *mode, bool isprivate)
  75. {
  76. wchar_t *wmode = dup_mb_to_wc(DEFAULT_CODEPAGE, mode);
  77. return _wfopen(fn->wpath, wmode);
  78. sfree(wmode);
  79. }