<commit_before>/** 
 * \file sendtr.c
 * Example program to send a music track to a device.
 * This program is derived from the exact equivalent in libnjb.
 * based on Enrique Jorreto Ledesma's work on the original program by 
 * <NAME> and <NAME>.
 *
 * Copyright (C) 2003-2008 <NAME> <<EMAIL>>
 * Copyright (C) 2003-2005 <NAME>
 * Copyright (C) 2003-2005 <NAME>
 * Copyright (C) 2006 <NAME> <<EMAIL>>
 * Copyright (C) 2008 <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE

#include <string.h>
#include <libgen.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include "common.h"
#include "libmtp.h"
#include "pathutils.h"

extern LIBMTP_folder_t *folders;
extern LIBMTP_file_t *files;
extern LIBMTP_mtpdevice_t *device;

int sendtrack_function (char *, char *, char *, char *, char *, char *, uint16_t, uint16_t, uint16_t);
void sendtrack_command (int, char **);
void sendtrack_usage (void);

void sendtrack_usage (void)
{
  fprintf(stderr, "usage: sendtr [ -D debuglvl ] [ -q ] -t <title> -a <artist> -l <album>\n");
  fprintf(stderr, "       -c <codec> -g <genre> -n <track number> -y <year> \n");
  fprintf(stderr, "       -d <duration in seconds> <local path> <remote path>\n");
  fprintf(stderr, "(-q means the program will not ask for missing information.)\n");
}

static char *prompt (const char *prompt, char *buffer, size_t bufsz, int required)
{
  char *cp, *bp;
  
  while (1) {
    fprintf(stdout, "%s> ", prompt);
    if ( fgets(buffer, bufsz, stdin) == NULL ) {
      if (ferror(stdin)) {
	perror("fgets");
      } else {
	fprintf(stderr, "EOF on stdin\n");
      }
      return NULL;
    }
    
    cp = strrchr(buffer, '\n');
    if ( cp != NULL ) *cp = '\0';
    
    bp = buffer;
    while ( bp != cp ) {
      if ( *bp != ' ' && *bp != '\t' ) return bp;
      bp++;
    }
    
    if (! required) return bp;
  }
}

static int add_track_to_album(LIBMTP_album_t *albuminfo, LIBMTP_track_t *trackmeta)
{
  uint32_t *trackid = (uint32_t *)malloc(sizeof(uint32_t));
  int albumexists = 0;
  LIBMTP_album_t *album_list, *album, *tmp;
  int ret;

  *trackid = trackmeta->item_id;
  albuminfo->tracks = trackid;
  albuminfo->no_tracks = 1;

  /* Look for the album */
  album_list = LIBMTP_Get_Album_List(device);
  album = album_list;
  while(album != NULL) {
    if (strcmp(album->name, albuminfo->name) == 0 &&
	strcmp(album->artist, albuminfo->artist) == 0) {
      albumexists = 1;
      break;
    }
    tmp = album;
    album = album->next;
    LIBMTP_destroy_album_t(tmp);
  }
  
  if (albumexists) {
    uint32_t *tracks = (uint32_t *)malloc((album->no_tracks+1) * sizeof(uint32_t));
    printf("Album \"%s\" found: updating...\n", album->name);
    if (!tracks) {
      printf("failed malloc\n");
      return 1;
    }
    memcpy(tracks, album->tracks, album->no_tracks * sizeof(uint32_t));
    album->no_tracks++;
    album->tracks = tracks;
    tracks[album->no_tracks-1] = trackmeta->item_id;
    ret = LIBMTP_Update_Album(device, album);
    free(tracks);
  } else {
    printf("Album doesn't exist: creating...\n");
    ret = LIBMTP_Create_New_Album(device, albuminfo, 0);
  }
  
  if (ret != 0) {
    printf("Error creating or updating album.\n");
    LIBMTP_Dump_Errorstack(device);
    LIBMTP_Clear_Errorstack(device);
  } else {
    printf("success !\n");
  }
}

