~ubuntu-branches/ubuntu/utopic/cccc/utopic

« back to all changes in this revision

Viewing changes to vcaddin/FileList.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2003-08-23 04:34:05 UTC
  • Revision ID: james.westby@ubuntu.com-20030823043405-xnzd3mn3hwtvi6dr
Tags: upstream-3.pre81
ImportĀ upstreamĀ versionĀ 3.pre81

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "stdafx.h"
 
2
#include "FileList.h"
 
3
 
 
4
FileList::FileList()
 
5
:       m_fileList(100)
 
6
{
 
7
}
 
8
 
 
9
 
 
10
FileList::~FileList()
 
11
{
 
12
}
 
13
 
 
14
 
 
15
File& FileList::Add(const File& file)
 
16
{
 
17
        POSITION pos = m_fileList.AddTail((File&)file);
 
18
        return m_fileList.GetAt(pos);
 
19
}
 
20
 
 
21
        
 
22
void FileList::RemoveAll()
 
23
{
 
24
        m_fileList.RemoveAll();
 
25
        m_fileArray.RemoveAll();
 
26
}
 
27
 
 
28
 
 
29
void FileList::BuildArray()
 
30
{
 
31
        // Build the file array.
 
32
        m_fileArray.RemoveAll();
 
33
        m_fileArray.SetSize(m_fileList.GetCount());
 
34
 
 
35
        int arrayPos = 0;
 
36
        POSITION listPos = m_fileList.GetHeadPosition();
 
37
        while (listPos != NULL)
 
38
        {
 
39
                Info info;
 
40
                info.m_pos = listPos;
 
41
                info.m_file = &m_fileList.GetNext(listPos);
 
42
                m_fileArray[arrayPos++] = info;
 
43
        }
 
44
}
 
45
 
 
46
        
 
47
static int __cdecl CompareArray(const void* elem1, const void* elem2)
 
48
{
 
49
        FileList::Info* info1 = (FileList::Info*)elem1;
 
50
        FileList::Info* info2 = (FileList::Info*)elem2;
 
51
        File* file1 = info1->m_file;
 
52
        File* file2 = info2->m_file;
 
53
 
 
54
        int ret = file1->GetShortName().Compare(file2->GetShortName());
 
55
        if (ret == 0)
 
56
        {
 
57
                // If the names match, compare against the file extensions.
 
58
                ret = file1->GetExt().Compare(file2->GetExt());
 
59
                if (ret == 0)
 
60
                {
 
61
                        // If the extensions match, compare against the path.
 
62
                        ret = file1->GetPath().CompareNoCase(file2->GetPath());
 
63
                        if (ret == 0)
 
64
                        {
 
65
                                file1->SetDuplicate(true);
 
66
                                file2->SetDuplicate(true);
 
67
                        }
 
68
                }
 
69
        }
 
70
        return ret;
 
71
}
 
72
        
 
73
 
 
74
void FileList::Sort()
 
75
{
 
76
        // Build the file array.
 
77
        BuildArray();
 
78
        
 
79
        // Sort the file array.
 
80
        qsort(m_fileArray.GetData(), m_fileArray.GetSize(), sizeof(Info), CompareArray);
 
81
 
 
82
        // Remove duplicates.
 
83
        for (int curPos = 0; curPos < m_fileArray.GetSize(); curPos++)
 
84
        {
 
85
                File& file = *m_fileArray[curPos].m_file;
 
86
 
 
87
                // Check for duplicates.
 
88
                if (!file.m_duplicate)
 
89
                        continue;
 
90
 
 
91
                // Found a duplicate... see how many more match.
 
92
                for (int endPos = curPos + 1; endPos < m_fileArray.GetSize(); endPos++)
 
93
                {
 
94
                        File& fileCmp = *m_fileArray[endPos].m_file;
 
95
                        int ret = file.GetShortName().Compare(fileCmp.GetShortName());
 
96
                        if (ret)
 
97
                                break;
 
98
 
 
99
                        // If the names match, compare against the file extensions.
 
100
                        ret = file.GetExt().Compare(fileCmp.GetExt());
 
101
                        if (ret)
 
102
                                break;
 
103
 
 
104
                        // If the extensions match, compare against the path.
 
105
                        ret = file.GetPath().CompareNoCase(fileCmp.GetPath());
 
106
                        if (ret)
 
107
                                break;
 
108
                }
 
109
 
 
110
                // [endPos] is now the last one that matched.
 
111
                if (endPos - curPos < 2)
 
112
                {
 
113
                        // Huh?
 
114
                        continue;
 
115
                }
 
116
 
 
117
                // Remove them.
 
118
                for (int i = curPos + 1; i < endPos; i++)
 
119
                {
 
120
                        m_fileList.RemoveAt(m_fileArray[i].m_pos);
 
121
                }
 
122
 
 
123
                m_fileArray.RemoveAt(curPos + 1, endPos - curPos - 1);
 
124
        }
 
125
}
 
126
 
 
127
// Find exact file index.
 
128
int FileList::FindExact(File& file) const
 
129
{
 
130
        // Scan the file list.
 
131
        for (int i = 0; i < GetCount(); i++)
 
132
        {
 
133
                File& fileCmp = (*this)[i];
 
134
 
 
135
                // Compare the extension.
 
136
                if (!file.GetExt().IsEmpty()  &&  file.GetExt() != fileCmp.GetExt())
 
137
                        continue;
 
138
 
 
139
                // Compare the short name.
 
140
                if (file.GetShortName() != fileCmp.GetShortName())
 
141
                        continue;
 
142
 
 
143
                // Compare the path.
 
144
                if (file.GetPath().CompareNoCase(fileCmp.GetPath()) == 0)
 
145
                {
 
146
                        return i;
 
147
                }
 
148
        }
 
149
 
 
150
        return -1;
 
151
}
 
152
 
 
153
 
 
154
// Find next file index.
 
155
int FileList::FindNext(int startPos, File& file) const
 
156
{
 
157
        // Scan the file list.
 
158
        for (int i = startPos + 1; i < GetCount(); i++)
 
159
        {
 
160
                File& fileCmp = (*this)[i];
 
161
 
 
162
                // Compare the short name.
 
163
                if (file.GetShortName() == fileCmp.GetShortName())
 
164
                        return i;
 
165
        }
 
166
 
 
167
        for (i = 0; i < startPos; i++)
 
168
        {
 
169
                File& fileCmp = (*this)[i];
 
170
 
 
171
                // Compare the short name.
 
172
                if (file.GetShortName() == fileCmp.GetShortName())
 
173
                        return i;
 
174
        }
 
175
 
 
176
        return -1;
 
177
}
 
178
 
 
179
 
 
180
// Find next file index.
 
181
int FileList::FindPrevious(int startPos, File& file) const
 
182
{
 
183
        // Scan the file list.
 
184
        for (int i = startPos - 1; i >= 0; i--)
 
185
        {
 
186
                File& fileCmp = (*this)[i];
 
187
 
 
188
                // Compare the short name.
 
189
                if (file.GetShortName() == fileCmp.GetShortName())
 
190
                        return i;
 
191
        }
 
192
 
 
193
        for (i = GetCount() - 1; i > startPos; i--)
 
194
        {
 
195
                File& fileCmp = (*this)[i];
 
196
 
 
197
                // Compare the short name.
 
198
                if (file.GetShortName() == fileCmp.GetShortName())
 
199
                        return i;
 
200
        }
 
201
 
 
202
        return -1;
 
203
}
 
204
 
 
205