2 path.cc - manipulation of paths and filenames.
13 #include "file-path.hh"
14 #include "flower-debug.hh"
32 /* Join components to full path. */
38 s = root + to_str (ROOTSEP);
40 s += dir + to_str (DIRSEP);
43 s += to_str (EXTSEP) + ext;
48 @param path the original full filename
49 @return 4 components of the path. They can be empty
52 split_path (String path)
55 int i = path.index_i (ROOTSEP);
58 p.root = path.left_str (i);
59 path = path.right_str (path.length_i () - i - 1);
62 i = path.index_last_i (DIRSEP);
65 p.dir = path.left_str (i);
66 path = path.right_str (path.length_i () - i - 1);
69 i = path.index_last_i ('.');
72 p.base = path.left_str (i);
73 p.ext = path.right_str (path.length_i () - i - 1);
81 File_path::parse_path (String p)
85 while ( (l = p.length_i ()) )
87 int i = p.index_i(PATHSEP);
91 p = p.right_str (l- i - 1);
99 It will search in the current dir, in the construction-arg, and
100 in any other added path, in this order.
103 The full path if found, or empty string if not found
106 File_path::find (String nm) const
108 DEBUG_OUT << "looking for" << nm << ": ";
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);
121 DEBUG_OUT << path << "? ";
125 Check if directory. TODO: encapsulate for autoconf
128 if (stat (path.ch_C(), &sbuf) == ENOENT)
131 if (!(sbuf.st_mode & __S_IFREG))
134 #if !STAT_MACROS_BROKEN
136 if (stat (path.ch_C (), &sbuf) == ENOENT)
139 if (S_ISDIR (sbuf.st_mode))
143 FILE *f = fopen (path.ch_C(), "r"); // ugh!
146 DEBUG_OUT << "found\n";
156 Add a directory, return false if failed
159 File_path::try_add (String s)
163 FILE * f = fopen (s.ch_C(), "r");
173 File_path::add (String s)
179 File_path::str () const
182 for (int i=0; i< size (); i++)