Use singly linked list to store profile commands.
This commit is contained in:
		@@ -23,6 +23,7 @@
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <fatfs/ff.h>
 | 
			
		||||
#include <linklist-lib/singly-linked-list.h>
 | 
			
		||||
 | 
			
		||||
struct pl_command_list_map {
 | 
			
		||||
	enum pl_command_type command;
 | 
			
		||||
@@ -155,9 +156,20 @@ static int parse_line(char *line, struct pl_command *cmd)
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static SlList *copy_and_append_command_to_list(SlList *list, const struct pl_command *cmd)
 | 
			
		||||
{
 | 
			
		||||
	struct pl_command *alloced_cmd;
 | 
			
		||||
 | 
			
		||||
	alloced_cmd = (struct pl_command *)malloc(sizeof(struct pl_command));
 | 
			
		||||
	memcpy(alloced_cmd, cmd, sizeof(struct pl_command));
 | 
			
		||||
	list = sl_list_append(list, alloced_cmd);
 | 
			
		||||
 | 
			
		||||
	return list;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
enum pl_ret_val temp_profile_parse_from_file(const char *filename,
 | 
			
		||||
					     struct pl_command *cmd_list,
 | 
			
		||||
					     uint32_t cmd_list_length,
 | 
			
		||||
					     SlList **command_list,
 | 
			
		||||
					     uint32_t max_len,
 | 
			
		||||
					     uint32_t *cmds_parsed)
 | 
			
		||||
{
 | 
			
		||||
	FIL script_file;
 | 
			
		||||
@@ -165,9 +177,10 @@ enum pl_ret_val temp_profile_parse_from_file(const char *filename,
 | 
			
		||||
	int res;
 | 
			
		||||
	enum pl_ret_val ret = PL_RET_SUCCESS;
 | 
			
		||||
	char workbuff[256];
 | 
			
		||||
	struct pl_command temp_command;
 | 
			
		||||
	uint32_t cmd_idx;
 | 
			
		||||
 | 
			
		||||
	if (!filename || !cmd_list || !cmd_list_length || !cmds_parsed)
 | 
			
		||||
	if (!filename || !command_list || !max_len || !cmds_parsed)
 | 
			
		||||
		return PL_RET_PARAM_ERR;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -189,19 +202,22 @@ enum pl_ret_val temp_profile_parse_from_file(const char *filename,
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		/* Check if list already full */
 | 
			
		||||
		if (cmd_idx >= cmd_list_length) {
 | 
			
		||||
		if (cmd_idx >= max_len) {
 | 
			
		||||
			ret = PL_RET_LIST_FULL;
 | 
			
		||||
			goto exit_close;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		/* Parse the line */
 | 
			
		||||
		res = parse_line(workbuff, &cmd_list[cmd_idx]);
 | 
			
		||||
		res = parse_line(workbuff, &temp_command);
 | 
			
		||||
		if (res < 0) {
 | 
			
		||||
			ret = PL_RET_SCRIPT_ERR;
 | 
			
		||||
			goto exit_close;
 | 
			
		||||
		} else if (res == 0) {
 | 
			
		||||
			cmd_idx++;
 | 
			
		||||
			*cmds_parsed= cmd_idx;
 | 
			
		||||
			*cmds_parsed = cmd_idx;
 | 
			
		||||
 | 
			
		||||
			/* Append the temp_command to the list */
 | 
			
		||||
			*command_list = copy_and_append_command_to_list(*command_list, &temp_command);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -212,3 +228,15 @@ exit_close:
 | 
			
		||||
exit:
 | 
			
		||||
	return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void delete_pl_command(void *cmd)
 | 
			
		||||
{
 | 
			
		||||
	if (cmd)
 | 
			
		||||
		free(cmd);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void temp_profile_free_command_list(SlList **list)
 | 
			
		||||
{
 | 
			
		||||
	sl_list_free_full(*list, delete_pl_command);
 | 
			
		||||
	*list = NULL;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user