<commit_before>// vim:ts=8:expandtab
#include <stdio.h>
#include <string.h>
#include <mpd/client.h>

#include "i3status.h"

void print_mpd(yajl_gen json_gen, char *buffer, const char *format, const char *format_stopped) {
        const char *walk;
        char *outwalk = buffer;

        struct mpd_connection *conn;
        struct mpd_song *song;

        /* Use defaults */
        if ((conn = mpd_connection_new(NULL, 0, 0)) == NULL) {
                outwalk += sprintf(outwalk, "%s", format_stopped);
                OUTPUT_FULL_TEXT(buffer);
                return;
        }

        /* Get current song */
        song = mpd_run_current_song(conn);
        if (song == NULL) {
                outwalk += sprintf(outwalk, "%s", format_stopped);
                goto out;
        }

        walk = format;

        for (; *walk != '\0'; walk++) {
                if (*walk != '%') {
                        *(outwalk++) = *walk;
                        continue;
                }

                if (BEGINS_WITH(walk+1, "artist")) {
                        outwalk += sprintf(outwalk, "%s", mpd_song_get_tag(song, MPD_TAG_ARTIST, 0));
                        walk += strlen("artist");
                }

                if (BEGINS_WITH(walk+1, "album_artist")) {
                        outwalk += sprintf(outwalk, "%s", mpd_song_get_tag(song, MPD_TAG_ALBUM_ARTIST, 0));
                        walk += strlen("album_artist");
                } else if (BEGINS_WITH(walk+1, "album")) {
                        outwalk += sprintf(outwalk, "%s", mpd_song_get_tag(song, MPD_TAG_ALBUM, 0));
                        walk += strlen("album");
                }

                if (BEGINS_WITH(walk+1, "track")) {
                        outwalk += sprintf(outwalk, "%s", mpd_song_get_tag(song, MPD_TAG_TRACK, 0));
                        walk += strlen("track");
                }

                if (BEGINS_WITH(walk+1, "track")) {
                        outwalk += sprintf(outwalk, "%s", mpd_song_get_tag(song, MPD_TAG_TRACK, 0));
                        walk += strlen("track");
                }

                if (BEGINS_WITH(walk+1, "track")) {
                        outwalk += sprintf(outwalk, "%s", mpd_song_get_tag(song, MPD_TAG_TRACK, 0));
                        walk += strlen("track");
                }

                if (BEGINS_WITH(walk+1, "track")) {
                        outwalk += sprintf(outwalk, "%s", mpd_song_get_tag(song, MPD_TAG_TRACK, 0));
                        walk += strlen("track");
                }

                if (BEGINS_WITH(walk+1, "track")) {
                        outwalk += sprintf(outwalk, "%s", mpd_song_get_tag(song, MPD_TAG_TRACK, 0));
                        walk += strlen("track");
                }

                if (BEGINS_WITH(walk+1, "track")) {
                        outwalk += sprintf(outwalk, "%s", mpd_song_get_tag(song, MPD_TAG_TRACK, 0));
                        walk += strlen("track");
                }

                if (BEGINS_WITH(walk+1, "track")) {
                        outwalk += sprintf(outwalk, "%s", mpd_song_get_tag(song, MPD_TAG_TRACK, 0));
                        walk += strlen("track");
                }

                if (BEGINS_WITH(walk+1, "track")) {
                        outwalk += sprintf(outwalk, "%s", mpd_song_get_tag(song, MPD_TAG_TRACK, 0));
                        walk += strlen("track");
                }

                if (BEGINS_WITH(walk+1, "track")) {
                        outwalk += sprintf(outwalk, "%s", mpd_song_get_tag(song, MPD_TAG_TRACK, 0));
                        walk += strlen("track");
                }

                if (BEGINS_WITH(walk+1, "track")) {
                        outwalk += sprintf(outwalk, "%s", mpd_song_get_tag(song, MPD_TAG_TRACK, 0));
                        walk += strlen("track");
                }

                if (BEGINS_WITH(walk+1, "title")) {
                        outwalk += sprintf(outwalk, "%s", mpd_song_get_tag(song, MPD_TAG_TITLE, 0));
                        walk += strlen("title");
                }
        }

out:
        mpd_song_free(song);
        mpd_connection_free(conn);
        OUTPUT_FULL_TEXT(buffer);
        return;
}
<commit_msg>Use macro for output options<commit_after>// vim:ts=8:expandtab
#include <stdio.h>
#include <string.h>
#include <mpd/client.h>

#include "i3status.h"

#define OUTPUT_OPTION(option_name, tag_name) \
if (BEGINS_WITH(walk+1, option_name)) { \
        const char *value = mpd_song_get_tag(song, tag_name, 0); \
        if (value == NULL) { \
                *(outwalk++) = '?'; \
        } else { \
                outwalk += sprintf(outwalk, "%s", value); \
        } \
        walk += strlen(option_name); \
}

void print_mpd(yajl_gen json_gen, char *buffer, const char *format, const char *format_stopped) {
        const char *walk;
        char *outwalk = buffer;

        struct mpd_connection *conn;
        struct mpd_song *song;

        /* Use defaults */
        if ((conn = mpd_connection_new(NULL, 0, 0)) == NULL) {
                outwalk += sprintf(outwalk, "%s", format_stopped);
                OUTPUT_FULL_TEXT(buffer);
                return;
        }

        /* Get current song */
        song = mpd_run_current_song(conn);
        if (song == NULL) {
                outwalk += sprintf(outwalk, "%s", format_stopped);
                goto out;
        }

        walk = format;

        for (; *walk != '\0'; walk++) {
                if (*walk != '%') {
                        *(outwalk++) = *walk;
                        continue;
                }

                OUTPUT_OPTION("artist", MPD_TAG_ARTIST)
                OUTPUT_OPTION("album_artist", MPD_TAG_ALBUM_ARTIST)
                else OUTPUT_OPTION("album", MPD_TAG_ALBUM)
                OUTPUT_OPTION("title", MPD_TAG_TITLE)
                OUTPUT_OPTION("track", MPD_TAG_TRACK)
                OUTPUT_OPTION("name", MPD_TAG_NAME)
                OUTPUT_OPTION("genre", MPD_TAG_GENRE)
                OUTPUT_OPTION("date", MPD_TAG_DATE)
                OUTPUT_OPTION("composer", MPD_TAG_COMPOSER)
                OUTPUT_OPTION("performer", MPD_TAG_PERFORMER)
                OUTPUT_OPTION("comment", MPD_TAG_COMMENT)
                OUTPUT_OPTION("disc", MPD_TAG_DISC)
        }

out:
        mpd_song_free(song);
        mpd_connection_free(conn);
        OUTPUT_FULL_TEXT(buffer);
        return;
}

{'fraction_non_alphanumeric': 0.13182879377431905, 'fraction_numerical': 0.0062256809338521405, 'mean_word_length': 1.5369127516778522, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '58865', 'n_tokens_mistral': 2103, 'n_tokens_neox': 1936, 'n_words': 361}