11f5207b7SJohn Levon /*
21f5207b7SJohn Levon  * Copyright (C) 2013 Oracle.
31f5207b7SJohn Levon  *
41f5207b7SJohn Levon  * This program is free software; you can redistribute it and/or
51f5207b7SJohn Levon  * modify it under the terms of the GNU General Public License
61f5207b7SJohn Levon  * as published by the Free Software Foundation; either version 2
71f5207b7SJohn Levon  * of the License, or (at your option) any later version.
81f5207b7SJohn Levon  *
91f5207b7SJohn Levon  * This program is distributed in the hope that it will be useful,
101f5207b7SJohn Levon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
111f5207b7SJohn Levon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
121f5207b7SJohn Levon  * GNU General Public License for more details.
131f5207b7SJohn Levon  *
141f5207b7SJohn Levon  * You should have received a copy of the GNU General Public License
151f5207b7SJohn Levon  * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
161f5207b7SJohn Levon  */
171f5207b7SJohn Levon 
181f5207b7SJohn Levon #include "smatch.h"
191f5207b7SJohn Levon 
list_has_string(struct string_list * str_list,const char * str)201f5207b7SJohn Levon int list_has_string(struct string_list *str_list, const char *str)
211f5207b7SJohn Levon {
221f5207b7SJohn Levon 	char *tmp;
23*efe51d0cSJohn Levon 	int cmp;
241f5207b7SJohn Levon 
251f5207b7SJohn Levon 	if (!str)
261f5207b7SJohn Levon 		return 0;
271f5207b7SJohn Levon 
281f5207b7SJohn Levon 	FOR_EACH_PTR(str_list, tmp) {
29*efe51d0cSJohn Levon 		cmp = strcmp(tmp, str);
30*efe51d0cSJohn Levon 		if (cmp < 0)
311f5207b7SJohn Levon 			continue;
32*efe51d0cSJohn Levon 		if (cmp == 0)
331f5207b7SJohn Levon 			return 1;
341f5207b7SJohn Levon 		return 0;
351f5207b7SJohn Levon 	} END_FOR_EACH_PTR(tmp);
361f5207b7SJohn Levon 	return 0;
371f5207b7SJohn Levon }
381f5207b7SJohn Levon 
insert_string(struct string_list ** str_list,const char * _new)39*efe51d0cSJohn Levon int insert_string(struct string_list **str_list, const char *_new)
401f5207b7SJohn Levon {
411f5207b7SJohn Levon 	char *new = (char *)_new;
421f5207b7SJohn Levon 	char *tmp;
43*efe51d0cSJohn Levon 	int cmp;
441f5207b7SJohn Levon 
451f5207b7SJohn Levon 	FOR_EACH_PTR(*str_list, tmp) {
46*efe51d0cSJohn Levon 		cmp = strcmp(tmp, new);
47*efe51d0cSJohn Levon 		if (cmp < 0)
481f5207b7SJohn Levon 			continue;
49*efe51d0cSJohn Levon 		else if (cmp == 0) {
50*efe51d0cSJohn Levon 			return 0;
511f5207b7SJohn Levon 		} else {
521f5207b7SJohn Levon 			INSERT_CURRENT(alloc_string(new), tmp);
53*efe51d0cSJohn Levon 			return 1;
541f5207b7SJohn Levon 		}
551f5207b7SJohn Levon 	} END_FOR_EACH_PTR(tmp);
561f5207b7SJohn Levon 	new = alloc_string(new);
571f5207b7SJohn Levon 	add_ptr_list(str_list, new);
58*efe51d0cSJohn Levon 	return 1;
591f5207b7SJohn Levon }
601f5207b7SJohn Levon 
clone_str_list(struct string_list * orig)611f5207b7SJohn Levon struct string_list *clone_str_list(struct string_list *orig)
621f5207b7SJohn Levon {
631f5207b7SJohn Levon 	char *tmp;
641f5207b7SJohn Levon 	struct string_list *ret = NULL;
651f5207b7SJohn Levon 
661f5207b7SJohn Levon 	FOR_EACH_PTR(orig, tmp) {
671f5207b7SJohn Levon 		add_ptr_list(&ret, tmp);
681f5207b7SJohn Levon 	} END_FOR_EACH_PTR(tmp);
691f5207b7SJohn Levon 	return ret;
701f5207b7SJohn Levon }
711f5207b7SJohn Levon 
combine_string_lists(struct string_list * one,struct string_list * two)721f5207b7SJohn Levon struct string_list *combine_string_lists(struct string_list *one, struct string_list *two)
731f5207b7SJohn Levon {
741f5207b7SJohn Levon 	struct string_list *ret;
751f5207b7SJohn Levon 	char *tmp;
761f5207b7SJohn Levon 
771f5207b7SJohn Levon 	ret = clone_str_list(one);
781f5207b7SJohn Levon 	FOR_EACH_PTR(two, tmp) {
791f5207b7SJohn Levon 		insert_string(&ret, tmp);
801f5207b7SJohn Levon 	} END_FOR_EACH_PTR(tmp);
811f5207b7SJohn Levon 	return ret;
821f5207b7SJohn Levon }
831f5207b7SJohn Levon 
841f5207b7SJohn Levon 
85