2 path.cc - manipulation of paths and filenames.
13 #include "file-path.hh"
14 #include "flower-debug.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 DEBUG_OUT
<< "looking for" << nm
<< ": ";
110 if (!nm
.length_i () || (nm
== "-") )
112 for (int i
=0; i
< size (); i
++)
114 String path
= elem (i
);
115 String sep
= to_str (DIRSEP
);
116 String
right (path
.right_str (1));
117 if (path
.length_i () && right
!= sep
)
118 path
+= to_str (DIRSEP
);
122 DEBUG_OUT
<< path
<< "? ";
126 Check if directory. TODO: encapsulate for autoconf
129 if (stat (path
.ch_C (), &sbuf
) == ENOENT
)
132 if (! (sbuf
.st_mode
& __S_IFREG
))
135 #if !STAT_MACROS_BROKEN
137 if (stat (path
.ch_C (), &sbuf
) == ENOENT
)
140 if (S_ISDIR (sbuf
.st_mode
))
144 FILE *f
= fopen (path
.ch_C (), "r"); // ugh!
147 DEBUG_OUT
<< "found\n";
157 Add a directory, return false if failed
160 File_path::try_add (String s
)
164 FILE * f
= fopen (s
.ch_C (), "r");
174 File_path::add (String s
)
180 File_path::str () const
183 for (int i
=0; i
< size (); i
++)