int sendtrack_function(char * from_path, char * to_path, char *partist, char *ptitle, char *pgenre, char *palbum, uint16_t tracknum, uint16_t length, uint16_t year)
{
  char *filename, *parent;
  char artist[80], title[80], genre[80], album[80];
  char num[80];
  uint64_t filesize;
  uint32_t parent_id = 0;
#ifdef __USE_LARGEFILE64
  struct stat64 sb;
#else
  struct stat sb;
#endif
  LIBMTP_track_t *trackmeta;
  LIBMTP_album_t *albuminfo;
  int ret;<commit_msg>Fix a few memory leaks<commit_after>/** 
 * \file sendtr.c
 * Example program to send a music track to a device.
 * This program is derived from the exact equivalent in libnjb.
 * based on <NAME>'s work on the original program by 
 * <NAME> and <NAME>.
 *
 * Copyright (C) 2003-2008 <NAME> <<EMAIL>>
 * Copyright (C) 2003-2005 <NAME>
 * Copyright (C) 2003-2005 <NAME>
 * Copyright (C) 2006 <NAME> <<EMAIL>>
 * Copyright (C) 2008 <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE

#include <string.h>
#include <libgen.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include "common.h"
#include "libmtp.h"
#include "pathutils.h"

extern LIBMTP_folder_t *folders;
extern LIBMTP_file_t *files;
extern LIBMTP_mtpdevice_t *device;

int sendtrack_function (char *, char *, char *, char *, char *, char *, uint16_t, uint16_t, uint16_t);
void sendtrack_command (int, char **);
void sendtrack_usage (void);

void sendtrack_usage (void)
{
  fprintf(stderr, "usage: sendtr [ -D debuglvl ] [ -q ] -t <title> -a <artist> -l <album>\n");
  fprintf(stderr, "       -c <codec> -g <genre> -n <track number> -y <year> \n");
  fprintf(stderr, "       -d <duration in seconds> <local path> <remote path>\n");
  fprintf(stderr, "(-q means the program will not ask for missing information.)\n");
}

static char *prompt (const char *prompt, char *buffer, size_t bufsz, int required)
{
  char *cp, *bp;
  
  while (1) {
    fprintf(stdout, "%s> ", prompt);
    if ( fgets(buffer, bufsz, stdin) == NULL ) {
      if (ferror(stdin)) {
	perror("fgets");
      } else {
	fprintf(stderr, "EOF on stdin\n");
      }
      return NULL;
    }
    
    cp = strrchr(buffer, '\n');
    if ( cp != NULL ) *cp = '\0';
    
    bp = buffer;
    while ( bp != cp ) {
      if ( *bp != ' ' && *bp != '\t' ) return bp;
      bp++;
    }
    
    if (! required) return bp;
  }
}

static int add_track_to_album(LIBMTP_album_t *albuminfo, LIBMTP_track_t *trackmeta)
{
  LIBMTP_album_t *album;
  LIBMTP_album_t *found_album = NULL;
  int ret;

  /* Look for the album */
  album = LIBMTP_Get_Album_List(device);
  while(album != NULL) {
    if (!strcmp(album->name, albuminfo->name) &&
	!strcmp(album->artist, albuminfo->artist)) {
      /* Disconnect this album for later use */
      found_album = album;
      album = album->next;
      found_album->next = NULL;
    } else {
      LIBMTP_album_t *tmp;

      tmp = album;
      album = album->next;
      LIBMTP_destroy_album_t(tmp);
    }
  }
  
  if (found_album != NULL) {
    uint32_t *tracks;

    tracks = (uint32_t *)malloc((found_album->no_tracks+1) * sizeof(uint32_t));
    printf("Album \"%s\" found: updating...\n", found_album->name);
    if (!tracks) {
      printf("failed malloc in add_track_to_album()\n");
      return 1;
    }
    found_album->no_tracks++;
    if (found_album->tracks != NULL) {
      memcpy(tracks, found_album->tracks, found_album->no_tracks * sizeof(uint32_t));
      free(found_album->tracks);
    }
    found_album->tracks = tracks;
    tracks[found_album->no_tracks-1] = trackmeta->item_id;
    ret = LIBMTP_Update_Album(device, found_album);
    LIBMTP_destroy_album_t(found_album);
  } else {
    uint32_t *trackid;
    
    trackid = (uint32_t *)malloc(sizeof(uint32_t));
    *trackid = trackmeta->item_id;
    albuminfo->tracks = trackid;
    albuminfo->no_tracks = 1;
    printf("Album doesn't exist: creating...\n");
    ret = LIBMTP_Create_New_Album(device, albuminfo, 0);
    /* albuminfo will be destroyed later by caller */
  }
  
  if (ret != 0) {
    printf("Error creating or updating album.\n");
    LIBMTP_Dump_Errorstack(device);
    LIBMTP_Clear_Errorstack(device);
  } else {
    printf("success!\n");
  }
}

int sendtrack_function(char * from_path, char * to_path, char *partist, char *ptitle, char *pgenre, char *palbum, uint16_t tracknum, uint16_t length, uint16_t year)
{
  char *filename, *parent;
  char artist[80], title[80], genre[80], album[80];
  char num[80];
  uint64_t filesize;
  uint32_t parent_id = 0;
#ifdef __USE_LARGEFILE64
  struct stat64 sb;
#else
  struct stat sb;
#endif
  LIBMTP_track_t *trackmeta;
  LIBMTP_album_t *albuminfo;
  int ret;
{'fraction_non_alphanumeric': 0.13496089621644472, 'fraction_numerical': 0.02198266751215388, 'mean_word_length': 3.448989186647861, 'pattern_counts': {'":': 0, '<': 60, '<?xml version=': 0, '>': 98, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7440596', 'n_tokens_mistral': 3501, 'n_tokens_neox': 3235, 'n_words': 984}