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.
 
 
 
 
 
 

96 lines
1.6 KiB

  1. /*
  2. * cuefile.c -- cue/toc functions
  3. *
  4. * Copyright (C) 2004, 2005, 2006, 2007, 2013 Svend Sorensen
  5. * For license terms, see the file COPYING in this distribution.
  6. */
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "cuefile.h"
  10. #include "cue.h"
  11. #include "toc.h"
  12. Cd *cf_parse(char *name, int *format)
  13. {
  14. FILE *fp = NULL;
  15. Cd *cd = NULL;
  16. if (UNKNOWN == *format) {
  17. if (UNKNOWN == (*format = cf_format_from_suffix(name))) {
  18. fprintf(stderr, "%s: unknown file suffix\n", name);
  19. return NULL;
  20. }
  21. }
  22. if (0 == strcmp("-", name)) {
  23. fp = stdin;
  24. } else if (NULL == (fp = fopen(name, "r"))) {
  25. fprintf(stderr, "%s: error opening file\n", name);
  26. return NULL;
  27. }
  28. switch (*format) {
  29. case CUE:
  30. cd = cue_parse(fp);
  31. break;
  32. case TOC:
  33. cd = toc_parse(fp);
  34. break;
  35. }
  36. if(stdin != fp) {
  37. fclose(fp);
  38. }
  39. return cd;
  40. }
  41. int cf_print(char *name, int *format, Cd *cd)
  42. {
  43. FILE *fp = NULL;
  44. if (UNKNOWN == *format) {
  45. if (UNKNOWN == (*format = cf_format_from_suffix(name))) {
  46. fprintf(stderr, "%s: unknown file suffix\n", name);
  47. return -1;
  48. }
  49. }
  50. if (0 == strcmp("-", name)) {
  51. fp = stdout;
  52. } else if (NULL == (fp = fopen(name, "w"))) {
  53. fprintf(stderr, "%s: error opening file\n", name);
  54. return -1;
  55. }
  56. switch (*format) {
  57. case CUE:
  58. cue_print(fp, cd);
  59. break;
  60. case TOC:
  61. toc_print(fp, cd);
  62. break;
  63. }
  64. if(stdout != fp) {
  65. fclose(fp);
  66. }
  67. return 0;
  68. }
  69. int cf_format_from_suffix(char *name)
  70. {
  71. char *suffix;
  72. if (0 != (suffix = strrchr(name, '.'))) {
  73. if (0 == strcasecmp(".cue", suffix)) {
  74. return CUE;
  75. } else if (0 == strcasecmp(".toc", suffix)) {
  76. return TOC;
  77. }
  78. }
  79. return UNKNOWN;
  80. }