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.
 
 
 
 
 
 

121 lines
2.4 KiB

  1. /*
  2. * cueconvert.c -- convert between cue/toc formats
  3. *
  4. * Copyright (C) 2004 Svend Sorensen
  5. * For license terms, see the file COPYING in this distribution.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <getopt.h>
  11. #include "cuefile.h"
  12. char *progname;
  13. void usage (int status)
  14. {
  15. if (0 == status) {
  16. fprintf(stdout, "%s: usage: cueconvert [option...] [infile [outfile]]\n", progname);
  17. fputs("\
  18. \n\
  19. OPTIONS\n\
  20. -h, --help print usage\n\
  21. -i, --input-format cue|toc set format of input file\n\
  22. -o, --output-format cue|toc set format of output file\n\
  23. ", stdout);
  24. } else {
  25. fprintf(stderr, "run `%s --help' for usage\n", progname);
  26. }
  27. exit (status);
  28. }
  29. int convert (char *iname, int iformat, char *oname, int oformat)
  30. {
  31. Cd *cd = NULL;
  32. if (NULL == (cd = cf_parse(iname, &iformat))) {
  33. fprintf(stderr, "input file error\n");
  34. return -1;
  35. }
  36. if (UNKNOWN == oformat) {
  37. /* first use file suffix */
  38. if (UNKNOWN == (oformat = cf_format_from_suffix(oname))) {
  39. /* then use opposite of input format */
  40. switch(iformat) {
  41. case CUE:
  42. oformat = TOC;
  43. break;
  44. case TOC:
  45. oformat = CUE;
  46. break;
  47. }
  48. }
  49. }
  50. return cf_print(oname, &oformat, cd);
  51. }
  52. int main (int argc, char **argv)
  53. {
  54. int iformat = UNKNOWN;
  55. int oformat = UNKNOWN;
  56. /* option variables */
  57. char c;
  58. /* getopt_long() variables */
  59. extern char *optarg;
  60. extern int optind;
  61. static struct option longopts[] = {
  62. {"help", no_argument, NULL, 'h'},
  63. {"input-format", required_argument, NULL, 'i'},
  64. {"output-format", required_argument, NULL, 'o'},
  65. {NULL, 0, NULL, 0}
  66. };
  67. progname = *argv;
  68. while (-1 != (c = getopt_long(argc, argv, "hi:o:", longopts, NULL))) {
  69. switch (c) {
  70. case 'h':
  71. usage(0);
  72. break;
  73. case 'i':
  74. if (0 == strcmp("cue", optarg))
  75. iformat = CUE;
  76. else if (0 == strcmp("toc", optarg))
  77. iformat = TOC;
  78. else
  79. fprintf(stderr, "%s: illegal format `%s'\n", progname, optarg);
  80. usage(1);
  81. break;
  82. case 'o':
  83. if (0 == strcmp("cue", optarg))
  84. oformat = CUE;
  85. else if (0 == strcmp("toc", optarg))
  86. oformat = TOC;
  87. else
  88. fprintf(stderr, "%s: illegal format `%s'\n", progname, optarg);
  89. usage(1);
  90. break;
  91. default:
  92. usage(1);
  93. break;
  94. }
  95. }
  96. if (optind == argc) {
  97. convert("-", iformat, "-", oformat);
  98. } else if (optind == argc - 1) {
  99. convert(argv[optind], iformat, "-", oformat);
  100. } else if (optind == argc - 2) {
  101. convert(argv[optind], iformat, argv[optind + 1], oformat);
  102. } else {
  103. usage(1);
  104. }
  105. return 0;
  106. }