Convert stuff

This commit is contained in:
Julian Barathieu 2019-01-01 17:37:58 +01:00
parent bea3e8a927
commit 5c84ba8b68
5 changed files with 65 additions and 14 deletions

View File

@ -71,25 +71,36 @@ COMMSRCS=$(COMMDIR)/string.c $(COMMDIR)/status.c $(COMMDIR)/rand.c \
$(COMMDIR)/convert.c $(COMMDIR)/memory.c $(COMMDIR)/arith.c
COMMOBJS=$(COBJDIR)/string.o $(COBJDIR)/status.o $(COBJDIR)/rand.o \
$(COBJDIR)/convert.o $(COBJDIR)/memory.o $(COBJDIR)/arith.o
$(COBJDIR)/memory.o $(COBJDIR)/arith.o \
$(COBJDIR)/itoa.o $(COBJDIR)/ltoa.o $(COBJDIR)/utoa.o $(COBJDIR)/ultoa.o
common: $(COMMDEPS) $(COMMSRCS)
comm-convert:
$(KCC) -c $(COMMDIR)/convert.c -D_NEED_ITOA -o $(COBJDIR)/itoa.o
$(KCC) -c $(COMMDIR)/convert.c -D_NEED_LTOA -o $(COBJDIR)/ltoa.o
$(KCC) -c $(COMMDIR)/convert.c -D_NEED_UTOA -o $(COBJDIR)/utoa.o
$(KCC) -c $(COMMDIR)/convert.c -D_NEED_ULTOA -o $(COBJDIR)/ultoa.o
common: $(COMMDEPS) $(COMMSRCS) comm-convert
$(KCC) -c $(COMMDIR)/rand.c -o $(COBJDIR)/rand.o
$(KCC) -c $(COMMDIR)/arith.c -o $(COBJDIR)/arith.o
$(KCC) -c $(COMMDIR)/string.c -o $(COBJDIR)/string.o
$(KCC) -c $(COMMDIR)/status.c -o $(COBJDIR)/status.o
$(KCC) -c $(COMMDIR)/memory.c -o $(COBJDIR)/memory.o
$(KCC) -c $(COMMDIR)/convert.c -o $(COBJDIR)/convert.o
CCC=$(CC2NAME) $(COPTIM) $(CWARNS) $(CINCLUDES)
tests: $(COMMSRCS)
tests-comm-convert:
$(CCC) -c $(COMMDIR)/convert.c -D_NEED_ITOA -o $(COBJDIR)/itoa.o
$(CCC) -c $(COMMDIR)/convert.c -D_NEED_LTOA -o $(COBJDIR)/ltoa.o
$(CCC) -c $(COMMDIR)/convert.c -D_NEED_UTOA -o $(COBJDIR)/utoa.o
$(CCC) -c $(COMMDIR)/convert.c -D_NEED_ULTOA -o $(COBJDIR)/ultoa.o
tests: $(COMMSRCS) tests-comm-convert
$(CCC) -c $(COMMDIR)/rand.c -o $(COBJDIR)/rand.o
$(CCC) -c $(COMMDIR)/arith.c -o $(COBJDIR)/arith.o
$(CCC) -c $(COMMDIR)/string.c -o $(COBJDIR)/string.o
$(CCC) -c $(COMMDIR)/status.c -o $(COBJDIR)/status.o
$(CCC) -c $(COMMDIR)/memory.c -o $(COBJDIR)/memory.o
$(CCC) -c $(COMMDIR)/convert.c -o $(COBJDIR)/convert.o
$(CCC) -c $(LINXDIR)/test-common.c -o $(LOBJDIR)/test-common.o
$(CCC) $(COMMOBJS) $(LOBJDIR)/test-common.o -o $(BINDIR)/kaleid-common.elf

View File

@ -15,24 +15,51 @@
static const char digits[36] =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
#if defined(_NEED_UTOA) || defined(_NEED_ULTOA)
#define _S unsigned
#else
#define _S
#endif
#if defined (_NEED_ITOA) || defined(_NEED_UTOA)
#define _T int
#else
#define _T long
#endif
//
// Integer to string in any base between 2 and 36 (included)
//
#if defined(_NEED_ITOA)
char *itoa(int i, char *str, int base)
#elif defined(_NEED_LTOA)
char *ltoa(long i, char *str, int base)
#elif defined(_NEED_UTOA)
char *utoa(uint i, char *str, int base)
#elif defined(_NEED_ULTOA)
char *ultoa(ulong i, char *str, int base)
#else
#error "What am I supposed to declare?"
#endif
{
#if defined(_NEED_ITOA) || defined(_NEED_LTOA)
int neg = 0;
#endif
char *orig = str;
if (base < 2 || base > 36)
return NULL;
#if defined(_NEED_ITOA) || defined(_NEED_LTOA)
// deal with negatives
if (i < 0) {
neg = 1;
i = -i;
}
#endif
// deal with zero separatly
// deal with zero separately
if (i == 0) {
*str++ = '0';
}
@ -43,7 +70,10 @@ char *itoa(int i, char *str, int base)
i /= base;
}
#if defined(_NEED_ITOA) || defined(_NEED_LTOA)
if (neg) *str++ = '-';
#endif
*str = '\0';
return reverse(orig);

View File

@ -11,6 +11,10 @@
#include <stdio.h>
void *_osk_memsetw(void *, int, long);
char *_osk_itoa(int, char *, int);
char *_osk_ltoa(long, char *, int);
char *_osk_utoa(unsigned int, char *, int);
char *_osk_ultoa(unsigned long, char *, int);
int main(int argc, char *argv[])
{
@ -27,7 +31,8 @@ int main(int argc, char *argv[])
//const size_t size1 = strlen(test1);
//char *test2 = malloc(size1);
//char *test3 = malloc(size1);
#if 0
const size_t sizex = 130;
short *xxx = (short *)malloc(sizex * sizeof(short));
//printf("%ld\n",(ulong)xxx%8);
@ -39,10 +44,15 @@ int main(int argc, char *argv[])
short s = *(xxx + it);
printf("%hd", s);
}
free((void *)xxx);
#endif
char buf[256];
puts(_osk_ultoa(5000000000, buf, 10));
puts("");
free((void *)xxx);
//const char *str = "ceci est un string de test!";
//char *str2 = malloc((strlen(str) + 3) * sizeof(char));

View File

@ -45,12 +45,12 @@ noreturn void _assert_handler(const char *, const char *, int, const char *);
#else
#if !defined(NDEBUG)
# define NDEBUG 1
#ifndef NDEBUG
#define NDEBUG 1
#endif
#if !defined(_NO_DEBUG)
# define _NO_DEBUG 1
#ifndef _NO_DEBUG
#define _NO_DEBUG 1
#endif
#ifndef assert

View File

@ -20,7 +20,7 @@
# endif
#endif
#ifndef _OSK_SOURCE
#if !defined(_OSK_SOURCE)
# ifndef _KALMASK_H
# include <kalmask.h>
# endif