summaryrefslogtreecommitdiff
path: root/flower/path.cc
blob: 5674ca90a35dc9c728166b52a0d8cdb30394d12f (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
   path.cc - manipulation of paths and filenames.
*/
#include <stdio.h>
#include "path.hh"
#include "flower-debug.hh"

#ifndef PATHSEP
#define PATHSEP '/'
#endif

/**
   @param path the original full filename
   @return 4 components of the path. They can be empty
*/
void
split_path(String path, 
	   String &drive, String &dirs, String &filebase, String &extension)
{
    // peel off components, one by one.
    int di = path.index_i(':');
    if (di >= 0) 
	{
	drive = path.left_str(di + 1);
	path = path.right_str(path.len() - di -1);
	} 
    else
	drive = "";
    
    di = path.index_last_i(PATHSEP);
    if (di >=0) 
	{
	dirs = path.left_str(di + 1);
	path = path.right_str(path.len()-di -1);
	}
    else
	dirs = "";
    
    di = path.index_last_i('.');
    if (di >= 0) 
	{
	filebase = path.left_str(di);
	extension =path.right_str(path.len()-di);	
	} 
    else 
	{
	extension = "";   
	filebase = path;
	}
}

/** find a file. 
  It will search in the current dir, in the construction-arg, and
  in any other added path, in this order.
  */
String
File_path::find(String nm)const

{
    fdebug << "looking for " << nm << ": ";
    if ( !nm.length_i() || ( nm == "-" ) )
	return nm;
    for (int i=0; i < size(); i++) {

	 String path  = (*this)[i];
	 path+= String(path.length_i()? "/":"")+nm;

	 fdebug << path << "? ";
	 FILE *f = fopen(path.ch_C(), "r"); // ugh!
	 if (f) {
	     fdebug << "found\n";
	     fclose(f);
	     return path;
	 }
     }
    fdebug << "\n";
    return "";
}