You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sort.cc 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. Arbitrary Sorting Filter(s)
  3. Copyright (C) 2004 Robert Lipe, robertlipe+source@gpsbabel.org
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
  15. */
  16. #include "defs.h"
  17. #include "filterdefs.h"
  18. #include <stdlib.h>
  19. #if FILTERS_ENABLED
  20. typedef enum {
  21. sm_unknown = 0,
  22. sm_gcid,
  23. sm_shortname,
  24. sm_description,
  25. sm_time
  26. } sort_mode_;
  27. sort_mode_ sort_mode = sm_shortname; /* How are we sorting these? */
  28. static char* opt_sm_gcid, *opt_sm_shortname, *opt_sm_description, *opt_sm_time;
  29. static
  30. arglist_t sort_args[] = {
  31. {
  32. "gcid", &opt_sm_gcid, "Sort by numeric geocache ID",
  33. NULL, ARGTYPE_BOOL, ARG_NOMINMAX
  34. },
  35. {
  36. "shortname", &opt_sm_shortname, "Sort by waypoint short name",
  37. NULL, ARGTYPE_BOOL, ARG_NOMINMAX
  38. },
  39. {
  40. "description", &opt_sm_description, "Sort by waypoint description",
  41. NULL, ARGTYPE_BOOL, ARG_NOMINMAX
  42. },
  43. {
  44. "time", &opt_sm_time, "Sort by time",
  45. NULL, ARGTYPE_BOOL, ARG_NOMINMAX
  46. },
  47. ARG_TERMINATOR
  48. };
  49. static int
  50. sort_comp(const queue* a, const queue* b)
  51. {
  52. const Waypoint* x1 = (Waypoint*)a;
  53. const Waypoint* x2 = (Waypoint*)b;
  54. switch (sort_mode) {
  55. case sm_gcid:
  56. return x1->gc_data->id - x2->gc_data->id;
  57. case sm_shortname:
  58. return x1->shortname.compare(x2->shortname);
  59. case sm_description:
  60. return x1->description.compare(x2->description);
  61. case sm_time:
  62. return x1->GetCreationTime().toTime_t() - x2->GetCreationTime().toTime_t();
  63. default:
  64. abort();
  65. return 0; /* Internal caller error. */
  66. }
  67. }
  68. void
  69. sort_process(void)
  70. {
  71. sortqueue(&waypt_head, sort_comp);
  72. }
  73. void
  74. sort_init(const char* args)
  75. {
  76. if (opt_sm_gcid) {
  77. sort_mode = sm_gcid;
  78. }
  79. if (opt_sm_shortname) {
  80. sort_mode = sm_shortname;
  81. }
  82. if (opt_sm_description) {
  83. sort_mode = sm_description;
  84. }
  85. if (opt_sm_time) {
  86. sort_mode = sm_time;
  87. }
  88. }
  89. filter_vecs_t sort_vecs = {
  90. sort_init,
  91. sort_process,
  92. NULL,
  93. NULL,
  94. sort_args
  95. };
  96. #endif // FILTERS_ENABLED