path_utils.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. FTP file system
  3. Copyright (C) 2007 Robson Braga Araujo <robsonbraga@gmail.com>
  4. This program can be distributed under the terms of the GNU GPL.
  5. See the file COPYING.
  6. */
  7. #include "path_utils.h"
  8. #include "charset_utils.h"
  9. #include "ftpfs.h"
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <glib.h>
  13. char* get_file_name(const char* path) {
  14. const char* filename = strrchr(path, '/');
  15. if (filename == NULL) filename = path;
  16. else ++filename;
  17. char* ret = strdup(filename);
  18. if (ftpfs.codepage) {
  19. convert_charsets(ftpfs.iocharset, ftpfs.codepage, &ret);
  20. }
  21. return ret;
  22. }
  23. char* get_full_path(const char* path) {
  24. char* ret;
  25. char* converted_path = NULL;
  26. ++path;
  27. if (ftpfs.codepage && strlen(path)) {
  28. converted_path = strdup(path);
  29. convert_charsets(ftpfs.iocharset, ftpfs.codepage, &converted_path);
  30. path = converted_path;
  31. }
  32. const char *const escaped_path = g_uri_escape_string(path, "/", FALSE);
  33. ret = g_strdup_printf("%s%s", ftpfs.host, escaped_path);
  34. free(converted_path);
  35. free((char *) escaped_path);
  36. return ret;
  37. }
  38. char* get_fulldir_path(const char* path) {
  39. char* ret;
  40. char* converted_path = NULL;
  41. ++path;
  42. if (ftpfs.codepage && strlen(path)) {
  43. converted_path = strdup(path);
  44. convert_charsets(ftpfs.iocharset, ftpfs.codepage, &converted_path);
  45. path = converted_path;
  46. }
  47. const char *const escaped_path = g_uri_escape_string(path, "/", FALSE);
  48. ret = g_strdup_printf(
  49. "%s%s%s", ftpfs.host, escaped_path, strlen(escaped_path) ? "/" : "");
  50. free(converted_path);
  51. free((char *) escaped_path);
  52. return ret;
  53. }
  54. char* get_dir_path(const char* path) {
  55. char* ret;
  56. char* converted_path = NULL;
  57. const char *lastdir;
  58. ++path;
  59. if (ftpfs.codepage) {
  60. converted_path = g_strdup(path);
  61. convert_charsets(ftpfs.iocharset, ftpfs.codepage, &converted_path);
  62. path = converted_path;
  63. }
  64. const char *const escaped_path = g_uri_escape_string(path, "/", FALSE);
  65. lastdir = strrchr(escaped_path, '/');
  66. if (lastdir == NULL) lastdir = escaped_path;
  67. ret = g_strdup_printf("%s%.*s%s",
  68. ftpfs.host,
  69. lastdir - escaped_path,
  70. escaped_path,
  71. lastdir - escaped_path ? "/" : "");
  72. free(converted_path);
  73. free((char *) escaped_path);
  74. return ret;
  75. }