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.

session.cc 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. GPSBabel session (format session) management
  3. Copyright (C) 2008 Olaf Klein, o.b.klein@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 "session.h"
  18. static queue session_list;
  19. static int session_ct;
  20. static void session_free(session_t* s);
  21. void
  22. session_init(void)
  23. {
  24. QUEUE_INIT(&session_list);
  25. session_ct = 0;
  26. }
  27. void
  28. session_exit(void)
  29. {
  30. queue* elem, *tmp;
  31. QUEUE_FOR_EACH(&session_list, elem, tmp) {
  32. session_t* s = (session_t*)elem;
  33. dequeue(&s->Q);
  34. session_free(s);
  35. }
  36. }
  37. void
  38. start_session(const char* name, const char* filename)
  39. {
  40. session_t* s;
  41. if (session_ct == 0) {
  42. QUEUE_INIT(&session_list);
  43. }
  44. session_ct++;
  45. s = (session_t*) xcalloc(1, sizeof(*s));
  46. ENQUEUE_TAIL(&session_list, &s->Q);
  47. QUEUE_INIT(&s->category_list);
  48. s->nr = session_ct;
  49. s->name = name;
  50. s->filename = xstrdup(filename);
  51. }
  52. session_t*
  53. curr_session(void)
  54. {
  55. return (session_t*) session_list.prev;
  56. }
  57. /* in work
  58. int
  59. session_add_category(const char *name, const int id)
  60. {
  61. queue *elem, *tmp;
  62. session_t *s;
  63. category_t *c;
  64. s = curr_session();
  65. QUEUE_FOR_EACH(&s->category_list, elem, tmp) {
  66. c = (category_t *) elem;
  67. if (case_ignore_strcmp(c->name, name) == 0) {
  68. if (id >= 0) c->id = id;
  69. return c->id;
  70. }
  71. }
  72. c = xmalloc(sizeof(*c));
  73. c->name = xstrdup(name);
  74. if (id < 0) c->id = -(++s->unknown_category_ct);
  75. else c->id = id;
  76. s->category_ct++;
  77. ENQUEUE_TAIL(&s->category_list, &c->Q);
  78. return c->id;
  79. }
  80. */
  81. /* non public functions */
  82. static void
  83. session_free(session_t* s)
  84. {
  85. queue* elem, *tmp;
  86. QUEUE_FOR_EACH(&s->category_list, elem, tmp) {
  87. category_t* c = (category_t*) elem;
  88. dequeue(&c->Q);
  89. xfree(c);
  90. }
  91. xfree(s->filename);
  92. xfree(s);
  93. }