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