loadsave.c 4.13 KB
Newer Older
1 2 3
/*****************************************************************************
 * loadsave.c : Playlist loading / saving functions
 *****************************************************************************
4
 * Copyright (C) 1999-2004 VideoLAN
5
 * $Id: loadsave.c,v 1.5 2004/01/12 21:22:23 hartman Exp $
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
 *
 * 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>

#include "stream_control.h"
#include "input_ext-intf.h"

#include "vlc_playlist.h"

zorglub's avatar
zorglub committed
36
#define PLAYLIST_FILE_HEADER  "# vlc playlist file version 0.5"
37 38


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

zorglub's avatar
zorglub committed
53
    msg_Dbg( p_playlist, "clearing playlist");
54

zorglub's avatar
zorglub committed
55 56
    /* Create our "fake" playlist item */
    playlist_Clear( p_playlist );
57 58


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

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

zorglub's avatar
zorglub committed
65 66
    p_item = playlist_GetItemById( p_playlist, i_id );
    p_item->b_autodeletion = VLC_TRUE;
67

zorglub's avatar
zorglub committed
68
    //p_playlist->i_index = 0;
69

zorglub's avatar
zorglub committed
70 71
/*
 *     if( p_item )
72
    {
zorglub's avatar
zorglub committed
73
        p_playlist->p_input = input_CreateThread( p_playlist, p_item );
74
    }
zorglub's avatar
zorglub committed
75
    */
76

zorglub's avatar
zorglub committed
77
    return VLC_SUCCESS;
78 79
}

80 81 82 83 84 85 86 87 88
/**
 * Export a playlist to a file
 *
 * Export a playlist to a certain type of playlistfile
 * \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.
 * \return 0 on succes
 */
zorglub's avatar
zorglub committed
89 90
int playlist_Export( playlist_t * p_playlist, const char *psz_filename ,
                     const char *psz_type)
91
{
zorglub's avatar
zorglub committed
92 93 94
    extern int errno;
    module_t *p_module;
    playlist_export_t *p_export;
95

zorglub's avatar
zorglub committed
96
    msg_Info( p_playlist, "Saving playlist to file %s", psz_filename );
97

zorglub's avatar
zorglub committed
98 99 100 101 102 103 104 105 106
    /* Prepare the playlist_export_t structure */
    p_export = (playlist_export_t *)malloc( sizeof(playlist_export_t) );
    if( !p_export)
    {
        msg_Err( p_playlist, "Out of memory");
        return VLC_ENOMEM;
    }
    p_export->p_file = fopen( psz_filename, "wt" );
    if( !p_export->p_file )
107
    {
zorglub's avatar
zorglub committed
108 109
        msg_Err( p_playlist , "Could not create playlist file %s (%s)"
                , psz_filename, strerror(errno) );
110 111 112
        return -1;
    }

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

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

zorglub's avatar
zorglub committed
127
    fclose( p_export->p_file );
128 129 130

    vlc_mutex_unlock( &p_playlist->object_lock );

zorglub's avatar
zorglub committed
131
    return VLC_SUCCESS;
132
}