|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +/** |
| 6 | + * _____ _____ ___ ______ _____ |
| 7 | + * / ___|| _ |/ _ \ | ___ \ ___| |
| 8 | + * \ `--. | | | / /_\ \| |_/ / |__ |
| 9 | + * `--. \| | | | _ || /| __| |
| 10 | + * /\__/ /\ \_/ / | | || |\ \| |___ |
| 11 | + * \____/ \___/\_| |_/\_| \_\____/ |
| 12 | + * |
| 13 | + * Antoine LANDRIEUX (MIT License) <Custom.c> |
| 14 | + * <https://github.com/AntoineLandrieux/SOARE/> |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +#include <SOARE/SOARE.h> |
| 19 | + |
| 20 | +/* |
| 21 | +
|
| 22 | +============================================================== |
| 23 | +Example: Custom Function - Add Numbers with Variable Arguments |
| 24 | +============================================================== |
| 25 | +
|
| 26 | +This example demonstrates how to implement a custom function |
| 27 | +that adds together an unknown number of integer arguments. |
| 28 | +
|
| 29 | +---------------------------------------------------------- |
| 30 | +Function Name: |
| 31 | + char *int_add(soare_arguments_list args) |
| 32 | +
|
| 33 | +Arguments: |
| 34 | + - args: A linked list of arguments passed to the function. |
| 35 | + Use soare_getarg(args, i) to retrieve the i-th argument |
| 36 | + as a string. |
| 37 | +
|
| 38 | +Return Value: |
| 39 | + - Returns NULL (no value returned to SOARE). |
| 40 | + - If you want to return a value, allocate memory for the |
| 41 | + result string before returning. |
| 42 | +
|
| 43 | +---------------------------------------------------------- |
| 44 | +Implementation Steps: |
| 45 | + 1. Initialize result accumulator. |
| 46 | + 2. Loop through arguments using soare_getarg. |
| 47 | + 3. Convert each argument from string to integer. |
| 48 | + 4. Add to result. |
| 49 | + 5. Print the result. |
| 50 | + 6. Return NULL (or a string if needed). |
| 51 | +
|
| 52 | +---------------------------------------------------------- |
| 53 | +Memory Management: |
| 54 | + - If returning a string, always allocate memory for it. |
| 55 | + - Example: |
| 56 | + char *ret = malloc(6 * sizeof(char)); |
| 57 | + if (!ret) |
| 58 | + return NULL; |
| 59 | + strcpy(ret, "value"); |
| 60 | + return ret; |
| 61 | +
|
| 62 | +---------------------------------------------------------- |
| 63 | +Code: |
| 64 | +---------------------------------------------------------- |
| 65 | +
|
| 66 | +char *int_add(soare_arguments_list args) |
| 67 | +{ |
| 68 | + // Accumulator for the sum |
| 69 | + int result = 0; |
| 70 | + // Pointer to current argument string |
| 71 | + char *x = NULL; |
| 72 | +
|
| 73 | + // Loop through all arguments |
| 74 | + for (int i = 0; 1; i++) |
| 75 | + { |
| 76 | + // Retrieve the i-th argument |
| 77 | + x = soare_getarg(args, i); |
| 78 | + if (!x) |
| 79 | + // Exit loop if no more arguments |
| 80 | + break; |
| 81 | +
|
| 82 | + // Convert argument to integer and add to result |
| 83 | + result += atoi(x); |
| 84 | + } |
| 85 | +
|
| 86 | + // Output the result to the console |
| 87 | + printf("%d", result); |
| 88 | +
|
| 89 | + // Return NULL (no value returned to SOARE) |
| 90 | + // If you need to return a value, allocate memory as shown above |
| 91 | + return NULL; |
| 92 | +} |
| 93 | +
|
| 94 | +Implement this function: soare_addfunction(<function name>, <function>) |
| 95 | +
|
| 96 | +soare_addfunction("int_add", int_add); |
| 97 | +
|
| 98 | +---------------------------------------------------------- |
| 99 | +
|
| 100 | +*/ |
| 101 | + |
| 102 | +/* Functions */ |
| 103 | +static struct soare_functions functions_list[100]; |
| 104 | +/* Count */ |
| 105 | +static size_t functions_count = 0; |
| 106 | + |
| 107 | +/** |
| 108 | + * @brief Add defined function |
| 109 | + * |
| 110 | + * @param name |
| 111 | + * @param function |
| 112 | + * @return unsigned int |
| 113 | + */ |
| 114 | +unsigned int soare_addfunction(char *name, char *(*function)(soare_arguments_list)) |
| 115 | +{ |
| 116 | + if (functions_count >= 99 || !name || !function) |
| 117 | + return 0; |
| 118 | + |
| 119 | + struct soare_functions fn = {name, function}; |
| 120 | + |
| 121 | + functions_list[functions_count] = fn; |
| 122 | + functions_list[functions_count + 1].name = NULL; |
| 123 | + |
| 124 | + functions_count = functions_count + 1; |
| 125 | + return functions_count; |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * @brief Get defined function |
| 130 | + * |
| 131 | + * @param name |
| 132 | + * @return soare_function |
| 133 | + */ |
| 134 | +soare_function soare_getfunction(char *name) |
| 135 | +{ |
| 136 | + static soare_function none = {NULL, NULL}; |
| 137 | + |
| 138 | + if (!name) |
| 139 | + return none; |
| 140 | + |
| 141 | + for (size_t i = 0; i < functions_count; i++) |
| 142 | + if (!strcmp(functions_list[i].name, name)) |
| 143 | + return functions_list[i]; |
| 144 | + |
| 145 | + return none; |
| 146 | +} |
| 147 | + |
| 148 | +/** |
| 149 | + * @brief Get argument from a function call |
| 150 | + * |
| 151 | + * @param args |
| 152 | + * @param position |
| 153 | + * @return char* |
| 154 | + */ |
| 155 | +char *soare_getarg(soare_arguments_list args, unsigned int position) |
| 156 | +{ |
| 157 | + for (; position && args; position--) |
| 158 | + args = args->sibling; |
| 159 | + return Eval(args); |
| 160 | +} |
| 161 | + |
| 162 | +/* |
| 163 | +
|
| 164 | +============================================================== |
| 165 | +Example: Custom Keyword - Clear screen |
| 166 | +============================================================== |
| 167 | +
|
| 168 | +This example demonstrates how to implement a custom keyword. |
| 169 | +
|
| 170 | +---------------------------------------------------------- |
| 171 | +Function Name: |
| 172 | + void clear(void) |
| 173 | +
|
| 174 | +---------------------------------------------------------- |
| 175 | +Code: |
| 176 | +---------------------------------------------------------- |
| 177 | +
|
| 178 | +void clear(void) |
| 179 | +{ |
| 180 | + // ANSI escape code to clear the screen |
| 181 | + printf("\033c\033[3J"); |
| 182 | +} |
| 183 | +
|
| 184 | +Implement this keyword: soare_addkeyword(<keyword name>, <function>) |
| 185 | +
|
| 186 | +soare_addkeyword("clear", clear); |
| 187 | +
|
| 188 | +---------------------------------------------------------- |
| 189 | +
|
| 190 | +*/ |
| 191 | + |
| 192 | +/* Keyword */ |
| 193 | +static struct soare_keywords keywords_list[100]; |
| 194 | +/* Count */ |
| 195 | +static size_t keywords_count = 0; |
| 196 | + |
| 197 | +/** |
| 198 | + * @brief Add defined keyword |
| 199 | + * |
| 200 | + * @param name |
| 201 | + * @param keyword |
| 202 | + * @return unsigned int |
| 203 | + */ |
| 204 | +unsigned int soare_addkeyword(char *name, void (*keyword)(void)) |
| 205 | +{ |
| 206 | + if (keywords_count >= 99 || !name || !keyword) |
| 207 | + return 0; |
| 208 | + |
| 209 | + struct soare_keywords fn = {name, keyword}; |
| 210 | + |
| 211 | + keywords_list[keywords_count] = fn; |
| 212 | + keywords_list[keywords_count + 1].name = NULL; |
| 213 | + |
| 214 | + keywords_count = keywords_count + 1; |
| 215 | + return keywords_count; |
| 216 | +} |
| 217 | + |
| 218 | +/** |
| 219 | + * @brief Get defined keyword |
| 220 | + * |
| 221 | + * @param name |
| 222 | + * @return soare_keyword |
| 223 | + */ |
| 224 | +soare_keyword soare_getkeyword(char *name) |
| 225 | +{ |
| 226 | + static soare_keyword none = {NULL, NULL}; |
| 227 | + |
| 228 | + if (!name) |
| 229 | + return none; |
| 230 | + |
| 231 | + for (size_t i = 0; i < keywords_count; i++) |
| 232 | + if (!strcmp(keywords_list[i].name, name)) |
| 233 | + return keywords_list[i]; |
| 234 | + |
| 235 | + return none; |
| 236 | +} |
| 237 | + |
| 238 | +/** |
| 239 | + * @brief Check if a keyword exists |
| 240 | + * |
| 241 | + * @param name |
| 242 | + * @return unsigned char |
| 243 | + */ |
| 244 | +unsigned char soare_iskeyword(char *name) |
| 245 | +{ |
| 246 | + if (!name) |
| 247 | + return 0; |
| 248 | + |
| 249 | + for (size_t i = 0; i < keywords_count; i++) |
| 250 | + if (!strcmp(keywords_list[i].name, name)) |
| 251 | + return 1; |
| 252 | + |
| 253 | + return 0; |
| 254 | +} |
0 commit comments