File: EPathFinder.cc

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 (204 lines) | stat: -rw-r--r-- 6,533 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**********************************************************************
 ** Copyright (C) 2002 stephan beal (stephan@wanderinghorse.net).  
 ** All rights reserved.
 **
 ** This file may be distributed and/or modified under the terms of the
 ** See LICENSE file in this directory. version 2 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.GPL included in the
 ** packaging of this file.
 **
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 **
 **********************************************************************/

#include "EPathFinder.h"
#include <iostream>
#include <unistd.h>

        ostream &
        operator<<(ostream &os, const EPathFinder &obj)
        {
                os << obj.getPathString();
                return os;
        }
        string &
        operator<<(string &os, const EPathFinder &obj)
        {
                os += obj.getPathString();
                return os;
        }

        EPathFinder &
        operator +=(EPathFinder &obj, const string &os)
        {
                obj.addPath( os );
                return obj;
        }


        EPathFinder::EPathFinder( const string &p, const string &e, const string &pathsep )
        {
                this->setPathSeparator( pathsep );
                this->setPath( p );
                this->setExtensions( e );
        }


        const string &
        EPathFinder::getPathSeparator() const
        {
                return ((EPathFinder *)this)->pathseparator;
        }

        void
        EPathFinder::setPathSeparator( const string &sep )
        {
                this->pathseparator = sep;
        }

        string
        EPathFinder::getPathString() const
        {
                return this->paths.toString( this->pathseparator );
        }

        const EStringList &
        EPathFinder::getPath()
        {
                return this->paths;
        }

        string
        EPathFinder::getExtensionsString() const
        {
                return this->exts.toString( this->pathseparator );
        }

        const EStringList &
        EPathFinder::getExtensions()
        {
                return this->exts;
        }

        int
        EPathFinder::setPath( const string &p ) 
        { 
                return this->setPath( EStringList::split( p, this->pathseparator ) );
        }

        int
        EPathFinder::setPath( const EStringList &p ) 
        { 
                this->paths = p;
                return this->paths.count();
        }

        void
        EPathFinder::addPath( const string &p ) 
        { 
                //if( p.empty() ) return;
                this->paths += p;
                //CERR << "addPath( " << p << " ); path="<<this->paths.toString(":")<<endl;
        }


        int
        EPathFinder::setExtensions( const string &p ) 
        { 
                const EStringList & newp = EStringList::split( p, this->pathseparator );
                return this->setExtensions( newp );
        }

        int
        EPathFinder::setExtensions( const EStringList &e ) 
        {
                this->exts = e;
                //CERR << "setExtensions( " << this->exts << " );"<<endl;
                return this->exts.count();
        }

        void
        EPathFinder::addExtension( const string &p ) 
        { 
                this->exts += p;
                //CERR << "addExtension( " << p << " ); extensions="<<this->exts<<endl;
        }

        // static
        bool
        EPathFinder::isAccessible( const string &path ) 
        { 

                int err = access( path.c_str(), R_OK );
                if( err != 0 )
                {
                        // LIBE_VERBOSE << "exists("<<path<<"): access() returned error: "<<err<<endl;
                }
                return err == 0;
        }


        string
        EPathFinder::basename( const std::string & name )
        {
                string::size_type slashat = name.find_last_of( EPathFinder::getDirectorySeparator() );
                if( slashat == string::npos ) return name;
                return name.substr( slashat + 1 );
        }



        string 
        EPathFinder::find( const string & resource ) const
        {
                static const std::string NOT_FOUND = "EPathFinder::find() : no findie";
                if( resource.empty() ) return string();
                string checkat;

#define CHECKPATH(CHECKAT) checkat = CHECKAT; \
        if( ! checkat.empty() && EPathFinder::isAccessible( checkat ) ) \
        { this->hitcache[resource] = checkat; return checkat; }

                //LIBE_VERBOSE << "find( " << resource << " )" << endl;
                checkat = this->hitcache[resource];
                if( checkat == NOT_FOUND ) return string();
                if( ! checkat.empty() ) return checkat;

                CHECKPATH(resource);

                EStringList::const_iterator piter = this->paths.begin();
                EStringList::const_iterator eiter = this->exts.begin();
    
                string path;
                string ext;

                if( EPathFinder::isAccessible( resource ) ) return resource;

                piter = this->paths.begin();
                string checkhere;
                while( piter != this->paths.end() )
                {
                        path = (*piter);
                        if( ! path.empty() )
                        {
                                path += EPathFinder::getDirectorySeparator();
                        }
                        ++piter;
                        checkhere = path + resource; 
                        //LIBE_VERBOSE << "find( " << resource << " ) checking " << checkhere << endl;
                        CHECKPATH( checkhere );
                        eiter = this->exts.begin();
                        while( eiter != this->exts.end() )
                        {
                                ext = (*eiter);
                                ++eiter;
                                checkhere = path + resource + ext;
                                //LIBE_VERBOSE << "find( " << resource << " ) checking " << checkhere << endl;
                                CHECKPATH( checkhere );
                        }
                }
                //LIBE_VERBOSE << "find( "<<resource<<" ): not found :(" << endl;
                this->hitcache[resource] = NOT_FOUND;
                return string();
        }