filename.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. const wchar_t *filename_to_wstr(const Filename *fn)
  43. {
  44. return fn->wpath;
  45. }
  46. bool filename_equal(const Filename *f1, const Filename *f2)
  47. {
  48. /* wpath is primary: two filenames refer to the same file if they
  49. * have the same wpath */
  50. return !wcscmp(f1->wpath, f2->wpath);
  51. }
  52. bool filename_is_null(const Filename *fn)
  53. {
  54. return !*fn->wpath;
  55. }
  56. void filename_free(Filename *fn)
  57. {
  58. sfree(fn->wpath);
  59. sfree(fn->cpath);
  60. sfree(fn->utf8path);
  61. sfree(fn);
  62. }
  63. void filename_serialise(BinarySink *bs, const Filename *f)
  64. {
  65. put_asciz(bs, f->utf8path);
  66. }
  67. Filename *filename_deserialise(BinarySource *src)
  68. {
  69. const char *utf8 = get_asciz(src);
  70. return filename_from_utf8(utf8);
  71. }
  72. char filename_char_sanitise(char c)
  73. {
  74. if (strchr("<>:\"/\\|?*", c))
  75. return '.';
  76. return c;
  77. }
  78. FILE *f_open(const Filename *fn, const char *mode, bool isprivate)
  79. {
  80. #ifdef LEGACY_WINDOWS
  81. /* Fallback for legacy pre-NT windows, where as far as I can see
  82. * _wfopen just doesn't work at all */
  83. init_winver();
  84. if (osPlatformId == VER_PLATFORM_WIN32_WINDOWS ||
  85. osPlatformId == VER_PLATFORM_WIN32s)
  86. return fopen(fn->cpath, mode);
  87. #endif
  88. wchar_t *wmode = dup_mb_to_wc(DEFAULT_CODEPAGE, mode);
  89. FILE *fp = _wfopen(fn->wpath, wmode);
  90. sfree(wmode);
  91. return fp;
  92. }