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.

824 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 * file_type_data [file_type_count] = {
  168. ".txt", ".s", ".fasm", ".gasm", ".nasm", ".yasm", ".c", ".h", ".adb", ".ads", ".cpp", ".hpp",
  169. ".f90", ".x"
  170. };
  171. int type = 0;
  172. while (* name != '.') {
  173. ++name;
  174. }
  175. for (type = 0; type != file_type_count; ++type) {
  176. if (string_compare (name, file_type_data [type])) {
  177. return (type);
  178. }
  179. }
  180. return (-1);
  181. }
  182. void * file_record (char * name) {
  183. int file = -1;
  184. int size = -1;
  185. char * data = null;
  186. fatal_failure (name == null, "file_import: Failed to import file, name is null pointer.");
  187. file = file_open (name, O_RDONLY);
  188. size = file_size (name);
  189. data = allocate (size);
  190. file_read (file, data, size);
  191. file = file_close (file);
  192. return (data);
  193. }
  194. char * file_import (char * name) {
  195. int file = -1;
  196. int size = -1;
  197. char * data = null;
  198. fatal_failure (name == null, "file_import: Failed to import file, name is null pointer.");
  199. file = file_open (name, O_RDONLY);
  200. size = file_size (name) + 1;
  201. data = allocate (size);
  202. file_read (file, data, size - 1);
  203. data [size - 1] = '\0';
  204. file = file_close (file);
  205. return (data);
  206. }
  207. void file_export (char * name, void * data) {
  208. (void) name;
  209. (void) data;
  210. }
  211. void file_list_import (char * name) {
  212. fatal_failure (name == null, "file_list_import: Failed to import file, name is null pointer.");
  213. ++file_list_count;
  214. file_list_active = file_list_count - 1;
  215. file_list_mark = reallocate (file_list_mark, (int) sizeof (* file_list_mark) * file_list_count);
  216. file_list_size = reallocate (file_list_size, (int) sizeof (* file_list_size) * file_list_count);
  217. file_list_name = reallocate (file_list_name, (int) sizeof (* file_list_name) * file_list_count);
  218. file_list_data = reallocate (file_list_data, (int) sizeof (* file_list_data) * file_list_count);
  219. file_list_mark [file_list_active] = -1;
  220. file_list_size [file_list_active] = -1;
  221. file_list_name [file_list_active] = null;
  222. file_list_data [file_list_active] = null;
  223. file_list_name [file_list_active] = allocate (string_length (name) + 1);
  224. (void) string_copy_limit (file_list_name [file_list_active], name, string_length (name) + 1);
  225. file_list_mark [file_list_active] = open (name, O_RDWR);
  226. fatal_failure (file_list_mark [file_list_active] == -1, "file_list_import: Failed to open file, function open returned invalid descriptor.");
  227. file_list_size [file_list_active] = (int) lseek (file_list_mark [file_list_active], 0, SEEK_END) + 1;
  228. (void) lseek (file_list_mark [file_list_active], 0, SEEK_SET);
  229. file_list_data [file_list_active] = allocate (file_list_size [file_list_active]);
  230. (void) read (file_list_mark [file_list_active], file_list_data [file_list_active], (unsigned long int) (file_list_size [file_list_active] - 1));
  231. close (file_list_mark [file_list_active]);
  232. file_list_data [file_list_active] [file_list_size [file_list_active] - 1] = '\0';
  233. }
  234. void file_list_export (char * name) {
  235. fatal_failure (name == null, "file_list_export: Failed to export file, name is null pointer.");
  236. file_list_mark [file_list_active] = open (name, O_WRONLY | O_CREAT | O_TRUNC);
  237. (void) write (file_list_mark [file_list_active], file_list_data [file_list_active], (unsigned long int) file_list_size [file_list_active]);
  238. close (file_list_mark [file_list_active]);
  239. }
  240. void file_list_insert_character (char character, int position) {
  241. int offset;
  242. ++file_list_size [file_list_active];
  243. if (file_list_size [file_list_active] < string_length (file_list_data [file_list_active])) {
  244. file_list_data [file_list_active] = reallocate (file_list_data [file_list_active], file_list_size [file_list_active]);
  245. }
  246. file_list_data [file_list_active] = reallocate (file_list_data [file_list_active], file_list_size [file_list_active]);
  247. for (offset = file_list_size [file_list_active] - 1; offset != position; --offset) {
  248. file_list_data [file_list_active] [offset] = file_list_data [file_list_active] [offset - 1];
  249. }
  250. file_list_data [file_list_active] [position] = character;
  251. }
  252. void file_list_remove_character (int position) {
  253. int offset;
  254. if (position == 0) {
  255. return;
  256. }
  257. --file_list_size [file_list_active];
  258. for (offset = position - 1; offset != file_list_size [file_list_active] - 1; ++offset) {
  259. file_list_data [file_list_active] [offset] = file_list_data [file_list_active] [offset + 1];
  260. }
  261. file_list_data [file_list_active] [offset] = '\0';
  262. }
  263. void file_list_delete (void) {
  264. int i;
  265. for (i = 0; i != file_list_count; ++i) {
  266. file_list_name [i] = deallocate (file_list_name [i]);
  267. file_list_data [i] = deallocate (file_list_data [i]);
  268. }
  269. file_list_mark = deallocate (file_list_mark);
  270. file_list_size = deallocate (file_list_size);
  271. file_list_name = deallocate (file_list_name);
  272. file_list_data = deallocate (file_list_data);
  273. }
  274. int character_is_uppercase (char character) {
  275. return ((int) ((character >= 'A') && (character <= 'Z')));
  276. }
  277. int character_is_lowercase (char character) {
  278. return ((int) ((character >= 'a') && (character <= 'z')));
  279. }
  280. int character_is_digit (char character) {
  281. return ((int) ((character >= '0') && (character <= '9')));
  282. }
  283. int character_is_blank (char character) {
  284. return ((int) ((character == ' ') || (character == '\t') || (character == '\r') || (character == '\n')));
  285. }
  286. int character_is_alpha (char character) {
  287. return ((character_is_uppercase (character) != 0) || (character_is_lowercase (character) != 0));
  288. }
  289. int character_is_symbol (char character) {
  290. char * symbols = "~!@#$%^&*()+{}|:\"<>?`-=[]\\;',./";
  291. return (character_compare_array (character, symbols));
  292. }
  293. int character_is_visible (char character) {
  294. return ((int) ((character >= ' ') && (character <= '~')));
  295. }
  296. int character_is_invisible (char character) {
  297. return (character_is_visible (character) == 0);
  298. }
  299. int character_is_escape (char character) {
  300. return ((int) (character == '\033'));
  301. }
  302. int character_is_underscore (char character) {
  303. return ((int) (character == '_'));
  304. }
  305. int character_is_hexadecimal (char character) {
  306. char * hexadecimals = "0123456789ABCDEF";
  307. return (character_compare_array (character, hexadecimals));
  308. }
  309. int character_compare_array (char character, char * character_array) {
  310. int i = 0;
  311. do {
  312. if (character == character_array [i]) {
  313. return (true);
  314. }
  315. } while (++i != string_length (character_array));
  316. return (false);
  317. }
  318. int character_count (char * string, char this, int from, int to, char stop) {
  319. int count;
  320. for (count = 0; (from != to) && (string [from] != stop); from += ((to < from) ? -1 : 1)) {
  321. count += (int) ((string [from] == this) || (this == '\0'));
  322. }
  323. return (count);
  324. }
  325. int string_length (char * string) {
  326. int length;
  327. if (string == null) {
  328. return (0);
  329. }
  330. for (length = 0; string [length] != '\0'; ++length);
  331. return (length);
  332. }
  333. char * string_reverse_limit (char * string, int limit) {
  334. int i;
  335. fatal_failure (string == null, "string_reverse: String is null pointer.");
  336. for (i = 0; i < limit / 2; ++i) {
  337. char temporary = string [i];
  338. string [i] = string [limit - 1 - i];
  339. string [limit - 1 - i] = temporary;
  340. }
  341. return (string);
  342. }
  343. char * string_reverse (char * string) {
  344. return (string_reverse_limit (string, string_length (string)));
  345. }
  346. char * string_delete (char * string, int length) {
  347. int i;
  348. if ((string == null) || (length <= 0)) {
  349. return (string);
  350. }
  351. for (i = 0; i != length; ++i) {
  352. string [i] = '\0';
  353. }
  354. return (string);
  355. }
  356. int string_compare (char * string_0, char * string_1) {
  357. int i = 0;
  358. fatal_failure (string_0 == null, "string_compare: Destination string is null pointer.");
  359. fatal_failure (string_1 == null, "string_compare: Source string is null pointer.");
  360. for (i = 0; (string_0 [i] != '\0') && (string_1 [i] != '\0'); ++i) {
  361. if (string_0 [i] != string_1 [i]) {
  362. return (false);
  363. }
  364. }
  365. return (true);
  366. }
  367. char * string_copy (char * string_0, char * string_1) {
  368. int i = 0;
  369. fatal_failure (string_0 == null, "string_copy: Destination string is null pointer.");
  370. fatal_failure (string_1 == null, "string_copy: Source string is null pointer.");
  371. for (i = 0; i != string_length (string_1) + 1; ++i) {
  372. string_0 [i] = string_1 [i];
  373. }
  374. return (string_0);
  375. }
  376. char * string_concatenate (char * string_0, char * string_1) {
  377. fatal_failure (string_0 == null, "string_concatenate: Destination string is null pointer.");
  378. fatal_failure (string_1 == null, "string_concatenate: Source string is null pointer.");
  379. string_0 += string_length (string_0);
  380. while (* string_1 != '\0') {
  381. * string_0++ = * string_1++;
  382. /*++string_0;
  383. ++string_1;*/
  384. }
  385. * string_0 = '\0';
  386. return (string_0);
  387. }
  388. int string_compare_limit (char * string_0, char * string_1, int limit) {
  389. int i = 0;
  390. fatal_failure (string_0 == null, "string_compare_limit: Destination string is null pointer.");
  391. fatal_failure (string_1 == null, "string_compare_limit: Source string is null pointer.");
  392. for (i = 0; i != limit; ++i) {
  393. if (string_0 [i] != string_1 [i]) {
  394. return (false);
  395. }
  396. }
  397. return (true);
  398. }
  399. char * string_copy_limit (char * string_0, char * string_1, int limit) {
  400. int i = 0;
  401. fatal_failure (string_0 == null, "string_copy_limit: Destination string is null pointer.");
  402. fatal_failure (string_1 == null, "string_copy_limit: Source string is null pointer.");
  403. if (limit <= 0) {
  404. return (string_0);
  405. }
  406. for (i = 0; i != limit; ++i) {
  407. string_0 [i] = string_1 [i];
  408. }
  409. return (string_0);
  410. }
  411. char * string_concatenate_limit (char * string_0, char * string_1, int limit) {
  412. int i = 0;
  413. int length_0 = 0;
  414. int length_1 = 0;
  415. fatal_failure (string_0 == null, "string_concatenate_limit: Destination string is null pointer.");
  416. fatal_failure (string_1 == null, "string_concatenate_limit: Source string is null pointer.");
  417. if (limit <= 0) {
  418. return (string_0);
  419. }
  420. length_0 = string_length (string_0);
  421. length_1 = string_length (string_1);
  422. for (i = 0; (i != length_1) && (i != limit); ++i) {
  423. string_0 [length_0 + i] = string_1 [i];
  424. }
  425. return (string_0);
  426. }
  427. int string_split_space (char * string) {
  428. int i = 0;
  429. int count = 0;
  430. fatal_failure (string == null, "string_split_space: Source string is null pointer.");
  431. for (i = 0; string [i] != '\0'; ++i) {
  432. if ((string [i] == ' ') || (string [i] == '\t') || (string [i] == '\n')) {
  433. string [i] = '\0';
  434. ++count;
  435. }
  436. }
  437. return (++count);
  438. }
  439. char * string_realign (char * string, int amount, char character) {
  440. int offset, length;
  441. length = string_length (string);
  442. for (offset = 0; offset != length; ++offset) {
  443. string [amount - offset - 1] = string [length - offset - 1];
  444. }
  445. for (offset = 0; offset != amount - length; ++offset) {
  446. string [offset] = character;
  447. }
  448. string [amount] = '\0';
  449. return (string);
  450. }
  451. void memory_delete (void * memory, int length) {
  452. int i = 0;
  453. char * cast = (char *) memory;
  454. fatal_failure (memory == null, "memory_delete: Memory is null pointer.");
  455. if (length <= 0) {
  456. return;
  457. }
  458. for (i = 0; i != length; ++i) {
  459. cast [i] = '\0';
  460. }
  461. }
  462. int memory_compare (void * memory, void * source, int length) {
  463. int i = 0;
  464. char * cast_0 = (char *) memory;
  465. char * cast_1 = (char *) source;
  466. fatal_failure (memory == null, "memory_compare: Memory is null pointer.");
  467. fatal_failure (source == null, "memory_compare: Source is null pointer.");
  468. if (length <= 0) {
  469. return (-1);
  470. }
  471. for (i = 0; (cast_0 [i] != '\0') && (cast_1 [i] != '\0'); ++i) {
  472. if (cast_0 [i] != cast_1 [i]) {
  473. return (false);
  474. }
  475. }
  476. return (true);
  477. }
  478. void memory_copy (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_copy: Memory is null pointer.");
  483. fatal_failure (source == null, "memory_copy: Source is null pointer.");
  484. if (length <= 0) {
  485. return;
  486. }
  487. for (i = 0; i != length; ++i) {
  488. cast_0 [i] = cast_1 [i];
  489. }
  490. }
  491. void terminal_clear (void) {
  492. echo ("\033[2J\033[H");
  493. }
  494. void terminal_colour (int colour, int effect) {
  495. char format [8] = "\033[ ;3 m";
  496. format [2] = (char) (effect % effect_count) + '0';
  497. format [5] = (char) (colour % colour_count) + '0';
  498. echo (format);
  499. }
  500. void terminal_cancel (void) {
  501. echo ("\033[0m");
  502. }
  503. void terminal_show_cursor (int show) {
  504. if (show != 0) {
  505. echo ("\033[?25h");
  506. } else {
  507. echo ("\033[?25l");
  508. }
  509. }
  510. int encode_byte (char * byte) {
  511. int encode = 0;
  512. fatal_failure (character_is_hexadecimal (byte [0]) == 0, "encode_byte: Upper byte character is not hexadecimal digit.");
  513. fatal_failure (character_is_hexadecimal (byte [1]) == 0, "encode_byte: Lower byte character is not hexadecimal digit.");
  514. encode = ((byte [0] - (character_is_digit (byte [0]) ? ('0') : ('A' - 10))) << 4) | (byte [1] - (character_is_digit (byte [1]) ? ('0') : ('A' - 10)));
  515. return (encode);
  516. }
  517. char * decode_byte (int byte) {
  518. char * decode = " ";
  519. fatal_failure ((byte <= -1) || (byte >= 256), "decode_byte: Byte is out of range.");
  520. decode [0] = byte / 16;
  521. decode [1] = byte % 16;
  522. return (decode);
  523. }
  524. char * number_to_string (int number) {
  525. int i, sign;
  526. static char string [32];
  527. string_delete (string, 32);
  528. if (number == 0) {
  529. string [0] = '0';
  530. string [1] = '\0';
  531. return (string);
  532. }
  533. if (number < 0) {
  534. number *= -1;
  535. sign = 1;
  536. } else {
  537. sign = 0;
  538. }
  539. for (i = (string [0] == '-'); number != 0; ++i) {
  540. string [i] = (char) (number % 10) + '0';
  541. number /= 10;
  542. }
  543. if (sign != 0) {
  544. string [i] = '-';
  545. ++i;
  546. }
  547. string [i] = '\0';
  548. string_reverse (string);
  549. return (string);
  550. }
  551. char * format_to_string (int number, int sign, int base, int amount, char character) {
  552. int i;
  553. static char string [32];
  554. string_delete (string, 32);
  555. if (number == 0) {
  556. string [0] = '0';
  557. string [1] = '\0';
  558. string_realign (string, amount, character);
  559. return (string);
  560. }
  561. if (number < 0) {
  562. number *= -1;
  563. }
  564. for (i = (string [0] == '-'); number != 0; ++i) {
  565. string [i] = "0123456789ABCDEF" [number % base];
  566. number /= base;
  567. }
  568. if (sign != 0) {
  569. string [i] = '-';
  570. ++i;
  571. }
  572. string [i] = '\0';
  573. string_reverse (string);
  574. string_realign (string, amount, character);
  575. return (string);
  576. }
  577. #endif