Xtandard stuff...
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

263 lines
5.8KB

  1. /*
  2. * Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic
  3. *
  4. * Xtandard is free software! You will redistribute it or modify it under the terms of the GNU General Public License by Free Software Foundation.
  5. * And when you do redistribute it or modify it, it will use either version 3 of the License, or (at yours truly opinion) any later version.
  6. * It is distributed in the hope that it will be useful or harmful, it really depends... But no warranty what so ever, seriously. See GNU/GPLv3.
  7. */
  8. #ifndef XTANDARD_SOURCE
  9. #define XTANDARD_SOURCE
  10. #include "xtandard.h"
  11. void in (void * data, int size) {
  12. (void) read (STDIN_FILENO, data, (unsigned long int) size);
  13. }
  14. void out (void * data, int size) {
  15. (void) write (STDOUT_FILENO, data, (unsigned long int) size);
  16. }
  17. void fatal_failure (int condition, char * message) {
  18. if (condition != 0) {
  19. out ("\033[1;31m[!]\033[0m ", 15);
  20. out (message, string_length (message));
  21. out ("\n", 1);
  22. exit (EXIT_FAILURE);
  23. }
  24. }
  25. void * allocate (int size) {
  26. char * data = NULL;
  27. data = calloc ((unsigned long int) size, sizeof (* data));
  28. fatal_failure (data == NULL, "allocate: Oh no...");
  29. return ((void *) data);
  30. }
  31. void * reallocate (void * data, int size) {
  32. data = realloc (data, (unsigned long int) size);
  33. fatal_failure (data == NULL, "reallocate: Oh no...");
  34. return (data);
  35. }
  36. void * deallocate (void * data) {
  37. fatal_failure (data == NULL, "deallocate: Oh no...");
  38. free (data);
  39. return (NULL);
  40. }
  41. void * memorize (int size) {
  42. static char * buffer = NULL;
  43. char * points = NULL;
  44. static int length = 0;
  45. static int chunks = 0;
  46. static int loling = 1024;
  47. if (size == 0) {
  48. free (buffer);
  49. buffer = NULL;
  50. length = 0;
  51. chunks = 0;
  52. return (NULL);
  53. }
  54. for (; length + size > chunks * loling; ) {
  55. int i;
  56. ++chunks;
  57. buffer = realloc (buffer, (unsigned long int) (chunks * loling));
  58. fatal_failure (buffer == NULL, "memorize: Oh no...");
  59. for (i = (chunks - 1) * loling; i != chunks * loling; ++i) {
  60. buffer [i] = '\0';
  61. }
  62. }
  63. points = & buffer [length];
  64. length += size;
  65. return ((void *) points);
  66. }
  67. void * record (void) {
  68. char * buffer = NULL;
  69. int offset = 0;
  70. int loling = 1024;
  71. buffer = reallocate (buffer, loling);
  72. do {
  73. if ((offset + 1) % loling == 0) {
  74. buffer = reallocate (buffer, ((offset + 1) / loling + 1) * loling);
  75. }
  76. buffer [offset] = '\0';
  77. in (& buffer [offset], sizeof (* buffer));
  78. ++offset;
  79. } while (buffer [offset - 1] != '\0');
  80. buffer [offset - 1] = '\0';
  81. return (buffer);
  82. }
  83. void curses_style (int effect, int colour) {
  84. char format [8] = "\033[ ;3 m";
  85. if ((effect == -1) || (colour == -1)) {
  86. out ("\033[0m", 5);
  87. } else {
  88. format [2] = (char) effect + '0';
  89. format [5] = (char) colour + '0';
  90. out (format, 8);
  91. }
  92. }
  93. void curses_clear (void) {
  94. out ("\033[2J", 5);
  95. }
  96. int string_length (char * string) {
  97. int length = 0;
  98. fatal_failure (string == NULL, "string_length: String is null.");
  99. for (length = 0; string [length] != '\0'; ++length);
  100. return (length);
  101. }
  102. void string_delete (char * string) {
  103. string [0] = '\0';
  104. }
  105. void string_reverse (char * string) {
  106. int i = 0;
  107. int length = 0;
  108. fatal_failure (string == NULL, "string_reverse: String is null.");
  109. length = string_length (string);
  110. for (i = 0; string [i] != '\0'; ++i) {
  111. string [i] = string [length - i];
  112. }
  113. }
  114. int string_compare (char * string_0, char * string_1) {
  115. int i = 0;
  116. fatal_failure (string_0 == NULL, "string_compare: Destination string is null.");
  117. fatal_failure (string_1 == NULL, "string_compare: Source string is null.");
  118. for (i = 0; (string_0 [i] != '\0') && (string_1 [i] != '\0'); ++i) {
  119. if (string_0 [i] != string_1 [i]) {
  120. return (0);
  121. }
  122. }
  123. return (1);
  124. }
  125. void string_copy (char * string_0, char * string_1) {
  126. int i = 0;
  127. int length = 0;
  128. fatal_failure (string_0 == NULL, "string_copy: Destination string is null.");
  129. fatal_failure (string_1 == NULL, "string_copy: Source string is null.");
  130. length = string_length (string_1);
  131. for (i = 0; i != length; ++i) {
  132. string_0 [i] = string_1 [i];
  133. }
  134. }
  135. void string_concatenate (char * string_0, char * string_1) {
  136. int i = 0;
  137. int length_0 = 0;
  138. int length_1 = 0;
  139. fatal_failure (string_0 == NULL, "string_concatenate: Destination string is null.");
  140. fatal_failure (string_1 == NULL, "string_concatenate: Source string is null.");
  141. length_0 = string_length (string_0);
  142. length_1 = string_length (string_1);
  143. for (i = 0; i != length_1; ++i) {
  144. string_0 [length_0 + i] = string_1 [i];
  145. }
  146. }
  147. int string_compare_limit (char * string_0, char * string_1, int limit) {
  148. int i = 0;
  149. fatal_failure (string_0 == NULL, "string_compare_limit: Destination string is null.");
  150. fatal_failure (string_1 == NULL, "string_compare_limit: Source string is null.");
  151. for (i = 0; (string_0 [i] != '\0') && (string_1 [i] != '\0') && (i != limit); ++i) {
  152. if (string_0 [i] != string_1 [i]) {
  153. return (0);
  154. }
  155. }
  156. return (1);
  157. }
  158. void string_copy_limit (char * string_0, char * string_1, int limit) {
  159. int i = 0;
  160. int length = 0;
  161. fatal_failure (string_0 == NULL, "string_copy_limit: Destination string is null.");
  162. fatal_failure (string_1 == NULL, "string_copy_limit: Source string is null.");
  163. if (limit <= 0) {
  164. return;
  165. }
  166. length = string_length (string_1);
  167. for (i = 0; (i != length) && (i != limit); ++i) {
  168. string_0 [i] = string_1 [i];
  169. }
  170. }
  171. void string_concatenate_limit (char * string_0, char * string_1, int limit) {
  172. int i = 0;
  173. int length_0 = 0;
  174. int length_1 = 0;
  175. fatal_failure (string_0 == NULL, "string_concatenate_limit: Destination string is null.");
  176. fatal_failure (string_1 == NULL, "string_concatenate_limit: Source string is null.");
  177. if (limit <= 0) {
  178. return;
  179. }
  180. length_0 = string_length (string_0);
  181. length_1 = string_length (string_1);
  182. for (i = 0; (i != length_1) && (i != limit); ++i) {
  183. string_0 [length_0 + i] = string_1 [i];
  184. }
  185. }
  186. #endif