<commit_before>/*
Copyright (c) 2008-2009
	<NAME> <<EMAIL>>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

/* everything that interacts with the user */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <readline/readline.h>

#include "ui.h"

/*	output message and flush stdout
 *	@param message
 */
inline void BarUiMsg (const char *msg) {
	printf ("%s", msg);
	fflush (stdout);
}

/*	prints human readable status message based on return value
 *	@param piano return value
 */
inline PianoReturn_t BarUiPrintPianoStatus (PianoReturn_t ret) {
	if (ret != PIANO_RET_OK) {
		printf ("Error: %s\n", PianoErrorToStr (ret));
	} else {
		printf ("Ok.\n");
	}
	return ret;
}

/*	check if all characters of string are numeric
 *	@param the string
 *	@return 1 = yes, 0 = not numeric
 */
char BarIsNumericStr (const char *str) {
	while (*str != '\0') {
		if (isdigit (*str) == 0) {
			return 0;
		}
		str++;
	}
	return 1;
}

/*	use readline to get integer value
 *	@param prompt or NULL
 *	@param returns integer
 *	@return 1 = success, 0 = failure (not an integer, ...)
 */
char BarReadlineInt (const char *prompt, int *retVal) {
	char *buf;
	char ret = 0;

	if ((buf = readline (prompt)) != NULL && strlen (buf) > 0 &&
			BarIsNumericStr (buf)) {
		*retVal = atoi (buf);
		ret = 1;
	}
	if (buf != NULL) {
		free (buf);
	}
	return ret;
}

/*	sort linked list (station)
 *	@param stations
 *	@return NULL-terminated array with sorted stations
 */
PianoStation_t **BarSortedStations (PianoStation_t *unsortedStations) {
	PianoStation_t *currStation, **sortedStations, **currSortedStation;
	PianoStation_t *oldStation, *veryOldStation;
	size_t unsortedStationsN = 0;
	char inserted;

	/* get size */
	currStation = unsortedStations;
	while (currStation != NULL) {
		unsortedStationsN++;
		currStation = currStation->next;
	}
	sortedStations = calloc (unsortedStationsN+1, sizeof (*sortedStations));

	currStation = unsortedStations;
	while (currStation != NULL) {
		currSortedStation = sortedStations;
		inserted = 0;
		while (*currSortedStation != NULL && !inserted) {
			/* item has to be inserted _before_ current item? */
			/* FIXME: this doesn't handle multibyte chars correctly */
			if (strcasecmp (currStation->name,
					(*currSortedStation)->name) < 0) {
				oldStation = *currSortedStation;
				*currSortedStation = currStation;
				currSortedStation++;
				/* move items */
				while (*currSortedStation != NULL) {
					veryOldStation = *currSortedStation;
					*currSortedStation = oldStation;
					oldStation = veryOldStation;
					currSortedStation++;
				}
				/* append last item */
				if (oldStation != NULL) {
					*currSortedStation = oldStation;
				}
				inserted = 1;
			}
			currSortedStation++;
		}
		/* item could not be inserted: append */
		if (!inserted) {
			*currSortedStation = currStation;
		}
		currStation = currStation->next;
	}
	return sortedStations;
}

/*	let user pick one station
 *	@param piano handle
 *	@return pointer to selected station or NULL
 */
