-
Notifications
You must be signed in to change notification settings - Fork 0
/
CALLBACK.C
643 lines (568 loc) · 17.8 KB
/
CALLBACK.C
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/* ********************************************************************
FILE : CALLBACK.c
PROGRAM DESCRIPTION : demo of callback function with arithmetic operation of addition, subtraction, multipliation and division and
bit operation of AND, OR, XOR, Complement
AUTHOR : K.M. Arun Kumar alias Arunkumar Murugeswaran
KNOWN BUGS :
NOTE :
CHANGE LOGS :
*****************************************************************************/
#include <stdio.h>
#include <limits.h>
#include <ctype.h>
#include <stdbool.h>
#include <string.h>
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long uint64_t;
typedef char int8_t;
typedef short int int16_t;
typedef int int32_t;
typedef long int64_t;
#define NULL_DATA_PTR ((void *)0)
#define NULL_CHAR ('\0')
#define STR_MAX_CHARS (7)
typedef enum { SUCCESS, FAILURE, FSM_OPERAND_1, FSM_OPERAND_2, FSM_NUM_OPER_TYPE, FSM_ARITH_OPER, FSM_LOGIC_OPER,
FSM_NUM_OPER_RESULT, FSM_TO_CONTINUE} sys_status_t;
unsigned char system_fsm;
typedef void *(*callback)( const void *const, const void *const );
typedef void *(*register_callback) ( const void *const, const void *const, const char );
void *switch_callback_reg( const void *const, const void *const, const char );
void *register_callback_func(const void *const, const void *const, const char arithmetic, register_callback );
void *arith_callback_reg( const void *const ptr_oper1_num, const void *const ptr_oper2_num, const char arithmetic_oper);
void *logic_callback_reg( const void *const ptr_oper1_num, const void *const ptr_oper2_num, const char logic_oper);
void *add_arith( const void *const ptr_oper1_num, const void *const ptr_oper2_num);
void *sub_arith( const void *const ptr_oper1_num, const void *const ptr_oper2_num);
void *mul_arith( const void *const ptr_oper1_num, const void *const ptr_oper2_num);
void *div_arith( const void *const ptr_oper1_num, const void *const ptr_oper2_num);
void *mod_arith( const void *const ptr_oper1_num, const void *const ptr_oper2_num);
void *and_logic(const void *const ptr_oper1_num, const void *const ptr_oper2_num);
void *or_logic(const void *const ptr_oper1_num, const void *const ptr_oper2_num);
void *xor_logic(const void *const ptr_oper1_num, const void *const ptr_oper2_num);
void *comp1_logic(const void *const ptr_oper1_num, const void *const ptr_oper2_num);
void *comp2_logic(const void *const ptr_oper1_num, const void *const ptr_oper2_num);
uint16_t Get_Validate_Input_Number(void *const int32_input_num_ptr, char *const input_str_ptr, const unsigned int input_str_max_chars, const int32_t valid_min_value, const int32_t valid_max_value);
uint16_t Get_Input_Str(char *const input_str_ptr, const unsigned int input_str_max_chars);
uint16_t Str_to_Num_Conv(void *const num_conv_from_str, const char *const num_in_str);
uint32_t Power_Of(const uint8_t base, const uint8_t power);
void *voidptr_ans_num =NULL;
int oper1_num, oper2_num, ans_num;
char oper[2];
char input_str[STR_MAX_CHARS];
int main()
{
int *ptr_ans_num;
char continue_flag[2] = "Y", type_oper[2] = "A";
system_fsm = FSM_OPERAND_1;
continue_flag[0] = toupper(continue_flag[0]);
while(1)
{
switch(system_fsm)
{
case FSM_OPERAND_1:
printf("\n Enter oper1_num = ");
if((Get_Validate_Input_Number(&oper1_num, input_str, STR_MAX_CHARS, INT_MIN, INT_MAX )) != SUCCESS)
{
printf("\n ERR: Invalid operand 1 ");
}
else
system_fsm = FSM_OPERAND_2;
break;
case FSM_OPERAND_2:
printf("\n Enter oper2_num = ");
if((Get_Validate_Input_Number(&oper2_num, input_str, STR_MAX_CHARS, INT_MIN, INT_MAX )) != SUCCESS)
{
printf("\n ERR: Invalid operand 2 ");
}
else
system_fsm = FSM_NUM_OPER_TYPE;
break;
case FSM_NUM_OPER_TYPE:
printf("\n MENU for operation : 'A' - arith , 'L' - logic ");
printf("\n Enter oper = ");
//scanf("%1s", &type_oper);
if((Get_Input_Str(type_oper, 2)) != SUCCESS)
{
printf("\n ERR: Invalid operation ");
}
else
{
type_oper[0] = toupper(type_oper[0]);
switch( type_oper[0])
{
case 'A':
case 'L':
system_fsm = FSM_NUM_OPER_RESULT;
break;
default:
printf("\n ERR: Invalid numeric operation");
}
}
break;
case FSM_NUM_OPER_RESULT:
ptr_ans_num = (int *)switch_callback_reg(&oper1_num, &oper2_num, type_oper[0]);
if(ptr_ans_num)
{
ans_num = *ptr_ans_num;
switch(type_oper[0])
{
case 'A':
printf("\n %d '%c' %d = %d", oper1_num, oper[0], oper2_num, ans_num );
break;
case 'L':
printf("\n 0x%X '%c' 0x%X = 0x%X", oper1_num, oper[0],oper2_num, ans_num );
break;
default:
printf("\n ERR: Invalid numeric operator");
}
}
system_fsm = FSM_TO_CONTINUE;
break;
case FSM_TO_CONTINUE:
printf("\n Continue - type 'y' or 'Y', any other char to exit");
printf("\n Enter : ");
if((Get_Input_Str(continue_flag, 2)) != SUCCESS)
{
printf("\n ERR: Invalid operation ");
}
continue_flag[0] = toupper(continue_flag[0]);
if(continue_flag[0] != 'Y')
{
return 0;
}
system_fsm = FSM_OPERAND_1;
break;
}
}
return 0;
}
void *switch_callback_reg( const void *const ptr_oper1_num, const void *const ptr_oper2_num, const char oper_type)
{
register_callback callback_reg;
bool is_error = false;
do
{
switch(oper_type)
{
case 'A':
printf("\n MENU : For addition : '+' , subtraction : '-' , mul : '*' , div : '/' , Mod : '%%'" );
printf("\n Enter arithimetic operation : ");
if((Get_Input_Str(oper, 2)) != SUCCESS)
{
printf("\n ERR: Invalid arthimetic operation ");
is_error = true;
}
else
{
switch(oper[0])
{
case '+':
case '-':
case '*':
case '/':
case '%':
callback_reg = arith_callback_reg;
is_error = false;
break;
default:
printf("\n ERR: Invalid arithimetic operation ");
is_error = true;
}
}
break;
case 'L':
printf("\n MENU : For AND : '&' , OR : '|' , XOR : '^' , comp1 : '~' , comp2 : '@'" );
printf("\n Enter logic operation : ");
if((Get_Input_Str(oper, 2)) != SUCCESS)
{
printf("\n ERR: Invalid arthimetic operation ");
is_error = true;
}
else
{
switch(oper[0])
{
case '&':
case '|':
case '^':
case '~':
case '@':
callback_reg = logic_callback_reg;
is_error = false;
break;
default:
printf("\n ERR: Invalid logic operation ");
is_error = true;
}
}
break;
default:
printf("\n ERR: Invalid numeric operation");
callback_reg = NULL;
}
} while(is_error == true);
if(callback_reg)
return( register_callback_func(ptr_oper1_num, ptr_oper2_num, oper[0], callback_reg));
return NULL;
}
void *register_callback_func(const void *const ptr_oper1_num, const void *const ptr_oper2_num, const char arithmetic_oper, register_callback callback_func )
{
return callback_func(ptr_oper1_num,ptr_oper2_num,arithmetic_oper);
}
void *arith_callback_reg( const void *const ptr_oper1_num, const void *const ptr_oper2_num, const char arithmetic_oper)
{
callback arith_callback;
switch(arithmetic_oper)
{
case '+':
arith_callback = add_arith;
break;
case '-':
arith_callback = sub_arith;
break;
case '*':
arith_callback = mul_arith;
break;
case '/':
arith_callback = div_arith;
break;
case '%':
arith_callback = mod_arith;
break;
default:
arith_callback = NULL;
}
if(arith_callback)
return arith_callback(ptr_oper1_num,ptr_oper2_num);
return NULL;
}
void *logic_callback_reg( const void *const ptr_oper1_num, const void *const ptr_oper2_num, const char logic_oper)
{
callback logic_callback;
switch(logic_oper)
{
case '&':
logic_callback = and_logic;
break;
case '|':
logic_callback = or_logic;
break;
case '^':
logic_callback = xor_logic;
break;
case '~':
logic_callback = comp1_logic;
break;
case '@':
logic_callback = comp2_logic;
break;
default:
logic_callback = NULL;
}
if(logic_callback)
return logic_callback(ptr_oper1_num,ptr_oper2_num);
return NULL;
}
void *add_arith( const void *const ptr_oper1_num, const void *const ptr_oper2_num)
{
int oper1_num, oper2_num ;
oper1_num = *(int *)ptr_oper1_num;
oper2_num = *(int *)ptr_oper2_num;
ans_num = oper1_num + oper2_num;
return(&ans_num);
}
void *sub_arith( const void *const ptr_oper1_num, const void *const ptr_oper2_num)
{
int oper1_num, oper2_num ;
oper1_num = *(int *)ptr_oper1_num;
oper2_num = *(int *)ptr_oper2_num;
ans_num = oper1_num - oper2_num;
return(&ans_num);
}
void *mul_arith( const void *const ptr_oper1_num, const void *const ptr_oper2_num)
{
int oper1_num, oper2_num ;
oper1_num = *(int *)ptr_oper1_num;
oper2_num = *(int *)ptr_oper2_num;
ans_num = oper1_num * oper2_num;
return(&ans_num);
}
void *div_arith( const void *const ptr_oper1_num, const void *const ptr_oper2_num)
{
int oper1_num, oper2_num ;
oper1_num = *(int *)ptr_oper1_num;
oper2_num = *(int *)ptr_oper2_num;
if(oper2_num == 0)
{
printf("\n ERR: divisor = 0");
return NULL;
}
ans_num = oper1_num * oper2_num;
return(&ans_num);
}
void *mod_arith( const void *const ptr_oper1_num, const void *const ptr_oper2_num)
{
int oper1_num, oper2_num ;
oper1_num = *(int *)ptr_oper1_num;
oper2_num = *(int *)ptr_oper2_num;
if(oper2_num == 0)
{
printf("\n ERR: divisor = 0");
return NULL;
}
ans_num = oper1_num % oper2_num;
return(&ans_num);
}
void *and_logic(const void *const ptr_oper1_num, const void *const ptr_oper2_num)
{
int oper1_num, oper2_num ;
oper1_num = *(int *)ptr_oper1_num;
oper2_num = *(int *)ptr_oper2_num;
ans_num = oper1_num & oper2_num;
return(&ans_num);
}
void *or_logic(const void *const ptr_oper1_num, const void *const ptr_oper2_num)
{
int oper1_num, oper2_num ;
oper1_num = *(int *)ptr_oper1_num;
oper2_num = *(int *)ptr_oper2_num;
ans_num = oper1_num | oper2_num;
return(&ans_num);
}
void *xor_logic(const void *const ptr_oper1_num, const void *const ptr_oper2_num)
{
int oper1_num, oper2_num ;
oper1_num = *(int *)ptr_oper1_num;
oper2_num = *(int *)ptr_oper2_num;
ans_num = oper1_num ^ oper2_num;
return(&ans_num);
}
void *comp1_logic(const void *const ptr_oper1_num, const void *const ptr_oper2_num)
{
int oper1_num, oper2_num ;
oper1_num = *(int *)ptr_oper1_num;
ans_num = ~oper1_num;
return(&ans_num);
}
void *comp2_logic(const void *const ptr_oper1_num, const void *const ptr_oper2_num)
{
int oper2_num ;
oper2_num = *(int *)ptr_oper2_num;
ans_num = ~oper2_num;
return(&ans_num);
}
/*------------------------------------------------------------*
FUNCTION NAME : Str_to_Num_Conv
DESCRIPTION :
INPUT :
OUTPUT :
NOTE : digits are extracted from left to right format from digit in num_in_str
Func ID : 02.04
BUGS :
-*------------------------------------------------------------*/
uint16_t Str_to_Num_Conv( void *const num_conv_from_str_ptr, const char *const num_in_str)
{
int32_t num = 0, *num_conv_from_str;
uint32_t place;
int16_t cur_unit;
uint8_t num_chars = 0, base = 10, pos = 0, start_num_pos = 0 ;
if(num_conv_from_str_ptr == NULL_DATA_PTR || num_in_str == NULL_DATA_PTR )
{
#ifdef TRACE_ERROR
printf("ERR: data are null ptr \n");
#endif
return FAILURE;
}
num_conv_from_str = (int32_t *)num_conv_from_str_ptr;
*num_conv_from_str = 0;
if(num_in_str[0] >= '0' && num_in_str[0] <= '9')
{
start_num_pos = 0;
}
else if(num_in_str[0] == '-')
{
start_num_pos = 1;
}
else
{
#ifdef TRACE_ERROR
printf("ERR: invalid char: %c \n", num_in_str[0]);
#endif
return FAILURE;
}
num_chars = strlen(num_in_str + start_num_pos);
if(num_chars == 0)
{
#ifdef TRACE_ERROR
printf("ERR: data empty \n");
#endif
return FAILURE;
}
pos = start_num_pos;
for( place = Power_Of(base, num_chars - 1); place >= 1; place /= base, ++pos )
{
cur_unit = num_in_str[pos] - '0';
if(cur_unit < 0 || cur_unit > 9 )
{
#ifdef TRACE_ERROR
printf("ERR: invalid char at data[%d] = %c \n", pos, num_in_str[pos]);
#endif
return FAILURE;
}
num += (cur_unit * place);
}
if(num_in_str[0] == '-')
{
*num_conv_from_str = -num;
}
else
{
*num_conv_from_str = num;
}
return SUCCESS;
}
/*------------------------------------------------------------*
FUNCTION NAME : Power_Of
DESCRIPTION :
INPUT :
OUTPUT :
NOTE :
Func ID : 02.10
Bugs :
-*------------------------------------------------------------*/
uint32_t Power_Of(const uint8_t base, const uint8_t power )
{
uint32_t power_val = 1;
uint8_t i = 0;
if(power == 0)
{
return power_val;
}
for(i = 1; i <= power; ++i)
{
power_val *= base;
}
return power_val;
}
/*------------------------------------------------------------*
FUNCTION NAME : Validate_Number_Input
DESCRIPTION :
INPUT :
OUTPUT :
NOTE :
Func ID : 01.02
BUGS :
-*------------------------------------------------------------*/
uint16_t Get_Validate_Input_Number(void *const input_num_ptr, char *const input_str_ptr, const unsigned int input_str_max_chars, const int32_t valid_min_value, const int32_t valid_max_value)
{
int32_t temp_int, *int32_input_num_ptr;
if(int32_input_num_ptr == NULL_DATA_PTR)
{
return FAILURE;
}
if(valid_min_value > valid_max_value)
{
return FAILURE;
}
int32_input_num_ptr = (int32_t *)input_num_ptr;
*int32_input_num_ptr = 0;
if((Get_Input_Str(input_str_ptr, input_str_max_chars)) != SUCCESS)
return FAILURE;
if((Str_to_Num_Conv(&temp_int, input_str_ptr)) != SUCCESS)
{
memset(input_str_ptr, NULL_CHAR, input_str_max_chars);
return FAILURE;
}
memset(input_str_ptr, NULL_CHAR, input_str_max_chars);
if(temp_int < valid_min_value || temp_int > valid_max_value)
{
#ifdef TRACE_ERROR
printf("ERR: input data - %d, out of range [%d,%d] \n", temp_int, valid_min_value, valid_max_value);
#endif
return FAILURE;
}
*int32_input_num_ptr = temp_int;
return SUCCESS;
}
/*------------------------------------------------------------*
FUNCTION NAME : Get_Input_Str
DESCRIPTION :
INPUT :
OUTPUT :
NOTE :
Func ID : 01.02
BUGS :
-*------------------------------------------------------------*/
uint16_t Get_Input_Str(char *const input_str_ptr, const unsigned int input_str_max_chars)
{
unsigned int input_str_num_chars = 0;
char rcvd_char;
if(input_str_ptr == NULL_DATA_PTR || input_str_max_chars <= 1)
{
return FAILURE;
}
memset(input_str_ptr, NULL_CHAR, input_str_max_chars);
while (1)
{
rcvd_char = (char) getchar();
//scanf("%c", &rcvd_char);
switch(rcvd_char)
{
case '\b':
if(input_str_num_chars > 0)
{
input_str_ptr[input_str_num_chars] = NULL_CHAR;
--input_str_num_chars;
}
break;
case '\n':
if(input_str_num_chars != 0)
{
input_str_ptr[input_str_num_chars] = NULL_CHAR;
return SUCCESS;
}
break;
default:
if(input_str_num_chars + 1 < input_str_max_chars )
{
input_str_ptr[input_str_num_chars] = rcvd_char;
++input_str_num_chars;
}
else
{
printf("ERR: Input data num chars exceeds max chars : %u \n", input_str_max_chars - 1);
memset(input_str_ptr, NULL_CHAR, input_str_max_chars);
fflush(stdin);
return FAILURE;
}
}
}
return SUCCESS;
}
#ifdef EXAMPLE_CALLBACK
typedef void (*callback)(void);
void register_callback(callback ptr_reg_callback);
void my_callback(void)
{
printf("inside my_callback\n");
}
int main(void)
{
/* initialize function pointer to
my_callback */
callback ptr_my_callback=my_callback;
printf("This is a program demonstrating function callback\n");
/* register our callback function */
register_callback(ptr_my_callback);
printf("back inside main program\n");
return 0;
}
/* registration goes here */
void register_callback(callback ptr_reg_callback)
{
printf("inside register_callback\n");
/* calling our callback function my_callback */
(*ptr_reg_callback)();
}
#endif