|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- /*
- * cuefile.c -- cue/toc functions
- *
- * Copyright (C) 2004, 2005, 2006, 2007 Svend Sorensen
- * For license terms, see the file COPYING in this distribution.
- */
-
- #include <stdlib.h>
- #include <string.h>
- #include "cuefile.h"
- #include "cue.h"
- #include "toc.h"
-
- Cd *cf_parse(char *name, int *format)
- {
- FILE *fp = NULL;
- Cd *cd = NULL;
-
- if (UNKNOWN == *format) {
- if (UNKNOWN == (*format = cf_format_from_suffix(name))) {
- fprintf(stderr, "%s: unknown file suffix\n", name);
- return NULL;
- }
- }
-
- if (0 == strcmp("-", name)) {
- fp = stdin;
- } else if (NULL == (fp = fopen(name, "r"))) {
- fprintf(stderr, "%s: error opening file\n", name);
- return NULL;
- }
-
- switch (*format) {
- case CUE:
- cd = cue_parse(fp);
- break;
- case TOC:
- cd = toc_parse(fp);
- break;
- }
-
- if(stdin != fp) {
- fclose(fp);
- }
-
- return cd;
- }
-
- int cf_print(char *name, int *format, Cd *cd)
- {
- FILE *fp = NULL;
-
- if (UNKNOWN == *format) {
- if (UNKNOWN == (*format = cf_format_from_suffix(name))) {
- fprintf(stderr, "%s: unknown file suffix\n", name);
- return -1;
- }
- }
-
- if (0 == strcmp("-", name)) {
- fp = stdout;
- } else if (NULL == (fp = fopen(name, "w"))) {
- fprintf(stderr, "%s: error opening file\n", name);
- return -1;
- }
-
- switch (*format) {
- case CUE:
- cue_print(fp, cd);
- break;
- case TOC:
- toc_print(fp, cd);
- break;
- }
-
- if(stdout != fp) {
- fclose(fp);
- }
-
- return 0;
- }
-
- int cf_format_from_suffix(char *name)
- {
- char *suffix;
- if (0 != (suffix = strrchr(name, '.'))) {
- if (0 == strcasecmp(".cue", suffix)) {
- return CUE;
- } else if (0 == strcasecmp(".toc", suffix)) {
- return TOC;
- }
- }
-
- return UNKNOWN;
- }
|