Xtandard stuff...
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

771 líneas
20KB

  1. /*
  2. Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic
  3. Xtandard is free software! You will redistribute it or modify it under the terms of the GNU General Public License by Free Software Foundation.
  4. 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.
  5. 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.
  6. */
  7. #ifndef XTANDARD_SOURCE
  8. #define XTANDARD_SOURCE
  9. #include <xolatile/xtandard.h>
  10. #include <stdlib.h>
  11. #include <unistd.h>
  12. #include <fcntl.h>
  13. static char * log_notify = NULL;
  14. static int argument_count = 0;
  15. static char * * argument_nick = NULL;
  16. static char * * argument_name = NULL;
  17. static char * argument_input = NULL;
  18. static char * argument_output = NULL;
  19. static void (* * argument_function) (void) = NULL;
  20. static int file_list_active = 0;
  21. static int file_list_count = 0;
  22. static int * file_list_mark = NULL;
  23. static int * file_list_size = NULL;
  24. static char * * file_list_name = NULL;
  25. static char * * file_list_data = NULL;
  26. void in (void * data, int size) {
  27. fatal_failure (data == NULL, "in: Failed to read from standard input, data is null pointer.");
  28. fatal_failure (size == 0, "in: Failed to read from standard input, size is zero.");
  29. (void) read (STDIN_FILENO, data, (unsigned long int) size);
  30. }
  31. void out (void * data, int size) {
  32. fatal_failure (data == NULL, "out: Failed to write to standard output, data is null pointer.");
  33. fatal_failure (size == 0, "out: Failed to write to standard output, size is zero.");
  34. (void) write (STDOUT_FILENO, data, (unsigned long int) size);
  35. }
  36. void log_in (int type, int flag, char * data) {
  37. int start;
  38. char * type_mark [LOG_COUNT] = {
  39. "[\033[1;32mSuccess\033[0m] ",
  40. "[\033[1;33mWarning\033[0m] ",
  41. "[\033[1;31mFailure\033[0m] ",
  42. "[\033[1;30mComment\033[0m] "
  43. };
  44. if ((flag == 0) || (type <= -1) || (type >= LOG_COUNT)) {
  45. return;
  46. }
  47. start = string_length (log_notify);
  48. log_notify = reallocate (log_notify, start + string_length (type_mark [type]) + string_length (data) + 2);
  49. log_notify [start] = '\0';
  50. string_concatenate (log_notify, type_mark [type]);
  51. string_concatenate (log_notify, data);
  52. string_concatenate (log_notify, "\n");
  53. }
  54. void log_out (char * name) {
  55. dump (name, log_notify);
  56. log_notify = deallocate (log_notify);
  57. }
  58. void echo (char * data) {
  59. if (data == NULL) {
  60. return;
  61. }
  62. out (data, string_length (data));
  63. }
  64. void dump (char * name, char * data) {
  65. int file = -1;
  66. if (name == NULL) {
  67. echo (data);
  68. return;
  69. }
  70. if (data == NULL) {
  71. return;
  72. }
  73. file = file_open (name, O_RDWR | O_CREAT | O_APPEND);
  74. file_write (file, data, string_length (data));
  75. file = file_close (file);
  76. }
  77. void echo_byte (int byte) {
  78. out ("0123456789ABCDEF" + (byte % 256) / 16, 1);
  79. out ("0123456789ABCDEF" + (byte % 256) % 16, 1);
  80. out (" ", 1);
  81. }
  82. void fatal_failure (int condition, char * message) {
  83. if (condition != 0) {
  84. echo ("[\033[1;31mExiting\033[0m] ");
  85. echo (message);
  86. echo ("\n");
  87. exit (EXIT_FAILURE);
  88. }
  89. }
  90. void limit (int * value, int minimum, int maximum) {
  91. if (value == NULL) {
  92. return;
  93. }
  94. if (* value <= minimum) {
  95. * value = minimum;
  96. }
  97. if (* value >= maximum) {
  98. * value = maximum;
  99. }
  100. }
  101. void * allocate (int size) {
  102. char * data = NULL;
  103. if (size <= 0) {
  104. return (NULL);
  105. }
  106. data = calloc ((unsigned long int) size, sizeof (* data));
  107. fatal_failure (data == NULL, "standard : allocate : Failed to allocate memory, internal function 'calloc' returned null pointer.");
  108. return ((void *) data);
  109. }
  110. void * reallocate (void * data, int size) {
  111. if (size <= 0) {
  112. return (data);
  113. }
  114. data = realloc (data, (unsigned long int) size);
  115. fatal_failure (data == NULL, "standard : reallocate: Failed to reallocate memory, internal function 'realloc' returned null pointer.");
  116. /* Set new data to 0. */
  117. return (data);
  118. }
  119. void * deallocate (void * data) {
  120. if (data != NULL) {
  121. free (data);
  122. }
  123. return (NULL);
  124. }
  125. void * memorize (int size) { /* I broke this for testing something out... */
  126. static char * buffer = NULL;
  127. char * points = NULL;
  128. static int length = 0;
  129. static int chunks = 0;
  130. static int loling = 1024;
  131. if (size == 0) {
  132. free (buffer);
  133. buffer = NULL;
  134. length = 0;
  135. chunks = 0;
  136. return (NULL);
  137. }
  138. for (; length + size > chunks * loling; ) {
  139. int i;
  140. ++chunks;
  141. buffer = realloc (buffer, (unsigned long int) (chunks * loling));
  142. fatal_failure (buffer == NULL, "memorize: Oh no...");
  143. for (i = (chunks - 1) * loling; i != chunks * loling; ++i) {
  144. buffer [i] = '\0';
  145. }
  146. }
  147. points = & buffer [length];
  148. length += size;
  149. return ((void *) points);
  150. }
  151. void * record (void) {
  152. char * buffer = NULL;
  153. int offset = 0;
  154. int loling = 1024;
  155. buffer = reallocate (buffer, loling);
  156. do {
  157. if ((offset + 1) % loling == 0) {
  158. buffer = reallocate (buffer, ((offset + 1) / loling + 1) * loling);
  159. }
  160. buffer [offset] = '\0';
  161. in (& buffer [offset], sizeof (* buffer));
  162. ++offset;
  163. } while (buffer [offset - 1] != '\0');
  164. buffer [offset - 1] = '\0';
  165. return (buffer);
  166. }
  167. void argument_define (char * nick, char * name, void (* function) (void)) {
  168. fatal_failure (nick == NULL, "argument_define: Failed to define an argument, nick is null pointer.");
  169. fatal_failure (name == NULL, "argument_define: Failed to define an argument, name is null pointer.");
  170. fatal_failure (function == NULL, "argument_define: Failed to define an argument, function is null pointer.");
  171. ++argument_count;
  172. argument_nick = reallocate (argument_nick, argument_count * (int) sizeof (* argument_nick));
  173. argument_name = reallocate (argument_name, argument_count * (int) sizeof (* argument_name));
  174. argument_function = reallocate (argument_function, argument_count * (int) sizeof (* argument_function));
  175. argument_nick [argument_count - 1] = allocate (string_length (nick) + 1);
  176. argument_name [argument_count - 1] = allocate (string_length (name) + 1);
  177. string_copy (argument_nick [argument_count - 1], nick);
  178. string_copy (argument_name [argument_count - 1], name);
  179. argument_function [argument_count - 1] = function;
  180. }
  181. void argument_select (int count, char * * array) {
  182. int index_a = 0;
  183. int index_b = 0;
  184. if ((count == 1) || (array == NULL)) {
  185. return;
  186. }
  187. for (index_a = 1; index_a != count; ++index_a) {
  188. for (index_b = 0; index_b != argument_count; ++index_b) {
  189. if ((string_compare (array [index_a], "-h") != 0) || (string_compare (array [index_a], "--help") != 0)) {
  190. for (index_b = 0; index_b != argument_count; ++index_b) {
  191. echo ("\t"); echo (argument_nick [index_b]);
  192. echo (" "); echo (argument_name [index_b]);
  193. echo ("\n");
  194. }
  195. } else if ((string_compare (array [index_a], "-i") != 0) || (string_compare (array [index_a], "--input") != 0)) {
  196. ++index_a;
  197. argument_input = array [index_a];
  198. break;
  199. } else if ((string_compare (array [index_a], "-o") != 0) || (string_compare (array [index_a], "--output") != 0)) {
  200. ++index_a;
  201. argument_output = array [index_a];
  202. break;
  203. } else if ((string_compare (array [index_a], argument_nick [index_b]) != 0) || (string_compare (array [index_a], argument_name [index_b]) != 0)) {
  204. argument_function [index_b] ();
  205. break;
  206. }
  207. }
  208. }
  209. }
  210. void argument_delete (void) {
  211. int index;
  212. for (index = 0; index != argument_count; ++index) {
  213. argument_nick [index] = deallocate (argument_nick [index]);
  214. argument_name [index] = deallocate (argument_name [index]);
  215. }
  216. argument_nick = deallocate (argument_nick);
  217. argument_name = deallocate (argument_name);
  218. argument_function = deallocate (argument_function);
  219. }
  220. int file_open (char * name, int mode) {
  221. int descriptor = -1;
  222. fatal_failure (name == NULL, "file_open: Failed to open file, name is null pointer.");
  223. descriptor = open (name, mode);
  224. fatal_failure (descriptor == -1, "file_open: Failed to open file, function open returned invalid descriptor.");
  225. return (descriptor);
  226. }
  227. int file_close (int file) {
  228. fatal_failure (file == -1, "file_close: Failed to close file, invalid file descriptor.");
  229. fatal_failure (close (file) == -1, "file_close: Failed to close file, function close returned invalid code.");
  230. return (-1);
  231. }
  232. void file_read (int file, void * data, int size) {
  233. fatal_failure (file == -1, "file_read: Failed to read from file, invalid descriptor.");
  234. fatal_failure (data == NULL, "file_read: Failed to read from file, data is null pointer.");
  235. fatal_failure (size == 0, "file_read: Failed to read from file, size is zero.");
  236. (void) read (file, data, (unsigned long int) size);
  237. }
  238. void file_write (int file, void * data, int size) {
  239. fatal_failure (file == -1, "file_write: Failed to write to file, invalid descriptor.");
  240. fatal_failure (data == NULL, "file_write: Failed to write to file, data is null pointer.");
  241. fatal_failure (size == 0, "file_write: Failed to write to file, size is zero.");
  242. (void) write (file, data, (unsigned long int) size);
  243. }
  244. int file_seek (int file, int whence) {
  245. fatal_failure (file == -1, "file_seek: Failed to seek in file, invalid descriptor.");
  246. return ((int) lseek (file, 0, whence));
  247. }
  248. int file_size (int file) {
  249. int size = 0;
  250. fatal_failure (file == -1, "file_size: Failed to get size of file, invalid descriptor.");
  251. size = lseek (file, 0, SEEK_END);
  252. (void) lseek (file, 0, SEEK_SET);
  253. fatal_failure (size == -1, "file_size: Failed to get size of file, invalid file size.");
  254. return (size);
  255. }
  256. int file_type (char * name) {
  257. char * file_type_data [FILE_TYPE_COUNT] = {
  258. ".txt", ".s", ".fasm", ".gasm", ".nasm", ".yasm", ".c", ".h",
  259. ".adb", ".ads", ".cpp", ".hpp"
  260. };
  261. int type = 0;
  262. while (* name != '.') {
  263. ++name;
  264. }
  265. for (type = 0; type != FILE_TYPE_COUNT; ++type) {
  266. if (string_compare (name, file_type_data [type]) != 0) {
  267. return (type);
  268. }
  269. }
  270. return (-1);
  271. }
  272. char * file_import (char * name) {
  273. int file = -1;
  274. int size = -1;
  275. char * data = NULL;
  276. fatal_failure (name == NULL, "file_import: Failed to import file, name is null pointer.");
  277. fatal_failure (data != NULL, "file_import: Failed to import file, data is not null pointer.");
  278. file = file_open (name, O_RDONLY);
  279. size = file_size (file) + 1;
  280. data = allocate (size);
  281. file_read (file, data, size - 1);
  282. data [size - 1] = '\0';
  283. file = file_close (file);
  284. return (data);
  285. }
  286. void file_export (char * name, void * data) {
  287. (void) name;
  288. (void) data;
  289. }
  290. void file_list_import (char * name) {
  291. fatal_failure (name == NULL, "file_list_import: Failed to import file, name is null pointer.");
  292. ++file_list_count;
  293. file_list_active = file_list_count - 1;
  294. file_list_mark = reallocate (file_list_mark, (int) sizeof (* file_list_mark) * file_list_count);
  295. file_list_size = reallocate (file_list_size, (int) sizeof (* file_list_size) * file_list_count);
  296. file_list_name = reallocate (file_list_name, (int) sizeof (* file_list_name) * file_list_count);
  297. file_list_data = reallocate (file_list_data, (int) sizeof (* file_list_data) * file_list_count);
  298. file_list_mark [file_list_count - 1] = -1;
  299. file_list_size [file_list_count - 1] = -1;
  300. file_list_name [file_list_count - 1] = NULL;
  301. file_list_data [file_list_count - 1] = NULL;
  302. file_list_name [file_list_count - 1] = allocate (string_length (name) + 1);
  303. (void) string_copy_limit (file_list_name [file_list_count - 1], name, string_length (name) + 1);
  304. file_list_mark [file_list_count - 1] = open (name, O_RDONLY);
  305. fatal_failure (file_list_mark [file_list_count - 1] == -1, "file_list_import: Failed to open file, function open returned invalid descriptor.");
  306. file_list_size [file_list_count - 1] = (int) lseek (file_list_mark [file_list_count - 1], 0, SEEK_END) + 1;
  307. (void) lseek (file_list_mark [file_list_count - 1], 0, SEEK_SET);
  308. file_list_data [file_list_count - 1] = allocate (file_list_size [file_list_count - 1]);
  309. (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));
  310. close (file_list_mark [file_list_count - 1]);
  311. file_list_data [file_list_count - 1] [file_list_size [file_list_count - 1] - 1] = '\0';
  312. }
  313. void file_list_export (char * name) {
  314. fatal_failure (name == NULL, "file_list_export: Failed to export file, name is null pointer.");
  315. file_list_mark [file_list_active] = open (name, O_WRONLY | O_CREAT | O_TRUNC);
  316. (void) write (file_list_mark [file_list_active], file_list_data [file_list_active], (unsigned long int) file_list_size [file_list_active]);
  317. close (file_list_mark [file_list_active]);
  318. }
  319. void file_list_delete (void) {
  320. int i;
  321. for (i = 0; i != file_list_count; ++i) {
  322. file_list_name [i] = deallocate (file_list_name [i]);
  323. file_list_data [i] = deallocate (file_list_data [i]);
  324. }
  325. file_list_mark = deallocate (file_list_mark);
  326. file_list_size = deallocate (file_list_size);
  327. file_list_name = deallocate (file_list_name);
  328. file_list_data = deallocate (file_list_data);
  329. }
  330. int character_is_uppercase (char character) {
  331. return ((int) ((character >= 'A') && (character <= 'Z')));
  332. }
  333. int character_is_lowercase (char character) {
  334. return ((int) ((character >= 'a') && (character <= 'z')));
  335. }
  336. int character_is_digit (char character) {
  337. return ((int) ((character >= '0') && (character <= '9')));
  338. }
  339. int character_is_blank (char character) {
  340. return ((int) ((character == ' ') || (character == '\t') || (character == '\r') || (character == '\n')));
  341. }
  342. int character_is_alpha (char character) {
  343. return ((character_is_uppercase (character) != 0) || (character_is_lowercase (character) != 0));
  344. }
  345. int character_is_symbol (char character) {
  346. char * symbols = "~!@#$%^&*()+{}|:\"<>?`-=[]\\;',./";
  347. return (character_compare_array (character, symbols));
  348. }
  349. int character_is_visible (char character) {
  350. return ((int) ((character >= ' ') && (character <= '~')));
  351. }
  352. int character_is_invisible (char character) {
  353. return (character_is_visible (character) == 0);
  354. }
  355. int character_is_escape (char character) {
  356. return ((int) (character == '\033'));
  357. }
  358. int character_is_underscore (char character) {
  359. return ((int) (character == '_'));
  360. }
  361. int character_is_hexadecimal (char character) {
  362. char * hexadecimals = "0123456789ABCDEF";
  363. return (character_compare_array (character, hexadecimals));
  364. }
  365. int character_compare_array (char character, char * character_array) {
  366. int i = 0;
  367. do {
  368. if (character == character_array [i]) {
  369. return (1);
  370. }
  371. } while (++i != string_length (character_array));
  372. return (0);
  373. }
  374. int string_length (char * string) {
  375. int length = 0;
  376. if (string == NULL) {
  377. return (0);
  378. }
  379. for (length = 0; string [length] != '\0'; ++length);
  380. return (length);
  381. }
  382. void string_reverse (char * string) {
  383. int i = 0;
  384. int length = 0;
  385. fatal_failure (string == NULL, "string_reverse: String is null pointer.");
  386. length = string_length (string);
  387. for (i = 0; string [i] != '\0'; ++i) {
  388. string [i] = string [length - i];
  389. }
  390. }
  391. void string_delete (char * string, int length) {
  392. int i;
  393. if ((string == NULL) || (length <= 0)) {
  394. return;
  395. }
  396. for (i = 0; i != length; ++i) {
  397. string [i] = '\0';
  398. }
  399. }
  400. int string_compare (char * string_0, char * string_1) {
  401. int i = 0;
  402. fatal_failure (string_0 == NULL, "string_compare: Destination string is null pointer.");
  403. fatal_failure (string_1 == NULL, "string_compare: Source string is null pointer.");
  404. for (i = 0; (string_0 [i] != '\0') && (string_1 [i] != '\0'); ++i) {
  405. if (string_0 [i] != string_1 [i]) {
  406. return (0);
  407. }
  408. }
  409. return (1);
  410. }
  411. void string_copy (char * string_0, char * string_1) {
  412. int i = 0;
  413. fatal_failure (string_0 == NULL, "string_copy: Destination string is null pointer.");
  414. fatal_failure (string_1 == NULL, "string_copy: Source string is null pointer.");
  415. for (i = 0; i != string_length (string_1) + 1; ++i) {
  416. string_0 [i] = string_1 [i];
  417. }
  418. }
  419. void string_concatenate (char * string_0, char * string_1) {
  420. fatal_failure (string_0 == NULL, "string_concatenate: Destination string is null pointer.");
  421. fatal_failure (string_1 == NULL, "string_concatenate: Source string is null pointer.");
  422. string_0 += string_length (string_0);
  423. while (* string_1 != '\0') {
  424. * string_0++ = * string_1++;
  425. /*++string_0;
  426. ++string_1;*/
  427. }
  428. * string_0 = '\0';
  429. }
  430. int string_compare_limit (char * string_0, char * string_1, int limit) {
  431. int i = 0;
  432. fatal_failure (string_0 == NULL, "string_compare_limit: Destination string is null pointer.");
  433. fatal_failure (string_1 == NULL, "string_compare_limit: Source string is null pointer.");
  434. for (i = 0; i != limit; ++i) {
  435. if (string_0 [i] != string_1 [i]) {
  436. return (0);
  437. }
  438. }
  439. return (1);
  440. }
  441. void string_copy_limit (char * string_0, char * string_1, int limit) {
  442. int i = 0;
  443. fatal_failure (string_0 == NULL, "string_copy_limit: Destination string is null pointer.");
  444. fatal_failure (string_1 == NULL, "string_copy_limit: Source string is null pointer.");
  445. if (limit <= 0) {
  446. return;
  447. }
  448. for (i = 0; i != limit; ++i) {
  449. string_0 [i] = string_1 [i];
  450. }
  451. }
  452. void string_concatenate_limit (char * string_0, char * string_1, int limit) {
  453. int i = 0;
  454. int length_0 = 0;
  455. int length_1 = 0;
  456. fatal_failure (string_0 == NULL, "string_concatenate_limit: Destination string is null pointer.");
  457. fatal_failure (string_1 == NULL, "string_concatenate_limit: Source string is null pointer.");
  458. if (limit <= 0) {
  459. return;
  460. }
  461. length_0 = string_length (string_0);
  462. length_1 = string_length (string_1);
  463. for (i = 0; (i != length_1) && (i != limit); ++i) {
  464. string_0 [length_0 + i] = string_1 [i];
  465. }
  466. }
  467. int string_split_space (char * string) {
  468. int i = 0;
  469. int count = 0;
  470. fatal_failure (string == NULL, "string_split_space: Source string is null pointer.");
  471. for (i = 0; string [i] != '\0'; ++i) {
  472. if ((string [i] == ' ') || (string [i] == '\t') || (string [i] == '\n')) {
  473. string [i] = '\0';
  474. ++count;
  475. }
  476. }
  477. return (++count);
  478. }
  479. void memory_delete (void * memory, int length) {
  480. int i = 0;
  481. char * cast = (char *) memory;
  482. fatal_failure (memory == NULL, "memory_delete: Memory is null pointer.");
  483. if (length <= 0) {
  484. return;
  485. }
  486. for (i = 0; i != length; ++i) {
  487. cast [i] = '\0';
  488. }
  489. }
  490. int memory_compare (void * memory, void * source, int length) {
  491. int i = 0;
  492. char * cast_0 = (char *) memory;
  493. char * cast_1 = (char *) source;
  494. fatal_failure (memory == NULL, "memory_compare: Memory is null pointer.");
  495. fatal_failure (source == NULL, "memory_compare: Source is null pointer.");
  496. if (length <= 0) {
  497. return (-1);
  498. }
  499. for (i = 0; (cast_0 [i] != '\0') && (cast_1 [i] != '\0'); ++i) {
  500. if (cast_0 [i] != cast_1 [i]) {
  501. return (0);
  502. }
  503. }
  504. return (1);
  505. }
  506. void memory_copy (void * memory, void * source, int length) {
  507. int i = 0;
  508. char * cast_0 = (char *) memory;
  509. char * cast_1 = (char *) source;
  510. fatal_failure (memory == NULL, "memory_copy: Memory is null pointer.");
  511. fatal_failure (source == NULL, "memory_copy: Source is null pointer.");
  512. if (length <= 0) {
  513. return;
  514. }
  515. for (i = 0; i != length; ++i) {
  516. cast_0 [i] = cast_1 [i];
  517. }
  518. }
  519. void terminal_clear (void) {
  520. echo ("\033[2J\033[H");
  521. }
  522. void terminal_colour (int colour, int effect) {
  523. char format [8] = "\033[ ;3 m";
  524. format [2] = (char) (effect % EFFECT_COUNT) + '0';
  525. format [5] = (char) (colour % COLOUR_COUNT) + '0';
  526. echo (format);
  527. }
  528. void terminal_cancel (void) {
  529. echo ("\033[0m");
  530. }
  531. void terminal_show_cursor (int show) {
  532. if (show != 0) {
  533. echo ("\033[?25h");
  534. } else {
  535. echo ("\033[?25l");
  536. }
  537. }
  538. int encode_byte (char * byte) {
  539. int encode = 0;
  540. fatal_failure (character_is_hexadecimal (byte [0]) == 0, "encode_byte: Upper byte character is not hexadecimal digit.");
  541. fatal_failure (character_is_hexadecimal (byte [1]) == 0, "encode_byte: Lower byte character is not hexadecimal digit.");
  542. encode = ((byte [0] - (character_is_digit (byte [0]) ? ('0') : ('A' - 10))) << 4) | (byte [1] - (character_is_digit (byte [1]) ? ('0') : ('A' - 10)));
  543. return (encode);
  544. }
  545. char * decode_byte (int byte) {
  546. char * decode = " ";
  547. fatal_failure ((byte <= -1) || (byte >= 256), "decode_byte: Byte is out of range.");
  548. decode [0] = byte / 16;
  549. decode [1] = byte % 16;
  550. return (decode);
  551. }
  552. #endif