Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This file is in the Public Domain.
- * This file has no warranties of any kind,
- * And can be distributed, modified or used freely.
- *
- * Author: Vinyl Scratched
- */
- /*
- * This file uses the LAMP C syntax extensions Library.
- * The lamp header is required for this file to be compiled.
- */
- #include <ctype.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdarg.h>
- #include <stdlib.h>
- #include <console.h>
- DECL(pchar_t, putchar)(
- DECL(pchar_t, character)
- ) {
- _FRET write_char(character);
- }
- DECL(pchar_t, puts)(
- DECL(CONST _STR, str)
- ) {
- _FRET write_str(str);
- }
- _TD FUNC(pchar_t, out_t)(
- DECL(_STR, buffer),
- DECL(pchar_t, character)
- );
- _TD FUNC(pchar_t, outbulk_t)(
- DECL(_STR, buffer),
- DECL(CONST _STR, str)
- );
- ENUM {
- LJUSTIFY = 0,
- FORCE_PLUS = 1,
- FORCE_SPACE = 2,
- HASH_FLAG = 3,
- ZERO_FILL = 4
- };
- ENUM {
- LMOD_NONE = ZERO,
- LMOD_HH,
- LMOD_H,
- LMOD_L,
- LMOD_LL,
- LMOD_J,
- LMOD_Z,
- LMOD_T,
- LMOD_LF
- };
- HIDDEN INLINE
- DECL(byte_t, do_flags)(
- DECL(CONST _STR, format),
- DECL(value_t *, count)
- ) {
- DECL(byte_t, flags) = ZERO;
- _IF(format[*count] == '-') {
- SET_BIT(flags, LJUSTIFY);
- *count += 1;
- }
- _IF(format[*count] == '+') {
- SET_BIT(flags, FORCE_PLUS);
- *count += 1;
- }
- _IF(format[*count] == ' ') {
- SET_BIT(flags, FORCE_SPACE);
- *count += 1;
- }
- _IF(format[*count] == '#') {
- SET_BIT(flags, HASH_FLAG);
- *count += 1;
- }
- _IF(format[*count] == ' ') {
- SET_BIT(flags, FORCE_SPACE);
- *count += 1;
- }
- _IF(format[*count] == '0') {
- SET_BIT(flags, ZERO_FILL);
- *count += 1;
- }
- _FRET flags;
- }
- HIDDEN INLINE
- DECL(byte_t, do_width)(
- DECL(CONST _STR, format),
- DECL(value_t *, count)
- ) {
- DECL(byte_t, str)[9];
- memset(str, ZERO, 9);
- DECL(byte_t, pos) = ZERO;
- DECL(byte_t, width) = ZERO;
- _IF(isdigit(format[*count]) && format[*count] != '0') {
- str[pos++] = format[*count]; *count += 1;
- _WL(isdigit(format[*count])) {
- _IF(pos >= 8) BREAK;
- str[pos++] = format[*count];
- *count += 1;
- }
- width = strtol((CONST _STR)str, ZERO, 10);
- }
- _FRET width;
- }
- HIDDEN INLINE
- DECL(byte_t, do_lmod)(
- DECL(CONST _STR, format),
- DECL(value_t *, count)
- ) {
- DECL(byte_t, lmod);
- _SW(format[*count]) {
- /* End of Stream */
- CASE ZERO: {
- _FRET ZERO;
- }
- /* short int */
- CASE 'h': {
- lmod = LMOD_H;
- *count += 1;
- /* short short int (char) */
- _IF(format[*count] == 'h') {
- lmod = LMOD_HH;
- *count += 1;
- }
- BREAK;
- }
- /* long int */
- CASE 'l': {
- lmod = LMOD_L;
- *count += 1;
- /* long long int */
- _IF(format[*count] == 'l') {
- lmod = LMOD_LL;
- *count += 1;
- }
- BREAK;
- }
- /* intmax_t */
- CASE 'j': {
- lmod = LMOD_J;
- *count += 1;
- BREAK;
- }
- /* size_t */
- CASE 'z': {
- lmod = LMOD_Z;
- *count += 1;
- BREAK;
- }
- /* ptrdiff_t */
- CASE 't': {
- lmod = LMOD_T;
- *count += 1;
- BREAK;
- }
- /* long double */
- CASE 'L': {
- lmod = LMOD_LF;
- *count += 1;
- BREAK;
- }
- /* None */
- DEFT: {
- lmod = LMOD_NONE;
- BREAK;
- }
- }
- _FRET lmod;
- }
- HIDDEN INLINE
- DECL(_V, do_transform)(
- DECL(out_t, out0),
- DECL(outbulk_t, out1),
- DECL(_STR, buffer),
- DECL(length_t, length),
- DECL(pchar_t *, total),
- DECL(signed long long, value),
- DECL(byte_t, flags),
- DECL(byte_t, width),
- DECL(byte_t, base),
- DECL(_STR, tmp)
- ) {
- _IF(CHK_BIT(flags, FORCE_PLUS)) {
- _IF(value >= ZERO && tmp[ZERO] != '-') {
- *total += out0(buffer, '+');
- _IF(*total >= (pchar_t)length) _FRET;
- }
- }
- _IF(CHK_BIT(flags, HASH_FLAG)) {
- _IF(base == 16) {
- *total += out1(buffer, "0x");
- _IF(*total >= (pchar_t)length) _FRET;
- } _LS _IF(base == 8) {
- *total += out1(buffer, "0o");
- _IF(*total >= (pchar_t)length) _FRET;
- } _LS _IF(base == 4) {
- *total += out1(buffer, "0q");
- _IF(*total >= (pchar_t)length) _FRET;
- } _LS _IF(base == 2) {
- *total += out1(buffer, "0b");
- _IF(*total >= (pchar_t)length) _FRET;
- }
- }
- _IF(CHK_BIT(flags, LJUSTIFY)) {
- *total += out1(buffer, tmp);
- }
- _IF(CHK_BIT(flags, ZERO_FILL) ||
- CHK_BIT(flags, FORCE_SPACE)) {
- DECL(sdword_t, diff);
- DECL(byte_t, ch) = CHK_BIT(flags, ZERO_FILL)
- ? '0' : ' ';
- diff = width - strlen(tmp);
- _WL(diff > ZERO) {
- *total += out0(buffer, ch);
- _IF(*total >= (pchar_t)length) _FRET;
- diff--;
- }
- }
- _IF(!CHK_BIT(flags, LJUSTIFY)) {
- *total += out1(buffer, tmp);
- }
- }
- HIDDEN INLINE
- DECL(_V, handle_lhex)(
- DECL(byte_t, lhex),
- DECL(_STR, tmp)
- ) {
- _IF(lhex) {
- _FI(dig, ZERO, 65) {
- _IF(!tmp[dig]) BREAK;
- tmp[dig] = tolower(tmp[dig]);
- }
- }
- }
- HIDDEN
- DECL(pchar_t, do_printf)(
- DECL(out_t, out0),
- DECL(outbulk_t, out1),
- DECL(_STR, buffer),
- DECL(length_t, length),
- DECL(CONST _STR, format),
- DECL(va_list, args),
- DECL(byte_t, term)
- ) {
- DECL(pchar_t, total) = ZERO;
- _FI(count, ZERO, length) {
- _IF(!format[count]) BREAK;
- _IF(total > (pchar_t)length) BREAK;
- _IF(format[count] == '%') {
- DECL(byte_t, lhex) = ZERO;
- DECL(byte_t, lmod) = ZERO;
- DECL(byte_t, flags) = ZERO;
- DECL(byte_t, width) = ZERO;
- count += 1;
- /* Modifier Flags */
- flags = do_flags(format, &count);
- /* Padding Width */
- _IF(flags) {
- width = do_width(format, &count);
- }
- /* Length Modifier */
- lmod = do_lmod(format, &count);
- /* Specifier */
- DECL(byte_t, base_val);
- _SW(format[count]) {
- /* End of Stream */
- CASE ZERO: {
- BREAK;
- }
- /* Byte Count */
- CASE 'n': {
- DECL(pchar_t *, ptr);
- ptr = (pchar_t *)_VA(args, dword_t);
- ptr[ZERO] = total;
- BREAK;
- }
- /* Percent Sign */
- CASE '%': {
- total += out0(buffer, '%');
- BREAK;
- }
- /* Signed Integer */
- CASE 'd': CASE 'i':
- base_val = 10;
- _SW(lmod) {
- CASE LMOD_Z: {
- DECL(char, tmp)[33];
- DECL(size_t, value);
- value = _VA(args, size_t);
- ltoa(value, tmp, base_val);
- handle_lhex(lhex, tmp);
- do_transform(
- out0, out1, buffer, length, &total,
- (signed long long)value, flags,
- width, base_val, tmp
- );
- BREAK;
- }
- CASE LMOD_J: {
- DECL(char, tmp)[65];
- DECL(intmax_t, value);
- value = _VA(args, intmax_t);
- lltoa(value, tmp, base_val);
- handle_lhex(lhex, tmp);
- do_transform(
- out0, out1, buffer, length, &total,
- (signed long long)value, flags,
- width, base_val, tmp
- );
- BREAK;
- }
- CASE LMOD_HH: {
- DECL(char, tmp)[9];
- DECL(sbyte_t, value);
- value = (sbyte_t)_VA(args, sdword_t);
- ctoa(value, tmp, base_val);
- handle_lhex(lhex, tmp);
- do_transform(
- out0, out1, buffer, length, &total,
- (signed long long)value, flags,
- width, base_val, tmp
- );
- BREAK;
- }
- CASE LMOD_H: {
- DECL(char, tmp)[17];
- DECL(sword_t, value);
- value = (sword_t)_VA(args, sdword_t);
- stoa(value, tmp, base_val);
- handle_lhex(lhex, tmp);
- do_transform(
- out0, out1, buffer, length, &total,
- (signed long long)value, flags,
- width, base_val, tmp
- );
- BREAK;
- }
- CASE LMOD_L: {
- DECL(char, tmp)[33];
- DECL(sdword_t, value);
- value = _VA(args, sdword_t);
- ltoa(value, tmp, base_val);
- handle_lhex(lhex, tmp);
- do_transform(
- out0, out1, buffer, length, &total,
- (signed long long)value, flags,
- width, base_val, tmp
- );
- BREAK;
- }
- CASE LMOD_LL: {
- DECL(char, tmp)[65];
- DECL(sqword_t, value);
- value = _VA(args, sqword_t);
- lltoa(value, tmp, base_val);
- handle_lhex(lhex, tmp);
- do_transform(
- out0, out1, buffer, length, &total,
- (signed long long)value, flags,
- width, base_val, tmp
- );
- BREAK;
- }
- CASE LMOD_NONE: {
- DECL(char, tmp)[33];
- DECL(sdword_t, value);
- value = _VA(args, sdword_t);
- itoa(value, tmp, base_val);
- handle_lhex(lhex, tmp);
- do_transform(
- out0, out1, buffer, length, &total,
- (signed long long)value, flags,
- width, base_val, tmp
- );
- BREAK;
- }
- }
- BREAK;
- /* Unsigned UInteger */
- CASE 'u': CASE 'o': CASE 'b':
- CASE 'x': CASE 'X': CASE 'q':
- _IF(format[count] == 'u') {
- base_val = 10;
- } _LS _IF(format[count] == 'o') {
- base_val = 8;
- } _LS _IF(format[count] == 'b') {
- base_val = 2;
- } _LS _IF(format[count] == 'q') {
- base_val = 4;
- } _LS {
- _IF(format[count] == 'x') lhex = 1;
- base_val = 16;
- }
- _SW(lmod) {
- CASE LMOD_Z: {
- DECL(char, tmp)[33];
- DECL(size_t, value);
- value = _VA(args, size_t);
- ltoa(value, tmp, base_val);
- handle_lhex(lhex, tmp);
- do_transform(
- out0, out1, buffer, length, &total,
- (signed long long)value, flags,
- width, base_val, tmp
- );
- BREAK;
- }
- CASE LMOD_J: {
- DECL(char, tmp)[65];
- DECL(uintmax_t, value);
- value = _VA(args, uintmax_t);
- lltoa(value, tmp, base_val);
- handle_lhex(lhex, tmp);
- do_transform(
- out0, out1, buffer, length, &total,
- (signed long long)value, flags,
- width, base_val, tmp
- );
- BREAK;
- }
- CASE LMOD_HH: {
- DECL(char, tmp)[9];
- DECL(byte_t, value);
- value = (byte_t)_VA(args, dword_t);
- ctoa(value, tmp, base_val);
- handle_lhex(lhex, tmp);
- do_transform(
- out0, out1, buffer, length, &total,
- (signed long long)value, flags,
- width, base_val, tmp
- );
- BREAK;
- }
- CASE LMOD_H: {
- DECL(char, tmp)[17];
- DECL(word_t, value);
- value = (word_t)_VA(args, dword_t);
- stoa(value, tmp, base_val);
- handle_lhex(lhex, tmp);
- do_transform(
- out0, out1, buffer, length, &total,
- (signed long long)value, flags,
- width, base_val, tmp
- );
- BREAK;
- }
- CASE LMOD_L: {
- DECL(char, tmp)[33];
- DECL(dword_t, value);
- value = _VA(args, dword_t);
- ltoa(value, tmp, base_val);
- handle_lhex(lhex, tmp);
- do_transform(
- out0, out1, buffer, length, &total,
- (signed long long)value, flags,
- width, base_val, tmp
- );
- BREAK;
- }
- CASE LMOD_LL: {
- DECL(char, tmp)[65];
- DECL(qword_t, value);
- value = _VA(args, qword_t);
- lltoa(value, tmp, base_val);
- handle_lhex(lhex, tmp);
- do_transform(
- out0, out1, buffer, length, &total,
- (signed long long)value, flags,
- width, base_val, tmp
- );
- BREAK;
- }
- CASE LMOD_NONE: {
- DECL(char, tmp)[33];
- DECL(dword_t, value);
- value = _VA(args, dword_t);
- itoa(value, tmp, base_val);
- handle_lhex(lhex, tmp);
- do_transform(
- out0, out1, buffer, length, &total,
- (signed long long)value, flags,
- width, base_val, tmp
- );
- BREAK;
- }
- }
- BREAK;
- /* Single Character */
- CASE 'c': {
- DECL(byte_t, value);
- value = (byte_t)_VA(args, dword_t);
- total += out0(buffer, value);
- BREAK;
- }
- /* String of Chars */
- CASE 's': {
- DECL(_STR, value) = _VA(args, _STR);
- //DECL(value_t, remain) = total - length;
- //_IF(remain >= strlen(value)) value[remain] = ZERO;
- total += out1(buffer, value);
- BREAK;
- }
- /* Pointer Address */
- CASE 'p': {
- DECL(char, tmp)[33];
- DECL(dword_t, pvalue);
- pvalue = (dword_t)_VA(args, _G);
- pvalue = pvalue;
- ltoa(pvalue, tmp, 16);
- handle_lhex(lhex, tmp);
- total += out1(buffer, tmp);
- BREAK;
- }
- /* Default Fallback */
- DEFT: {
- DECL(byte_t, value);
- value = (byte_t)_VA(args, dword_t);
- total += out0(buffer, value);
- BREAK;
- }
- }
- } _LS {
- total += out0(buffer, format[count]);
- }
- }
- _IF(term) total += out0(buffer, ZERO);
- _FRET total;
- }
- DECL(pchar_t, printf)(
- DECL(CONST _STR, format),
- VARGS
- ) {
- DECL(pchar_t, val);
- DECL(va_list, list);
- _VS(list, format);
- val = vprintf(format, list);
- _VE(list);
- _FRET val;
- }
- DECL(pchar_t, sprintf)(
- DECL(_STR, str),
- DECL(CONST _STR, format),
- VARGS
- ) {
- DECL(pchar_t, val);
- DECL(va_list, list);
- _VS(list, format);
- val = vsprintf(str, format, list);
- _VE(list);
- _FRET val;
- }
- DECL(pchar_t, snprintf)(
- DECL(_STR, str),
- DECL(length_t, ct),
- DECL(CONST _STR, format),
- VARGS
- ) {
- DECL(pchar_t, val);
- DECL(va_list, list);
- _VS(list, format);
- val = vsnprintf(str, ct, format, list);
- _VE(list);
- _FRET val;
- }
- HIDDEN
- DECL(pchar_t, vwrite0)(
- DECL(_STR, buffer),
- DECL(pchar_t, character)
- ) {
- buffer = buffer;
- _FRET putchar(character);
- }
- HIDDEN
- DECL(pchar_t, vwrite1)(
- DECL(_STR, buffer),
- DECL(CONST _STR, str)
- ) {
- buffer = buffer;
- _FRET puts(str);
- }
- DECL(pchar_t, vprintf)(
- DECL(CONST _STR, format),
- DECL(va_list, args)
- ) {
- _FRET do_printf(
- vwrite0, vwrite1,
- ZERO, INT32_MAX,
- format, args,
- ZERO
- );
- }
- HIDDEN
- DECL(pchar_t, vswrite0)(
- DECL(_STR, buffer),
- DECL(pchar_t, character)
- ) {
- *(byte_t *)buffer = (byte_t)character;
- _FRET 1;
- }
- HIDDEN
- DECL(pchar_t, vswrite1)(
- DECL(_STR, buffer),
- DECL(CONST _STR, str)
- ) {
- _FI(count, ZERO, UINT32_MAX) {
- _IF(!str[count]) _FRET count;
- vswrite0(&buffer[count], str[count]);
- }
- _FRET ZERO;
- }
- DECL(pchar_t, vsprintf)(
- DECL(_STR, str),
- DECL(CONST _STR, format),
- DECL(va_list, args)
- ) {
- _FRET do_printf(
- vswrite0, vswrite1,
- str, INT32_MAX,
- format, args,
- !ZERO
- );
- }
- DECL(pchar_t, vsnprintf)(
- DECL(_STR, str),
- DECL(length_t, ct),
- DECL(CONST _STR, format),
- DECL(va_list, args)
- ) {
- _FRET do_printf(
- vswrite0, vswrite1,
- str, ct, format, args,
- !ZERO
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment