2 path.cc - manipulation of paths and filenames.
12 #include "file-path.hh"
13 #include "flower-debug.hh"
24 @param path the original full filename
25 @return 4 components of the path. They can be empty
28 split_path (String path,
29 String &drive, String &dirs, String &filebase, String &extension)
31 // peel off components, one by one.
32 int di = path.index_i (':');
35 drive = path.left_str (di + 1);
36 path = path.right_str (path.length_i () - di -1);
41 di = path.index_last_i (DIRSEP);
44 dirs = path.left_str (di + 1);
45 path = path.right_str (path.length_i ()-di -1);
50 di = path.index_last_i ('.');
53 filebase = path.left_str (di);
54 extension =path.right_str (path.length_i ()-di);
64 File_path::parse_path (String p)
68 while ( (l = p.length_i ()) )
70 int i = p.index_i(PATHSEP);
74 p = p.right_str (l- i - 1);
82 It will search in the current dir, in the construction-arg, and
83 in any other added path, in this order.
86 The full path if found, or empty string if not found
89 File_path::find (String nm) const
91 DEBUG_OUT << "looking for" << nm << ": ";
92 if (!nm.length_i() || (nm == "-") )
94 for (int i=0; i < size(); i++)
96 String path = elem(i);
97 String sep = to_str (DIRSEP);
98 String right(path.right_str (1));
99 if (path.length_i () && right != sep)
100 path += to_str (DIRSEP);
104 DEBUG_OUT << path << "? ";
108 Check if directory. TODO: encapsulate for autoconf
111 if (stat (path.ch_C(), &sbuf) == ENOENT)
114 if (!(sbuf.st_mode & __S_IFREG))
117 #if !STAT_MACROS_BROKEN
119 if (stat (path.ch_C (), &sbuf) == ENOENT)
122 if (S_ISDIR (sbuf.st_mode))
126 FILE *f = fopen (path.ch_C(), "r"); // ugh!
129 DEBUG_OUT << "found\n";
139 Add a directory, return false if failed
142 File_path::try_add (String s)
146 FILE * f = fopen (s.ch_C(), "r");
156 File_path::add (String s)
162 File_path::str () const
165 for (int i=0; i< size (); i++)