first commit

This commit is contained in:
anschrammh 2019-02-23 13:03:12 +01:00
commit c41a3fbb99
13 changed files with 412 additions and 0 deletions

9
.gitignore vendored Normal file
View File

@ -0,0 +1,9 @@
/bin
/Data
/Obj
/doxygen
*.dll
*.exe
*.ico
*.jpg
*.jpeg

60
EMLib.cbp Normal file
View File

@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="EMLib" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/EMLib" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/EMLib" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
</Compiler>
<Unit filename="EMatoi.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="EMatoi.h" />
<Unit filename="EMsprintf.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="EMsprintf.h" />
<Unit filename="EMstrcpy.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="EMstrcpy.h" />
<Unit filename="EMstrncpy.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="EMstrncpy.h" />
<Unit filename="main.c">
<Option compilerVar="CC" />
</Unit>
<Extensions>
<code_completion />
<envvars />
<debugger />
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>

35
EMLib.depend Normal file
View File

@ -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
<stdio.h>
<stdlib.h>
<stdint.h>
<string.h>
"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
<stdarg.h>
<stdio.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"
<string.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

35
EMLib.layout Normal file
View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Release" />
<File name="EMsprintf.c" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="71" topLine="0" />
</Cursor>
</File>
<File name="EMstrcpy.c" open="1" top="0" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="11" topLine="0" />
</Cursor>
</File>
<File name="EMatoi.c" open="1" top="0" tabpos="7" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="19" topLine="0" />
</Cursor>
</File>
<File name="EMstrcpy.h" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
</Cursor>
</File>
<File name="EMsprintf.h" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="95" topLine="0" />
</Cursor>
</File>
<File name="main.c" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="371" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file>

89
EMatoi.c Normal file
View File

@ -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';
}

9
EMatoi.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef EMATOI_H
#define EMATOI_H
#include "EMstrncpy.h"
#include <string.h>
int EMatoi(const char *str);
#endif // EMATOI_H

94
EMsprintf.c Normal file
View File

@ -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;
}

9
EMsprintf.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef EMSPRINTF_H
#define EMSPRINTF_H
#include <stdarg.h>
#include <stdio.h>
void EMsprintf(char *str,const char *format, ...);
#endif //EMSPRINTF_H

21
EMstrcpy.c Normal file
View File

@ -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;
}

6
EMstrcpy.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef EMSTRCPY_H
#define EMSTRCPY_H
char *EMstrcpy(char *dest, const char *source);
#endif //EMSTRCPY_H

13
EMstrncpy.c Normal file
View File

@ -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;
}

6
EMstrncpy.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef EMSTRNCPY_H
#define EMSTRNCPY_H
char *EMstrncpy(char *dest, const char *src, int n);
#endif // EMSTRNCPY_H

26
main.c Normal file
View File

@ -0,0 +1,26 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#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;
}