0001 #include <cgreen/parameters.h>
0002 #include <cgreen/vector.h>
0003 #include <stdlib.h>
0004 #include <string.h>
0005 #include <ctype.h>
0006
0007 #ifdef __cplusplus
0008 namespace cgreen {
0009 #endif
0010 #ifdef _MSC_VER
0011
0012
0013 #pragma warning(disable:4996)
0014 #endif
0015
0016 static char *tokenise_by_commas_and_whitespace(char *list);
0017 static char *skip_nulls_until(char *pointer, char *pointer_end);
0018 static char *end_of_token(char *token);
0019 static char *strip_box_double(char *token);
0020 static char *strip_d_macro(char *token);
0021
0022 static char *stringdup(const char *string) {
0023 return strcpy((char *)malloc(strlen(string)+1), string);
0024 }
0025
0026 CgreenVector *create_vector_of_names(const char *parameters) {
0027 char *parameters_to_tokenize;
0028 char *parameters_end;
0029 char *tokens;
0030 char *token;
0031 CgreenVector *names = create_cgreen_vector(&free);
0032 if ((parameters == NULL) || (strlen(parameters) == 0)) {
0033 return names;
0034 }
0035
0036 parameters_to_tokenize = stringdup(parameters);
0037 if (parameters_to_tokenize == NULL) {
0038 return names;
0039 }
0040
0041 parameters_end = parameters_to_tokenize + strlen(parameters_to_tokenize);
0042 tokens = tokenise_by_commas_and_whitespace(parameters_to_tokenize);
0043 token = tokens;
0044 while (token < tokens + strlen(parameters)) {
0045 token = strip_d_macro(strip_box_double(skip_nulls_until(token, parameters_end)));
0046 cgreen_vector_add(names, (void*)stringdup(token));
0047 token = end_of_token(token);
0048 }
0049
0050 free(tokens);
0051 return names;
0052 }
0053
0054 static char *tokenise_by_commas_and_whitespace(char *list) {
0055 size_t i, length;
0056
0057 for (i = 0, length = strlen(list); i < length; i++) {
0058 if (isspace((int)list[i]) || list[i] == ',') {
0059 list[i] = '\0';
0060 }
0061 }
0062
0063 return list;
0064 }
0065
0066 static char *skip_nulls_until(char *pointer, char *pointer_end) {
0067 while (*pointer == '\0' && pointer < pointer_end) {
0068 pointer++;
0069 }
0070
0071 return pointer;
0072 }
0073
0074 static char *end_of_token(char *token) {
0075 return token + strlen(token);
0076 }
0077
0078 static char *strip_box_double(char *token) {
0079 if ((strncmp("box_double(", token, 11) == 0) && (*(end_of_token(token) - 1) == ')')) {
0080 memmove(token, token + 11, strlen(token) - 11 + 1);
0081 *(end_of_token(token) - 1) = '\0';
0082 }
0083
0084 return token;
0085 }
0086
0087 static char *strip_d_macro(char *token) {
0088 if ((strncmp("d(", token, 2) == 0) && (*(end_of_token(token) - 1) == ')')) {
0089 memmove(token, token + 2, strlen(token) - 2 + 1);
0090 *(end_of_token(token) - 1) = '\0';
0091 }
0092
0093 return token;
0094 }
0095
0096 #ifdef __cplusplus
0097 }
0098 #endif
0099
0100