2018-09-28 20:35:51 +02:00
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
/* COS2000 - Compatible Operating System - LGPL v3 - Hord<72> Nicolas */
|
|
|
|
|
/* */
|
2007-04-02 16:06:17 +02:00
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2018-08-17 16:46:56 +02:00
|
|
|
|
/******************************************************************************/
|
2007-04-02 16:06:17 +02:00
|
|
|
|
/* Compare 2 chaines de caract<63>re et renvoie la premiere distance (diff) */
|
2018-08-17 16:46:56 +02:00
|
|
|
|
|
2018-08-17 22:32:09 +02:00
|
|
|
|
s8 strcmp(const u8 * src, const u8 * dest)
|
2007-04-02 16:06:17 +02:00
|
|
|
|
{
|
|
|
|
|
register s8 result;
|
2018-08-17 16:46:56 +02:00
|
|
|
|
do {
|
2018-08-17 22:32:09 +02:00
|
|
|
|
if ((result = *src - *dest++) != 0)
|
2018-08-17 16:46:56 +02:00
|
|
|
|
break;
|
2007-04-02 16:06:17 +02:00
|
|
|
|
}
|
2018-08-17 16:46:56 +02:00
|
|
|
|
while (*src++ != 0);
|
|
|
|
|
return result;
|
2007-04-02 16:06:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-17 16:46:56 +02:00
|
|
|
|
/******************************************************************************/
|
2018-08-18 18:37:18 +02:00
|
|
|
|
/* Trouve la premiere occurence d'un caract<63>re dans une chaine et renvoie un pointeur */
|
2018-08-17 16:46:56 +02:00
|
|
|
|
|
|
|
|
|
u8 *strchr(const u8 * src, u8 achar)
|
2007-04-02 16:06:17 +02:00
|
|
|
|
{
|
2018-08-17 16:46:56 +02:00
|
|
|
|
for (; *src != achar; ++src)
|
|
|
|
|
if (*src == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
return (u8 *) src;
|
2007-04-02 16:06:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-17 16:46:56 +02:00
|
|
|
|
/******************************************************************************/
|
2018-08-18 18:37:18 +02:00
|
|
|
|
/* Trouve la premiere occurence d'un caract<63>re dans une chaine */
|
|
|
|
|
|
|
|
|
|
u32 strchrpos(const u8 * src, u8 achar)
|
|
|
|
|
{
|
2018-09-27 17:47:27 +02:00
|
|
|
|
u32 i;
|
|
|
|
|
for (i = 0; *(src + i) != achar; ++i)
|
|
|
|
|
if (*(src + i) == 0)
|
2018-08-18 18:37:18 +02:00
|
|
|
|
return 0;
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2007-04-02 16:06:17 +02:00
|
|
|
|
/* Renvoie la taille de la chaine */
|
2018-08-17 16:46:56 +02:00
|
|
|
|
|
|
|
|
|
u32 strlen(const u8 * src)
|
2007-04-02 16:06:17 +02:00
|
|
|
|
{
|
|
|
|
|
u32 size;
|
|
|
|
|
|
2018-08-17 16:46:56 +02:00
|
|
|
|
for (size = 0; *(src + size) != 0; size++) ;
|
2007-04-02 16:06:17 +02:00
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-17 16:46:56 +02:00
|
|
|
|
/******************************************************************************/
|
2007-04-02 16:06:17 +02:00
|
|
|
|
/* copie une chaine dans une autre */
|
2018-08-17 22:32:09 +02:00
|
|
|
|
u8 *strcpy(const u8 * src, u8 * dest)
|
2007-04-02 16:06:17 +02:00
|
|
|
|
{
|
2018-08-17 22:32:09 +02:00
|
|
|
|
u8 *temp = dest;
|
|
|
|
|
while ((*dest++ = *src++) != 0) ;
|
2007-04-02 16:06:17 +02:00
|
|
|
|
return temp;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-17 16:46:56 +02:00
|
|
|
|
/******************************************************************************/
|
2007-04-02 16:06:17 +02:00
|
|
|
|
/* copie une portion limit<69> d'une chaine asciiZ*/
|
|
|
|
|
|
2018-08-17 22:32:09 +02:00
|
|
|
|
u8 *strncpy(const u8 * src, u8 * dest, u32 count)
|
2007-04-02 16:06:17 +02:00
|
|
|
|
{
|
2018-08-17 22:32:09 +02:00
|
|
|
|
u8 *temp = dest;
|
2007-04-02 16:06:17 +02:00
|
|
|
|
while (count) {
|
2018-08-17 16:46:56 +02:00
|
|
|
|
if ((*temp = *src) != 0)
|
|
|
|
|
src++;
|
2007-04-02 16:06:17 +02:00
|
|
|
|
temp++;
|
|
|
|
|
count--;
|
|
|
|
|
}
|
2018-08-17 22:32:09 +02:00
|
|
|
|
return dest;
|
2007-04-02 16:06:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-17 16:46:56 +02:00
|
|
|
|
/******************************************************************************/
|
2007-04-02 16:06:17 +02:00
|
|
|
|
/* concatene 2 chaines de caract<63>re */
|
2018-08-17 18:10:20 +02:00
|
|
|
|
|
2018-08-17 22:32:09 +02:00
|
|
|
|
u8 *strcat(const u8 * src, u8 * dest)
|
2007-04-02 16:06:17 +02:00
|
|
|
|
{
|
2018-08-17 22:32:09 +02:00
|
|
|
|
u8 *temp = dest;
|
|
|
|
|
while (*dest != 0)
|
|
|
|
|
dest++;
|
|
|
|
|
while ((*dest++ = *src++) != 0) ;
|
2007-04-02 16:06:17 +02:00
|
|
|
|
return temp;
|
|
|
|
|
}
|
2018-08-17 18:10:20 +02:00
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/* Met en minuscule */
|
|
|
|
|
|
|
|
|
|
void strtolower(u8 * src)
|
|
|
|
|
{
|
|
|
|
|
while (*src != 0) {
|
2018-09-27 17:47:27 +02:00
|
|
|
|
if ((*src >= 'A') && (*src <= 'Z'))
|
|
|
|
|
*src = *src + 'a' - 'A';
|
2018-08-17 18:10:20 +02:00
|
|
|
|
src++;
|
2018-09-27 17:47:27 +02:00
|
|
|
|
}
|
2018-08-17 18:10:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/* Met en majuscule */
|
|
|
|
|
|
|
|
|
|
void strtoupper(u8 * src)
|
|
|
|
|
{
|
|
|
|
|
while (*src != 0) {
|
2018-09-27 17:47:27 +02:00
|
|
|
|
if ((*src >= 'a') && (*src <= 'z'))
|
|
|
|
|
*src = *src - ('a' - 'A');
|
2018-08-17 18:10:20 +02:00
|
|
|
|
src++;
|
2018-09-27 17:47:27 +02:00
|
|
|
|
}
|
2018-08-17 18:10:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/* inverse la casse */
|
|
|
|
|
|
|
|
|
|
void strinvertcase(u8 * src)
|
|
|
|
|
{
|
|
|
|
|
while (*src != 0) {
|
2018-09-27 17:47:27 +02:00
|
|
|
|
if ((*src >= 'A') && (*src <= 'Z'))
|
|
|
|
|
*src = *src + 'a' - 'A';
|
|
|
|
|
else if ((*src >= 'a') && (*src <= 'z'))
|
|
|
|
|
*src = *src - ('a' - 'A');
|
2018-08-17 18:10:20 +02:00
|
|
|
|
src++;
|
2018-09-27 17:47:27 +02:00
|
|
|
|
}
|
2018-08-17 18:10:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/* Met une majuscule et met le reste en minuscule */
|
|
|
|
|
|
|
|
|
|
void stronecase(u8 * src)
|
|
|
|
|
{
|
2018-09-27 17:47:27 +02:00
|
|
|
|
if ((*src >= 'a') && (*src <= 'z'))
|
|
|
|
|
*src = *src - ('a' - 'A');
|
|
|
|
|
while (*src != 0) {
|
2018-08-17 18:10:20 +02:00
|
|
|
|
src++;
|
2018-09-27 17:47:27 +02:00
|
|
|
|
if ((*src >= 'A') && (*src <= 'Z'))
|
|
|
|
|
*src = *src + 'a' - 'A';
|
|
|
|
|
}
|
2018-08-17 18:10:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/* Fixe la taille de la chaine */
|
|
|
|
|
|
2018-08-17 22:32:09 +02:00
|
|
|
|
void strsetlen(u8 * src, u32 size)
|
2018-08-17 18:10:20 +02:00
|
|
|
|
{
|
2018-09-27 17:47:27 +02:00
|
|
|
|
*(src + size) = '\000';
|
2018-08-17 18:10:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/* Inverse la chaine */
|
|
|
|
|
|
2018-09-27 17:47:27 +02:00
|
|
|
|
void strinvert(u8 * src)
|
2018-08-17 18:10:20 +02:00
|
|
|
|
{
|
2018-09-27 17:47:27 +02:00
|
|
|
|
u8 *dst = src + strlen(src) - 1;
|
|
|
|
|
while (src < dst) {
|
|
|
|
|
u8 char1 = *dst;
|
|
|
|
|
u8 char2 = *src;
|
|
|
|
|
*dst = char2;
|
|
|
|
|
*src = char1;
|
2018-08-17 18:10:20 +02:00
|
|
|
|
src++;
|
2018-09-27 17:47:27 +02:00
|
|
|
|
dst--;
|
|
|
|
|
}
|
2018-08-17 18:10:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/* Remplace tout les caract<63>res rechercher par un autre */
|
|
|
|
|
|
2018-09-27 17:47:27 +02:00
|
|
|
|
void strreplace(u8 * src, u8 search, u8 replaced)
|
2018-08-17 18:10:20 +02:00
|
|
|
|
{
|
2018-09-27 17:47:27 +02:00
|
|
|
|
u8 *pos = strchr(src, search);
|
|
|
|
|
while (pos != 0) {
|
|
|
|
|
*pos = replaced;
|
|
|
|
|
pos = strchr(src, search);
|
|
|
|
|
}
|
2018-08-17 18:10:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-17 22:32:09 +02:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/* Rempli de caract<63>re */
|
|
|
|
|
|
2018-09-27 17:47:27 +02:00
|
|
|
|
u8 *strfill(u8 * dst, u8 pattern, u32 size)
|
2018-08-17 22:32:09 +02:00
|
|
|
|
{
|
2018-09-27 17:47:27 +02:00
|
|
|
|
u32 i;
|
|
|
|
|
for (i = 0; i <= size; i++)
|
|
|
|
|
*(dst + i) = pattern;
|
|
|
|
|
*(dst + i + 1) = '\000';
|
|
|
|
|
return dst;
|
2018-08-17 22:32:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/* Renvoie la partie gauche d'une chaine */
|
|
|
|
|
|
2018-09-27 17:47:27 +02:00
|
|
|
|
void strleft(u8 * src, u8 * dest, u32 size)
|
|
|
|
|
{
|
|
|
|
|
u32 i;
|
|
|
|
|
for (i = 0; (*(src + i) != 0) && (i < size); i++)
|
|
|
|
|
*dest++ = *(src + i);
|
|
|
|
|
*dest++ = '\000';
|
2018-08-17 22:32:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/* Renvoie la partie droite d'une chaine */
|
|
|
|
|
|
2018-09-27 17:47:27 +02:00
|
|
|
|
void strright(u8 * src, u8 * dest, u32 size)
|
|
|
|
|
{
|
|
|
|
|
u32 i;
|
|
|
|
|
u32 begin = strlen(src) - size;
|
|
|
|
|
for (i = 0; (*(src + i + begin) != 0) && (i < size); i++)
|
|
|
|
|
*dest++ = *(src + i + begin);
|
|
|
|
|
*dest++ = '\000';
|
2018-08-17 22:32:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/* Supprime une portion de chaine */
|
|
|
|
|
|
2018-09-27 17:47:27 +02:00
|
|
|
|
void strdelete(u8 * src, u32 index, u32 size)
|
|
|
|
|
{
|
|
|
|
|
u32 i;
|
|
|
|
|
u32 realsize = strlen(src) - index - size;
|
|
|
|
|
for (i = 0; (*(src + index + size + i) != 0) && (i < realsize); i++)
|
|
|
|
|
*(src + index + i) = *(src + index + size + i);
|
|
|
|
|
*(src + index + i) = '\000';
|
2018-08-17 22:32:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/* Insert une portion dans la chaine */
|
|
|
|
|
|
2018-09-27 17:47:27 +02:00
|
|
|
|
void strinsert(u8 * src, u8 * dest, u32 index)
|
|
|
|
|
{
|
|
|
|
|
u32 i;
|
|
|
|
|
u32 realsize = strlen(src);
|
|
|
|
|
u32 copysize = strlen(dest) - index;
|
|
|
|
|
for (i = 0; i <= copysize; i++)
|
|
|
|
|
*(dest + index + realsize + copysize - i) =
|
|
|
|
|
*(dest + index + copysize - i);
|
|
|
|
|
for (i = 0; (*(src + i) != 0); i++)
|
|
|
|
|
*(dest + index + i) = *(src + i);
|
2018-08-17 22:32:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/* Supprime les d<>limiteurs cons<6E>cutifs */
|
2018-08-17 18:10:20 +02:00
|
|
|
|
|
2018-09-27 17:47:27 +02:00
|
|
|
|
void strcompressdelimiter(u8 * src, u8 delim)
|
|
|
|
|
{
|
|
|
|
|
u8 *pos = strchr(src, delim);
|
|
|
|
|
while (pos != 0) {
|
|
|
|
|
u8 i;
|
|
|
|
|
for (i = 0; (*(pos + i) != 0) && (*(pos + i) == delim); i++) ;
|
|
|
|
|
if (i > 1)
|
|
|
|
|
strdelete(pos, 1, i - 1);
|
|
|
|
|
pos = strchr(pos + 1, delim);
|
|
|
|
|
}
|
2018-08-17 22:32:09 +02:00
|
|
|
|
}
|
2018-08-18 18:37:18 +02:00
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/* R<>cup<75>r<EFBFBD>re l'<27>l<EFBFBD>ment N d'une liste utilisant un d<>limiteur */
|
|
|
|
|
|
2018-09-27 17:47:27 +02:00
|
|
|
|
u8 *strgetitem(u8 * src, u8 * dest, u8 delim, u32 index)
|
|
|
|
|
{
|
|
|
|
|
u32 i;
|
|
|
|
|
u8 *pos = strgetpointeritem(src, delim, index);
|
|
|
|
|
for (i = 0; (*(pos + i) != 0) && (*(pos + i) != delim); i++)
|
|
|
|
|
*(dest + i) = *(pos + i);
|
|
|
|
|
*(dest + i) = '\000';
|
|
|
|
|
return dest;
|
2018-08-18 18:37:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/* R<>cup<75>r<EFBFBD>re un pointeur sur l'<27>l<EFBFBD>ment N d'une liste utilisant un d<>limiteur */
|
|
|
|
|
|
2018-09-27 17:47:27 +02:00
|
|
|
|
u8 *strgetpointeritem(u8 * src, u8 delim, u32 index)
|
|
|
|
|
{
|
|
|
|
|
u32 i;
|
|
|
|
|
u8 *pos = src;
|
|
|
|
|
for (i = 0; i < index; i++)
|
|
|
|
|
pos = strchr(pos + 1, delim);
|
|
|
|
|
if (*pos == delim)
|
|
|
|
|
pos++;
|
|
|
|
|
return pos;
|
2018-08-18 18:37:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/* R<>cup<75>r<EFBFBD>re le nombre d'<27>l<EFBFBD>ments d'une liste utilisant un d<>limiteur */
|
|
|
|
|
|
2018-09-27 17:47:27 +02:00
|
|
|
|
u32 strgetnbitems(u8 * src, u8 delim)
|
|
|
|
|
{
|
|
|
|
|
u32 number = 0;
|
|
|
|
|
if (*src == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
number++;
|
|
|
|
|
u8 *pos = strchr(src, delim);
|
|
|
|
|
while (pos != 0) {
|
|
|
|
|
pos = strchr(pos + 1, delim);
|
|
|
|
|
number++;
|
|
|
|
|
}
|
|
|
|
|
return number;
|
2018-08-18 18:37:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/* Renvoie la base minimum du nombre */
|
|
|
|
|
|
2018-09-27 17:47:27 +02:00
|
|
|
|
u8 base[] = " 0123456789ABCDEF\000";
|
|
|
|
|
|
|
|
|
|
u8 strgetminbase(u8 * src)
|
|
|
|
|
{
|
|
|
|
|
u8 temp[] = " \000";
|
|
|
|
|
u8 *dst = &temp;
|
|
|
|
|
strtoupper(strcpy(src, &temp));
|
|
|
|
|
u8 max = 0;
|
|
|
|
|
while (*dst != 0) {
|
|
|
|
|
u32 result = strchrpos(&base, *dst++);
|
|
|
|
|
if (result == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
if (result > max)
|
|
|
|
|
max = result;
|
|
|
|
|
}
|
|
|
|
|
if (max > 10)
|
|
|
|
|
return 16;
|
|
|
|
|
else if (max > 8)
|
|
|
|
|
return 10;
|
|
|
|
|
else if (max > 2)
|
|
|
|
|
return 8;
|
|
|
|
|
else
|
|
|
|
|
return 2;
|
2018-08-18 18:37:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/* Renvoie la base du nombre */
|
|
|
|
|
|
2018-09-27 17:47:27 +02:00
|
|
|
|
u8 hexa[] = "0x\000";
|
|
|
|
|
u8 bases[] = " bodh\000";
|
|
|
|
|
u8 basesnum[] = { 0, 2, 8, 10, 16 };
|
|
|
|
|
|
|
|
|
|
u8 declaredbase = 10;
|
|
|
|
|
u8 minbase = 0;
|
|
|
|
|
u8 strgetbase(u8 * src)
|
|
|
|
|
{
|
|
|
|
|
u8 temp[] = " \000";
|
|
|
|
|
strleft(src, &temp, 2);
|
|
|
|
|
if (strcmp(&temp, &hexa) == 0) {
|
|
|
|
|
declaredbase = 16;
|
|
|
|
|
u8 size = strlen(src);
|
|
|
|
|
strright(src, &temp, size - 2);
|
|
|
|
|
minbase = strgetminbase(&temp);
|
|
|
|
|
} else {
|
|
|
|
|
strright(src, &temp, 1);
|
|
|
|
|
declaredbase = basesnum[strchrpos(&bases, temp[0])];
|
|
|
|
|
if (declaredbase > 0) {
|
|
|
|
|
u8 size = strlen(src);
|
|
|
|
|
strleft(src, &temp, size - 1);
|
|
|
|
|
minbase = strgetminbase(&temp);
|
|
|
|
|
} else {
|
|
|
|
|
minbase = strgetminbase(src);
|
|
|
|
|
declaredbase = minbase;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (declaredbase >= minbase && minbase != 0)
|
|
|
|
|
return declaredbase;
|
|
|
|
|
else
|
|
|
|
|
return 0;
|
2018-08-18 18:37:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2018-08-21 08:26:35 +02:00
|
|
|
|
/* Transforme une chaine en nombre */
|
2018-08-18 18:37:18 +02:00
|
|
|
|
|
2018-09-27 17:47:27 +02:00
|
|
|
|
u32 strtoint(u8 * src)
|
|
|
|
|
{
|
|
|
|
|
u8 *temp = src;
|
|
|
|
|
u32 result = 0;
|
|
|
|
|
u8 thebase = strgetbase(src);
|
|
|
|
|
u32 multi = 1;
|
|
|
|
|
u8 shorter = 0;
|
|
|
|
|
|
|
|
|
|
if (thebase == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
if (*(src + 1) == 'x')
|
|
|
|
|
shorter = 2;
|
|
|
|
|
while (*++temp != 0) ;
|
|
|
|
|
while (*temp == 0 || *temp == 'b' || *temp == 'o' || *temp == 'd'
|
|
|
|
|
|| *temp == 'h')
|
|
|
|
|
temp--;
|
|
|
|
|
while (src + shorter <= temp) {
|
|
|
|
|
u8 achar = *temp--;
|
|
|
|
|
if ((achar >= 'a') && (achar <= 'z'))
|
|
|
|
|
achar = achar - ('a' - 'A');
|
|
|
|
|
result = result + multi * (strchrpos(&base, achar) - 1);
|
|
|
|
|
multi = multi * thebase;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2018-08-18 18:37:18 +02:00
|
|
|
|
}
|
2018-09-28 20:35:51 +02:00
|
|
|
|
|
|
|
|
|
/*******************************************************************************/
|