2 path.cc - manipulation of paths and filenames.
12 #include "file-path.hh"
13 #include "flower-debug.hh"
36 s = root + to_str (ROOTSEP);
38 s += dir + to_str (DIRSEP);
41 s += to_str (EXTSEP) + ext;
46 @param path the original full filename
47 @return 4 components of the path. They can be empty
50 split_path (String path)
53 int i = path.index_i (ROOTSEP);
56 p.root = path.left_str (i);
57 path = path.right_str (path.length_i () - i - 1);
60 i = path.index_last_i (DIRSEP);
63 p.dir = path.left_str (i);
64 path = path.right_str (path.length_i () - i - 1);
67 i = path.index_last_i ('.');
70 p.base = path.left_str (i);
71 p.ext = path.right_str (path.length_i () - i - 1);
79 File_path::parse_path (String p)
83 while ( (l = p.length_i ()) )
85 int i = p.index_i(PATHSEP);
89 p = p.right_str (l- i - 1);
97 It will search in the current dir, in the construction-arg, and
98 in any other added path, in this order.
101 The full path if found, or empty string if not found
104 File_path::find (String nm) const
106 DEBUG_OUT << "looking for" << nm << ": ";
107 if (!nm.length_i() || (nm == "-") )
109 for (int i=0; i < size(); i++)
111 String path = elem(i);
112 String sep = to_str (DIRSEP);
113 String right(path.right_str (1));
114 if (path.length_i () && right != sep)
115 path += to_str (DIRSEP);
119 DEBUG_OUT << path << "? ";
123 Check if directory. TODO: encapsulate for autoconf
126 if (stat (path.ch_C(), &sbuf) == ENOENT)
129 if (!(sbuf.st_mode & __S_IFREG))
132 #if !STAT_MACROS_BROKEN
134 if (stat (path.ch_C (), &sbuf) == ENOENT)
137 if (S_ISDIR (sbuf.st_mode))
141 FILE *f = fopen (path.ch_C(), "r"); // ugh!
144 DEBUG_OUT << "found\n";
154 Add a directory, return false if failed
157 File_path::try_add (String s)
161 FILE * f = fopen (s.ch_C(), "r");
171 File_path::add (String s)
177 File_path::str () const
180 for (int i=0; i< size (); i++)