alistair
5 years ago
5 changed files with 262 additions and 87 deletions
@ -0,0 +1,32 @@
@@ -0,0 +1,32 @@
|
||||
|
||||
#include <stdlib.h> |
||||
#include <stdio.h> |
||||
#include <stdbool.h> |
||||
#include <sys/wait.h> |
||||
#include <unistd.h> |
||||
#include <string.h> |
||||
|
||||
|
||||
#ifndef MAINH |
||||
#define MAINH |
||||
|
||||
#define INITIAL_LINE_BUFFER 80 |
||||
#define INITIAL_WORD_SIZE 80 |
||||
#define INITIAL_NUM_ALIASES 50 |
||||
|
||||
#define NUM_BUILTINS 2 |
||||
|
||||
struct Alias { |
||||
char *name; |
||||
char *substitution; |
||||
}; |
||||
|
||||
struct State { |
||||
struct Alias* aliases; |
||||
int num_aliases; |
||||
}; |
||||
|
||||
typedef struct State State; |
||||
typedef struct Alias Alias; |
||||
|
||||
#endif |
@ -0,0 +1,113 @@
@@ -0,0 +1,113 @@
|
||||
#include "util.h" |
||||
|
||||
int readline(FILE* in, char **out) { |
||||
int size_of_buffer = INITIAL_LINE_BUFFER; |
||||
char *buffer = calloc(size_of_buffer, sizeof(char)); |
||||
char character; |
||||
int count = 0; |
||||
|
||||
while (true) { |
||||
|
||||
character = fgetc(in);
|
||||
if (character == EOF) { |
||||
buffer[count] = '\0'; |
||||
*out = buffer; |
||||
return EOF; |
||||
} |
||||
if (character == '\n') { |
||||
buffer[count] = '\0'; |
||||
*out = buffer; |
||||
return ER_SUCCESS; |
||||
} else { |
||||
if (count == (size_of_buffer - 3)) { |
||||
buffer = reallocarray(buffer, (size_of_buffer *= 2),
|
||||
sizeof(char)); |
||||
|
||||
if (!buffer) { |
||||
return ER_ALLOC; |
||||
} |
||||
} |
||||
|
||||
buffer[count] = character; |
||||
count++; |
||||
} |
||||
} |
||||
} |
||||
|
||||
/* Join an array of strings together using the delim string as the joiner. */ |
||||
int join_stirng(char **joined, int arr_len, char *sep_stirng[], char *delim) { |
||||
int length = 0; |
||||
int delim_len = strlen(delim); |
||||
|
||||
char *new_string = calloc(INITIAL_LINE_BUFFER, sizeof(char)); |
||||
int stringsize = INITIAL_LINE_BUFFER; |
||||
|
||||
for (int i = 0; i < arr_len; i++) { |
||||
if (length == stringsize - 2) { |
||||
new_string = reallocarray(new_string, stringsize *= 2, sizeof(char)); |
||||
} |
||||
|
||||
int len = strlen(sep_stirng[i]); |
||||
for (int k = 0; k < len; k++) { |
||||
new_string[length + k] = sep_stirng[i][k]; |
||||
} |
||||
|
||||
length += len; |
||||
|
||||
if (sep_stirng[i+1] == 0) { |
||||
*joined = new_string; |
||||
return ER_SUCCESS; |
||||
} else { |
||||
for (int k = 0; k < delim_len; k++) { |
||||
new_string[length + k] = delim[k]; |
||||
} |
||||
length += delim_len; |
||||
} |
||||
} |
||||
|
||||
return ER_FAILURE; |
||||
} |
||||
|
||||
/* Take a string and split it into an array of strings, separate by the delim
|
||||
* character. |
||||
*/ |
||||
int split_string(char **sep_string[], int *num, char *string, char delim) { |
||||
char **word_list = calloc(10, sizeof(char*)); |
||||
int word_size; |
||||
|
||||
char *word = calloc((word_size=INITIAL_WORD_SIZE), sizeof(char)); |
||||
int num_words = 0; |
||||
int word_length = 0; |
||||
|
||||
for (int i = 0; true; i++) { |
||||
if (string[i] == '\0') { |
||||
if (word_length) { |
||||
word_list[num_words] = word; |
||||
num_words++; |
||||
} |
||||
|
||||
*sep_string = word_list; |
||||
*num = num_words; |
||||
return ER_SUCCESS; |
||||
} |
||||
|
||||
if (string[i] == delim) { |
||||
if (word_length) { |
||||
word_list[num_words] = word; |
||||
|
||||
num_words++; |
||||
word_length = 0; |
||||
word = calloc((word_size=INITIAL_WORD_SIZE), sizeof(char)); |
||||
} |
||||
} else { |
||||
word[word_length] = string[i]; |
||||
word_length++; |
||||
|
||||
if (word_length == word_size - 2) { |
||||
word = reallocarray(word, word_size *= 2, sizeof(char)); |
||||
} |
||||
}
|
||||
} |
||||
|
||||
return ER_FAILURE; |
||||
} |
@ -1,7 +1,13 @@
@@ -1,7 +1,13 @@
|
||||
#include "main.h" |
||||
#include "error.h" |
||||
|
||||
#ifndef UTILH |
||||
#define UTILH |
||||
|
||||
int readline(FILE* in, char **out);
|
||||
|
||||
int split_string(char **sep_string[], int *num, char *string, char delim);
|
||||
|
||||
int join_stirng(char **joined, int arr_len, char *sep_stirng[], char *delim);
|
||||
|
||||
#endif |
||||
|
Loading…
Reference in new issue