-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.c
216 lines (201 loc) · 4.05 KB
/
helpers.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include "helpers.h"
void removeComment(char *s1){
//remove comment by replacing '#' with '\0'
if (s1 == NULL){
return;
}
int i = 0;
int newLength = strlen(s1);
for (; i < strlen(s1);i++) {
if (s1[i]=='#') {
newLength = i;
}
}
//check if replacement is necessary
if (newLength != strlen(s1)){
char buffer[newLength+1];
int i = 0;
int j = 0;
for (; i < strlen(s1);i++) {
if (s1[i]=='#'){
buffer[j] = '\0';
strcpy(s1,buffer);
return;
} else {
buffer[j] = s1[i];
j++;
}
}
}
}
void removeWhitespace(char *s1){
//remove whitespace from a string, in place
if (s1 == NULL){
return;
}
char buffer[strlen(s1)+1];
int i = 0;
int j = 0;
for (; i < strlen(s1);i++) {
if (!isspace(s1[i])) {
buffer[j] = s1[i];
j++;
}
}
buffer[j] = '\0';
strcpy(s1,buffer);
}
char** tokenify(char *s, const char *sep)
{
//this comment is new
char *word = NULL;
//copy string
char *temp1 = strdup(s);
char *temp2 = strdup(s);
//find out how many tokens we have
int words = 0;
for (word = strtok(temp1, sep);word;word = strtok(NULL,sep)){words++;}
//allocate the array of char *'s with one additional
char **array = malloc(sizeof(char*)*(words+1));
int i = 0;
int excluded = 0;
for (word = strtok(temp2, sep); word; word = strtok(NULL, sep)) {
char *tempword = strdup(word);
removeWhitespace(tempword);
if(!(tempword[0]=='\0'&&strlen(tempword)<1)){
array[i] = strdup(word);
i++;
}
else{
excluded++;
}
free(tempword);
}
free(temp1);
free(temp2);
array[words-excluded] = NULL;
return array;
}
char ***tokenify2(char **s, const char *sep)
{
int f = 0;
//count how big our array is
while(s[f] != NULL){
f++;
}
//allocate an array to hold all of our subarrays
char ***toreturn = malloc((sizeof(char **))*(f+1));
toreturn[f] = NULL;
int i = 0;
//tokenify the substrings into arrays that go into the new array
while(s[i] != NULL){
toreturn[i] = tokenify(s[i], sep);
i++;
}
return toreturn;
}
void freeAll1(char **array){
//free all memory in a **char
int i = 0;
while(array[i]!=NULL){
free(array[i]);
i++;
}
}
void freeAll2(char ***array){
//free all memory in a ***char
int i = 0;
while(array[i]!=NULL){
int j = 0;
while(array[i][j]!=NULL){
free(array[i][j]);
j++;
}
free(array[i]);
i++;
}
}
char **readFile(char *filename){
FILE *f;
char c;
char s[1024];
int lines = 0;
//find out how big of an array to allocate
f = fopen(filename, "r");
if(f == NULL){
return NULL;
}
while((c=fgetc(f)) != EOF){
if(c == '\n'){
lines++;
}
}
fclose(f);
//allocate a properly sized array
char **toreturn = malloc(sizeof(char*)*(lines+1));
f = fopen(filename, "r");
int i = 0;
int skipped = 0;
if(f != NULL){
while(fgets(s, 1024, f) != NULL){
removeWhitespace(s);
if(!(s[0]=='\0'&&strlen(s)<1)){
toreturn[i] = strdup(s);
i++;
}
else {
skipped++;
}
}
toreturn[lines-skipped] = NULL;
fclose(f);
}
return toreturn;
}
void listInsert(pid_t pid, char *command, int state, struct node **head){
struct node *newnode = malloc(sizeof(struct node));
strncpy(newnode->command, command, 1024);
newnode->pid = pid;
newnode->state = state;
newnode->next = *head;
*head = newnode;
}
void listDelete(pid_t pid, struct node **list){
//check if first node is the one to be deleted, adjust pointers accordingly if so, and free
if ((*list)->pid == pid){
struct node *tmp = *list;
*list = (*list)->next;
free(tmp);
return;
}
struct node *tmp = *list;
//check if any of the rest of the nodes is the one we're deleting
while (tmp->next != NULL) {
struct node *tmpnext = tmp->next;
if(tmpnext->pid == pid){
tmp->next = tmpnext->next;
free(tmpnext);
return;
}
tmp = tmp->next;
}
}
void setState(pid_t pid, int state, struct node *list){
while(list != NULL){
if(list->pid == pid){
list->state = state;
}
list = list->next;
}
}
void listClear(struct node *list) {
while (list != NULL) {
struct node *tmp = list;
list = list->next;
free(tmp);
}
}