PianoStation_t *BarUiSelectStation (PianoHandle_t *ph, const char *prompt) {
	PianoStation_t **ss = NULL, **ssCurr = NULL, *retStation;
	int i = 0;

	/* sort and print stations */
	ss = BarSortedStations (ph->stations);
	ssCurr = ss;
	while (*ssCurr != NULL) {
		printf ("%2i) %c%c%c %s\n", i,
				(*ssCurr)->useQuickMix ? 'q' : ' ',
				(*ssCurr)->isQuickMix ? 'Q' : ' ',
				!(*ssCurr)->isCreator ? 'S' : ' ',
				(*ssCurr)->name);
		ssCurr++;
		i++;
	}

	if (!BarReadlineInt (prompt, &i)) {
		return NULL;
	}
	ssCurr = ss;
	while (*ssCurr != NULL && i > 0) {<commit_msg>Fix memory leak in station selector<commit_after>/*
Copyright (c) 2008-2009
	<NAME> <<EMAIL>>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

/* everything that interacts with the user */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <readline/readline.h>

#include "ui.h"

/*	output message and flush stdout
 *	@param message
 */
inline void BarUiMsg (const char *msg) {
	printf ("%s", msg);
	fflush (stdout);
}

/*	prints human readable status message based on return value
 *	@param piano return value
 */
inline PianoReturn_t BarUiPrintPianoStatus (PianoReturn_t ret) {
	if (ret != PIANO_RET_OK) {
		printf ("Error: %s\n", PianoErrorToStr (ret));
	} else {
		printf ("Ok.\n");
	}
	return ret;
}

/*	check if all characters of string are numeric
 *	@param the string
 *	@return 1 = yes, 0 = not numeric
 */
char BarIsNumericStr (const char *str) {
	while (*str != '\0') {
		if (isdigit (*str) == 0) {
			return 0;
		}
		str++;
	}
	return 1;
}

/*	use readline to get integer value
 *	@param prompt or NULL
 *	@param returns integer
 *	@return 1 = success, 0 = failure (not an integer, ...)
 */
char BarReadlineInt (const char *prompt, int *retVal) {
	char *buf;
	char ret = 0;

	if ((buf = readline (prompt)) != NULL && strlen (buf) > 0 &&
			BarIsNumericStr (buf)) {
		*retVal = atoi (buf);
		ret = 1;
	}
	if (buf != NULL) {
		free (buf);
	}
	return ret;
}

/*	sort linked list (station)
 *	@param stations
 *	@return NULL-terminated array with sorted stations
 */
PianoStation_t **BarSortedStations (PianoStation_t *unsortedStations) {
	PianoStation_t *currStation, **sortedStations, **currSortedStation;
	PianoStation_t *oldStation, *veryOldStation;
	size_t unsortedStationsN = 0;
	char inserted;

	/* get size */
	currStation = unsortedStations;
	while (currStation != NULL) {
		unsortedStationsN++;
		currStation = currStation->next;
	}
	sortedStations = calloc (unsortedStationsN+1, sizeof (*sortedStations));

	currStation = unsortedStations;
	while (currStation != NULL) {
		currSortedStation = sortedStations;
		inserted = 0;
		while (*currSortedStation != NULL && !inserted) {
			/* item has to be inserted _before_ current item? */
			/* FIXME: this doesn't handle multibyte chars correctly */
			if (strcasecmp (currStation->name,
					(*currSortedStation)->name) < 0) {
				oldStation = *currSortedStation;
				*currSortedStation = currStation;
				currSortedStation++;
				/* move items */
				while (*currSortedStation != NULL) {
					veryOldStation = *currSortedStation;
					*currSortedStation = oldStation;
					oldStation = veryOldStation;
					currSortedStation++;
				}
				/* append last item */
				if (oldStation != NULL) {
					*currSortedStation = oldStation;
				}
				inserted = 1;
			}
			currSortedStation++;
		}
		/* item could not be inserted: append */
		if (!inserted) {
			*currSortedStation = currStation;
		}
		currStation = currStation->next;
	}
	return sortedStations;
}

/*	let user pick one station
 *	@param piano handle
 *	@return pointer to selected station or NULL
 */
PianoStation_t *BarUiSelectStation (PianoHandle_t *ph, const char *prompt) {
	PianoStation_t **ss = NULL, **ssCurr = NULL, *retStation;
	int i = 0;

	/* sort and print stations */
	ss = BarSortedStations (ph->stations);
	ssCurr = ss;
	while (*ssCurr != NULL) {
		printf ("%2i) %c%c%c %s\n", i,
				(*ssCurr)->useQuickMix ? 'q' : ' ',
				(*ssCurr)->isQuickMix ? 'Q' : ' ',
				!(*ssCurr)->isCreator ? 'S' : ' ',
				(*ssCurr)->name);
		ssCurr++;
		i++;
	}

	if (!BarReadlineInt (prompt, &i)) {
		free (ss);
		return NULL;
	}
	ssCurr = ss;
	while (*ssCurr != NULL && i > 0) {
{'fraction_non_alphanumeric': 0.11954748177961493, 'fraction_numerical': 0.005874034591537039, 'mean_word_length': 3.84404636459431, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 41, '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': '7496040', 'n_tokens_mistral': 3303, 'n_tokens_neox': 2981, 'n_words': 1031}