bool_t save_preset_file (EqualizerPreset * preset, const char * filename)
{
    GKeyFile *rcfile;
    int i;
    char *data;
    gsize len;
    GError *error = NULL;

    rcfile = g_key_file_new();
    g_key_file_set_double(rcfile, "Equalizer preset", "Preamp", preset->preamp);

    for (i = 0; i < 10; i++) {
        char tmp[7];
        g_snprintf(tmp, sizeof(tmp), "Band%d", i);
        g_key_file_set_double(rcfile, "Equalizer preset", tmp,
                              preset->bands[i]);
    }

    data = g_key_file_to_data(rcfile, &len, &error);

    bool_t success = FALSE;

    VFSFile * file = vfs_fopen (filename, "w");
    if (file == NULL)
        goto DONE;
    if (vfs_fwrite (data, 1, strlen (data), file) == strlen (data))
        success = TRUE;
    vfs_fclose (file);

DONE:
    g_free(data);
    g_key_file_free(rcfile);
    return success;
}
Esempio n. 2
0
VFSFile * probe_buffer_new (const gchar * filename)
{
    VFSFile * file = vfs_fopen (filename, "r");

    if (! file)
        return NULL;

    ProbeBuffer * p = g_malloc (sizeof (ProbeBuffer));
    p->decoder = NULL;
    p->filename = filename;
    p->file = file;
    p->filled = 0;
    p->at = 0;
    p->read_warned = NULL;
    p->seek_warned = NULL;

    VFSFile * file2 = g_malloc (sizeof (VFSFile));
    file2->base = & probe_buffer_table;
    file2->handle = p;
    file2->uri = g_strdup (filename);
    file2->ref = 1;
    file2->sig = VFS_SIG;

    return file2;
}
Esempio n. 3
0
STREAMFILE *open_vfs(const char *path)
{
  VFSFile *vfsFile = vfs_fopen(path, "rb");
  if (!vfsFile)
    return NULL;

  return open_vfs_by_VFSFILE(vfsFile, path);
}
Esempio n. 4
0
LIBMTP_track_t *track_metadata(Tuple *from_tuple)
{
    LIBMTP_track_t *tr;
    gchar *filename, *uri_path;
    VFSFile *f;
    uint64_t filesize;
    struct stat sb;

    uri_path = strdup_tuple_filename (from_tuple);
    gchar *tmp = g_strescape(uri_path,NULL);
    filename=g_filename_from_uri(tmp,NULL,NULL);
    g_free(tmp);
    /* dealing the stream upload (invalidating)*/
    if(filename)
    {
        f = vfs_fopen(uri_path,"r");
        g_free(uri_path);
        if(vfs_is_streaming(f))
        {
            vfs_fclose(f);
            g_free(filename);
            return NULL;
        }
    }
    else
    {
        g_print("Warning! the filename is NULL, exiting");
        return NULL;

    }

    if ( stat(filename, &sb) == -1 )
    {
#if DEBUG
        g_print("ERROR! encountered while stat()'ing \"%s\"\n",filename);
#endif
        g_free(filename);
        return NULL;
    }
    filesize = (uint64_t) sb.st_size;

    /* track metadata*/
    tr = LIBMTP_new_track_t();
    tr->title = strdup_tuple_field (from_tuple, FIELD_TITLE);
    tr->artist = strdup_tuple_field (from_tuple, FIELD_ARTIST);
    tr->album = strdup_tuple_field (from_tuple, FIELD_ALBUM);
    tr->filesize = filesize;
    tr->filename = strdup_tuple_field (from_tuple, FIELD_FILE_NAME);
    tr->duration = (uint32_t)tuple_get_int(from_tuple, FIELD_LENGTH, NULL);
    tr->filetype = find_filetype (filename);
    tr->genre = strdup_tuple_field (from_tuple, FIELD_GENRE);
    tr->date = strdup_tuple_field (from_tuple, FIELD_YEAR);
    g_free(filename);
    return tr;
}
Esempio n. 5
0
/** Open specified .vtx file and read vtx file header
 *
 *  Open specified .vtx file and read vtx file header in struct vtx
 *  Return value: true if success, else false
 */
