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