]> git.donarmstrong.com Git - lilypond.git/blob - flower/file-path.cc
release: 0.1.61
[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 String
79 File_path::find (String nm) const
80 {
81   fdebug << _("looking for ") << nm << ": ";
82   if (!nm.length_i() || (nm == "-") )
83     return nm;
84   for (int i=0; i < size(); i++)
85     {
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;
91
92       path += nm;
93
94       fdebug << path << "? ";
95       FILE *f = fopen (path.ch_C(), "r"); // ugh!
96       if (f)
97         {
98           fdebug << _("found\n");
99           fclose (f);
100           return path;
101         }
102     }
103   fdebug << "\n";
104   return "";
105 }
106
107 void
108 File_path::add (String s)
109 {
110    push (s); 
111 }