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.

246 lines
5.5KB

  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. int string_length (char * string) {
  84. int length = 0;
  85. fatal_failure (string == NULL, "string_length: String is null.");
  86. for (length = 0; string [length] != '\0'; ++length);
  87. return (length);
  88. }
  89. void string_delete (char * string) {
  90. string [0] = '\0';
  91. }
  92. void string_reverse (char * string) {
  93. int i = 0;
  94. int length = 0;
  95. fatal_failure (string == NULL, "string_reverse: String is null.");
  96. length = string_length (string);
  97. for (i = 0; string [i] != '\0'; ++i) {
  98. string [i] = string [length - i];
  99. }
  100. }
  101. int string_compare (char * string_0, char * string_1) {
  102. int i = 0;
  103. fatal_failure (string_0 == NULL, "string_compare: Destination string is null.");
  104. fatal_failure (string_1 == NULL, "string_compare: Source string is null.");
  105. for (i = 0; (string_0 [i] != '\0') && (string_1 [i] != '\0'); ++i) {
  106. if (string_0 [i] != string_1 [i]) {
  107. return (0);
  108. }
  109. }
  110. return (1);
  111. }
  112. void string_copy (char * string_0, char * string_1) {
  113. int i = 0;
  114. int length = 0;
  115. fatal_failure (string_0 == NULL, "string_copy: Destination string is null.");
  116. fatal_failure (string_1 == NULL, "string_copy: Source string is null.");
  117. length = string_length (string_1);
  118. for (i = 0; i != length; ++i) {
  119. string_0 [i] = string_1 [i];
  120. }
  121. }
  122. void string_concatenate (char * string_0, char * string_1) {
  123. int i = 0;
  124. int length_0 = 0;
  125. int length_1 = 0;
  126. fatal_failure (string_0 == NULL, "string_concatenate: Destination string is null.");
  127. fatal_failure (string_1 == NULL, "string_concatenate: Source string is null.");
  128. length_0 = string_length (string_0);
  129. length_1 = string_length (string_1);
  130. for (i = 0; i != length_1; ++i) {
  131. string_0 [length_0 + i] = string_1 [i];
  132. }
  133. }
  134. int string_compare_limit (char * string_0, char * string_1, int limit) {
  135. int i = 0;
  136. fatal_failure (string_0 == NULL, "string_compare_limit: Destination string is null.");
  137. fatal_failure (string_1 == NULL, "string_compare_limit: Source string is null.");
  138. for (i = 0; (string_0 [i] != '\0') && (string_1 [i] != '\0') && (i != limit); ++i) {
  139. if (string_0 [i] != string_1 [i]) {
  140. return (0);
  141. }
  142. }
  143. return (1);
  144. }
  145. void string_copy_limit (char * string_0, char * string_1, int limit) {
  146. int i = 0;
  147. int length = 0;
  148. fatal_failure (string_0 == NULL, "string_copy_limit: Destination string is null.");
  149. fatal_failure (string_1 == NULL, "string_copy_limit: Source string is null.");
  150. if (limit <= 0) {
  151. return;
  152. }
  153. length = string_length (string_1);
  154. for (i = 0; (i != length) && (i != limit); ++i) {
  155. string_0 [i] = string_1 [i];
  156. }
  157. }
  158. void string_concatenate_limit (char * string_0, char * string_1, int limit) {
  159. int i = 0;
  160. int length_0 = 0;
  161. int length_1 = 0;
  162. fatal_failure (string_0 == NULL, "string_concatenate_limit: Destination string is null.");
  163. fatal_failure (string_1 == NULL, "string_concatenate_limit: Source string is null.");
  164. if (limit <= 0) {
  165. return;
  166. }
  167. length_0 = string_length (string_0);
  168. length_1 = string_length (string_1);
  169. for (i = 0; (i != length_1) && (i != limit); ++i) {
  170. string_0 [length_0 + i] = string_1 [i];
  171. }
  172. }
  173. #endif