Xtandard stuff...
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

753 linhas
19KB

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