status.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define LENGTH(a) (sizeof a / sizeof a[0])
  4. typedef union Func Func;
  5. typedef struct Layout Layout;
  6. #include "modules/status.h"
  7. typedef enum {
  8. type_int,
  9. type_unsigned_int,
  10. type_long,
  11. type_long_double,
  12. type_char,
  13. type_char_ptr
  14. } Type;
  15. union Func {
  16. int (*d) ();
  17. unsigned int (*u) ();
  18. long (*ld) ();
  19. long double (*Lf) ();
  20. char (*c) ();
  21. char * (*s) ();
  22. };
  23. struct Layout {
  24. const char * format;
  25. const int bufsize;
  26. Type type;
  27. Func func;
  28. };
  29. static int get_whole_buffer_size();
  30. static int concatenate_whole_buffer(char *, Arg *);
  31. static int append_string(char *, char *);
  32. static void update(Arg *);
  33. static void update_arg(Arg *);
  34. // must be defined in config.h
  35. static void set_status(char *);
  36. #include "config.h"
  37. int
  38. size(const char * str)
  39. {
  40. int size = 0;
  41. for (int i = 0; *(str + i); i++) size++;
  42. return size + 1;
  43. }
  44. int
  45. get_whole_buffer_size()
  46. {
  47. int size = 0;
  48. for (long unsigned int i = 0; i < LENGTH(layouts); i++) size += layouts[i].bufsize;
  49. return size;
  50. }
  51. int
  52. concatenate_whole_buffer(char * buf, Arg * arg)
  53. {
  54. for (long unsigned int i = 0; i < LENGTH(layouts); i++) {
  55. char temp_buf[layouts[i].bufsize];
  56. // call .func with .arg and embed it's return value in .format
  57. // idk if there is a better way, soooo
  58. if (layouts[i].type == type_int) {
  59. sprintf(
  60. temp_buf,
  61. layouts[i].format,
  62. layouts[i].func.d(arg)
  63. );
  64. } else if (layouts[i].type == type_unsigned_int) {
  65. sprintf(
  66. temp_buf,
  67. layouts[i].format,
  68. layouts[i].func.u(arg)
  69. );
  70. } else if (layouts[i].type == type_long) {
  71. sprintf(
  72. temp_buf,
  73. layouts[i].format,
  74. layouts[i].func.ld(arg)
  75. );
  76. } else if (layouts[i].type == type_long_double) {
  77. sprintf(
  78. temp_buf,
  79. layouts[i].format,
  80. layouts[i].func.Lf(arg)
  81. );
  82. } else if (layouts[i].type == type_char) {
  83. sprintf(
  84. temp_buf,
  85. layouts[i].format,
  86. layouts[i].func.c(arg)
  87. );
  88. } else if (layouts[i].type == type_char_ptr) {
  89. sprintf(
  90. temp_buf,
  91. layouts[i].format,
  92. layouts[i].func.s(arg)
  93. );
  94. }
  95. // append temp_buf to the end of buf
  96. append_string(buf, temp_buf);
  97. }
  98. return 1;
  99. }
  100. int
  101. append_string(char * buf, char * new)
  102. {
  103. int bufsize = 0;
  104. for (int i = 0; buf[i] != '\0'; i++) bufsize++;
  105. for (int i = 0; new[i] != '\0'; i++) {
  106. buf[bufsize] = new[i];
  107. bufsize++;
  108. }
  109. buf[bufsize] = '\0';
  110. return 0;
  111. }
  112. void
  113. update(Arg * arg)
  114. {
  115. update_arg(arg);
  116. char str[get_whole_buffer_size()];
  117. str[0] = '\0';
  118. concatenate_whole_buffer(str, arg);
  119. set_status(str);
  120. }
  121. int
  122. main()
  123. {
  124. Arg * arg = malloc(sizeof(Arg));
  125. while (1) update(arg);
  126. // unreachable
  127. free(arg);
  128. return 0;
  129. }