]> git.donarmstrong.com Git - lilypond.git/blob - flower/path.cc
bd6a6660d2c6bce301bffb25ed247365ee4e5d6d
[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          String path  = (*this)[i];
66          path+= String(path.length_i()? "/":"")+nm;
67
68          fdebug << path << "? ";
69          FILE *f = fopen(path, "r"); // ugh!
70          if (f) {
71              fdebug << "found\n";
72              fclose(f);
73              return path;
74          }
75      }
76     fdebug << "\n";
77     return "";
78 }