File: FileList.cpp

package info (click to toggle)
cccc 3.pre84-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 3,816 kB
  • ctags: 4,969
  • sloc: ansic: 33,244; cpp: 10,694; java: 622; makefile: 164; sh: 11
file content (205 lines) | stat: -rw-r--r-- 4,008 bytes parent folder | download | duplicates (10)
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
#include "stdafx.h"
#include "FileList.h"

FileList::FileList()
:	m_fileList(100)
{
}


FileList::~FileList()
{
}


File& FileList::Add(const File& file)
{
	POSITION pos = m_fileList.AddTail((File&)file);
	return m_fileList.GetAt(pos);
}

	
void FileList::RemoveAll()
{
	m_fileList.RemoveAll();
	m_fileArray.RemoveAll();
}


void FileList::BuildArray()
{
	// Build the file array.
	m_fileArray.RemoveAll();
	m_fileArray.SetSize(m_fileList.GetCount());

	int arrayPos = 0;
	POSITION listPos = m_fileList.GetHeadPosition();
	while (listPos != NULL)
	{
		Info info;
		info.m_pos = listPos;
		info.m_file = &m_fileList.GetNext(listPos);
		m_fileArray[arrayPos++] = info;
	}
}

	
static int __cdecl CompareArray(const void* elem1, const void* elem2)
{
	FileList::Info* info1 = (FileList::Info*)elem1;
	FileList::Info* info2 = (FileList::Info*)elem2;
	File* file1 = info1->m_file;
	File* file2 = info2->m_file;

	int ret = file1->GetShortName().Compare(file2->GetShortName());
	if (ret == 0)
	{
		// If the names match, compare against the file extensions.
		ret = file1->GetExt().Compare(file2->GetExt());
		if (ret == 0)
		{
			// If the extensions match, compare against the path.
			ret = file1->GetPath().CompareNoCase(file2->GetPath());
			if (ret == 0)
			{
				file1->SetDuplicate(true);
				file2->SetDuplicate(true);
			}
		}
	}
	return ret;
}
	

void FileList::Sort()
{
	// Build the file array.
	BuildArray();
	
	// Sort the file array.
	qsort(m_fileArray.GetData(), m_fileArray.GetSize(), sizeof(Info), CompareArray);

	// Remove duplicates.
	for (int curPos = 0; curPos < m_fileArray.GetSize(); curPos++)
	{
		File& file = *m_fileArray[curPos].m_file;

		// Check for duplicates.
		if (!file.m_duplicate)
			continue;

		// Found a duplicate... see how many more match.
		for (int endPos = curPos + 1; endPos < m_fileArray.GetSize(); endPos++)
		{
			File& fileCmp = *m_fileArray[endPos].m_file;
			int ret = file.GetShortName().Compare(fileCmp.GetShortName());
			if (ret)
				break;

			// If the names match, compare against the file extensions.
			ret = file.GetExt().Compare(fileCmp.GetExt());
			if (ret)
				break;

			// If the extensions match, compare against the path.
			ret = file.GetPath().CompareNoCase(fileCmp.GetPath());
			if (ret)
				break;
		}

		// [endPos] is now the last one that matched.
		if (endPos - curPos < 2)
		{
			// Huh?
			continue;
		}

		// Remove them.
		for (int i = curPos + 1; i < endPos; i++)
		{
			m_fileList.RemoveAt(m_fileArray[i].m_pos);
		}

		m_fileArray.RemoveAt(curPos + 1, endPos - curPos - 1);
	}
}

// Find exact file index.
int FileList::FindExact(File& file) const
{
	// Scan the file list.
	for (int i = 0; i < GetCount(); i++)
	{
		File& fileCmp = (*this)[i];

		// Compare the extension.
		if (!file.GetExt().IsEmpty()  &&  file.GetExt() != fileCmp.GetExt())
			continue;

		// Compare the short name.
		if (file.GetShortName() != fileCmp.GetShortName())
			continue;

		// Compare the path.
		if (file.GetPath().CompareNoCase(fileCmp.GetPath()) == 0)
		{
			return i;
		}
	}

	return -1;
}


// Find next file index.
int FileList::FindNext(int startPos, File& file) const
{
	// Scan the file list.
	for (int i = startPos + 1; i < GetCount(); i++)
	{
		File& fileCmp = (*this)[i];

		// Compare the short name.
		if (file.GetShortName() == fileCmp.GetShortName())
			return i;
	}

	for (i = 0; i < startPos; i++)
	{
		File& fileCmp = (*this)[i];

		// Compare the short name.
		if (file.GetShortName() == fileCmp.GetShortName())
			return i;
	}

	return -1;
}


// Find next file index.
int FileList::FindPrevious(int startPos, File& file) const
{
	// Scan the file list.
	for (int i = startPos - 1; i >= 0; i--)
	{
		File& fileCmp = (*this)[i];

		// Compare the short name.
		if (file.GetShortName() == fileCmp.GetShortName())
			return i;
	}

	for (i = GetCount() - 1; i > startPos; i--)
	{
		File& fileCmp = (*this)[i];

		// Compare the short name.
		if (file.GetShortName() == fileCmp.GetShortName())
			return i;
	}

	return -1;
}