File: EPathFinder.h

package info (click to toggle)
miwm 1.1-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,296 kB
  • ctags: 910
  • sloc: cpp: 8,179; sh: 231; makefile: 148
file content (130 lines) | stat: -rw-r--r-- 4,164 bytes parent folder | download | duplicates (3)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// EPathFinder.h
#ifndef PATHFINDER_H
#define PATHFINDER_H

/**********************************************************************
 ** Author: stephan beal (stephan@wanderinghorse.net).  
 ** License: See LICENSE file in this directory.
 **
 **********************************************************************/

#include <string>
#include <list>
#include <map>
#include "EStringList.h"
using namespace std; // todo: explicitely std:: all members.
class EPathFinder 
{
public:
        /*
          operator << adds this object's path to os.
        */
        friend ostream & operator << (ostream &os, const EPathFinder &);
        friend string & operator << (string &os, const EPathFinder &);

        /*
          Add p to this object's path.
        */
        friend EPathFinder & operator += (EPathFinder &list, const string &p);


        /**
           Creates object with the given path/extension list and path separator.
        */
        EPathFinder( const string &path = string(), const string &ext = string(), const string &pathsep = ":" );

        virtual ~EPathFinder() {};

        /**
           Returns a getPathSeparator()-separated string of all paths
           added via add/setPath().

           This /should/ be static, i think, but it's not, for reasons
           i'm not yet certain about.
        */
        string getPathString() const;
        const EStringList &getPath();

        void setPathSeparator( const string &sep );
        const string &getPathSeparator() const;

        /**
           Sets the path to p, which should be a ":"-delimited string.
           Returns the number of path elements parsed from p.
        */
        virtual int setPath( const string &p );
        virtual int setPath( const EStringList &p );

        /**
           Adds p to our path. May be ":"-delimited
        */
        virtual void addPath( const string &p );

        /**
           Adds a "search extension." Sample:

           finder.addExtension( ".txt:README" );

           Will now try all path combinations with the rightmost
           characters matching ext. Be sure to include a period if you
           want that searched (that is so this class can be used to
           find non-files or files without traditional-style
           extensions).
        */
        virtual void addExtension( const string &ext = string() );

        /**
           like addExtension(), but overwrites extension list.
        */
        virtual int setExtensions( const string &ext );
        virtual int setExtensions( const EStringList &p );

        /**
           Returns the getPathSeparator()-delimited listed of file
           suffixes to use when searching for a path.
        */
        string getExtensionsString() const;
        const EStringList &getExtensions();


        static bool isAccessible( const string &path );

        /**
           Returns the "base name" of the given string: any part
           following the final directory separator character.
         */
        static std::string basename( const std::string & );


        static string getDirectorySeparator() { return string("/"); }
    
        /**
           Returns the full path of the given resource, provided it could be found
           in all paths or with all extensions added and provided it is readable.
           Note that this might return a relative path, especially if the
           resourcename passed to it immediately resolves to an existing resource.
           It returns an empty string if the resourcename cannot be found in
           the filesystem tree (or is otherwise unaccessible).
        */
        string find( const string &resourcename ) const;

private:
        EStringList paths;
        EStringList exts;
        string pathseparator;

        typedef std::map<std::string,std::string> StringStringMap;
        typedef StringStringMap::iterator StringStringIterator;

        mutable StringStringMap hitcache;

};

#include <stdlib.h>
class BinPathFinder : public EPathFinder
{
public:
        BinPathFinder() : EPathFinder( getenv( "PATH" ) ) {};
        virtual ~BinPathFinder(){};
};
#endif // PATHFINDER_H