]> git.donarmstrong.com Git - lilypond.git/blob - flower/file-path.cc
patch::: 1.1.2.hwn1
[lilypond.git] / flower / file-path.cc
1 /*
2    path.cc - manipulation of paths and filenames.
3 */
4 #include <stdio.h>
5 #include "config.hh"
6 #include "file-path.hh"
7 #include "flower-debug.hh"
8
9 #ifndef DIRSEP
10 #define DIRSEP '/'
11 #endif
12
13 #ifndef PATHSEP
14 #define PATHSEP ':'
15 #endif
16
17 /**
18    @param path the original full filename
19    @return 4 components of the path. They can be empty
20 */
21 void
22 split_path (String path,
23             String &drive, String &dirs, String &filebase, String &extension)
24 {
25   // peel off components, one by one.
26   int di = path.index_i (':');
27   if (di >= 0)
28     {
29       drive = path.left_str (di + 1);
30       path = path.right_str (path.length_i () - di -1);
31     }
32   else
33     drive = "";
34
35   di = path.index_last_i (DIRSEP);
36   if (di >=0)
37     {
38       dirs = path.left_str (di + 1);
39       path = path.right_str (path.length_i ()-di -1);
40     }
41   else
42     dirs = "";
43
44   di = path.index_last_i ('.');
45   if (di >= 0)
46     {
47       filebase = path.left_str (di);
48       extension =path.right_str (path.length_i ()-di);
49     }
50   else
51     {
52       extension = "";
53       filebase = path;
54     }
55 }
56
57 void
58 File_path::parse_path (String p)
59 {
60   int l;
61   
62   while ( (l = p.length_i ()) )
63     {
64       int i = p.index_i(PATHSEP);
65       if (i <0) 
66         i = l;
67       add (p.left_str(i));
68       p = p.right_str (l- i - 1);
69     }
70 }
71
72
73
74
75 /** find a file.
76   It will search in the current dir, in the construction-arg, and
77   in any other added path, in this order.
78
79   @return
80   The full path if found, or empty string if not found
81   */
82 String
83 File_path::find (String nm) const
84 {
85   fdebug << "looking for" << nm << ": ";
86   if (!nm.length_i() || (nm == "-") )
87     return nm;
88   for (int i=0; i < size(); i++)
89     {
90       String path  = elem(i);
91       String sep = to_str (DIRSEP);
92       String right(path.right_str (1));
93       if (path.length_i () && right != sep)
94         path += to_str (DIRSEP);
95
96       path += nm;
97
98       fdebug << path << "? ";
99       FILE *f = fopen (path.ch_C(), "r"); // ugh!
100       if (f)
101         {
102           fdebug << "found\n";
103           fclose (f);
104           return path;
105         }
106     }
107   fdebug << '\n';
108   return "";
109 }
110
111 void
112 File_path::add (String s)
113 {
114    push (s); 
115 }
116
117 String
118 File_path::str () const
119 {
120   String s;
121   for (int i=0; i< size (); i++)
122     {
123       s = s + elem (i);
124       if (i < size () -1 )
125         s += ":";
126     }
127   return s;
128 }