This repository has been archived by the owner on Jul 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.c
161 lines (125 loc) · 4.75 KB
/
command.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright © 2018 Dimonchik0036. All rights reserved.
*/
#include "command.h"
static char *string_copy(char const *str);
static char *string_create(size_t len, size_t capacity);
static char *string_concat(char *dst,
char const *src,
size_t *dst_capacity,
size_t *dst_len);
static char *command_copy_arguments(Command const *command,
size_t offset,
char *dst,
size_t *dst_capacity,
size_t *dst_len);
static Command *command_base_copy(Command const *command);
Command *command_copy_for_job(Command const *command) {
Command *new_command = command_base_copy(command);
new_command->arguments[0] = string_copy(command->arguments[0]);
size_t arguments_len = 0;
size_t arguments_capacity = 10;
char *arguments = string_create(arguments_len, arguments_capacity);
arguments = command_copy_arguments(command, 1, arguments,
&arguments_capacity,
&arguments_len);
new_command->arguments[1] = arguments;
new_command->arguments[2] = NULL;
return new_command;
}
void command_free(Command *command) {
if (!command) {
return;
}
free(command->infile);
free(command->outfile);
int index;
for (index = 0; command->arguments[index]; ++index) {
free(command->arguments[index]);
}
free(command);
}
char *command_get_name(const Command *command) {
return command->arguments[0] ? command->arguments[0] : "";
}
char *command_get_args(const Command *command) {
return command->arguments[1] ? command->arguments[1] : "";
}
Command *command_concat(const Command *commands, size_t count) {
if (count == 0) {
return NULL;
}
size_t last_index = count - 1;
Command *new_command = command_base_copy(&commands[last_index]);
size_t command_name_len = 0;
size_t command_name_capacity = 10;
char *command_name = string_create(command_name_len, command_name_capacity);
size_t command_index;
for (command_index = 0; command_index < count; ++command_index) {
Command const *current_command = &commands[command_index];
command_name = command_copy_arguments(current_command, 0, command_name,
&command_name_capacity,
&command_name_len);
if (command_index != last_index) {
command_name = string_concat(command_name, " | ",
&command_name_capacity,
&command_name_len);
}
}
new_command->arguments[0] = command_name;
new_command->arguments[1] = NULL;
return new_command;
}
static char *string_copy(char const *str) {
if (!str) {
return NULL;
}
return strdup(str);
}
static char *string_create(size_t len, size_t capacity) {
char *new_string = malloc(capacity * sizeof(char));
check_memory(new_string);
new_string[len] = END;
return new_string;
}
static char *string_concat(char *dst,
char const *src,
size_t *dst_capacity,
size_t *dst_len) {
size_t src_len = strlen(src);
size_t result_len = *dst_len + src_len + 1;
if (result_len >= *dst_capacity) {
*dst_capacity = *dst_capacity * 2 > result_len
? *dst_capacity * 2
: result_len;
dst = realloc(dst, *dst_capacity * sizeof(char));
}
dst = strcat(dst, src);
*dst_len += src_len;
return dst;
}
static char *command_copy_arguments(Command const *command,
size_t offset,
char *dst,
size_t *dst_capacity,
size_t *dst_len) {
size_t argument_index = offset;
while (command->arguments[argument_index] != NULL) {
char *current_argument = command->arguments[argument_index];
if (argument_index != offset) {
dst = string_concat(dst, " ", dst_capacity, dst_len);
}
dst = string_concat(dst, current_argument, dst_capacity, dst_len);
++argument_index;
}
return dst;
}
static Command *command_base_copy(Command const *command) {
Command *new_command = malloc(sizeof(Command));
check_memory(new_command);
new_command->flag = command->flag;
new_command->appfile = command->appfile;
new_command->outfile = string_copy(command->outfile);
new_command->infile = string_copy(command->infile);
return new_command;
}