| 
									
										
										
										
											2021-06-05 21:26:18 -04:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Looking Glass | 
					
						
							| 
									
										
										
										
											2024-02-01 17:16:31 +11:00
										 |  |  |  * Copyright © 2017-2024 The Looking Glass Authors | 
					
						
							| 
									
										
										
										
											2021-06-05 21:26:18 -04:00
										 |  |  |  * https://looking-glass.io
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or modify it | 
					
						
							|  |  |  |  * under the terms of the GNU General Public License as published by the Free | 
					
						
							|  |  |  |  * Software Foundation; either version 2 of the License, or (at your option) | 
					
						
							|  |  |  |  * any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, but WITHOUT | 
					
						
							|  |  |  |  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | 
					
						
							|  |  |  |  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | 
					
						
							|  |  |  |  * more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License along | 
					
						
							|  |  |  |  * with this program; if not, write to the Free Software Foundation, Inc., 59 | 
					
						
							|  |  |  |  * Temple Place, Suite 330, Boston, MA 02111-1307 USA | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-05-12 16:12:12 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "common/stringlist.h"
 | 
					
						
							| 
									
										
										
										
											2021-08-27 00:32:22 -04:00
										 |  |  | #include "common/vector.h"
 | 
					
						
							| 
									
										
										
										
											2022-03-07 10:13:54 +11:00
										 |  |  | #include "common/debug.h"
 | 
					
						
							| 
									
										
										
										
											2019-05-12 16:12:12 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct StringList | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-08-27 00:32:22 -04:00
										 |  |  |   bool   owns_strings; | 
					
						
							|  |  |  |   Vector vector; | 
					
						
							| 
									
										
										
										
											2019-05-12 16:12:12 +10:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | StringList stringlist_new(bool owns_strings) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-08-15 12:18:12 -04:00
										 |  |  |   StringList sl = malloc(sizeof(*sl)); | 
					
						
							| 
									
										
										
										
											2022-03-07 10:13:54 +11:00
										 |  |  |   if (!sl) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     DEBUG_ERROR("out of memory"); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-21 11:30:05 +10:00
										 |  |  |   sl->owns_strings = owns_strings; | 
					
						
							| 
									
										
										
										
											2019-05-12 16:12:12 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-27 00:32:22 -04:00
										 |  |  |   if (!vector_create(&sl->vector, sizeof(char *), 32)) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     free(sl); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-05-12 16:12:12 +10:00
										 |  |  |   return sl; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void stringlist_free(StringList * sl) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-01-08 15:37:44 +11:00
										 |  |  |   stringlist_clear(*sl); | 
					
						
							| 
									
										
										
										
											2019-05-12 16:12:12 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-27 00:32:22 -04:00
										 |  |  |   vector_destroy(&(*sl)->vector); | 
					
						
							| 
									
										
										
										
											2019-05-12 16:12:12 +10:00
										 |  |  |   free((*sl)); | 
					
						
							|  |  |  |   *sl = NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-27 00:32:22 -04:00
										 |  |  | int stringlist_push(StringList sl, char * str) | 
					
						
							| 
									
										
										
										
											2019-05-12 16:12:12 +10:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-08-27 00:32:22 -04:00
										 |  |  |   int index = vector_size(&sl->vector); | 
					
						
							|  |  |  |   vector_push(&sl->vector, &str); | 
					
						
							| 
									
										
										
										
											2019-05-12 16:12:12 +10:00
										 |  |  |   return index; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-28 18:22:11 -04:00
										 |  |  | void stringlist_remove(StringList sl, unsigned int index) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   vector_remove(&sl->vector, index); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-12 16:12:12 +10:00
										 |  |  | unsigned int stringlist_count(StringList sl) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-08-27 00:32:22 -04:00
										 |  |  |   return vector_size(&sl->vector); | 
					
						
							| 
									
										
										
										
											2019-05-12 16:12:12 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | char * stringlist_at(StringList sl, unsigned int index) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-08-27 00:32:22 -04:00
										 |  |  |   if (index >= vector_size(&sl->vector)) | 
					
						
							| 
									
										
										
										
											2019-05-12 16:12:12 +10:00
										 |  |  |     return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-27 00:32:22 -04:00
										 |  |  |   char * ptr; | 
					
						
							|  |  |  |   vector_at(&sl->vector, index, &ptr); | 
					
						
							|  |  |  |   return ptr; | 
					
						
							| 
									
										
										
										
											2021-08-15 12:18:12 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2022-01-08 15:37:44 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  | void stringlist_clear(StringList sl) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (sl->owns_strings) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     char * ptr; | 
					
						
							|  |  |  |     vector_forEach(ptr, &sl->vector) | 
					
						
							|  |  |  |       free(ptr); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   vector_clear(&sl->vector); | 
					
						
							|  |  |  | } |