tools

various tools I have been using throughout the years
Log | Files | Refs | README | LICENSE

mpd_add_to_playlist.c (2623B)


      1 #define _POSIX_C_SOURCE 200809L
      2 
      3 #include "mpd_common.h"
      4 
      5 #include <string.h>
      6 
      7 static void usage(FILE *file, const char *program) {
      8   fprintf(file, "Usage: %s [--verbose] PLAYLIST [FILE|-]\n", program);
      9 }
     10 
     11 int main(int argc, char **argv) {
     12   bool verbose = false;
     13   const char *playlist = NULL;
     14   const char *path = NULL;
     15   for (int i = 1; i < argc; i++) {
     16     if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
     17       usage(stdout, argv[0]);
     18       return 0;
     19     }
     20     if (strcmp(argv[i], "--verbose") == 0) {
     21       verbose = true;
     22     } else if (argv[i][0] == '-' && strcmp(argv[i], "-") != 0) {
     23       usage(stderr, argv[0]);
     24       return 2;
     25     } else if (playlist == NULL) {
     26       playlist = argv[i];
     27     } else if (path == NULL) {
     28       path = argv[i];
     29     } else {
     30       usage(stderr, argv[0]);
     31       return 2;
     32     }
     33   }
     34   if (playlist == NULL) {
     35     usage(stderr, argv[0]);
     36     return 2;
     37   }
     38 
     39   struct mt_strvec tracks;
     40   mt_strvec_init(&tracks);
     41   struct mpd_connection *connection = NULL;
     42   bool ok = true;
     43   size_t count = 0U;
     44 
     45   if (path == NULL) {
     46     connection = mt_connect();
     47     if (connection == NULL)
     48       return 1;
     49     struct mpd_song *song = mpd_run_current_song(connection);
     50     if (song == NULL) {
     51       if (mpd_connection_get_error(connection) == MPD_ERROR_SUCCESS)
     52         fprintf(stderr, "mpd: no current song\n");
     53       else
     54         mt_report_error(connection, "get current song");
     55       ok = false;
     56     } else {
     57       const char *uri = mpd_song_get_uri(song);
     58       ok = uri != NULL && mt_strvec_push(&tracks, uri);
     59       if (!ok)
     60         perror("mpd: store current song");
     61       mpd_song_free(song);
     62       if (ok)
     63         count = 1U;
     64     }
     65   } else {
     66     bool close_input;
     67     FILE *input = mt_open_input(path, &close_input);
     68     if (input == NULL)
     69       return 1;
     70     connection = mt_connect();
     71     if (connection == NULL)
     72       ok = false;
     73     if (ok)
     74       ok = mt_add_playlist_stream(connection, playlist, input, verbose, &count);
     75     if (close_input && fclose(input) != 0) {
     76       perror("mpd: close input");
     77       ok = false;
     78     }
     79   }
     80 
     81   if (ok && connection == NULL)
     82     connection = mt_connect();
     83   if (ok && connection == NULL)
     84     ok = false;
     85   if (ok && tracks.len > 0U)
     86     ok = mt_add_playlist(connection, playlist, &tracks, verbose);
     87   if (ok && count == 0U) {
     88     fprintf(stderr, "mpd: no tracks to add to %s\n", playlist);
     89     ok = false;
     90   }
     91   if (ok)
     92     printf("Added %zu track%s to %s.\n", count, count == 1U ? "" : "s",
     93            playlist);
     94 
     95   if (connection != NULL)
     96     mpd_connection_free(connection);
     97   mt_strvec_clear(&tracks);
     98   return ok ? 0 : 1;
     99 }