dvbcfg_test.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * dvbcfg testing.
  3. *
  4. * Copyright (c) 2005 by Andrew de Quincey <adq_dvb@lidskialf.net>
  5. *
  6. * This library is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as
  8. * published by the Free Software Foundation; either version 2.1 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <libdvbcfg/dvbcfg_zapchannel.h>
  24. void syntax(void);
  25. struct dvbcfg_zapchannel *channels = NULL;
  26. int zapcount = 0;
  27. int zappos = 0;
  28. int zapload_callback(struct dvbcfg_zapchannel *channel, void *private);
  29. int zapsave_callback(struct dvbcfg_zapchannel *channel, void *private);
  30. int main(int argc, char *argv[])
  31. {
  32. if (argc != 4) {
  33. syntax();
  34. }
  35. if (!strcmp(argv[1], "-zapchannel")) {
  36. FILE *f = fopen(argv[2], "r");
  37. if (!f) {
  38. fprintf(stderr, "Unable to load %s\n", argv[2]);
  39. exit(1);
  40. }
  41. dvbcfg_zapchannel_parse(f, zapload_callback, NULL);
  42. fclose(f);
  43. f = fopen(argv[3], "w");
  44. if (!f) {
  45. fprintf(stderr, "Unable to write %s\n", argv[3]);
  46. exit(1);
  47. }
  48. dvbcfg_zapchannel_save(f, zapsave_callback, NULL);
  49. fclose(f);
  50. } else {
  51. syntax();
  52. }
  53. exit(0);
  54. }
  55. int zapload_callback(struct dvbcfg_zapchannel *channel, void *private)
  56. {
  57. (void) private;
  58. struct dvbcfg_zapchannel *tmp = realloc(channels, (zapcount+1) * sizeof(struct dvbcfg_zapchannel));
  59. if (tmp == NULL) {
  60. fprintf(stderr, "Out of memory\n");
  61. exit(1);
  62. }
  63. channels = tmp;
  64. memcpy(&channels[zapcount++], channel, sizeof(struct dvbcfg_zapchannel));
  65. return 0;
  66. }
  67. int zapsave_callback(struct dvbcfg_zapchannel *channel, void *private)
  68. {
  69. (void) private;
  70. if (zappos >= zapcount)
  71. return 1;
  72. memcpy(channel, channels + zappos, sizeof(struct dvbcfg_zapchannel));
  73. zappos++;
  74. return 0;
  75. }
  76. void syntax()
  77. {
  78. fprintf(stderr,
  79. "Syntax: dvbcfg_test <-zapchannel> <input filename> <output filename>\n");
  80. exit(1);
  81. }