filename.cxx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Filename: filename.cxx
  2. // Created by: drose (19Oct00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. #include "filename.h"
  6. #include <ctype.h>
  7. ////////////////////////////////////////////////////////////////////
  8. // Function: PPScope::is_fullpath
  9. // Description: Returns true if the given pathname appears to be a
  10. // fully-specified pathname. This means it begins with
  11. // a slash for unix_platform, and it begins with a slash
  12. // or backslash, with an optional drive leterr, for
  13. // windows_platform.
  14. ////////////////////////////////////////////////////////////////////
  15. bool
  16. is_fullpath(const string &pathname) {
  17. if (pathname.empty()) {
  18. return false;
  19. }
  20. if (pathname[0] == '/') {
  21. return true;
  22. }
  23. if (windows_platform) {
  24. if (pathname.length() > 2 &&
  25. isalpha(pathname[0]) && pathname[1] == ':') {
  26. // A drive-letter prefix.
  27. return (pathname[2] == '/' || pathname[2] == '\\');
  28. }
  29. // No drive-letter prefix.
  30. return (pathname[0] == '\\');
  31. }
  32. return false;
  33. }
  34. ////////////////////////////////////////////////////////////////////
  35. // Function: PPScope::to_os_filename
  36. // Description: Changes forward slashes to backslashes, but only if
  37. // windows_platform is set. Otherwise returns the
  38. // string unchanged.
  39. ////////////////////////////////////////////////////////////////////
  40. string
  41. to_os_filename(string pathname) {
  42. if (windows_platform) {
  43. string::iterator si;
  44. for (si = pathname.begin(); si != pathname.end(); ++si) {
  45. if ((*si) == '/') {
  46. (*si) = '\\';
  47. }
  48. }
  49. }
  50. return pathname;
  51. }
  52. ////////////////////////////////////////////////////////////////////
  53. // Function: PPScope::to_unix_filename
  54. // Description: Changes backslashes to forward slashes, but only if
  55. // windows_platform is set. Otherwise returns the
  56. // string unchanged.
  57. ////////////////////////////////////////////////////////////////////
  58. string
  59. to_unix_filename(string pathname) {
  60. if (windows_platform) {
  61. string::iterator si;
  62. for (si = pathname.begin(); si != pathname.end(); ++si) {
  63. if ((*si) == '\\') {
  64. (*si) = '/';
  65. }
  66. }
  67. }
  68. return pathname;
  69. }