]> git.donarmstrong.com Git - lilypond.git/blob - flower/path.cc
release: 0.1.11
[lilypond.git] / flower / path.cc
1 /*
2    path.cc - manipulation of paths and filenames.
3 */
4 #include <stdio.h>
5 #include "path.hh"
6 #include "flower-debug.hh"
7
8 #ifndef PATHSEP
9 #define PATHSEP '/'
10 #endif
11
12 /**
13    @param path the original full filename
14    @return 4 components of the path. They can be empty
15 */
16 void
17 split_path (String path, 
18            String &drive, String &dirs, String &filebase, String &extension)
19 {
20   // peel off components, one by one.
21   int di = path.index_i (':');
22   if (di >= 0) 
23         {
24         drive = path.left_str (di + 1);
25         path = path.right_str (path.len() - di -1);
26           }
27   else
28         drive = "";
29   
30   di = path.index_last_i (PATHSEP);
31   if (di >=0) 
32         {
33         dirs = path.left_str (di + 1);
34         path = path.right_str (path.len()-di -1);
35           }
36   else
37         dirs = "";
38   
39   di = path.index_last_i ('.');
40   if (di >= 0) 
41         {
42         filebase = path.left_str (di);
43         extension =path.right_str (path.len()-di);      
44           }
45   else 
46         {
47         extension = "";   
48         filebase = path;
49           }
50 }
51
52 /** find a file. 
53   It will search in the current dir, in the construction-arg, and
54   in any other added path, in this order.
55   */
56 String
57 File_path::find (String nm) const
58
59 {
60   fdebug << "looking for " << nm << ": ";
61   if ( !nm.length_i() || ( nm == "-") )
62         return nm;
63   for (int i=0; i < size(); i++) 
64     {
65
66          String path  = elem(i);
67          if ( path.length_i() )
68              path += "/";
69          
70          path += nm;
71
72          fdebug << path << "? ";
73          FILE *f = fopen (path.ch_C(), "r"); // ugh!
74          if (f) 
75            {
76              fdebug << "found\n";
77              fclose (f);
78              return path;
79            }
80      }
81   fdebug << "\n";
82   return "";
83 }