int ayemu_vtx_open (ayemu_vtx_t *vtx, const char *filename)
{
  char buf[2];
  int error = 0;
  int32_t int_regdata_size;

  vtx->regdata = NULL;

  if ((vtx->fp = vfs_fopen (filename, "rb")) == NULL) {
    fprintf(stderr, "ayemu_vtx_open: Cannot open file %s: %s\n", filename, strerror(errno));
    return 0;
  }

  if (vfs_fread(buf, 2, 1, vtx->fp) != 1) {
    fprintf(stderr,"ayemu_vtx_open: Can't read from %s: %s\n", filename, strerror(errno));
    error = 1;
  }

  buf[0] = tolower(buf[0]);
  buf[1] = tolower(buf[1]);

  if (strncmp(buf, "ay", 2) == 0)
    vtx->hdr.chiptype = AYEMU_AY;
  else if (strncmp (buf, "ym", 2) == 0)
    vtx->hdr.chiptype = AYEMU_YM;
  else {
    fprintf (stderr, "File %s is _not_ VORTEX format!\nIt not begins from AY or YM.\n", filename);
    error = 1;
  }

  /* read VTX header info in order format specified, see http:// ..... */
  if (!error) error = read_byte(vtx->fp, &vtx->hdr.stereo);
  if (!error) error = read_word16(vtx->fp, &vtx->hdr.loop);
  if (!error) error = read_word32(vtx->fp, &vtx->hdr.chipFreq);
  if (!error) error = read_byte(vtx->fp, &vtx->hdr.playerFreq);
  if (!error) error = read_word16(vtx->fp, &vtx->hdr.year);
  if (!error) {
	error = read_word32(vtx->fp, &int_regdata_size);
	vtx->hdr.regdata_size = (size_t) int_regdata_size;
  }

  if (!error) error = read_NTstring(vtx->fp, vtx->hdr.title);
  if (!error) error = read_NTstring(vtx->fp, vtx->hdr.author);
  if (!error) error = read_NTstring(vtx->fp, vtx->hdr.from);
  if (!error) error = read_NTstring(vtx->fp, vtx->hdr.tracker);
  if (!error) error = read_NTstring (vtx->fp, vtx->hdr.comment);

  if (error) {
    vfs_fclose(vtx->fp);
    vtx->fp = NULL;
  }
  return !error;
}
Esempio n. 6
0
static int load_module(const char *filename, VFSFile *file, unsigned char *module)
{
	int module_len;
	if (file != NULL)
		return vfs_fread(module, 1, ASAPInfo_MAX_MODULE_LENGTH, file);
	file = vfs_fopen(filename, "rb");
	if (file == NULL)
		return -1;
	module_len = vfs_fread(module, 1, ASAPInfo_MAX_MODULE_LENGTH, file);
	vfs_fclose(file);
	return module_len;
}
Esempio n. 7
0
static VFSFile *
open_vfs_file(const gchar *filename, const gchar *mode)
{
    VFSFile *file;

    if (!(file = vfs_fopen(filename, mode))) {
        SPRINTF (error, _("Error loading file '%s'"), filename);
        aud_interface_show_error (error);
    }

    return file;
}
Esempio n. 8
0
/**
 * Gets contents of the file into a buffer. Buffer of filesize bytes
 * is allocated by this function as necessary.
 *
 * @param filename URI of the file to read in.
 * @param buf Pointer to a pointer variable of buffer.
 * @param size Pointer to gsize variable that will hold the amount of
 * read data e.g. filesize.
 */
