home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / doc / cdrdao / examples / psxcopy-0.2 / cdjob.c next >
Encoding:
C/C++ Source or Header  |  2000-02-04  |  5.0 KB  |  187 lines

  1. /*  cdjob.c 
  2.  *
  3.  *  Copyright (C) 1999  Fabio Baracca <fabiobar@tiscalinet.it>
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2 of the License, or
  8.  *  (at your option) any later version.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License
  16.  *  along with this program; if not, write to the Free Software
  17.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. #include <stdio.h>
  21. #include <string.h>
  22.  
  23. #define BUF_S 1024
  24. #define DATA_SIGN "DATAFILE"
  25. #define AUDIO_SIGN "FILE"
  26. #define TRACKTYPE_SIGN "TRACK"
  27. #define MODE1_SIGN "MODE1"
  28. #define MODE2_SIGN "MODE2"
  29. #define SECT_SIZE 2048
  30.  
  31. FILE *inf, *outf, *device;
  32. char *fname, *track_info, *cdrom_device;
  33. char buf[BUF_S], file_to_read[BUF_S];
  34. int is_data_track = 0, line_count = 0;
  35. int i, j, k, x, data_size, chr;
  36. int min, sec, frame, size_in_sect, rcode = 0, rem, readin;
  37. int audio_begin = 0, audio_end = 0, tracks = 0, prevseek = 0;
  38.  
  39. int main(int argc, char *argv[])
  40. {
  41.     if (argc != 3) {
  42.     fprintf(stderr, "Usage: %s <cue-sheet> <cdrom device>\n", argv[0]);
  43.     return 1;
  44.     }
  45.     fname = argv[1];
  46.     cdrom_device = argv[2];
  47.  
  48.     inf = (FILE *) fopen(fname, "r+");
  49.     if (inf == NULL) {
  50.     fprintf(stderr, "Unable to open \"%s\" cue-sheet!\n", fname);
  51.     return 2;
  52.     }
  53.     device = (FILE *) fopen(cdrom_device, "r");
  54.  
  55. /*
  56.     if (device == NULL) {
  57.     fprintf(stderr, "Unable to open \"%s\" for reading!\n", cdrom_device);
  58.     return 4;
  59.     }
  60.     fclose (device);
  61. */    
  62.     /* Testing for PSX CD */
  63.  
  64.     sprintf (buf, "psxdump -T -d %s", cdrom_device);
  65.     i=system (buf);   
  66.     
  67.     if (i!=0) {
  68.        fprintf(stderr, "Unable to read from %s or disk is not PSX, please check and retry.\n", cdrom_device);
  69.        exit (1);
  70.     }
  71.     
  72.     /* Total bytes (about to) read */
  73.     readin = 0;
  74.  
  75.     printf("cdjob 0.2 - Fabio Baracca <fabiobar@tiscalinet.it>\n\n");
  76.  
  77.     while (!feof(inf)) {
  78.         prevseek=ftell (inf);
  79.     fgets(buf, BUF_S, inf);
  80.     if (!feof(inf)) {
  81.         line_count++;
  82.  
  83.         /* Store data type */
  84.         if (strncasecmp(buf, TRACKTYPE_SIGN, strlen(TRACKTYPE_SIGN)) == 0) {
  85.         if (strstr(buf, MODE1_SIGN))
  86.             is_data_track = 1;
  87.         else if (strstr(buf, MODE2_SIGN)) { /* HACK IT! */
  88.             is_data_track = 1;
  89.             buf[11]=0x20;
  90.             buf[12]='/';
  91.             buf[13]='/';
  92.             fseek (inf, prevseek, SEEK_SET);
  93.             fputs (buf, inf);
  94.         }
  95.         else
  96.             is_data_track = 0;
  97.         }
  98.         /* Data to be read.. */
  99.         if ((strncasecmp(buf, DATA_SIGN, strlen(DATA_SIGN)) == 0) ||
  100.         (strncasecmp(buf, AUDIO_SIGN, strlen(AUDIO_SIGN)) == 0)) {
  101.         /* Zero vars */
  102.         i = j = k = x = 0;
  103.  
  104.         /* Search the name */
  105.         for (j = 0, i = 0; i < strlen(buf); i++) {
  106.             if (buf[i] == '"') {
  107.             j++;
  108.             if (j == 1)
  109.                 k = i + 1;
  110.             else if (j >= 2) {
  111.                 x = i - 1;
  112.                 break;
  113.             }
  114.             }
  115.         }
  116.  
  117.         /* Check the name */
  118.         if (j != 2) {
  119.             fprintf(stderr, "Bogus characters at line %d\n", line_count);
  120.             rcode = 100;
  121.         } else if ((x - k + 1) > BUF_S) {
  122.             fprintf(stderr, "Ooppss.. filename too long..\n");
  123.             rcode = 101;
  124.         } else {
  125.             strcpy(file_to_read, buf + k);
  126.             /* Track size info */
  127.             track_info = buf + x + 2;
  128.             for (j = 0, i = x + 2; i < strlen(buf); i++) {
  129.             if (buf[i] == ':') {
  130.                 j++;
  131.                 if (j >= 2) {
  132.                 track_info[i - x + 1] = 0;
  133.                 break;
  134.                 }
  135.             }
  136.             }
  137.  
  138.             x -= k;
  139.             file_to_read[++x] = 0;
  140.  
  141.             if (!is_data_track) {    /* audio ? :-D */
  142.             tracks++;
  143.             if (audio_begin != 0)
  144.                 audio_end = tracks;
  145.             else
  146.                 audio_begin = tracks;
  147.             }
  148.             if (is_data_track) {
  149.             tracks++;
  150. /*            printf("About data file: \"%s\"..", file_to_read);*/
  151.             if ((sscanf(track_info, "%d:%d:%d", &min, &sec, &frame)) != 3) {
  152.                 printf("bogus size info!\n");
  153.                 rcode = 102;
  154.             } else {
  155.                             char toshell[1024]; /* HACK!!! */
  156.                 size_in_sect = (((min * 60 + sec) * 75 + frame) * SECT_SIZE);
  157.                 track_info[1]=0x20;
  158.                 track_info[2]='/';
  159.                 track_info[3]='/';
  160.                             fseek (inf, prevseek, SEEK_SET);
  161.                             fputs (buf, inf);
  162.                                                        
  163.                 sprintf (toshell, "psxdump -f %s -d %s", file_to_read, cdrom_device);
  164.                             system (toshell);
  165.             }
  166.             } else {
  167.             /* Silenty trash garbage */
  168.             /* printf ("mm.. not a data file.. ignoring!\n"); */
  169.             /* rcode=200; */
  170.             }
  171.         }
  172.         }
  173.     }
  174.     }
  175.  
  176.     fclose(inf);
  177.     fclose(device);
  178.     if (audio_begin != 0) {    /* Start cdparanoia */
  179.     printf("Ok.. now reading audio part of the disk.\n\n");
  180.     sprintf(buf, "cdparanoia -z %d- data.wav", audio_begin);
  181.     if (system(buf) != 0)
  182.         fprintf(stderr, "\n\nCdparanoia execution error. - CD dump can be corrupted.\n");
  183.     }
  184.     printf ("\n");
  185.     return rcode;
  186. }
  187.