From c41a3fbb994360a6d04797f8a112f8b17d8f5a3c Mon Sep 17 00:00:00 2001 From: anschrammh Date: Sat, 23 Feb 2019 13:03:12 +0100 Subject: [PATCH] first commit --- .gitignore | 9 +++++ EMLib.cbp | 60 +++++++++++++++++++++++++++++++++ EMLib.depend | 35 +++++++++++++++++++ EMLib.layout | 35 +++++++++++++++++++ EMatoi.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++ EMatoi.h | 9 +++++ EMsprintf.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++ EMsprintf.h | 9 +++++ EMstrcpy.c | 21 ++++++++++++ EMstrcpy.h | 6 ++++ EMstrncpy.c | 13 ++++++++ EMstrncpy.h | 6 ++++ main.c | 26 +++++++++++++++ 13 files changed, 412 insertions(+) create mode 100644 .gitignore create mode 100644 EMLib.cbp create mode 100644 EMLib.depend create mode 100644 EMLib.layout create mode 100644 EMatoi.c create mode 100644 EMatoi.h create mode 100644 EMsprintf.c create mode 100644 EMsprintf.h create mode 100644 EMstrcpy.c create mode 100644 EMstrcpy.h create mode 100644 EMstrncpy.c create mode 100644 EMstrncpy.h create mode 100644 main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..42fdec4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +/bin +/Data +/Obj +/doxygen +*.dll +*.exe +*.ico +*.jpg +*.jpeg \ No newline at end of file diff --git a/EMLib.cbp b/EMLib.cbp new file mode 100644 index 0000000..b03edf4 --- /dev/null +++ b/EMLib.cbp @@ -0,0 +1,60 @@ + + + + + + diff --git a/EMLib.depend b/EMLib.depend new file mode 100644 index 0000000..b8316b0 --- /dev/null +++ b/EMLib.depend @@ -0,0 +1,35 @@ +# depslib dependency file v1.0 +1549565384 source:d:\users\think\desktop\mes documents\programmation\c\programmation en c\emlib\main.c + + + + + "EMsprintf.h" + "EMstrcpy.h" + "EMstrncpy.h" + "EMatoi.h" + +1549564154 source:d:\users\think\desktop\mes documents\programmation\c\programmation en c\emlib\emsprintf.c + "EMsprintf.h" + +1549529062 d:\users\think\desktop\mes documents\programmation\c\programmation en c\emlib\emsprintf.h + + + +1550923261 source:d:\users\think\desktop\mes documents\programmation\c\programmation en c\emlib\emstrcpy.c + "EMstrcpy.h" + +1549529231 d:\users\think\desktop\mes documents\programmation\c\programmation en c\emlib\emstrcpy.h + +1549563398 source:d:\users\think\desktop\mes documents\programmation\c\programmation en c\emlib\ematoi.c + "EMatoi.h" + +1550923268 d:\users\think\desktop\mes documents\programmation\c\programmation en c\emlib\ematoi.h + "EMstrncpy.h" + + +1549543765 source:d:\users\think\desktop\mes documents\programmation\c\programmation en c\emlib\emstrncpy.c + "EMstrncpy.h" + +1549545458 d:\users\think\desktop\mes documents\programmation\c\programmation en c\emlib\emstrncpy.h + diff --git a/EMLib.layout b/EMLib.layout new file mode 100644 index 0000000..0505a0c --- /dev/null +++ b/EMLib.layout @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/EMatoi.c b/EMatoi.c new file mode 100644 index 0000000..abcf9e0 --- /dev/null +++ b/EMatoi.c @@ -0,0 +1,89 @@ +#include "EMatoi.h" + +static char *str_swap(char *str, int size); +static void sanitize(char *buff,const char *str, int size); + +/** \brief + * + * \param + * \param + * \return + * + */ + +int EMatoi(const char *str) +{ + short unsigned int size = sizeof(int) == 4 ? 11 /*max : 2 147 483 647 (10 characters + 1 for \0)*/: 6/*max : 32 768 (5 characters + 1 for \0)*/; + char buff[size]; + int val = 0, index = 0; + unsigned int power_of_ten = 1; + char done = 0; + + sanitize(buff, str, size); + + str_swap(buff,strlen(buff)-1); + + for(;*(buff+index) != '\0' && !done;index++) + { + switch(*(buff+index)) + { + case '+': + done = 1; + break; + case '-': + val = -val; + done = 1; + break; + default: + if(*(buff+index) >= '0' && *(buff+index) <= '9' ) + { + val += (*(buff+index) - '1'+1) * power_of_ten; + power_of_ten *= 10; + } + else + done = 1; + break; + } + } + + return val; +} + +static char *str_swap(char *str, int size) +{ + int left_pos = 0, right_pos = 0; + char c_temp, *p_start = str, *p_end = str + size; + + for(;left_pos + right_pos < size;left_pos++,right_pos++) + { + c_temp = *(p_start+left_pos); + *(p_start+left_pos) = *(p_end-right_pos); + *(p_end-right_pos) = c_temp; + } + + return str; +} + +/** \brief Remove blanks at the beginning of the string and replace the first non numeric character with '\0' except + or - + * + * \param char *str -> string to be sanitized + * \return char *p -> pointing to the string + * + */ + +static void sanitize(char *buff,const char *str, int size) +{ + unsigned char number_of_white_space = 0, index = 0; + + for(;*(str+index) == ' ';index++)number_of_white_space++; + + EMstrncpy(buff, str+number_of_white_space,size-1); + + if(*buff == '-' || *buff == '+' || (*(buff) >= '0' && *(buff) <= '9')) + { + for(index = 1;(*(buff+index) >= '0' && *(buff+index) <= '9');index++); + buff[index] = '\0'; + } + else + *buff = '\0'; +} diff --git a/EMatoi.h b/EMatoi.h new file mode 100644 index 0000000..83c81e7 --- /dev/null +++ b/EMatoi.h @@ -0,0 +1,9 @@ +#ifndef EMATOI_H +#define EMATOI_H + +#include "EMstrncpy.h" +#include + +int EMatoi(const char *str); + +#endif // EMATOI_H diff --git a/EMsprintf.c b/EMsprintf.c new file mode 100644 index 0000000..5d6a6cc --- /dev/null +++ b/EMsprintf.c @@ -0,0 +1,94 @@ +#include "EMsprintf.h" + +static char *str_swap(char *str, int size); + +/** \brief A light-weight version of sprintf() + * + * \param char *str -> the destination buffer + * \param const char *format -> the string format + * \param arguments ... + * \return nothing + * + * A light-weight version of sprintf() targeted toward micro-controller + * Only supports %s and %d + */ + +void EMsprintf(char *str,const char *format, ...) +{ + va_list arg_p; + char *pointer = NULL, *p_str_ref = str; + const char *p_format_ref = NULL; + int arg_int = 0; + + va_start(arg_p,format); + //will stop once it's NULL + // | + // V + for(p_format_ref = format; *p_format_ref; p_format_ref++) + { + if(*p_format_ref != '%') + { + *p_str_ref++ = *p_format_ref; + continue; + } + + switch(*(++p_format_ref)) + { + case 's': + //get the arg as a string + for(pointer = va_arg(arg_p, char *); *pointer; pointer++) + *p_str_ref++ = *pointer; + break; + case 'd': + //get the arg as an int + arg_int = va_arg(arg_p, int); + + if(arg_int < 0) + { + *p_str_ref++ = '-';arg_int = -arg_int; + } + { + int temp = 0; + char *p_start = p_str_ref, *p_end = NULL; + if(arg_int) + { + for(;arg_int;) + { + temp = (arg_int % 10); + *p_str_ref++ = temp + ('1'-1); + arg_int = (arg_int-temp) == 0 ? 0 : (arg_int-temp)/10; + } + + p_end = p_str_ref-1; + int length = p_end-p_start; + p_start = str_swap(p_start, length); + } + else + *p_str_ref++ = '0'; + } + break; + default: + *p_str_ref++ = *p_format_ref; + break; + } + } + + va_end(arg_p); + + *p_str_ref = '\0'; +} + +static char *str_swap(char *str, int size) +{ + int left_pos = 0, right_pos = 0; + char c_temp, *p_start = str, *p_end = str + size; + + for(;left_pos + right_pos < size;left_pos++,right_pos++) + { + c_temp = *(p_start+left_pos); + *(p_start+left_pos) = *(p_end-right_pos); + *(p_end-right_pos) = c_temp; + } + + return str; +} diff --git a/EMsprintf.h b/EMsprintf.h new file mode 100644 index 0000000..d9764c8 --- /dev/null +++ b/EMsprintf.h @@ -0,0 +1,9 @@ +#ifndef EMSPRINTF_H +#define EMSPRINTF_H + +#include +#include + +void EMsprintf(char *str,const char *format, ...); + +#endif //EMSPRINTF_H diff --git a/EMstrcpy.c b/EMstrcpy.c new file mode 100644 index 0000000..652c2d1 --- /dev/null +++ b/EMstrcpy.c @@ -0,0 +1,21 @@ +#include "EMstrcpy.h" + +/** \brief A simple implementation of strcpy + * + * \param char *dest -> destination buffer + * \param char *source -> source buffer + * \return char *p -> pointing to the destination buffer + * + * A bit lighter than the standard strcpy + */ + +char *EMstrcpy(char *dest, const char *source) +{ + const char *p_src = source; + for(;*p_src;) + { + *dest++ = *p_src++; + } + + return dest; +} diff --git a/EMstrcpy.h b/EMstrcpy.h new file mode 100644 index 0000000..b9ac695 --- /dev/null +++ b/EMstrcpy.h @@ -0,0 +1,6 @@ +#ifndef EMSTRCPY_H +#define EMSTRCPY_H + +char *EMstrcpy(char *dest, const char *source); + +#endif //EMSTRCPY_H \ No newline at end of file diff --git a/EMstrncpy.c b/EMstrncpy.c new file mode 100644 index 0000000..780be0e --- /dev/null +++ b/EMstrncpy.c @@ -0,0 +1,13 @@ +#include "EMstrncpy.h" + +char *EMstrncpy(char *dest, const char *src, int n) +{ + int index = 0; + + for(;index < n && *(src+index) != '\0';index++) + *(dest+index) = *(src+index); + + *(dest+index) = '\0'; + + return dest; +} diff --git a/EMstrncpy.h b/EMstrncpy.h new file mode 100644 index 0000000..f109a53 --- /dev/null +++ b/EMstrncpy.h @@ -0,0 +1,6 @@ +#ifndef EMSTRNCPY_H +#define EMSTRNCPY_H + +char *EMstrncpy(char *dest, const char *src, int n); + +#endif // EMSTRNCPY_H diff --git a/main.c b/main.c new file mode 100644 index 0000000..65d4f5f --- /dev/null +++ b/main.c @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include "EMsprintf.h" +#include "EMstrcpy.h" +#include "EMstrncpy.h" +#include "EMatoi.h" + +int main(int argc, char **argv) +{ + char buf[50] = "Une jolie semaine de fevrier", buf_cpy[60] = ""; + EMsprintf(buf,"Le %s a %s %d %s %d!!!","loup","egorge", -7895,"agneaux", EMatoi("666")); + + /* printf("$%d$\n",EMatoi("000-580")); + printf("#%d#\n",atoi("000580"));*/ + + EMstrcpy(buf_cpy,buf); + printf("%s\n%s\n",buf,buf_cpy); + + EMstrncpy(buf_cpy,buf,strlen(buf)); + printf("%s\n#%s#\n",buf,buf_cpy); + /*int16_t mx_int = 0; + printf("Max int : %d, sizeof() : %d", (mx_int | 1 << 15), sizeof(mx_int));*/ + return 0; +}