PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Backend/Modules/dotnetpath.cs

https://bitbucket.org/AdamMil/boaold
C# | 195 lines | 139 code | 35 blank | 21 comment | 33 complexity | 47c8a5096e05c1f67b2b9779774915ec MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. Boa is the reference implementation for a language similar to Python,
  3. also called Boa. This implementation is both interpreted and compiled,
  4. targeting the Microsoft .NET Framework.
  5. http://www.adammil.net/
  6. Copyright (C) 2004-2005 Adam Milazzo
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU General Public License
  9. as published by the Free Software Foundation; either version 2
  10. of the License, or (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. using System;
  20. using System.Collections;
  21. using System.IO;
  22. using System.Text.RegularExpressions;
  23. using Boa.AST;
  24. using Boa.Runtime;
  25. namespace Boa.Modules
  26. {
  27. [BoaType("module")]
  28. public sealed class dotnetpath
  29. { dotnetpath() { }
  30. public static string __repr__() { return "<module 'dotnetpath' (built-in)>"; }
  31. public static string __str__() { return __repr__(); }
  32. public static string abspath(string path) { return normpath(join(dotnet.getcwd(), path)); }
  33. public static string basename(string path)
  34. { string basePart, namePart;
  35. split(path, out basePart, out namePart);
  36. return namePart;
  37. }
  38. public static string commonprefix(object seq)
  39. { ArrayList list = new ArrayList();
  40. IEnumerator e = Ops.GetEnumerator(seq);
  41. while(e.MoveNext()) list.Add(Ops.ToString(e.Current));
  42. if(list.Count==0) return string.Empty;
  43. string[] strings = (string[])list.ToArray(typeof(string));
  44. string ret = string.Empty;
  45. for(int ci=0; ci<strings.Length; ci++)
  46. { if(strings[0].Length==ci) goto done;
  47. char c = strings[0][ci];
  48. for(int i=1; i<strings.Length; i++)
  49. { string s = strings[i];
  50. if(s.Length==ci || s[ci]!=c) goto done;
  51. }
  52. ret += c;
  53. }
  54. done: return ret;
  55. }
  56. public static string dirname(string path)
  57. { string basePart, namePart;
  58. split(path, out basePart, out namePart);
  59. return basePart;
  60. }
  61. public static bool exists(string path) { return File.Exists(path) || Directory.Exists(path); }
  62. public static string expanduser(string path)
  63. { string home = dotnet.environ["HOME"] as string;
  64. if(home==null) return path;
  65. if(path=="~") return home;
  66. if(path.StartsWith("~"+Path.DirectorySeparatorChar) || path.StartsWith("~"+Path.AltDirectorySeparatorChar))
  67. return home + path.Substring(1);
  68. // TODO: implement user directory lookups
  69. return path;
  70. }
  71. public static string expandvars(string path) { return varre.Replace(path, new MatchEvaluator(VarReplace)); }
  72. public static long getatime(string path)
  73. { if(File.Exists(path)) return (long)_time.fromDateTime(File.GetLastAccessTime(path));
  74. else if(Directory.Exists(path)) return (long)_time.fromDateTime(Directory.GetLastAccessTime(path));
  75. else throw new FileNotFoundException("path not found: "+path);
  76. }
  77. public static long getctime(string path)
  78. { if(File.Exists(path)) return (long)_time.fromDateTime(File.GetCreationTime(path));
  79. else if(Directory.Exists(path)) return (long)_time.fromDateTime(Directory.GetCreationTime(path));
  80. else throw new FileNotFoundException("path not found: "+path);
  81. }
  82. public static long getmtime(string path)
  83. { if(File.Exists(path)) return (long)_time.fromDateTime(File.GetLastWriteTime(path));
  84. else if(Directory.Exists(path)) return (long)_time.fromDateTime(Directory.GetLastWriteTime(path));
  85. else throw new FileNotFoundException("path not found: "+path);
  86. }
  87. public static long getsize(string path) { return new FileInfo(path).Length; }
  88. public static bool isabs(string path) { return Path.IsPathRooted(path); }
  89. public static bool isdir(string path) { return Directory.Exists(path); }
  90. public static bool isfile(string path) { return File.Exists(path); }
  91. public static bool islink(string path) { return false; }
  92. public static bool ismount(string path) { return false; }
  93. public static bool lexists(string path) { return exists(path); }
  94. public static string join(string path, params string[] paths)
  95. { for(int i=0; i<paths.Length; i++) path = Path.Combine(path, paths[i]);
  96. return path;
  97. }
  98. public static string normcase(string path)
  99. { return path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar).ToLower();
  100. }
  101. public static string normpath(string path)
  102. { return dotre.Replace(path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar), "");
  103. }
  104. public static string realpath(string path) { return path; }
  105. public static bool samefile(string path1, string path2)
  106. { if(path1==null || path2==null) throw Ops.OSError("samefile(): path cannot be null");
  107. try { path1 = Path.GetFullPath(normpath(path1)).ToLower(); }
  108. catch(ArgumentException) { throw dotnet.Invalid(path1); }
  109. try { path2 = Path.GetFullPath(normpath(path2)).ToLower(); }
  110. catch(ArgumentException) { throw dotnet.Invalid(path2); }
  111. return path1==path2;
  112. }
  113. public static Tuple split(string path)
  114. { string basePart, namePart;
  115. split(path, out basePart, out namePart);
  116. return new Tuple(basePart, namePart);
  117. }
  118. public static Tuple splitdrive(string path)
  119. { if(path.Length<2 || !char.IsLetter(path[0]) || path[1]!=':') return new Tuple(string.Empty, path);
  120. return new Tuple(path.Substring(0, 2), path.Substring(2));
  121. }
  122. public static Tuple splitext(string path)
  123. { int index = path.LastIndexOf('.');
  124. if(index==-1 ||
  125. index<path.LastIndexOfAny(new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }))
  126. return new Tuple(path, string.Empty);
  127. return index==0 ? new Tuple(string.Empty, path) : new Tuple(path.Substring(0, index), path.Substring(index));
  128. }
  129. public void walk(string path, object visit, object arg) { throw new NotImplementedException(); }
  130. static void split(string path, out string basePart, out string namePart)
  131. { path = normpath(path);
  132. int index = path.LastIndexOf(Path.DirectorySeparatorChar);
  133. if(index==-1)
  134. { basePart=string.Empty;
  135. namePart=path;
  136. }
  137. else if(index==path.Length-1)
  138. { basePart=path;
  139. namePart=string.Empty;
  140. }
  141. else
  142. { index++;
  143. basePart=path.Substring(0, index);
  144. namePart=path.Substring(index);
  145. }
  146. }
  147. static string VarReplace(Match m)
  148. { string var = dotnet.environ[m.Groups["var"].Value] as string;
  149. return var==null ? string.Empty : var;
  150. }
  151. public static bool supports_unicode_filenames = true;
  152. static readonly Regex dotre =
  153. new Regex(@"(?<=\X)(?:\.\X|\.$|\X)|^\.\X|[^\X]+\X\.\.\X|[^\X]+\X\.\.$".Replace('X', Path.DirectorySeparatorChar),
  154. RegexOptions.Compiled|RegexOptions.Singleline);
  155. static readonly Regex varre = new Regex(@"\$(?:\{(?<var>.*?)\}|(?<var>\w+))",
  156. RegexOptions.Compiled|RegexOptions.Singleline);
  157. }
  158. } // namespace Boa.Modules