File: sort.c

package info (click to toggle)
gpsbabel 1.3.2-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 15,956 kB
  • ctags: 9,083
  • sloc: ansic: 87,514; xml: 13,640; pascal: 6,981; sh: 3,770; makefile: 811; perl: 754; tcl: 74; objc: 7
file content (92 lines) | stat: -rw-r--r-- 2,453 bytes parent folder | download | duplicates (2)
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
/*
    Arbitrary Sorting Filter(s)

    Copyright (C) 2004 Robert Lipe, robertlipe@usa.net

    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 USA

 */
#include "defs.h"
#include "filterdefs.h"

#if FILTERS_ENABLED

typedef enum {
	sm_unknown = 0,
	sm_gcid,
	sm_shortname,
	sm_description,
	sm_time
} sort_mode_;

sort_mode_ sort_mode = sm_shortname;	/* How are we sorting these? */

static char *opt_sm_gcid, *opt_sm_shortname, *opt_sm_description, *opt_sm_time;

static
arglist_t sort_args[] = {
	{"gcid", &opt_sm_gcid, "Sort by numeric geocache ID", 
		NULL, ARGTYPE_BOOL, ARG_NOMINMAX },
	{"shortname", &opt_sm_shortname, "Sort by waypoint short name", 
		NULL, ARGTYPE_BOOL, ARG_NOMINMAX },
	{"description", &opt_sm_description, "Sort by waypoint description", 
		NULL, ARGTYPE_BOOL, ARG_NOMINMAX },
	{"time", &opt_sm_time, "Sort by time", 
		NULL, ARGTYPE_BOOL, ARG_NOMINMAX },
	ARG_TERMINATOR
};

static int
sort_comp(const queue * a, const queue * b)
{
	const waypoint *x1 = (waypoint *)a;
	const waypoint *x2 = (waypoint *)b;

	switch (sort_mode)  {
	   case sm_gcid: return x1->gc_data.id - x2->gc_data.id;
	   case sm_shortname: return strcmp (x1->shortname, x2->shortname);
	   case sm_description: return strcmp (x1->description, x2->description);
	   case sm_time: return x1->creation_time - x2->creation_time;
	   default: abort(); return 0; /* Internal caller error. */
	}
}

void 
sort_process(void)
{
	sortqueue(&waypt_head, sort_comp);
}

void
sort_init(const char *args) 
{
	if (opt_sm_gcid)
		sort_mode = sm_gcid;
	if (opt_sm_shortname)
		sort_mode = sm_shortname;
	if (opt_sm_description)
		sort_mode = sm_description;
	if (opt_sm_time)
		sort_mode = sm_time;
}

filter_vecs_t sort_vecs = {
	sort_init,
	sort_process,
	NULL,
	NULL,
	sort_args
};
#endif // FILTERS_ENABLED