From 435f1ee6598e87aa35f70a8e146154144bf64237 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Fr=C3=B6schle?= Date: Tue, 26 Feb 2013 11:04:25 +0000 Subject: [PATCH] separated "standard library" string functions into bas_string.[ch] --- BaS_gcc/Makefile | 1 + BaS_gcc/bas.lk.in | 1 + BaS_gcc/include/bas_string.h | 25 ++++++++++++++ BaS_gcc/sources/bas_printf.c | 30 +---------------- BaS_gcc/sources/bas_string.c | 65 ++++++++++++++++++++++++++++++++++++ BaS_gcc/sources/basflash.c | 44 +----------------------- 6 files changed, 94 insertions(+), 72 deletions(-) create mode 100644 BaS_gcc/include/bas_string.h create mode 100644 BaS_gcc/sources/bas_string.c diff --git a/BaS_gcc/Makefile b/BaS_gcc/Makefile index 15188ba..39801ef 100644 --- a/BaS_gcc/Makefile +++ b/BaS_gcc/Makefile @@ -62,6 +62,7 @@ CSRCS= \ $(SRCDIR)/sysinit.c \ $(SRCDIR)/init_fpga.c \ $(SRCDIR)/bas_printf.c \ + $(SRCDIR)/bas_string.c \ $(SRCDIR)/BaS.c \ $(SRCDIR)/cache.c \ $(SRCDIR)/mmc.c \ diff --git a/BaS_gcc/bas.lk.in b/BaS_gcc/bas.lk.in index b23f738..b029720 100644 --- a/BaS_gcc/bas.lk.in +++ b/BaS_gcc/bas.lk.in @@ -37,6 +37,7 @@ SECTIONS objs/sd_card.o(.text) objs/s19reader.o(.text) objs/bas_printf.o(.text) + objs/bas_string.o(.text) objs/printf_helper.o(.text) objs/cache.o(.text) objs/mmu.o(.text) diff --git a/BaS_gcc/include/bas_string.h b/BaS_gcc/include/bas_string.h new file mode 100644 index 0000000..303c4c4 --- /dev/null +++ b/BaS_gcc/include/bas_string.h @@ -0,0 +1,25 @@ +/* + * bas_string.h + * + * Created on: 26.02.2013 + * Author: mfro + */ + +#ifndef BAS_STRING_H_ +#define BAS_STRING_H_ + +#include + +extern int strncmp(const char *s1, const char *s2, int max); +extern char *strcpy(char *dst, const char *src); +extern size_t strlen(const char *str); +extern int strncat(char *dst, char *src, int max); +extern int atoi(const char *c); + +#define isdigit(c) (((c) >= '0') && ((c) <= '9')) +#define isupper(c) ((c) >= 'A' && ((c) <= 'Z')) +#define islower(c) ((c) >= 'a' && ((c) <= 'z')) +#define isalpha(c) (isupper((c)) || islower(c)) +#define tolower(c) (isupper(c) ? ((c) + 'a' - 'A') : (c)) + +#endif /* BAS_STRING_H_ */ diff --git a/BaS_gcc/sources/bas_printf.c b/BaS_gcc/sources/bas_printf.c index 333923f..9f53791 100644 --- a/BaS_gcc/sources/bas_printf.c +++ b/BaS_gcc/sources/bas_printf.c @@ -35,6 +35,7 @@ #include #include "MCF5475.h" #include "bas_printf.h" +#include "bas_string.h" /* * Lexical definitions. @@ -71,35 +72,6 @@ static void xputchar(int c) ); } -#define isdigit(c) (((c) >= '0') && ((c) <= '9')) -#define isupper(c) ((c) >= 'A' && ((c) <= 'Z')) -#define islower(c) ((c) >= 'a' && ((c) <= 'z')) -#define isalpha(c) (isupper((c)) || islower(c)) -#define tolower(c) (isupper(c) ? ((c) + 'a' - 'A') : (c)) - -static int atoi(const char *c) -{ - int value = 0; - while (isdigit(*c)) - { - value *= 10; - value += (int) (*c - '0'); - c++; - } - return value; -} - -size_t strlen(const char *s) -{ - int length = 0; - - while (*s++) - { - length++; - } - return length; -} - static void doprnt(void (*addchar)(int), const char *sfmt, va_list ap) { char buf[128]; /* FIXME: this gets allocated in BSS which is not reachable in -mpcrel code */ diff --git a/BaS_gcc/sources/bas_string.c b/BaS_gcc/sources/bas_string.c new file mode 100644 index 0000000..0b01add --- /dev/null +++ b/BaS_gcc/sources/bas_string.c @@ -0,0 +1,65 @@ +/* + * bas_string.c + * + * Created on: 26.02.2013 + * Author: mfro + */ + +#include "bas_string.h" + +int strncmp(const char *s1, const char *s2, int max) +{ + int i; + int cmp; + + for (i = 0; i < max; i++); + { + cmp = (*s1++ - *s2++); + if (cmp != 0) return cmp; + } + return cmp; +} + +char *strcpy(char *dst, const char *src) +{ + char *ptr = dst; + + while ((*dst++ = *src++) != '\0'); + return ptr; +} + +int atoi(const char *c) +{ + int value = 0; + while (isdigit(*c)) + { + value *= 10; + value += (int) (*c - '0'); + c++; + } + return value; +} + +size_t strlen(const char *s) +{ + const char *start = s; + + while (*s++); + + return s - start; +} + + +int strncat(char *dst, char *src, int max) +{ + int i; + + dst = &dst[strlen(dst) + 1]; + for (i = 0; i < max && *src; i++) + { + *dst++ = *src++; + } + *dst++ = '\0'; + + return i; +} diff --git a/BaS_gcc/sources/basflash.c b/BaS_gcc/sources/basflash.c index 8a86555..36cbc4f 100644 --- a/BaS_gcc/sources/basflash.c +++ b/BaS_gcc/sources/basflash.c @@ -7,55 +7,13 @@ #include #include +#include "bas_string.h" #include "bas_printf.h" #include "diskio.h" #include "ff.h" #include "s19reader.h" -int strncmp(const char *s1, const char *s2, int max) -{ - int i; - int cmp; - for (i = 0; i < max; i++); - { - cmp = (*s1++ - *s2++); - if (cmp != 0) return cmp; - } - return cmp; -} - -char *strcpy(char *dst, const char *src) -{ - while ((*dst++ = *src++)); - return dst; -} - -extern int strlen(char *str); - -#ifdef _NOT_USED_ /* already defined in printf.c */ -int strlen(char *str) -{ - int i = 0; - - do {} while (str[i++]); - return i - 1; -} -#endif - -int strncat(char *dst, char *src, int max) -{ - int i; - - dst = &dst[strlen(dst) + 1]; - for (i = 0; i < max && *src; i++) - { - *dst++ = *src++; - } - *dst++ = '\0'; - - return i; -} static uint32_t mx29lv640d_flash_sectors[] = {