EXPORT void vfs_file_get_contents (const char * filename, void * * buf, int64_t * size)
{
    * buf = NULL;
    if (size)
        * size = 0;

    VFSFile * file = vfs_fopen (filename, "r");
    if (! file)
        return;

    vfs_file_read_all (file, buf, size);
    vfs_fclose (file);
}
Esempio n. 9
0
VFSFile * probe_buffer_new (const char * filename)
{
    VFSFile * file = vfs_fopen (filename, "r");

    if (! file)
        return NULL;

    ProbeBuffer * p = g_slice_new (ProbeBuffer);
    p->file = file;
    p->filled = 0;
    p->at = 0;

    return vfs_new (filename, & probe_buffer_table, p);
}
Esempio n. 10
0
bool_t playlist_save (int list, const char * filename)
{
    AUDDBG ("Saving playlist %s.\n", filename);

    PluginHandle * plugin = get_plugin (filename, TRUE);
    if (! plugin)
        return FALSE;

    PlaylistPlugin * pp = plugin_get_header (plugin);
    g_return_val_if_fail (pp && PLUGIN_HAS_FUNC (pp, load), FALSE);

    bool_t fast = get_bool (NULL, "metadata_on_play");

    VFSFile * file = vfs_fopen (filename, "w");
    if (! file)
        return FALSE;

    char * title = playlist_get_title (list);

    int entries = playlist_entry_count (list);
    Index * filenames = index_new ();
    index_allocate (filenames, entries);
    Index * tuples = index_new ();
    index_allocate (tuples, entries);

    for (int i = 0; i < entries; i ++)
    {
        index_append (filenames, playlist_entry_get_filename (list, i));
        index_append (tuples, playlist_entry_get_tuple (list, i, fast));
    }

    bool_t success = pp->save (filename, file, title, filenames, tuples);

    vfs_fclose (file);
    str_unref (title);

    for (int i = 0; i < entries; i ++)
    {
        str_unref (index_get (filenames, i));
        Tuple * tuple = index_get (tuples, i);
        if (tuple)
            tuple_unref (tuple);
    }

    index_free (filenames);
    index_free (tuples);

    return success;
}
Esempio n. 11
0
static VFSFile * safe_create (const gchar * filename)
{
    if (! vfs_file_test (filename, G_FILE_TEST_EXISTS))
        return vfs_fopen (filename, "w");

    const gchar * extension = strrchr (filename, '.');
    gint length = strlen (filename);
    gchar scratch[length + 3];
    gint count;

    for (count = 1; count < 100; count ++)
    {
        if (extension == NULL)
            sprintf (scratch, "%s-%d", filename, count);
        else
            sprintf (scratch, "%.*s-%d%s", (gint) (extension - filename),
             filename, count, extension);

        if (! vfs_file_test (scratch, G_FILE_TEST_EXISTS))
            return vfs_fopen (scratch, "w");
    }

    return NULL;
}
Esempio n. 12
0
static bool_t wv_attach (const char * filename, VFSFile * wv_input,
 VFSFile * * wvc_input, WavpackContext * * ctx, char * error, int flags)
{
    if (flags & OPEN_WVC)
    {
        SPRINTF (corrFilename, "%sc", filename);
        if (vfs_file_test (corrFilename, VFS_IS_REGULAR))
            * wvc_input = vfs_fopen (corrFilename, "r");
        else
            * wvc_input = NULL;
    }

    * ctx = WavpackOpenFileInputEx (& wv_readers, wv_input, * wvc_input, error, flags, 0);
    return (* ctx != NULL);
}
Esempio n. 13
0
VFSFile * open_local_file_nocase (const char * folder, const char * basename)
{
    char * path = find_file_case_path (folder, basename);
    if (! path)
        return NULL;

    char * uri = filename_to_uri (path);
    g_free (path);

    if (! uri)
        return NULL;

    VFSFile * file = vfs_fopen (uri, "r");
    str_unref (uri);
    return file;
}
Esempio n. 14
0
gboolean write_and_pivot_files (vcedit_state * state)
{
    gchar * temp;
    GError * error;
    gint handle = g_file_open_tmp (NULL, & temp, & error);

    if (handle < 0)
    {
        fprintf (stderr, "Failed to create temp file: %s.\n", error->message);
        g_error_free (error);
        return FALSE;
    }

    close (handle);

    gchar * temp_uri = filename_to_uri (temp);
    g_return_val_if_fail (temp_uri, FALSE);
    VFSFile * temp_vfs = vfs_fopen (temp_uri, "r+");
    g_return_val_if_fail (temp_vfs, FALSE);

    str_unref (temp_uri);

    if (vcedit_write (state, temp_vfs) < 0)
    {
        fprintf (stderr, "Tag update failed: %s.\n", state->lasterror);
        vfs_fclose (temp_vfs);
        g_free (temp);
        return FALSE;
    }

    if (! copy_vfs (temp_vfs, state->in))
    {
        fprintf (stderr, "Failed to copy temp file.  The temp file has not "
         "been deleted: %s.\n", temp);
        vfs_fclose (temp_vfs);
        g_free (temp);
        return FALSE;
    }

    vfs_fclose (temp_vfs);

    if (g_unlink (temp) < 0)
        fprintf (stderr, "Failed to delete temp file: %s.\n", temp);

    g_free (temp);
    return TRUE;
}
Esempio n. 15
0
static bool_t wv_attach (const char * filename, VFSFile * wv_input,
 VFSFile * * wvc_input, WavpackContext * * ctx, char * error, int flags)
{
    if (flags & OPEN_WVC)
    {
        SPRINTF (corrFilename, "%sc", filename);
        *wvc_input = vfs_fopen(corrFilename, "rb");
    }

    * ctx = WavpackOpenFileInputEx (& wv_readers, wv_input, * wvc_input, error,
     flags, 0);

    if (ctx == NULL)
        return FALSE;
    else
        return TRUE;
}
Esempio n. 16
0
static void * playback_thread (void * unused)
{
    PluginHandle * p = playback_entry_get_decoder ();
    current_decoder = p ? plugin_get_header (p) : NULL;

    if (! current_decoder)
    {
        char * error = g_strdup_printf (_("No decoder found for %s."),
         current_filename);
        interface_show_error (error);
        g_free (error);
        playback_error = TRUE;
        goto DONE;
    }

    current_data = NULL;
    current_bitrate = 0;
    current_samplerate = 0;
    current_channels = 0;

    Tuple * tuple = playback_entry_get_tuple ();
    read_gain_from_tuple (tuple);
    if (tuple)
        tuple_unref (tuple);

    bool_t seekable = (playback_entry_get_length () > 0);

    VFSFile * file = vfs_fopen (current_filename, "r");

    time_offset = seekable ? playback_entry_get_start_time () : 0;
    playback_error = ! current_decoder->play (& playback_api, current_filename,
     file, seekable ? time_offset + initial_seek : 0,
     seekable ? playback_entry_get_end_time () : -1, paused);

    output_close_audio ();

    if (file)
        vfs_fclose (file);

DONE:
    if (! ready_flag)
        set_pb_ready (& playback_api);

    end_source = g_timeout_add (0, end_cb, NULL);
    return NULL;
}
Esempio n. 17
0
static VFSFile *
open_vfs_file(const gchar *filename, const gchar *mode)
{
    VFSFile *file;
    GtkWidget *dialog;

    if (!(file = vfs_fopen(filename, mode))) {
        dialog = gtk_message_dialog_new (GTK_WINDOW (mainwin),
                                         GTK_DIALOG_DESTROY_WITH_PARENT,
                                         GTK_MESSAGE_ERROR,
                                         GTK_BUTTONS_CLOSE,
                                         "Error loading file '%s'",
                                         filename);
        gtk_dialog_run (GTK_DIALOG (dialog));
        gtk_widget_destroy (dialog);
    }

    return file;
}
Esempio n. 18
0
bool_t playlist_load (const char * filename, char * * title,
 Index * * filenames_p, Index * * tuples_p)
{
    AUDDBG ("Loading playlist %s.\n", filename);

    PluginHandle * plugin = get_plugin (filename, FALSE);
    if (! plugin)
        return FALSE;

    PlaylistPlugin * pp = plugin_get_header (plugin);
    g_return_val_if_fail (pp && PLUGIN_HAS_FUNC (pp, load), FALSE);

    VFSFile * file = vfs_fopen (filename, "r");
    if (! file)
        return FALSE;

    Index * filenames = index_new ();
    Index * tuples = index_new ();
    bool_t success = pp->load (filename, file, title, filenames, tuples);

    vfs_fclose (file);

    if (! success)
    {
        index_free (filenames);
        index_free (tuples);
        return FALSE;
    }

    if (index_count (tuples))
        g_return_val_if_fail (index_count (tuples) == index_count (filenames),
         FALSE);
    else
    {
        index_free (tuples);
        tuples = NULL;
    }

    * filenames_p = filenames;
    * tuples_p = tuples;
    return TRUE;
}
Esempio n. 19
0
static gboolean wv_attach (const gchar * filename, VFSFile * wv_input,
 VFSFile * * wvc_input, WavpackContext * * ctx, gchar * error, gint flags)
{
    gchar *corrFilename;

    if (flags & OPEN_WVC)
    {
        corrFilename = g_strconcat(filename, "c", NULL);
        *wvc_input = vfs_fopen(corrFilename, "rb");
        g_free(corrFilename);
    }

    * ctx = WavpackOpenFileInputEx (& wv_readers, wv_input, * wvc_input, error,
     flags, 0);

    if (ctx == NULL)
        return FALSE;
    else
        return TRUE;
}
Esempio n. 20
0
gchar * load_text_file (const gchar * filename)
{
    VFSFile * file;
    gint size;
    gchar * buffer;

    file = vfs_fopen (filename, "r");

    if (file == NULL)
        return NULL;

    size = vfs_fsize (file);
    size = MAX (0, size);
    buffer = g_malloc (size + 1);

    size = vfs_fread (buffer, 1, size, file);
    size = MAX (0, size);
    buffer[size] = 0;

    vfs_fclose (file);

    return buffer;
}
Esempio n. 21
0
static int xs_get_sid_hash(const char *filename, xs_md5hash_t hash)
{
    VFSFile *inFile;
    xs_md5state_t inState;
    psidv1_header_t psidH;
    psidv2_header_t psidH2;
    uint8_t *songData;
    uint8_t ib8[2], i8;
    int index, result;

    /* Try to open the file */
    if ((inFile = vfs_fopen(filename, "rb")) == NULL)
        return -1;

    /* Read PSID header in */
    if (vfs_fread(psidH.magicID, 1, sizeof psidH.magicID, inFile) < sizeof psidH.magicID) {
        vfs_fclose(inFile);
        return -1;
    }

    if (strncmp(psidH.magicID, "PSID", 4) && strncmp(psidH.magicID, "RSID", 4)) {
        vfs_fclose(inFile);
        xs_error("Not a PSID or RSID file '%s'\n", filename);
        return -2;
    }

    psidH.version = xs_fread_be16(inFile);
    psidH.dataOffset = xs_fread_be16(inFile);
    psidH.loadAddress = xs_fread_be16(inFile);
    psidH.initAddress = xs_fread_be16(inFile);
    psidH.playAddress = xs_fread_be16(inFile);
    psidH.nSongs = xs_fread_be16(inFile);
    psidH.startSong = xs_fread_be16(inFile);
    psidH.speed = xs_fread_be32(inFile);

    if (vfs_fread(psidH.sidName, 1, sizeof psidH.sidName, inFile) < sizeof psidH.sidName
     || vfs_fread(psidH.sidAuthor, 1, sizeof psidH.sidAuthor, inFile) < sizeof psidH.sidAuthor
     || vfs_fread(psidH.sidCopyright, 1, sizeof psidH.sidCopyright, inFile) < sizeof psidH.sidCopyright) {
        vfs_fclose(inFile);
        xs_error("Error reading SID file header from '%s'\n", filename);
        return -4;
    }

    /* Check if we need to load PSIDv2NG header ... */
    psidH2.flags = 0;    /* Just silence a stupid gcc warning */

    if (psidH.version == 2) {
        /* Yes, we need to */
        psidH2.flags = xs_fread_be16(inFile);
        psidH2.startPage = vfs_getc(inFile);
        psidH2.pageLength = vfs_getc(inFile);
        psidH2.reserved = xs_fread_be16(inFile);
    }

    /* Allocate buffer */
    songData = (uint8_t *) malloc(XS_SIDBUF_SIZE * sizeof(uint8_t));
    if (!songData) {
        vfs_fclose(inFile);
        xs_error("Error allocating temp data buffer for file '%s'\n", filename);
        return -3;
    }

    /* Read data to buffer */
    result = vfs_fread(songData, sizeof(uint8_t), XS_SIDBUF_SIZE, inFile);
    vfs_fclose(inFile);

    /* Initialize and start MD5-hash calculation */
    xs_md5_init(&inState);

    if (psidH.loadAddress == 0) {
        /* Strip load address (2 first bytes) */
        xs_md5_append(&inState, &songData[2], result - 2);
    } else {
        /* Append "as is" */
        xs_md5_append(&inState, songData, result);
    }

    /* Free buffer */
    free(songData);

    /* Append header data to hash */
#define XSADDHASH(QDATAB) do {                    \
    ib8[0] = (QDATAB & 0xff);                \
    ib8[1] = (QDATAB >> 8);                    \
    xs_md5_append(&inState, (uint8_t *) &ib8, sizeof(ib8));    \
    } while (0)

    XSADDHASH(psidH.initAddress);
    XSADDHASH(psidH.playAddress);
    XSADDHASH(psidH.nSongs);
#undef XSADDHASH

    /* Append song speed data to hash */
    i8 = 0;
    for (index = 0; (index < psidH.nSongs) && (index < 32); index++) {
        i8 = (psidH.speed & (1 << index)) ? 60 : 0;
        xs_md5_append(&inState, &i8, sizeof(i8));
    }

    /* Rest of songs (more than 32) */
    for (index = 32; index < psidH.nSongs; index++) {
        xs_md5_append(&inState, &i8, sizeof(i8));
    }

    /* PSIDv2NG specific */
    if (psidH.version == 2) {
        /* SEE SIDPLAY HEADERS FOR INFO */
        i8 = (psidH2.flags >> 2) & 3;
        if (i8 == 2)
            xs_md5_append(&inState, &i8, sizeof(i8));
    }