Parent: [f0538b] (diff)

Child: [c79410] (diff)

Download this file

sortseq.cpp    111 lines (99 with data), 3.3 kB

  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
#ifndef lint
static char rcsid[] = "@(#$Id: sortseq.cpp,v 1.14 2008-09-29 11:33:55 dockes Exp $ (C) 2005 J.F.Dockes";
#endif
/*
* 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 2 of the License, 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.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <algorithm>
#include "debuglog.h"
#include "sortseq.h"
using std::string;
class CompareDocs {
DocSeqSortSpec ss;
public:
CompareDocs(const DocSeqSortSpec &sortspec) : ss(sortspec) {}
// It's not too clear in the std::sort doc what this should do. This
// behaves as operator<
int operator()(const Rcl::Doc *x, const Rcl::Doc *y)
{
LOGDEB1(("Comparing .. \n"));
// Compare using each criterion in term. Further comparisons must only
// be made if previous order ones are equal.
for (unsigned int i = 0; i < ss.crits.size(); i++) {
switch (ss.crits[i]) {
case DocSeqSortSpec::RCLFLD_MTIME:
{
long xmtime = x->dmtime.empty() ? atol(x->fmtime.c_str()) :
atol(x->dmtime.c_str());
long ymtime = y->dmtime.empty() ? atol(y->fmtime.c_str()) :
atol(y->dmtime.c_str());
LOGDEB1((" MTIME %ld %ld\n", xmtime, ymtime));
if (ss.dirs[i] ? xmtime > ymtime : xmtime < ymtime)
return 1;
else if (xmtime != ymtime)
return 0;
}
break;
case DocSeqSortSpec::RCLFLD_MIMETYPE:
LOGDEB1((" MIMETYPE\n"));
if (ss.dirs[i] ? x->mimetype > y->mimetype :
x->mimetype < y->mimetype)
return 1;
else if (x->mimetype != y->mimetype)
return 0;
break;
}
}
// Did all comparisons
return 0;
}
};
DocSeqSorted::DocSeqSorted(RefCntr<DocSequence> iseq, DocSeqSortSpec &sortspec,
const std::string &t)
: DocSeqModifier(t, iseq)
{
setSortSpec(sortspec);
}
bool DocSeqSorted::setSortSpec(DocSeqSortSpec &sortspec)
{
m_spec = sortspec;
LOGDEB(("DocSeqSorted:: count %d\n", m_spec.sortdepth));
m_docs.resize(m_spec.sortdepth);
int i;
for (i = 0; i < m_spec.sortdepth; i++) {
if (!m_seq->getDoc(i, m_docs[i])) {
LOGERR(("DocSeqSorted: getDoc failed for doc %d\n", i));
break;
}
}
m_spec.sortdepth = i;
LOGDEB(("DocSeqSorted:: m_count %d\n", m_spec.sortdepth));
m_docs.resize(m_spec.sortdepth);
m_docsp.resize(m_spec.sortdepth);
for (i = 0; i < m_spec.sortdepth; i++)
m_docsp[i] = &m_docs[i];
CompareDocs cmp(sortspec);
sort(m_docsp.begin(), m_docsp.end(), cmp);
return true;
}
bool DocSeqSorted::getDoc(int num, Rcl::Doc &doc, string *)
{
LOGDEB1(("DocSeqSorted: fetching %d\n", num));
if (num >= m_spec.sortdepth)
return false;
doc = *m_docsp[num];
return true;
}