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.

822 lines
19KB

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