]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
libvaladoc/filehelper: update realpath
authorFlorian Brosch <flo.brosch@gmail.com>
Wed, 8 Sep 2010 16:57:50 +0000 (18:57 +0200)
committerFlorian Brosch <flo.brosch@gmail.com>
Wed, 8 Sep 2010 16:57:50 +0000 (18:57 +0200)
src/libvaladoc/filehelper.vala

index 6bbafb24e95a686f359ce8d1ec1e29b091ed30a5..47fba3799874cb654a0e104186d58a7d86eaaaa0 100755 (executable)
@@ -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;