File: nignore.cpp

package info (click to toggle)
xstow 1.0.2-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,064 kB
  • ctags: 1,073
  • sloc: cpp: 7,264; sh: 1,216; makefile: 196; awk: 26
file content (173 lines) | stat: -rw-r--r-- 3,540 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
/*
 * $Log: nignore.cpp,v $
 * Revision 1.3  2005/07/04 21:59:42  martin
 * added logging to all files
 *
 */
#include "nignore.h"

#ifdef CAN_USE_NIGNORE

#include "tree.h"
#include "leoini.h"
#include "debug.h"
#include "setup.h"

extern "C" {
#  include <fnmatch.h>
}


#define OUT(level) DEBUG_OUT(level, MODULE::NIGNORE)

NIgnore::NIgnore( const std::string &name, Leo::Ini &ini  )
    : name( name ),
      is_valid( true )
{
    OUT(1) << "HERE\n";

    read_ini_value( ini, name, "dir", dir );

    std::string foo;

    read_ini_value( ini, name, "follow", foo );
    follow = s2x<bool>( foo );
}

NIgnore::MATCH NIgnore::match( Ref<Node> node, std::string & strip )
{
    return match_dir( node, strip );
}

NIgnore::MATCH NIgnore::match_dir( Ref<Node> node, std::string & strip )
{    
    std::string path = node->getFullPathName();

    DEBUG( OUT(1) << "nignore path full: " << path << '\n' );

    std::string wd = setup.working_dir();
    std::string cp = setup.current_package();
    std::string foo;

    CppDir::split_name( cp, foo, cp );

    DEBUG( OUT(3) << "wd: " << wd << " cp: " << cp << '\n' );

    if( path.size() > CppDir::concat_dir( wd, cp ).size() )
	path = path.substr( CppDir::concat_dir( wd, cp ).size() );

    if( !strip.empty() && strip.size() < path.size() )
	path = path.substr( strip.size() );

    if( path.empty() )
	return MATCH::TRUE;

    OUT(2) << "nignore path: " << path << '\n';

    for( unsigned i = 0; i < dir.size(); ++i )
    {
	if( path == dir[i] )
	{
	    strip = dir[i];
	    OUT(1) << "match equal (strip): " << dir[i] << '\n';
	    return MATCH::TRUE;
	}
    }

    for( unsigned i = 0; i < dir.size(); ++i )
    {
	if( fnmatch( dir[i].c_str(), path.c_str(), FNM_PATHNAME | FNM_PERIOD ) == 0 )
	{
	    OUT(2) << "match (fnmatch): " << dir[i] << '\n';
	    strip = path;
	    return MATCH::TRUE;
	}  
    }


    unsigned max_length = 0;

    for( unsigned i = 0; i < dir.size(); ++i )
    {
	if( path.find( dir[i] ) == 0 )
	{
	    if( dir[i].size() > max_length )
	    {
		strip = dir[i];
		OUT(2) << "match (strip): " << dir[i] << '\n';
		max_length = dir[i].size();
	    }
	}
    }

    if( ( max_length > 0 ) && follow )
	return MATCH::TRUE;

    if( max_length > 0 )
	return MATCH::REQUIRE_NEXT;

    return MATCH::FALSE;
}

std::ostream & operator<<( std::ostream &out, Ref<NIgnore> ni )
{
    out << format( "nignore: %s\n", ni->name );
    out << format( "\tdir: %s\n", ni->dir );
    out << format( "\tfollow: %s\n", ni->follow );

    return out;
}

std::ostream & operator<<( std::ostream &out, Ref<NIgnoreChain> nc )
{
    std::vector< Ref<NIgnore> > & ni = nc->nignores;

    out << "NIgnoreChain: ";

    for( unsigned i = 0; i < ni.size(); ++i )
    {
	out << ni[i]->getName() << '/';
    }

    out << '\n';

    for( unsigned i = 0; i < ni.size(); ++i )
	out << ni[i];

    return out;
}

bool NIgnoreChain::match( Ref<Node> node )
{
    std::string strip;
   
    for( unsigned i = 0; i < nignores.size(); ++i )
    {       
	if( !strip.empty() )
	    OUT(2) << "strip: " << strip << '\n';

	switch( nignores[i]->match( node, strip ) )
	{
	    case NIgnore::MATCH::TRUE: return true;
	    case NIgnore::MATCH::FALSE: return false;
	    case NIgnore::MATCH::REQUIRE_NEXT: break;
		case NIgnore::MATCH::FIRST__:
		case NIgnore::MATCH::LAST__:	
			break;
	}
    }
    
    return false;
}

std::string NIgnoreChain::getName()
{
    std::string ret;

    for( unsigned i = 0; i < nignores.size(); ++i )
	ret+= nignores[i]->getName() + '/';

    return ret;
}

#endif