From: Florian Brosch Date: Wed, 8 Sep 2010 16:57:50 +0000 (+0200) Subject: libvaladoc/filehelper: update realpath X-Git-Tag: 0.37.1~3^2~421 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d2335cb0175837036cff66b1e07fc60bc35dd7fa;p=thirdparty%2Fvala.git libvaladoc/filehelper: update realpath --- diff --git a/src/libvaladoc/filehelper.vala b/src/libvaladoc/filehelper.vala index 6bbafb24e..47fba3799 100755 --- a/src/libvaladoc/filehelper.vala +++ b/src/libvaladoc/filehelper.vala @@ -62,58 +62,71 @@ namespace Valadoc { return true; } - public string realpath (string name) { + /* cp from valacompiler.vala, ported from glibc */ + private static string realpath (string name) { string rpath; - if (name.get_char () != '/') { + // start of path component + weak string start; + // end of path component + weak string end; + + if (!Path.is_absolute (name)) { // relative path rpath = Environment.get_current_dir (); - } - else { - rpath = "/"; + + start = end = name; + } else { + // set start after root + start = end = Path.skip_root (name); + + // extract root + rpath = name.substring (0, name.pointer_to_offset (start)); } - weak string start; - weak string end; + long root_len = rpath.pointer_to_offset (Path.skip_root (rpath)); - for (start = end = name; start.get_char () != 0; start = end) { + for (; start.get_char () != 0; start = end) { // skip sequence of multiple path-separators - while (start.get_char () == '/') { + while (Path.is_dir_separator (start.get_char ())) { start = start.next_char (); } // find end of path component long len = 0; - for (end = start; end.get_char () != 0 && end.get_char () != '/'; end = end.next_char ()) { + for (end = start; end.get_char () != 0 && !Path.is_dir_separator (end.get_char ()); end = end.next_char ()) { len++; } if (len == 0) { break; - } - else if (len == 1 && start.get_char () == '.') { + } else if (len == 1 && start.get_char () == '.') { // do nothing - } - else if (len == 2 && start.has_prefix ("..")) { + } else if (len == 2 && start.has_prefix ("..")) { // back up to previous component, ignore if at root already - if (rpath.len () > 1) { + if (rpath.length > root_len) { do { - rpath = rpath.substring (0, rpath.len () - 1); - } - while (!rpath.has_suffix ("/")); + rpath = rpath.substring (0, rpath.length - 1); + } while (!ends_with_dir_separator (rpath)); } - } - else { - if (!rpath.has_suffix ("/")) { - rpath += "/"; + } else { + if (!ends_with_dir_separator (rpath)) { + rpath += Path.DIR_SEPARATOR_S; } rpath += start.substring (0, len); } } - if (rpath.len () > 1 && rpath.has_suffix ("/")) { - rpath = rpath.substring (0, rpath.len () - 1); + if (rpath.length > root_len && ends_with_dir_separator (rpath)) { + rpath = rpath.substring (0, rpath.length - 1); + } + + if (Path.DIR_SEPARATOR != '/') { + // don't use backslashes internally, + // to avoid problems in #include directives + string[] components = rpath.split ("\\"); + rpath = string.joinv ("/", components); } return rpath;