fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define ACTION_OPEN_FILE 0
  5. #define ACTION_WRITE_TO_STDOUT 1
  6.  
  7. typedef struct action_open_file
  8. {
  9. int type;
  10. const char *path;
  11. FILE *file;
  12. } action_open_file_t;
  13.  
  14. typedef struct action_write_to_stdout
  15. {
  16. int type;
  17. const char *string;
  18. } action_write_to_stdout_t;
  19.  
  20. void execute_action_open_file(action_open_file_t *action)
  21. {
  22. printf("the open file action is called.\n");
  23.  
  24. action->file = !NULL; /* Типа открыл. */
  25. }
  26.  
  27. void execute_action_write_to_stdout(action_write_to_stdout_t *action)
  28. {
  29. printf("the write to stdout action is called.\n");
  30.  
  31. printf("%s\n", action->string);
  32. }
  33.  
  34. void execute_action(void *action)
  35. {
  36. switch (*(int *)action)
  37. {
  38. case ACTION_OPEN_FILE:
  39. return execute_action_open_file(action);
  40.  
  41. case ACTION_WRITE_TO_STDOUT:
  42. return execute_action_write_to_stdout(action);
  43. }
  44. }
  45.  
  46. int main(void)
  47. {
  48. action_open_file_t action_1 =
  49. {
  50. .type = ACTION_OPEN_FILE,
  51. .path = "...",
  52. .file = NULL
  53. };
  54.  
  55. action_write_to_stdout_t action_2 =
  56. {
  57. .type = ACTION_WRITE_TO_STDOUT,
  58. .string = "Hello World"
  59. };
  60.  
  61. execute_action(&action_1); /* Вызывается функция execute_action_open_file. */
  62. execute_action(&action_2); /* Вызывается функция execute_action_write_to_stdout. */
  63.  
  64. return EXIT_SUCCESS;
  65. }
  66.  
Success #stdin #stdout 0s 5400KB
stdin
Standard input is empty
stdout
the open file action is called.
the write to stdout action is called.
Hello World