1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#define CDPARANOIA_OUTPUT_BUF_LENGTH 1024
#define CDPARANOIA_OUTPUT_LINE_LENGTH 74
#define CDPARANOIA_STAT_LENGTH 42
#define CDPARANOIA_GRAPH_LENGTH 30
#define PRINTOUT_INTERVAL 0.5
extern int errno;
int cdparanoia_read_stat (unsigned *current, char **graph_string);
int find_cdparanoia_output_read_offset (char *buf, int begin, int end);
void print_msg (int begin, int length, int current, char *graph);
int
cdparanoia_read_stat (unsigned int *current, char **graph_string)
{
extern int errno;
static char buf[CDPARANOIA_OUTPUT_BUF_LENGTH];
static char status[CDPARANOIA_STAT_LENGTH + 1];
static char graph[CDPARANOIA_GRAPH_LENGTH + 1];
ssize_t bytes_read;
int bytes_avail;
static int prev_bytes_avail = -1;
int read_offset, temp_offset, count;
ioctl (0, FIONREAD, &bytes_avail);
if (bytes_avail < 4 * CDPARANOIA_OUTPUT_LINE_LENGTH) {
if (bytes_avail == prev_bytes_avail)
/* nothing available, let's wait */
return -1;
else {
/* Record available bytes, and let's just wait */
prev_bytes_avail = bytes_avail;
return -1;
}
}
prev_bytes_avail = -1;
count = 0;
do {
bytes_read = read (0, (void *) buf, sizeof (buf));
temp_offset = bytes_read - 4 * CDPARANOIA_OUTPUT_LINE_LENGTH - 1;
read_offset = find_cdparanoia_output_read_offset (buf,
temp_offset,
sizeof (buf) - 1);
if (read_offset < 0
|| read_offset > sizeof (buf) - CDPARANOIA_OUTPUT_LINE_LENGTH) {
if (count == 0)
return -1;
else
break;
}
strncpy (status, buf + read_offset, sizeof (status));
status[sizeof (status) - 1] = '\0';
count++;
} while (bytes_read == sizeof (buf));
strncpy (graph, status, sizeof (graph));
graph[sizeof (graph) - 1] = '\0';
*graph_string = graph;
temp_offset = CDPARANOIA_GRAPH_LENGTH + 1;
sscanf (status + temp_offset + 1, "%u", current);
return 0;
}
int
find_cdparanoia_output_read_offset (char *buf, int begin, int end)
{
int i;
i = begin;
do {
while (buf[i] != '=' && i <= end - 16)
i++;
if (buf[i + 3] == 'P'
&& isdigit (buf[i + 16 + CDPARANOIA_GRAPH_LENGTH + 3]))
return i + 16;
else
i++;
} while (i <= end - 16);
return -1;
}
// print out [P 0.xxxx "----> "]\n
void
print_msg (int begin, int length, int current, char *graph)
{
printf ("[P ");
printf ("%f ", (double) (current - begin) / (double) length);
printf ("\"%s\"]\n", graph);
}
int
main (int argc, char **argv)
{
int begin, length;
unsigned int current;
char *graph_string;
if (argc != 3) {
fprintf (stderr, "This is ripperX plugin for cdparanoia. Syntax is\n"
"ripperX_plugin-cdparanoia beginning_sector length_in_sector\n");
exit (1);
}
sscanf (argv[1], "%d", &begin);
sscanf (argv[2], "%d", &length);
while (1) {
if (cdparanoia_read_stat (¤t, &graph_string) == 0)
print_msg (begin, length, current, graph_string);
usleep (PRINTOUT_INTERVAL * 1000000);
}
}
|