Xtandard stuff...
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

935 rindas
23KB

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