]> git.donarmstrong.com Git - lilypond.git/blob - flower/path.cc
release: 0.0.44
[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 File_path::File_path(String pref)
53 {
54     add(".");
55     add(pref);
56 }
57
58
59 /** find a file. 
60   It will search in the current dir, in the construction-arg, and
61   in any other added path, in this order.
62   */
63 String
64 File_path::find(String nm)const
65
66 {
67     fdebug << "looking for " << nm ;
68     for (int i=0; i < size(); i++) {
69
70          String path  = (*this)[i];
71          path+= "/"+nm;
72
73          fdebug << path << "? ";
74          FILE *f = fopen(path, "r"); // ugh!
75          if (f) {
76              fdebug << "found\n";
77              fclose(f);
78              return path;
79          }
80      }
81     fdebug << "\n";
82     return "";
83 }