lilypond-1.3.9
[lilypond.git] / flower / file-path.cc
blob5f430ce9a070f6941a2c18e5ab7544f39c1efd03
1 /*
2 path.cc - manipulation of paths and filenames.
3 */
5 #include <stdio.h>
7 #include "config.h"
8 #include "file-path.hh"
9 #include "flower-debug.hh"
11 #ifndef DIRSEP
12 #define DIRSEP '/'
13 #endif
15 #ifndef PATHSEP
16 #define PATHSEP ':'
17 #endif
19 /**
20 @param path the original full filename
21 @return 4 components of the path. They can be empty
23 void
24 split_path (String path,
25 String &drive, String &dirs, String &filebase, String &extension)
27 // peel off components, one by one.
28 int di = path.index_i (':');
29 if (di >= 0)
31 drive = path.left_str (di + 1);
32 path = path.right_str (path.length_i () - di -1);
34 else
35 drive = "";
37 di = path.index_last_i (DIRSEP);
38 if (di >=0)
40 dirs = path.left_str (di + 1);
41 path = path.right_str (path.length_i ()-di -1);
43 else
44 dirs = "";
46 di = path.index_last_i ('.');
47 if (di >= 0)
49 filebase = path.left_str (di);
50 extension =path.right_str (path.length_i ()-di);
52 else
54 extension = "";
55 filebase = path;
59 void
60 File_path::parse_path (String p)
62 int l;
64 while ( (l = p.length_i ()) )
66 int i = p.index_i(PATHSEP);
67 if (i <0)
68 i = l;
69 add (p.left_str(i));
70 p = p.right_str (l- i - 1);
77 /** Find a file.
78 It will search in the current dir, in the construction-arg, and
79 in any other added path, in this order.
81 @return
82 The full path if found, or empty string if not found
84 String
85 File_path::find (String nm) const
87 DEBUG_OUT << "looking for" << nm << ": ";
88 if (!nm.length_i() || (nm == "-") )
89 return nm;
90 for (int i=0; i < size(); i++)
92 String path = elem(i);
93 String sep = to_str (DIRSEP);
94 String right(path.right_str (1));
95 if (path.length_i () && right != sep)
96 path += to_str (DIRSEP);
98 path += nm;
100 DEBUG_OUT << path << "? ";
101 FILE *f = fopen (path.ch_C(), "r"); // ugh!
102 if (f)
104 DEBUG_OUT << "found\n";
105 fclose (f);
106 return path;
109 DEBUG_OUT << '\n';
110 return "";
113 void
114 File_path::add (String s)
116 push (s);
119 String
120 File_path::str () const
122 String s;
123 for (int i=0; i< size (); i++)
125 s = s + elem (i);
126 if (i < size () -1 )
127 s += ":";
129 return s;