Blame src/libout123/stringlists.c

Packit c32a2d
/*
Packit c32a2d
	stringlists: creation of paired string lists for one-time consumption
Packit c32a2d
Packit c32a2d
	copyright 2015 by the mpg123 project
Packit c32a2d
	free software under the terms of the LGPL 2.1
Packit c32a2d
	see COPYING and AUTHORS files in distribution or http://mpg123.org
Packit c32a2d
	initially written by Thomas Orgis
Packit c32a2d
Packit c32a2d
	Thomas did not want to introduce a list type complete with management
Packit c32a2d
	functions just for returning driver module lists.
Packit c32a2d
*/
Packit c32a2d
Packit c32a2d
#include "compat.h"
Packit c32a2d
Packit c32a2d
/* Construction helper for paired string lists.
Packit c32a2d
   Returns 0 on success. */
Packit c32a2d
int stringlists_add( char ***alist, char ***blist
Packit c32a2d
                   , const char *atext, const char *btext, int *count)
Packit c32a2d
{
Packit c32a2d
	char *atextcopy = NULL;
Packit c32a2d
	char *btextcopy = NULL;
Packit c32a2d
	char **morealist = NULL;
Packit c32a2d
	char **moreblist = NULL;
Packit c32a2d
Packit c32a2d
	/* If one of these succeeded, the old memory is gone, so always overwrite
Packit c32a2d
	   the old pointer, worst case is wasted but not leaked memory in an
Packit c32a2d
	   out-of-memory situation. */
Packit c32a2d
	if((morealist = safe_realloc(*alist, sizeof(char*)*(*count+1))))
Packit c32a2d
		*alist = morealist;
Packit c32a2d
	if((moreblist = safe_realloc(*blist, sizeof(char*)*(*count+1))))
Packit c32a2d
		*blist = moreblist;
Packit c32a2d
	if(!morealist || !moreblist)
Packit c32a2d
		return -1;
Packit c32a2d
Packit c32a2d
	if(
Packit c32a2d
		(atextcopy = compat_strdup(atext))
Packit c32a2d
	&&	(btextcopy = compat_strdup(btext))
Packit c32a2d
	)
Packit c32a2d
	{
Packit c32a2d
		(*alist)[*count] = atextcopy;
Packit c32a2d
		(*blist)[*count] = btextcopy;
Packit c32a2d
		++*count;
Packit c32a2d
		return 0;
Packit c32a2d
	}
Packit c32a2d
	else
Packit c32a2d
	{
Packit c32a2d
		free(atextcopy);
Packit c32a2d
		return -1;
Packit c32a2d
	}
Packit c32a2d
}
Packit c32a2d