X-Git-Url: https://git.distorted.org.uk/~mdw/dvdrip/blobdiff_plain/972381afacd6efbd0d20c77ff7e00981f95b7418..33d287563815f1b50edd1ebf1855702192efddf6:/lib.c diff --git a/lib.c b/lib.c index 2529f24..55c4b48 100644 --- a/lib.c +++ b/lib.c @@ -1,5 +1,34 @@ +/* -*-c-*- + * + * Common functions for the DVDrip C utilities. + * + * (c) 2022 Mark Wooding + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of the DVD ripping toolset. + * + * DVDrip is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 3 of the License, or (at your + * option) any later version. + * + * DVDrip is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License + * along with DVDrip. If not, see . + */ + +/*----- Header files ------------------------------------------------------*/ + #include "lib.h" +/*----- Diagnostics -------------------------------------------------------*/ + const char *prog = ""; void set_prog(const char *p) @@ -33,6 +62,8 @@ void bail_syserr(int err, const char *fmt, ...) exit(2); } +/*----- Parsing utilities -------------------------------------------------*/ + double parse_float(const char **p_inout, unsigned f, double min, double max, const char *what) { @@ -67,6 +98,8 @@ long parse_int(const char **p_inout, unsigned f, return (x); } +/*----- System utilities --------------------------------------------------*/ + void sit(double t) { struct timeval tv; @@ -138,80 +171,7 @@ off_t device_size(int fd, const char *file, int *blksz_out) return ((off_t)volsz); } -void store_filename(char *buf, ident id) -{ - switch (id_kind(id)) { - case RAW: - sprintf(buf, "#"); - break; - case IFO: - if (!id_title(id)) sprintf(buf, "/VIDEO_TS/VIDEO_TS.IFO"); - else sprintf(buf, "/VIDEO_TS/VTS_%02u_0.IFO", id_title(id)); - break; - case BUP: - if (!id_title(id)) sprintf(buf, "/VIDEO_TS/VIDEO_TS.BUP"); - else sprintf(buf, "/VIDEO_TS/VTS_%02u_0.BUP", id_title(id)); - break; - case VOB: - if (!id_title(id)) sprintf(buf, "/VIDEO_TS/VIDEO_TS.VOB"); - else - sprintf(buf, "/VIDEO_TS/VTS_%02u_%u.VOB", id_title(id), id_part(id)); - break; - default: - abort(); - } -} - -static char *copy_string(char *p, const char *q) -{ - while (*q) *p++ = *q++; - *p = 0; return (p); -} - -static char *copy_hex(char *p, const unsigned char *q, size_t sz) -{ - while (sz) { - sprintf(p, "%02x", *q); - p += 2; q++; sz--; - } - return (p); -} - -int dvd_id(char *p, dvd_reader_t *dvd, unsigned f, const char *file) -{ - char volid[33]; - unsigned char volsetid[16], discid[16]; - int rc; - size_t n; - - rc = DVDUDFVolumeInfo(dvd, - volid, sizeof(volid), - volsetid, sizeof(volsetid)); - if (!rc) { - p = copy_string(p, volid); - *p++ = '-'; - for (n = sizeof(volsetid); n && !volsetid[n - 1]; n--); - p = copy_hex(p, volsetid, n); - } else if (f&DIF_MUSTVOLINF) { - if (file) moan("failed to read volume info for `%s'", file); - else moan("failed to read volume info"); - return (-1); - } else - p = copy_string(p, ""); - - *p++ = ':'; - rc = DVDDiscID(dvd, discid); - if (!rc) - p = copy_hex(p, discid, sizeof(discid)); - else if (f&DIF_MUSTIFOHASH) { - if (file) moan("failed to determine disc id of `%s'", file); - else moan("failed to determine disc id"); - return (-1); - } else - p = copy_string(p, ""); - - return (0); -} +/*----- Progress utilities ------------------------------------------------*/ struct progress_state progress = PROGRESS_STATE_INIT; static struct banner_progress_item banner_progress; @@ -239,6 +199,8 @@ void hide_banner(void) progress_update(&progress); } +/*----- DVD utilities -----------------------------------------------------*/ + #ifdef notdef static void logfn(void *p, dvd_logger_level_t lev, const char *fmt, va_list ap) @@ -289,3 +251,80 @@ void open_dvd(const char *device, int mode, if (fd_out) *fd_out = fd; else close(fd); } + +void store_filename(char *buf, ident id) +{ + switch (id_kind(id)) { + case RAW: + sprintf(buf, "#"); + break; + case IFO: + if (!id_title(id)) sprintf(buf, "/VIDEO_TS/VIDEO_TS.IFO"); + else sprintf(buf, "/VIDEO_TS/VTS_%02u_0.IFO", id_title(id)); + break; + case BUP: + if (!id_title(id)) sprintf(buf, "/VIDEO_TS/VIDEO_TS.BUP"); + else sprintf(buf, "/VIDEO_TS/VTS_%02u_0.BUP", id_title(id)); + break; + case VOB: + if (!id_title(id)) sprintf(buf, "/VIDEO_TS/VIDEO_TS.VOB"); + else + sprintf(buf, "/VIDEO_TS/VTS_%02u_%u.VOB", id_title(id), id_part(id)); + break; + default: + abort(); + } +} + +static char *copy_string(char *p, const char *q) +{ + while (*q) *p++ = *q++; + *p = 0; return (p); +} + +static char *copy_hex(char *p, const unsigned char *q, size_t sz) +{ + while (sz) { + sprintf(p, "%02x", *q); + p += 2; q++; sz--; + } + return (p); +} + +int dvd_id(char *p, dvd_reader_t *dvd, unsigned f, const char *file) +{ + char volid[33]; + unsigned char volsetid[16], discid[16]; + int rc; + size_t n; + + rc = DVDUDFVolumeInfo(dvd, + volid, sizeof(volid), + volsetid, sizeof(volsetid)); + if (!rc) { + p = copy_string(p, volid); + *p++ = '-'; + for (n = sizeof(volsetid); n && !volsetid[n - 1]; n--); + p = copy_hex(p, volsetid, n); + } else if (f&DIF_MUSTVOLINF) { + if (file) moan("failed to read volume info for `%s'", file); + else moan("failed to read volume info"); + return (-1); + } else + p = copy_string(p, ""); + + *p++ = ':'; + rc = DVDDiscID(dvd, discid); + if (!rc) + p = copy_hex(p, discid, sizeof(discid)); + else if (f&DIF_MUSTIFOHASH) { + if (file) moan("failed to determine disc id of `%s'", file); + else moan("failed to determine disc id"); + return (-1); + } else + p = copy_string(p, ""); + + return (0); +} + +/*----- That's all, folks -------------------------------------------------*/