2 path.cc - manipulation of paths and filenames.
13 #include "file-path.hh"
20 /* We don't have multiple roots, set this to '\0'? */
33 /* Join components to full path. */
39 s = root + to_str (ROOTSEP);
41 s += dir + to_str (DIRSEP);
44 s += to_str (EXTSEP) + ext;
49 @param path the original full filename
50 @return 4 components of the path. They can be empty
53 split_path (String path)
56 int i = path.index_i (ROOTSEP);
59 p.root = path.left_str (i);
60 path = path.right_str (path.length_i () - i - 1);
63 i = path.index_last_i (DIRSEP);
66 p.dir = path.left_str (i);
67 path = path.right_str (path.length_i () - i - 1);
70 i = path.index_last_i ('.');
73 p.base = path.left_str (i);
74 p.ext = path.right_str (path.length_i () - i - 1);
82 File_path::parse_path (String p)
86 while ((l = p.length_i ()) )
88 int i = p.index_i (PATHSEP);
92 p = p.right_str (l- i - 1);
100 It will search in the current dir, in the construction-arg, and
101 in any other added path, in this order.
104 The full path if found, or empty string if not found
107 File_path::find (String nm) const
109 if (!nm.length_i () || (nm == "-") )
111 for (int i=0; i < size (); i++)
113 String path = elem (i);
114 String sep = to_str (DIRSEP);
115 String right (path.right_str (1));
116 if (path.length_i () && right != sep)
117 path += to_str (DIRSEP);
124 Check if directory. TODO: encapsulate for autoconf
127 if (stat (path.ch_C (), &sbuf) == ENOENT)
130 if (! (sbuf.st_mode & __S_IFREG))
133 #if !STAT_MACROS_BROKEN
135 if (stat (path.ch_C (), &sbuf) == ENOENT)
138 if (S_ISDIR (sbuf.st_mode))
142 FILE *f = fopen (path.ch_C (), "r"); // ugh!
153 Add a directory, return false if failed
156 File_path::try_add (String s)
160 FILE * f = fopen (s.ch_C (), "r");
170 File_path::add (String s)
176 File_path::str () const
179 for (int i=0; i< size (); i++)