cedet-files.el 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. ;;; cedet-files.el --- Common routines dealing with file names.
  2. ;; Copyright (C) 2007-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <eric@siege-engine.com>
  4. ;; Package: cedet
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;; Various useful routines for dealing with file names in the tools
  19. ;; which are a part of CEDET.
  20. ;;; Code:
  21. (defun cedet-directory-name-to-file-name (referencedir &optional testmode)
  22. "Convert the REFERENCEDIR (a full path name) into a filename.
  23. Convert directory separation characters into ! characters.
  24. Optional argument TESTMODE is used by tests to avoid conversion
  25. to the file's truename, and dodging platform tricks."
  26. (let ((file referencedir))
  27. ;; Expand to full file name
  28. (when (not testmode)
  29. (setq file (file-truename file)))
  30. ;; If FILE is a directory, then force it to end in /.
  31. (when (file-directory-p file)
  32. (setq file (file-name-as-directory file)))
  33. ;; Handle Windows Special cases
  34. (when (or (memq system-type '(windows-nt ms-dos)) testmode)
  35. ;; Replace any invalid file-name characters (for the
  36. ;; case of backing up remote files).
  37. (when (not testmode)
  38. (setq file (expand-file-name (convert-standard-filename file))))
  39. ;; Normalize DOSish file names.
  40. (if (eq (aref file 1) ?:)
  41. (setq file (concat "/"
  42. "drive_"
  43. (char-to-string (downcase (aref file 0)))
  44. (if (eq (aref file 2) ?/)
  45. ""
  46. "/")
  47. (substring file 2)))))
  48. ;; Make the name unique by substituting directory
  49. ;; separators. It may not really be worth bothering about
  50. ;; doubling `!'s in the original name...
  51. (setq file (subst-char-in-string
  52. ?/ ?!
  53. (replace-regexp-in-string "!" "!!" file)))
  54. file))
  55. (defun cedet-file-name-to-directory-name (referencefile &optional testmode)
  56. "Reverse the process of `cedet-directory-name-to-file-name'.
  57. Convert REFERENCEFILE to a directory name replacing ! with /.
  58. Optional TESTMODE is used in tests to avoid doing some platform
  59. specific conversions during tests."
  60. (let ((file referencefile))
  61. ;; Replace the ! with /
  62. (setq file (subst-char-in-string ?! ?/ file))
  63. ;; Occurrences of // meant there was once a single !.
  64. (setq file (replace-regexp-in-string "//" "!" file))
  65. ;; Handle Windows special cases
  66. (when (or (memq system-type '(windows-nt ms-dos)) testmode)
  67. ;; Handle drive letters from DOSish file names.
  68. (when (string-match "^/drive_\\([a-z]\\)/" file)
  69. (let ((driveletter (match-string 1 file))
  70. )
  71. (setq file (concat driveletter ":"
  72. (substring file (match-end 1))))))
  73. ;; Handle the \\file\name nomenclature on some Windows boxes.
  74. (when (string-match "^!" file)
  75. (setq file (concat "//" (substring file 1)))))
  76. file))
  77. (provide 'cedet-files)
  78. ;;; cedet-files.el ends here