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.

549 lines
15KB

  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. int argument_count = 0;
  12. char * * argument_nick = NULL;
  13. char * * argument_name = NULL;
  14. void (* * argument_function) (void) = NULL;
  15. int file_list_active = 0;
  16. int file_list_count = 0;
  17. int * file_list_mark = NULL;
  18. int * file_list_size = NULL;
  19. char * * file_list_name = NULL;
  20. char * * file_list_data = NULL;
  21. void in (void * data, int size) {
  22. fatal_failure (data == NULL, "in: Failed to read from standard input, data is null pointer.");
  23. fatal_failure (size == 0, "in: Failed to read from standard input, size is zero.");
  24. (void) read (STDIN_FILENO, data, (unsigned long int) size);
  25. }
  26. void out (void * data, int size) {
  27. fatal_failure (data == NULL, "out: Failed to write to standard output, data is null pointer.");
  28. fatal_failure (size == 0, "out: Failed to write to standard output, size is zero.");
  29. (void) write (STDOUT_FILENO, data, (unsigned long int) size);
  30. }
  31. void echo (char * data) {
  32. out (data, string_length (data));
  33. }
  34. void fatal_failure (int condition, char * message) {
  35. if (condition != 0) {
  36. echo ("\033[1;31m[!]\033[0m ");
  37. echo (message);
  38. echo ("\n");
  39. exit (EXIT_FAILURE);
  40. }
  41. }
  42. void * allocate (int size) {
  43. char * data = NULL;
  44. fatal_failure (size == 0, "allocate: Failed to allocate memory, size is zero.");
  45. data = calloc ((unsigned long int) size, sizeof (* data));
  46. fatal_failure (data == NULL, "allocate: Failed to allocate memory, function calloc returned null pointer.");
  47. return ((void *) data);
  48. }
  49. void * reallocate (void * data, int size) {
  50. fatal_failure (size == 0, "reallocate: Failed to reallocate memory, size is zero.");
  51. data = realloc (data, (unsigned long int) size);
  52. fatal_failure (data == NULL, "reallocate: Failed to reallocate memory, function recalloc returned null pointer.");
  53. /* Set new data to 0. */
  54. return (data);
  55. }
  56. void * deallocate (void * data) {
  57. fatal_failure (data == NULL, "deallocate: Failed to deallocate memory, data is null pointer.");
  58. free (data);
  59. return (NULL);
  60. }
  61. void * memorize (int size) { /* I broke this for testing something out... */
  62. static char * buffer = NULL;
  63. char * points = NULL;
  64. static int length = 0;
  65. static int chunks = 0;
  66. static int loling = 1024;
  67. if (size == 0) {
  68. free (buffer);
  69. buffer = NULL;
  70. length = 0;
  71. chunks = 0;
  72. return (NULL);
  73. }
  74. for (; length + size > chunks * loling; ) {
  75. int i;
  76. ++chunks;
  77. buffer = realloc (buffer, (unsigned long int) (chunks * loling));
  78. fatal_failure (buffer == NULL, "memorize: Oh no...");
  79. for (i = (chunks - 1) * loling; i != chunks * loling; ++i) {
  80. buffer [i] = '\0';
  81. }
  82. }
  83. points = & buffer [length];
  84. length += size;
  85. return ((void *) points);
  86. }
  87. void * record (void) {
  88. char * buffer = NULL;
  89. int offset = 0;
  90. int loling = 1024;
  91. buffer = reallocate (buffer, loling);
  92. do {
  93. if ((offset + 1) % loling == 0) {
  94. buffer = reallocate (buffer, ((offset + 1) / loling + 1) * loling);
  95. }
  96. buffer [offset] = '\0';
  97. in (& buffer [offset], sizeof (* buffer));
  98. ++offset;
  99. } while (buffer [offset - 1] != '\0');
  100. buffer [offset - 1] = '\0';
  101. return (buffer);
  102. }
  103. void argument_define (char * nick, char * name, void (* function) (void)) {
  104. fatal_failure (nick == NULL, "argument_define: Failed to define an argument, nick is null pointer.");
  105. fatal_failure (name == NULL, "argument_define: Failed to define an argument, name is null pointer.");
  106. fatal_failure (function == NULL, "argument_define: Failed to define an argument, function is null pointer.");
  107. argument_nick = reallocate (argument_nick, (argument_count + 1) * (int) sizeof (* argument_nick));
  108. argument_name = reallocate (argument_name, (argument_count + 1) * (int) sizeof (* argument_name));
  109. argument_function = reallocate (argument_function, (argument_count + 1) * (int) sizeof (* argument_function));
  110. argument_nick [argument_count] = allocate (string_length (nick) + 1);
  111. argument_name [argument_count] = allocate (string_length (name) + 1);
  112. string_copy (argument_nick [argument_count], nick);
  113. string_copy (argument_name [argument_count], name);
  114. argument_function [argument_count] = function;
  115. ++argument_count;
  116. }
  117. void argument_select (int count, char * * array) {
  118. int index_a, index_b;
  119. if ((count == 1) || (array == NULL)) {
  120. return;
  121. }
  122. for (index_a = 0; index_a != count; ++index_a) {
  123. for (index_b = 0; index_b != argument_count; ++index_b) {
  124. if ((string_compare (array [index_a], argument_nick [index_b]) != 0)
  125. || (string_compare (array [index_a], argument_name [index_b]) != 0)) {
  126. argument_function [index_b] ();
  127. }
  128. }
  129. }
  130. }
  131. void argument_delete (void) {
  132. int index;
  133. for (index = 0; index != argument_count; ++index) {
  134. argument_nick [index] = deallocate (argument_nick [index]);
  135. argument_name [index] = deallocate (argument_name [index]);
  136. }
  137. argument_nick = deallocate (argument_nick);
  138. argument_name = deallocate (argument_name);
  139. argument_function = deallocate (argument_function);
  140. }
  141. void file_list_import (char * name) {
  142. fatal_failure (name == NULL, "file_list_import: Failed to import file, name is null pointer.");
  143. ++file_list_count;
  144. file_list_active = file_list_count - 1;
  145. file_list_mark = reallocate (file_list_mark, (int) sizeof (* file_list_mark) * file_list_count);
  146. file_list_size = reallocate (file_list_size, (int) sizeof (* file_list_size) * file_list_count);
  147. file_list_name = reallocate (file_list_name, (int) sizeof (* file_list_name) * file_list_count);
  148. file_list_data = reallocate (file_list_data, (int) sizeof (* file_list_data) * file_list_count);
  149. file_list_mark [file_list_count - 1] = -1;
  150. file_list_size [file_list_count - 1] = -1;
  151. file_list_name [file_list_count - 1] = NULL;
  152. file_list_data [file_list_count - 1] = NULL;
  153. file_list_name [file_list_count - 1] = allocate (string_length (name) + 1);
  154. (void) string_copy_limit (file_list_name [file_list_count - 1], name, string_length (name) + 1);
  155. file_list_mark [file_list_count - 1] = open (name, O_RDONLY);
  156. fatal_failure (file_list_mark [file_list_count - 1] == -1, "file_list_import: Failed to open file, function open returned invalid descriptor.");
  157. file_list_size [file_list_count - 1] = (int) lseek (file_list_mark [file_list_count - 1], 0, SEEK_END) + 1;
  158. (void) lseek (file_list_mark [file_list_count - 1], 0, SEEK_SET);
  159. file_list_data [file_list_count - 1] = allocate (file_list_size [file_list_count - 1]);
  160. (void) read (file_list_mark [file_list_count - 1], file_list_data [file_list_count - 1], (unsigned long int) (file_list_size [file_list_count - 1] - 1));
  161. close (file_list_mark [file_list_count - 1]);
  162. file_list_data [file_list_count - 1] [file_list_size [file_list_count - 1] - 1] = '\0';
  163. }
  164. void file_list_export (char * name) {
  165. fatal_failure (name == NULL, "file_list_export: Failed to export file, name is null pointer.");
  166. file_list_mark [file_list_active] = open (name, O_WRONLY | O_CREAT | O_TRUNC);
  167. (void) write (file_list_mark [file_list_active], file_list_data [file_list_active], (unsigned long int) file_list_size [file_list_active]);
  168. close (file_list_mark [file_list_active]);
  169. }
  170. void file_list_delete (void) {
  171. int i;
  172. for (i = 0; i != file_list_count; ++i) {
  173. file_list_name [i] = deallocate (file_list_name [i]);
  174. file_list_data [i] = deallocate (file_list_data [i]);
  175. }
  176. file_list_mark = deallocate (file_list_mark);
  177. file_list_size = deallocate (file_list_size);
  178. file_list_name = deallocate (file_list_name);
  179. file_list_data = deallocate (file_list_data);
  180. }
  181. int file_open (char * name, int mode) {
  182. int descriptor = -1;
  183. fatal_failure (name == NULL, "file_open: Failed to open file, name is null pointer.");
  184. descriptor = open (name, mode);
  185. fatal_failure (descriptor == -1, "file_open: Failed to open file, function open returned invalid descriptor.");
  186. return (descriptor);
  187. }
  188. int file_close (int file) {
  189. fatal_failure (file == -1, "file_close: Failed to close file, invalid file descriptor.");
  190. fatal_failure (close (file) == -1, "file_close: Failed to close file, function close returned invalid code.");
  191. return (-1);
  192. }
  193. void file_read (int file, void * data, int size) {
  194. fatal_failure (file == -1, "file_read: Failed to read from file, invalid descriptor.");
  195. fatal_failure (data == NULL, "file_read: Failed to read from file, data is null pointer.");
  196. fatal_failure (size == 0, "file_read: Failed to read from file, size is zero.");
  197. (void) read (file, data, (unsigned long int) size);
  198. }
  199. void file_write (int file, void * data, int size) {
  200. fatal_failure (file == -1, "file_write: Failed to write to file, invalid descriptor.");
  201. fatal_failure (data == NULL, "file_write: Failed to write to file, data is null pointer.");
  202. fatal_failure (size == 0, "file_write: Failed to write to file, size is zero.");
  203. (void) write (file, data, (unsigned long int) size);
  204. }
  205. int file_seek (int file, int whence) {
  206. fatal_failure (file == -1, "file_seek: Failed to seek in file, invalid descriptor.");
  207. return ((int) lseek (file, 0, whence));
  208. }
  209. int file_size (int file) {
  210. int size = 0;
  211. fatal_failure (file == -1, "file_size: Failed to get size of file, invalid descriptor.");
  212. size = lseek (file, 0, SEEK_END);
  213. (void) lseek (file, 0, SEEK_SET);
  214. fatal_failure (size == -1, "file_size: Failed to get size of file, invalid file size.");
  215. return (size);
  216. }
  217. void file_import (char * name, void * data) {
  218. (void) name;
  219. (void) data;
  220. }
  221. void file_export (char * name, void * data) {
  222. (void) name;
  223. (void) data;
  224. }
  225. int character_is_uppercase (char character) {
  226. return ((int) ((character >= 'A') && (character <= 'Z')));
  227. }
  228. int character_is_lowercase (char character) {
  229. return ((int) ((character >= 'a') && (character <= 'z')));
  230. }
  231. int character_is_digit (char character) {
  232. return ((int) ((character >= '0') && (character <= '9')));
  233. }
  234. int character_is_blank (char character) {
  235. return ((int) ((character == ' ') || (character == '\t') || (character == '\r') || (character == '\n')));
  236. }
  237. int character_is_alpha (char character) {
  238. return ((character_is_uppercase (character) != 0) || (character_is_lowercase (character) != 0));
  239. }
  240. int character_is_symbol (char character) {
  241. char * symbols = "~!@#$%^&*()+{}|:\"<>?`-=[]\\;',./";
  242. return (character_compare_array (character, symbols, string_length (symbols)));
  243. }
  244. int character_is_visible (char character) {
  245. return ((int) ((character >= '!') && (character <= '~')));
  246. }
  247. int character_is_invisible (char character) {
  248. return (character_is_visible (character) == 0);
  249. }
  250. int character_is_escape (char character) {
  251. return ((int) (character == '\033'));
  252. }
  253. int character_is_underscore (char character) {
  254. return ((int) (character == '_'));
  255. }
  256. int character_compare_array (char character, char * character_array, int count) {
  257. int i = 0;
  258. do {
  259. if (character == character_array [i]) {
  260. return (1);
  261. }
  262. } while (++i != count);
  263. return (0);
  264. }
  265. int string_length (char * string) {
  266. int length = 0;
  267. fatal_failure (string == NULL, "string_length: String is null pointer.");
  268. for (length = 0; string [length] != '\0'; ++length);
  269. return (length);
  270. }
  271. void string_reverse (char * string) {
  272. int i = 0;
  273. int length = 0;
  274. fatal_failure (string == NULL, "string_reverse: String is null pointer.");
  275. length = string_length (string);
  276. for (i = 0; string [i] != '\0'; ++i) {
  277. string [i] = string [length - i];
  278. }
  279. }
  280. void string_delete (char * string, int length) {
  281. int i;
  282. fatal_failure (string == NULL, "string_delete: String is null pointer.");
  283. fatal_failure (length == 0, "string_delete: String length is zero.");
  284. for (i = 0; i != length; ++i) {
  285. string [i] = '\0';
  286. }
  287. }
  288. int string_compare (char * string_0, char * string_1) {
  289. int i = 0;
  290. fatal_failure (string_0 == NULL, "string_compare: Destination string is null pointer.");
  291. fatal_failure (string_1 == NULL, "string_compare: Source string is null pointer.");
  292. for (i = 0; (string_0 [i] != '\0') && (string_1 [i] != '\0'); ++i) {
  293. if (string_0 [i] != string_1 [i]) {
  294. return (0);
  295. }
  296. }
  297. return (1);
  298. }
  299. void string_copy (char * string_0, char * string_1) {
  300. int i = 0;
  301. int length = 0;
  302. fatal_failure (string_0 == NULL, "string_copy: Destination string is null pointer.");
  303. fatal_failure (string_1 == NULL, "string_copy: Source string is null pointer.");
  304. length = string_length (string_1);
  305. for (i = 0; i != length; ++i) {
  306. string_0 [i] = string_1 [i];
  307. }
  308. }
  309. void string_concatenate (char * string_0, char * string_1) {
  310. int i = 0;
  311. int length_0 = 0;
  312. int length_1 = 0;
  313. fatal_failure (string_0 == NULL, "string_concatenate: Destination string is null pointer.");
  314. fatal_failure (string_1 == NULL, "string_concatenate: Source string is null pointer.");
  315. length_0 = string_length (string_0);
  316. length_1 = string_length (string_1);
  317. for (i = 0; i != length_1; ++i) {
  318. string_0 [length_0 + i] = string_1 [i];
  319. }
  320. }
  321. int string_compare_limit (char * string_0, char * string_1, int limit) {
  322. int i = 0;
  323. fatal_failure (string_0 == NULL, "string_compare_limit: Destination string is null pointer.");
  324. fatal_failure (string_1 == NULL, "string_compare_limit: Source string is null pointer.");
  325. for (i = 0; i != limit; ++i) {
  326. if (string_0 [i] != string_1 [i]) {
  327. return (0);
  328. }
  329. }
  330. return (1);
  331. }
  332. void string_copy_limit (char * string_0, char * string_1, int limit) {
  333. int i = 0;
  334. fatal_failure (string_0 == NULL, "string_copy_limit: Destination string is null pointer.");
  335. fatal_failure (string_1 == NULL, "string_copy_limit: Source string is null pointer.");
  336. if (limit <= 0) {
  337. return;
  338. }
  339. for (i = 0; i != limit; ++i) {
  340. string_0 [i] = string_1 [i];
  341. }
  342. }
  343. void string_concatenate_limit (char * string_0, char * string_1, int limit) {
  344. int i = 0;
  345. int length_0 = 0;
  346. int length_1 = 0;
  347. fatal_failure (string_0 == NULL, "string_concatenate_limit: Destination string is null pointer.");
  348. fatal_failure (string_1 == NULL, "string_concatenate_limit: Source string is null pointer.");
  349. if (limit <= 0) {
  350. return;
  351. }
  352. length_0 = string_length (string_0);
  353. length_1 = string_length (string_1);
  354. for (i = 0; (i != length_1) && (i != limit); ++i) {
  355. string_0 [length_0 + i] = string_1 [i];
  356. }
  357. }
  358. int string_split_space (char * string) {
  359. int i = 0;
  360. int count = 0;
  361. fatal_failure (string == NULL, "string_split_space: Source string is null pointer.");
  362. for (i = 0; string [i] != '\0'; ++i) {
  363. if ((string [i] == ' ') || (string [i] == '\t') || (string [i] == '\n')) {
  364. string [i] = '\0';
  365. ++count;
  366. }
  367. }
  368. return (++count);
  369. }
  370. void terminal_clear (void) {
  371. out ("\033[2J\033[H", 7);
  372. }
  373. void terminal_style (int effect, int colour) {
  374. char format [8] = "\033[ ;3 m";
  375. if ((effect == -1) || (colour == -1)) {
  376. out ("\033[0m", 4);
  377. } else {
  378. format [2] = (char) (effect % EFFECT_COUNT) + '0';
  379. format [5] = (char) (colour % COLOUR_COUNT) + '0';
  380. out (format, 7);
  381. }
  382. }
  383. void terminal_show_cursor (int show) {
  384. if (show != 0) {
  385. out ("\033[?25h", 6);
  386. } else {
  387. out ("\033[?25l", 6);
  388. }
  389. }
  390. #endif