c-ipfs/commands/command.c

36 lines
1.0 KiB
C
Raw Permalink Normal View History

2016-10-27 18:11:34 +00:00
#include <stdlib.h>
2016-11-07 21:29:30 +00:00
#include "ipfs/commands/command.h"
2016-10-27 18:11:34 +00:00
int commands_command_init(struct Command* cmd) {
2016-10-27 18:11:34 +00:00
// allocate memory for Argument array
cmd->arguments = malloc(cmd->argument_count * sizeof(struct Argument*));
if (cmd->arguments == NULL)
return 0;
// allocate memory for each argument
for(int i = 0; i < cmd->argument_count; i++)
cmd->arguments[i] = malloc(sizeof(struct Argument));
2016-10-27 18:11:34 +00:00
// allocate memory for CommandOption array
cmd->options = malloc(cmd->option_count * sizeof(struct CommandOption*));
if (cmd->options == NULL)
return 0;
// allocate memory for each CommandOption
for(int i = 0; i < cmd->option_count; i++)
cmd->options[i] = malloc(sizeof(struct CommandOption));
return 1;
}
int commands_command_free(struct Command* cmd) {
// arguments
2016-10-27 18:11:34 +00:00
for(int i = 0; i < cmd->argument_count; i++)
commands_argument_free(cmd->arguments[i]);
2016-10-27 18:11:34 +00:00
free(cmd->arguments);
//command options
2016-10-27 18:11:34 +00:00
for(int i = 0; i < cmd->option_count; i++)
commands_command_option_free(cmd->options[i]);
2016-10-27 18:11:34 +00:00
free(cmd->options);
return 0;
}