| 1 | #include <cgreen/vector.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <stdbool.h> |
| 4 | #include <string.h> |
| 5 | #include <ctype.h> |
| 6 | |
| 7 | #include "parameters.h" |
| 8 | |
| 9 | static char *tokenise_by_commas_and_whitespace(char *list); |
| 10 | static char *skip_nulls_until(char *pointer, char *pointer_end); |
| 11 | static char *end_of_token(char *token); |
| 12 | static char *strip_function_from(char *token, const char *function_name); |
| 13 | static bool begins_with(char *token, const char *beginning); |
| 14 | |
| 15 | static char *stringdup(const char *string) { |
| 16 | return strcpy((char *)malloc(strlen(string)+1), string); |
| 17 | } |
| 18 | |
| 19 | CgreenVector *create_vector_of_names(const char *parameters) { |
| 20 | char *parameters_to_tokenize; |
| 21 | char *parameters_end; |
| 22 | char *tokens; |
| 23 | char *token; |
| 24 | CgreenVector *names = create_cgreen_vector(&free); |
| 25 | if ((parameters == NULL) || (strlen(parameters) == 0)) { |
| 26 | return names; |
| 27 | } |
| 28 | |
| 29 | parameters_to_tokenize = stringdup(parameters); |
| 30 | if (parameters_to_tokenize == NULL) { |
| 31 | return names; |
| 32 | } |
| 33 | |
| 34 | parameters_end = parameters_to_tokenize + strlen(parameters_to_tokenize); |
| 35 | tokens = tokenise_by_commas_and_whitespace(parameters_to_tokenize); |
| 36 | token = tokens; |
| 37 | while (token < tokens + strlen(parameters)) { |
| 38 | token = skip_nulls_until(token, parameters_end); |
| 39 | int length_of_token = strlen(token); |
| 40 | token = strip_function_from(token, "box_double"); |
| 41 | token = strip_function_from(token, "d"); |
| 42 | cgreen_vector_add(names, (void*)stringdup(token)); |
| 43 | token += length_of_token; |
| 44 | } |
| 45 | |
| 46 | free(tokens); |
| 47 | return names; |
| 48 | } |
| 49 | |
| 50 | static bool *pointer_to_bool(bool value) { |
| 51 | bool *ptr = (bool *)malloc(sizeof(value)); |
| 52 | *ptr = value; |
| 53 | return ptr; |
| 54 | } |
| 55 | |
| 56 | /* TODO: Much duplication ahead. This should probably be folded into |
| 57 | the 'create_vector_of_names()' above */ |
| 58 | CgreenVector *create_vector_of_double_markers_for(const char *parameters) { |
| 59 | char *parameters_to_tokenize; |
| 60 | char *parameters_end; |
| 61 | char *tokens; |
| 62 | char *token; |
| 63 | CgreenVector *markers = create_cgreen_vector(&free); |
| 64 | if ((parameters == NULL) || (strlen(parameters) == 0)) { |
| 65 | return markers; |
| 66 | } |
| 67 | |
| 68 | parameters_to_tokenize = stringdup(parameters); |
| 69 | if (parameters_to_tokenize == NULL) { |
| 70 | return markers; |
| 71 | } |
| 72 | |
| 73 | parameters_end = parameters_to_tokenize + strlen(parameters_to_tokenize); |
| 74 | tokens = tokenise_by_commas_and_whitespace(parameters_to_tokenize); |
| 75 | token = tokens; |
| 76 | while (token < tokens + strlen(parameters)) { |
| 77 | token = skip_nulls_until(token, parameters_end); |
| 78 | int length_of_token = strlen(token); |
| 79 | if (begins_with(token, "box_double")) |
| 80 | cgreen_vector_add(markers, pointer_to_bool(true)); |
| 81 | else |
| 82 | cgreen_vector_add(markers, pointer_to_bool(false)); |
| 83 | token = strip_function_from(token, "box_double"); |
| 84 | token = strip_function_from(token, "d"); |
| 85 | token += length_of_token; |
| 86 | } |
| 87 | |
| 88 | free(tokens); |
| 89 | return markers; |
| 90 | } |
| 91 | |
| 92 | static char *tokenise_by_commas_and_whitespace(char *list) { |
| 93 | size_t i, length; |
| 94 | |
| 95 | for (i = 0, length = strlen(list); i < length; i++) { |
| 96 | if (isspace((int)list[i]) || list[i] == ',') { |
| 97 | list[i] = '\0'; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return list; |
| 102 | } |
| 103 | |
| 104 | static char *skip_nulls_until(char *pointer, char *pointer_end) { |
| 105 | while (*pointer == '\0' && pointer < pointer_end) { |
| 106 | pointer++; |
| 107 | } |
| 108 | |
| 109 | return pointer; |
| 110 | } |
| 111 | |
| 112 | static char *end_of_token(char *token) { |
| 113 | return token + strlen(token); |
| 114 | } |
| 115 | |
| 116 | static char last_char_of(char *token) { |
| 117 | return *(end_of_token(token) - 1); |
| 118 | } |
| 119 | |
| 120 | static bool begins_with(char *token, const char *beginning) { |
| 121 | return strncmp(beginning, token, strlen(beginning)) == 0; |
| 122 | } |
| 123 | |
| 124 | static void move_parameter_to_beginning_of(char *token, const char *function_name, int parameter_length) { |
| 125 | memmove(token, token + strlen(function_name) + strlen("("), parameter_length+strlen(")")+1); |
| 126 | *(end_of_token(token) - 1) = '\0'; |
| 127 | } |
| 128 | |
| 129 | static char *strip_function_from(char *token, const char *function_name) { |
| 130 | if (begins_with(token, function_name) && token[strlen(function_name)] == '(' && last_char_of(token) == ')') { |
| 131 | int parameter_length = strlen(token) - strlen(function_name) - strlen("()"); |
| 132 | move_parameter_to_beginning_of(token, function_name, parameter_length); |
| 133 | } |
| 134 | |
| 135 | return token; |
| 136 | } |
| 137 | |
| 138 | /* vim: set ts=4 sw=4 et cindent: */ |