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
| #include<stdlib.h>
#include<stdio.h>
#include<string.h>
typedef struct s_chain t_chain;
typedef void (*PRINT) (t_chain*, char*);
typedef void (*DO) (t_chain*, unsigned short);
struct s_chain {
void* elem;
struct s_chain* next;
PRINT print;
DO do_something;
};
// Functions DO
void multiply(t_chain* t, unsigned short multiplier) {
if ((t == NULL) || (t->elem == NULL)) { return; }
printf("multiply: %d\n", multiplier);
*((size_t*) t->elem) *= multiplier;
}
void my_pow(t_chain* t, unsigned short p) {
if ((t == NULL) || (t->elem == NULL)) { return; }
printf("pow: %d\n", p);
size_t tmp = *((size_t*) t->elem);
for (; p > 1; --p) {
*((size_t*) t->elem) *= tmp;
}
}
void reverse_str(t_chain* t, unsigned short useless) {
if ((t == NULL) || (t->elem == NULL)) { return; }
size_t len = strlen((char*) t->elem), pos = 0;
char c = '\0';
for(--len; pos < (len / 2); ++pos) {
c = *((char*) t->elem + (len - pos));
*((char*) t->elem + (len - pos)) = *((char*) t->elem + pos);
*((char*) t->elem + pos) = c;
}
}
// Functions PRINT
void print_int(t_chain* t, char* extra) {
if ((t == NULL) || (t->elem == NULL)) { return; }
printf("Int: %d%s\n", *((int*) t->elem), ((extra)? extra: ""));
}
void print_my_type(t_chain* t, char* extra) {
if ((t == NULL) || (t->elem == NULL)) { return; }
size_t data = *((int*) t->elem);
printf("id: %d, char: %c%s\n", (data & (0XFFFFFF)), ((data >> 24) & 0XFF), ((extra)? extra: ""));
}
void print_str(t_chain* t, char* extra) {
if ((t == NULL) || (t->elem == NULL)) { return; }
printf("Str: %s%s\n", (char*) t->elem, ((extra)? extra: ""));
}
int main(void)
{
struct s_chain* first_elt = NULL;
struct s_chain* second_elt = NULL;
struct s_chain* third_elt = NULL;
struct s_chain* fourth_elt = NULL;
struct s_chain* tmp = NULL;
size_t tmp_1 = 157, tmp_2 = 0, tmp_3 = 54;
unsigned short count = 1;
char* str = NULL;
// First element
first_elt = malloc( sizeof(t_chain) );
if (first_elt == NULL) { return 0; }
first_elt->elem = &tmp_1;
first_elt->print = print_int;
first_elt->do_something = multiply;
// Second element
second_elt = malloc( sizeof(t_chain) );
if (second_elt == NULL) { free(first_elt); return 0; }
asprintf(&str, "test str %d", tmp_1);
second_elt->elem = str;
second_elt->print = print_str;
second_elt->do_something = reverse_str;
first_elt->next = second_elt;
// Third element
third_elt = malloc( sizeof(t_chain) );
if (third_elt == NULL) { free(first_elt); free(str); free(second_elt); return 0; }
tmp_2 = 'm';
tmp_2 = ((tmp_2 << 24) + 8542561);
third_elt->elem = &tmp_2;
third_elt->print = print_my_type;
third_elt->do_something = NULL;
second_elt->next = third_elt;
// Fourth element
fourth_elt = malloc( sizeof(t_chain) );
if (fourth_elt == NULL) { free(first_elt); free(str); free(second_elt); free(third_elt); return 0; }
fourth_elt->elem = &tmp_3;
fourth_elt->print = print_int;
fourth_elt->do_something = my_pow;
fourth_elt->next = NULL;
third_elt->next = fourth_elt;
// Process
tmp = first_elt;
do { // first exists
printf("%sElement %d:\n", ((count != 1)? "\n": ""), count);
count++;
if (tmp->do_something) {
tmp->print(tmp, " (before)");
tmp->do_something(tmp, count);
tmp->print(tmp, " (after)");
} else {
tmp->print(tmp, NULL);
}
tmp = tmp->next;
} while(tmp != NULL);
// Clean all
free(first_elt);
free(str);
free(second_elt);
free(third_elt);
free(fourth_elt);
return 0;
} |
Partager