File: CvsIgnore.cpp

package info (click to toggle)
gcvs 1.0final-12
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 12,248 kB
  • ctags: 10,631
  • sloc: ansic: 71,709; cpp: 39,780; sh: 18,434; makefile: 1,915; yacc: 1,299; tcl: 1,283; perl: 910; lex: 249; csh: 185; lisp: 7
file content (236 lines) | stat: -rwxr-xr-x 5,440 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 1, or (at your option)
** any later version.

** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.

** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/*
 * Author : Alexandre Parenteau <aubonbeurre@hotmail.com> --- June 1998
 */

/*
 * CvsIgnore.cpp --- parse the .cvsignore
 */

#include "stdafx.h"

#include <stdlib.h>
#include <string.h>

#include "FileTraversal.h"
#include "AppConsole.h"
#include "CvsIgnore.h"
#include "getline.h"
#include "fnmatch.h"
#include "CvsPrefs.h"

#ifndef WIN32
#	include <ctype.h>
#endif /* !WIN32 */

#ifdef qMacCvsPP
#       include "MacCvsApp.h"
#       include "MacMisc.h"
#endif /* qMacCvsPP */

#ifdef WIN32
#	include "wincvs.h"	

#	ifdef _DEBUG
#		define new DEBUG_NEW
#		undef THIS_FILE
		static char THIS_FILE[] = __FILE__;
#	endif
#endif /* WIN32 */

/* Parse a line of space-separated wildcards and add them to the list. */
/* mostly duplicated from cvs/src/ignore.c */
static void ign_add (char *ign, bool hold, std::vector<CStr> & ignlist)
{
	if (!ign || !*ign)
		return;

	int ign_hold = ignlist.size() == 0 ? -1 : (int)ignlist.size();

	for (; *ign; ign++)
	{
		char *mark;
		char save;

		/* ignore whitespace before the token */
		if (isspace (*ign))
			continue;

		/*
		* if we find a single character !, we must re-set the ignore list
		* (saving it if necessary).  We also catch * as a special case in a
		* global ignore file as an optimization
		*/
		if ((!*(ign+1) || isspace (*(ign+1))) && (*ign == '!' || *ign == '*'))
		{
			if (!hold)
			{
				/* permanently reset the ignore list */
				ignlist.erase(ignlist.begin(), ignlist.end());

				/* if we are doing a '!', continue; otherwise add the '*' */
				if (*ign == '!')
				{
					continue;
				}
			}
			else if (*ign == '!')
			{
				/* temporarily reset the ignore list */
				if (ign_hold >= 0)
				{
					ignlist.erase(ignlist.begin(), ignlist.begin() + ign_hold);
					ign_hold = -1;
				}
				continue;
			}
		}

		/* find the end of this token */
		for (mark = ign; *mark && !isspace (*mark); mark++)
			/* do nothing */ ;

		save = *mark;
		*mark = '\0';

		ignlist.push_back(CStr(ign));

		*mark = save;
		if (save)
			ign = mark;
		else
			ign = mark - 1;
	}
}


// Read .cvsignore file and append entries to ignlist
static bool ReadIgnoredFile(const char* fileName, std::vector<CStr> & ignlist)
{
	FILE *fp;
	char *line = NULL;
	size_t line_allocated = 0;

	fp = fopen(fileName, "r");
	if(fp != 0L)
	{
		while (getline (&line, &line_allocated, fp) >= 0)
			ign_add (line, false, ignlist);

		if (ferror (fp))
			cvs_err("Problem while parsing '%s'", fileName);

		fclose (fp);
	}
	
	if(line != 0L)
		free (line);

	return(fp != 0L);
}


void BuildIgnoredList(std::vector<CStr> & ignlist, const char *path)
{
	ignlist.erase(ignlist.begin(), ignlist.end());

	static std::vector<CStr> deflist;
	static bool deflistparsed = false;
	
	if(!deflistparsed)
	{
#ifdef macintosh
		UFSSpec theFolder;
		CStr home;
		
		if(MacGetPrefsFolder(theFolder, home) == noErr)
		{
#else /* !macintosh */
		if(gCvsPrefs.Home() != 0L)
		{
			CStr home(gCvsPrefs.Home());
#endif /* !macintosh */
			struct stat sb;
			if (stat(home, &sb) != 0 || !S_ISDIR(sb.st_mode))
			{
				cvs_err("Warning : unable to access your HOME path '%s'\n", gCvsPrefs.Home());
				cvs_err("          ~/.cvsignore will not be read\n");
			}
			if(!home.empty() && !home.endsWith(kPathDelimiter))
				home << kPathDelimiter;
			home << ".cvsignore";

			// Read .cvsignore file from HOME directory.
			// If HOME dir doesn't have such a file then
			// try to read global file from executable path.
			if(ReadIgnoredFile(home, deflist) == false)
			{
				CStr appPath;
				
#ifdef WIN32
				((CWincvsApp *)AfxGetApp())->GetAppPath(appPath);
#elif defined(qUnix)
				// Unix version uses only .cvsignore file in HOME dir
#else
#	ifdef qMacCvsPP
				appPath = CMacCvsApp::GetAppPath();
#	endif /* qMacCvsPP */
#endif
				if(!appPath.empty())
				{
					if(!appPath.endsWith(kPathDelimiter))
						appPath << kPathDelimiter;
					
					appPath << ".cvsignore";
					
					ReadIgnoredFile(appPath, deflist);
				}
			}
		}
		deflistparsed = true;
	}
	else
		ignlist = deflist;
	
#if qUnix
	// from cvs/src/ignore.c
	CStr ign_default(". .. core RCSLOG tags TAGS RCS SCCS .make.state\
 .nse_depinfo #* .#* cvslog.* ,* CVS CVS.adm .del-* *.a *.olb *.o *.obj\
 *.so *.Z *~ *.old *.elc *.ln *.bak *.BAK *.orig *.rej *.exe _$* *$");
	ign_add (ign_default, false, ignlist);
#endif

	UStr ignPath(path);
	if(!ignPath.endsWith(kPathDelimiter))
		ignPath << kPathDelimiter;
	ignPath << ".cvsignore";

	// load the file from the current source directory
	ReadIgnoredFile(ignPath, ignlist);
}

bool MatchIgnoredList(const char * filename, const std::vector<CStr> & ignlist)
{
	std::vector<CStr>::const_iterator i;
	for(i = ignlist.begin(); i != ignlist.end(); ++i)
	{
		if(fnmatch (*i, filename, 0) == 0)
			return true;
	}
	return false;
}