lilypond-1.1.44
[lilypond.git] / flower / path.cc
blobb9ed1c5320373920fc4e5fa7f23f8dc8e21e3ec5
1 /*
2 path.cc - manipulation of paths and filenames.
3 */
4 #include <stdio.h>
5 #include "path.hh"
6 #include "flower-debug.hh"
8 #ifndef DIRSEP
9 #define DIRSEP '/'
10 #endif
12 #ifndef PATHSEP
13 #define PATHSEP ':'
14 #endif
16 /**
17 @param path the original full filename
18 @return 4 components of the path. They can be empty
20 void
21 split_path (String path,
22 String &drive, String &dirs, String &filebase, String &extension)
24 // peel off components, one by one.
25 int di = path.index_i (':');
26 if (di >= 0)
28 drive = path.left_str (di + 1);
29 path = path.right_str (path.length_i () - di -1);
31 else
32 drive = "";
34 di = path.index_last_i (DIRSEP);
35 if (di >=0)
37 dirs = path.left_str (di + 1);
38 path = path.right_str (path.length_i ()-di -1);
40 else
41 dirs = "";
43 di = path.index_last_i ('.');
44 if (di >= 0)
46 filebase = path.left_str (di);
47 extension =path.right_str (path.length_i ()-di);
49 else
51 extension = "";
52 filebase = path;
56 void
57 File_path::parse_path (String p)
59 int l;
61 while ( (l = p.length_i ()) )
63 int i = p.index_i(PATHSEP);
64 if (i <0)
65 i = l;
66 add (p.left_str(i));
67 p = p.right_str (l- i - 1);
74 /** find a file.
75 It will search in the current dir, in the construction-arg, and
76 in any other added path, in this order.
78 String
79 File_path::find (String nm) const
81 fdebug << _("looking for ") << nm << ": ";
82 if (!nm.length_i() || (nm == "-") )
83 return nm;
84 for (int i=0; i < size(); i++)
86 String path = elem(i);
87 String sep (DIRSEP);
88 String right(path.right_str (1));
89 if (path.length_i () && right != sep)
90 path += DIRSEP;
92 path += nm;
94 fdebug << path << "? ";
95 FILE *f = fopen (path.ch_C(), "r"); // ugh!
96 if (f)
98 fdebug << _("found\n");
99 fclose (f);
100 return path;
103 fdebug << "\n";
104 return "";
107 void
108 File_path::add (String s)
110 push (s);