]> git.sesse.net Git - vlc/blobdiff - modules/access/vcd/cdrom.c
No need to add a '\n' at the end of messages passed to msg_*
[vlc] / modules / access / vcd / cdrom.c
index ddf5e3955543312f7ed71ad08743679b0dfe15d2..5ece9962c2c871bb8b960dd5f2884cda6204c435 100644 (file)
@@ -728,7 +728,7 @@ int ioctl_ReadSectors( vlc_object_t *p_this, const vcddev_t *p_vcddev,
         }
         if( sc.retsts || sc.error )
         {
-            msg_Err( p_this, "SCSI command failed: status %d error %d\n",
+            msg_Err( p_this, "SCSI command failed: status %d error %d",
                              sc.retsts, sc.error );
             if( i_type == VCD_TYPE ) free( p_block );
            return -1;
@@ -820,7 +820,9 @@ static int OpenVCDImage( vlc_object_t * p_this, const char *psz_dev,
     char *psz_vcdfile = NULL;
     char *psz_cuefile = NULL;
     FILE *cuefile     = NULL;
+    int *p_sectors    = NULL;
     char line[1024];
+    bool b_found      = false;
 
     /* Check if we are dealing with a .cue file */
     p_pos = strrchr( psz_dev, '.' );
@@ -828,17 +830,9 @@ static int OpenVCDImage( vlc_object_t * p_this, const char *psz_dev,
     {
         /* psz_dev must be the cue file. Let's assume there's a .bin
          * file with the same filename */
-        if( p_pos )
-        {
-            psz_vcdfile = malloc( p_pos - psz_dev + 5 /* ".bin" */ );
-            strncpy( psz_vcdfile, psz_dev, p_pos - psz_dev );
-            strcpy( psz_vcdfile + (p_pos - psz_dev), ".bin");
-        }
-        else
-        {
-            psz_vcdfile = malloc( strlen(psz_dev) + 5 /* ".bin" */ );
-            sprintf( psz_vcdfile, "%s.bin", psz_dev );
-        }
+        psz_vcdfile = malloc( p_pos - psz_dev + 5 /* ".bin" */ );
+        strncpy( psz_vcdfile, psz_dev, p_pos - psz_dev );
+        strcpy( psz_vcdfile + (p_pos - psz_dev), ".bin");
         psz_cuefile = strdup( psz_dev );
     }
     else
@@ -853,8 +847,8 @@ static int OpenVCDImage( vlc_object_t * p_this, const char *psz_dev,
         }
         else
         {
-            psz_cuefile = malloc( strlen(psz_dev) + 5 /* ".cue" */ );
-            sprintf( psz_cuefile, "%s.cue", psz_dev );
+            if( asprintf( &psz_cuefile, "%s.cue", psz_dev ) == -1 )
+                psz_cuefile = NULL;
         }
         /* If we need to look up the .cue file, then we don't have to look for the vcd */
         psz_vcdfile = strdup( psz_dev );
@@ -865,95 +859,101 @@ static int OpenVCDImage( vlc_object_t * p_this, const char *psz_dev,
     cuefile = utf8_fopen( psz_cuefile, "rt" );
     if( cuefile == NULL )
     {
-        i_ret = -1;
         msg_Dbg( p_this, "could not find .cue file" );
         goto error;
     }
 
-    msg_Dbg( p_this,"using vcd image file: %s", psz_vcdfile );
+    msg_Dbg( p_this,"guessing vcd image file: %s", psz_vcdfile );
     p_vcddev->i_vcdimage_handle = utf8_open( psz_vcdfile,
                                     O_RDONLY | O_NONBLOCK | O_BINARY, 0666 );
  
-    if( p_vcddev->i_vcdimage_handle == -1 &&
-        fscanf( cuefile, "FILE %c", line ) &&
-        fgets( line, 1024, cuefile ) )
+    while( fgets( line, 1024, cuefile ) && !b_found )
     {
         /* We have a cue file, but no valid vcd file yet */
-        free( psz_vcdfile );
-        p_pos = strchr( line, '"' );
-        if( p_pos )
+        char filename[1024];
+        char type[16];
+        int i_temp = sscanf( line, "FILE \"%1023[^\"]\" %15s", filename, type );
+        *p_pos = 0;
+        switch( i_temp )
         {
-            *p_pos = 0;
-
-            /* Take care of path standardization */
-            if( *line != '/' && ((p_pos = strrchr( psz_cuefile, '/' ))
-                || (p_pos = strrchr( psz_cuefile, '\\' ) )) )
-            {
-                psz_vcdfile = malloc( strlen(line) +
+            case 2:
+                msg_Dbg( p_this, "the cue file says the data file is %s", type );
+                if( strcasecmp( type, "BINARY" ) )
+                    goto error; /* Error if not binary, otherwise treat as case 1 */
+            case 1:
+                if( p_vcddev->i_vcdimage_handle == -1 )
+                {
+                    msg_Dbg( p_this, "we could not find the data file, but we found a new path" );
+                    free( psz_vcdfile);
+                    if( *filename != '/' && ((p_pos = strrchr( psz_cuefile, '/' ))
+                        || (p_pos = strrchr( psz_cuefile, '\\' ) )) )
+                    {
+                        psz_vcdfile = malloc( strlen(filename) +
                                       (p_pos - psz_cuefile + 1) + 1 );
-                strncpy( psz_vcdfile, psz_cuefile, (p_pos - psz_cuefile + 1) );
-                strcpy( psz_vcdfile + (p_pos - psz_cuefile + 1), line );
-            }
-            else psz_vcdfile = strdup( line );
-        }
-        msg_Dbg( p_this,"using vcd image file: %s", psz_vcdfile );
-        p_vcddev->i_vcdimage_handle = utf8_open( psz_vcdfile,
+                        strncpy( psz_vcdfile, psz_cuefile, (p_pos - psz_cuefile + 1) );
+                        strcpy( psz_vcdfile + (p_pos - psz_cuefile + 1), filename );
+                    } else psz_vcdfile = strdup( filename );
+                    msg_Dbg( p_this,"using vcd image file: %s", psz_vcdfile );
+                    p_vcddev->i_vcdimage_handle = utf8_open( psz_vcdfile,
                                         O_RDONLY | O_NONBLOCK | O_BINARY, 0666 );
+                }
+                b_found = true;
+            default:
+                break;
+        }
     }
 
     if( p_vcddev->i_vcdimage_handle == -1)
-    {
-        i_ret = -1;
         goto error;
-    }
-    else i_ret = 0;
 
     /* Try to parse the i_tracks and p_sectors info so we can just forget
      * about the cuefile */
-    if( i_ret == 0 )
+    size_t i_tracks = 0;
+
+    while( fgets( line, 1024, cuefile ) )
     {
-        int p_sectors[100];
-        int i_tracks = 0;
-        int i_num;
-        char psz_dummy[10];
+        /* look for a TRACK line */
+        char psz_dummy[9];
+        if( !sscanf( line, "%9s", psz_dummy ) || strcmp(psz_dummy, "TRACK") )
+            continue;
 
+        /* look for an INDEX line */
         while( fgets( line, 1024, cuefile ) )
         {
-            /* look for a TRACK line */
-            if( !sscanf( line, "%9s", psz_dummy ) ||
-                strcmp(psz_dummy, "TRACK") )
-                continue;
-
-            /* look for an INDEX line */
-            while( fgets( line, 1024, cuefile ) )
-            {
-                int i_min, i_sec, i_frame;
+            int i_num, i_min, i_sec, i_frame;
 
-                if( (sscanf( line, "%9s %2u %2u:%2u:%2u", psz_dummy, &i_num,
-                            &i_min, &i_sec, &i_frame ) != 5) || (i_num != 1) )
-                    continue;
+            if( (sscanf( line, "%*9s %2u %2u:%2u:%2u", &i_num,
+                         &i_min, &i_sec, &i_frame ) != 4) || (i_num != 1) )
+                continue;
 
-                i_tracks++;
-                p_sectors[i_tracks - 1] = MSF_TO_LBA(i_min, i_sec, i_frame);
-                msg_Dbg( p_this, "vcd track %i begins at sector:%i",
-                         i_tracks - 1, p_sectors[i_tracks - 1] );
-                break;
-            }
+            int *buf = realloc (p_sectors, (i_tracks + 1) * sizeof (int));
+            if (buf == NULL)
+                goto error;
+            p_sectors = buf;
+            p_sectors[i_tracks] = MSF_TO_LBA(i_min, i_sec, i_frame);
+            msg_Dbg( p_this, "vcd track %i begins at sector:%i",
+                     i_tracks, p_sectors[i_tracks] );
+            i_tracks++;
+            break;
         }
-
-        /* fill in the last entry */
-        p_sectors[i_tracks] = lseek(p_vcddev->i_vcdimage_handle, 0, SEEK_END)
-                                / VCD_SECTOR_SIZE;
-        msg_Dbg( p_this, "vcd track %i, begins at sector:%i",
-                 i_tracks, p_sectors[i_tracks] );
-        p_vcddev->i_tracks = i_tracks;
-        p_vcddev->p_sectors = malloc( (i_tracks + 1) * sizeof(int) );
-        memcpy( p_vcddev->p_sectors, p_sectors, (i_tracks + 1) * sizeof(int) );
-
     }
 
+    /* fill in the last entry */
+    int *buf = realloc (p_sectors, (i_tracks + 1) * sizeof (int));
+    if (buf == NULL)
+        goto error;
+    p_sectors = buf;
+    p_sectors[i_tracks] = lseek(p_vcddev->i_vcdimage_handle, 0, SEEK_END)
+                                 / VCD_SECTOR_SIZE;
+    msg_Dbg( p_this, "vcd track %i, begins at sector:%i",
+             i_tracks, p_sectors[i_tracks] );
+    p_vcddev->i_tracks = ++i_tracks;
+    p_vcddev->p_sectors = p_sectors;
+    i_ret = 0;
+
 error:
     if( cuefile ) fclose( cuefile );
+    free( p_sectors );
     free( psz_cuefile );
     free( psz_vcdfile );