mirror of
				https://github.com/asterisk/asterisk.git
				synced 2025-10-31 02:37:10 +00:00 
			
		
		
		
	strings/json: Add string delimter match, and object create with vars methods
Add a function to check if there is an exact match a one string between delimiters in another string. Add a function that will create an ast_json object out of a list of Asterisk variables. An excludes string can also optionally be passed in. Also, add a macro to make it easier to get object integers. Change-Id: I5f34f18e102126aef3997f19a553a266d70d6226
This commit is contained in:
		
				
					committed by
					
						 Friendly Automation
						Friendly Automation
					
				
			
			
				
	
			
			
			
						parent
						
							ba3f6c0b1e
						
					
				
				
					commit
					ed384e652c
				
			
							
								
								
									
										19
									
								
								main/json.c
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								main/json.c
									
									
									
									
									
								
							| @@ -852,3 +852,22 @@ struct ast_json *ast_json_channel_vars(struct varshead *channelvars) | ||||
|  | ||||
| 	return ret; | ||||
| } | ||||
|  | ||||
| struct ast_json *ast_json_object_create_vars(const struct ast_variable *variables, const char *excludes) | ||||
| { | ||||
| 	const struct ast_variable *i; | ||||
| 	struct ast_json *obj; | ||||
|  | ||||
| 	obj = ast_json_object_create(); | ||||
| 	if (!obj) { | ||||
| 		return NULL; | ||||
| 	} | ||||
|  | ||||
| 	for (i = variables; i; i = i->next) { | ||||
| 		if (!excludes || !ast_in_delimited_string(i->name, excludes, ',')) { | ||||
| 			ast_json_object_set(obj, i->name, ast_json_string_create(i->value)); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return obj; | ||||
| } | ||||
|   | ||||
| @@ -430,3 +430,28 @@ int ast_vector_string_split(struct ast_vector_string *dest, | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| int ast_in_delimited_string(const char *needle, const char *haystack, char delim) | ||||
| { | ||||
| 	const char *end; | ||||
| 	unsigned long needle_size; | ||||
|  | ||||
| 	ast_assert(haystack != NULL); | ||||
|  | ||||
| 	if (!needle) { | ||||
| 		return 0; | ||||
| 	} | ||||
|  | ||||
| 	needle_size = strlen(needle); | ||||
| 	haystack = ast_skip_blanks(haystack); | ||||
|  | ||||
| 	while ((end = strchr(haystack, delim))) { | ||||
| 		if (needle_size == end - haystack && !strncmp(haystack, needle, needle_size)) { | ||||
| 			return 1; | ||||
| 		} | ||||
| 		haystack = ast_skip_blanks(end + 1); | ||||
| 	} | ||||
|  | ||||
| 	return strcmp(haystack, needle) ? 0 : -1; | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user