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