loadsave.c 4.12 KB
Newer Older
1 2 3
/*****************************************************************************
 * loadsave.c : Playlist loading / saving functions
 *****************************************************************************
4
 * Copyright (C) 1999-2004 VideoLAN
zorglub's avatar
zorglub committed
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * Authors: Samuel Hocevar <sam@zoy.org>
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/
#include <stdlib.h>                                      /* free(), strtol() */
#include <stdio.h>                                              /* sprintf() */
#include <string.h>                                            /* strerror() */

#include <vlc/vlc.h>
#include <vlc/vout.h>
#include <vlc/sout.h>
Laurent Aimar's avatar
Laurent Aimar committed
30
#include <vlc/input.h>
31

gbazin's avatar
 
gbazin committed
32 33 34 35
#ifdef HAVE_ERRNO_H
#   include <errno.h>
#endif

36 37
#include "vlc_playlist.h"

zorglub's avatar
zorglub committed
38
#define PLAYLIST_FILE_HEADER  "# vlc playlist file version 0.5"
39 40


41 42
/**
 * Import a certain playlist file into the playlist
zorglub's avatar
zorglub committed
43
 *
44 45
 * \param p_playlist the playlist to which the new items will be added
 * \param psz_filename the name of the playlistfile to import
zorglub's avatar
zorglub committed
46
 * \return VLC_SUCCESS on success
47
 */
zorglub's avatar
zorglub committed
48
int playlist_Import( playlist_t * p_playlist, const char *psz_filename )
49
{
zorglub's avatar
zorglub committed
50 51 52
    playlist_item_t *p_item;
    char *psz_uri;
    int i_id;
53

zorglub's avatar
zorglub committed
54 55
    msg_Dbg( p_playlist, "clearing playlist");
    playlist_Clear( p_playlist );
56 57


zorglub's avatar
zorglub committed
58 59
    psz_uri = (char *)malloc(sizeof(char)*strlen(psz_filename) + 17 );
    sprintf( psz_uri, "file/playlist://%s", psz_filename);
60

zorglub's avatar
zorglub committed
61
    i_id = playlist_Add( p_playlist, psz_uri, psz_uri,
zorglub's avatar
zorglub committed
62
                  PLAYLIST_INSERT  , PLAYLIST_END);
63

zorglub's avatar
zorglub committed
64
    vlc_mutex_lock( &p_playlist->object_lock );
65
    p_item = playlist_ItemGetById( p_playlist, i_id );
zorglub's avatar
zorglub committed
66
    p_item->b_autodeletion = VLC_TRUE;
zorglub's avatar
zorglub committed
67
    vlc_mutex_unlock( &p_playlist->object_lock );
68

zorglub's avatar
zorglub committed
69 70
    playlist_Play(p_playlist);

zorglub's avatar
zorglub committed
71
    return VLC_SUCCESS;
72 73
}

74 75
/**
 * Export a playlist to a certain type of playlistfile
zorglub's avatar
zorglub committed
76
 *
77 78 79
 * \param p_playlist the playlist to export
 * \param psz_filename the location where the exported file will be saved
 * \param psz_type the type of playlist file to create.
zorglub's avatar
zorglub committed
80
 * \return VLC_SUCCESS on success
81
 */
zorglub's avatar
zorglub committed
82 83
int playlist_Export( playlist_t * p_playlist, const char *psz_filename ,
                     const char *psz_type)
84
{
zorglub's avatar
zorglub committed
85 86
    module_t *p_module;
    playlist_export_t *p_export;
87

zorglub's avatar
zorglub committed
88
    msg_Info( p_playlist, "saving playlist to file %s", psz_filename );
89

zorglub's avatar
zorglub committed
90 91 92 93
    /* Prepare the playlist_export_t structure */
    p_export = (playlist_export_t *)malloc( sizeof(playlist_export_t) );
    if( !p_export)
    {
zorglub's avatar
zorglub committed
94
        msg_Err( p_playlist, "out of memory");
zorglub's avatar
zorglub committed
95 96 97 98
        return VLC_ENOMEM;
    }
    p_export->p_file = fopen( psz_filename, "wt" );
    if( !p_export->p_file )
99
    {
gbazin's avatar
 
gbazin committed
100
#ifdef HAVE_ERRNO_H
zorglub's avatar
zorglub committed
101
        msg_Err( p_playlist , "could not create playlist file %s"
gbazin's avatar
 
gbazin committed
102 103
                 " (%s)", psz_filename, strerror(errno) );
#else
zorglub's avatar
zorglub committed
104
        msg_Err( p_playlist , "could not create playlist file %s"
gbazin's avatar
 
gbazin committed
105 106 107
                 , psz_filename );
#endif
        return VLC_EGENERIC;
108 109
    }

zorglub's avatar
zorglub committed
110 111 112
    p_playlist->p_private = (void *)p_export;
    /* Lock the playlist */
    vlc_mutex_lock( &p_playlist->object_lock );
113

zorglub's avatar
zorglub committed
114
    /* And call the module ! All work is done now */
gbazin's avatar
 
gbazin committed
115
    p_module = module_Need( p_playlist, "playlist export", psz_type, VLC_TRUE);
zorglub's avatar
zorglub committed
116
    if( !p_module )
117
    {
zorglub's avatar
zorglub committed
118
        msg_Warn( p_playlist, "failed to export playlist" );
zorglub's avatar
zorglub committed
119 120
        vlc_mutex_unlock( &p_playlist->object_lock );
        return VLC_ENOOBJ;
121
    }
zorglub's avatar
zorglub committed
122
    module_Unneed( p_playlist , p_module );
123

zorglub's avatar
zorglub committed
124
    fclose( p_export->p_file );
125 126 127

    vlc_mutex_unlock( &p_playlist->object_lock );

zorglub's avatar
zorglub committed
128
    return VLC_SUCCESS;
129
}