Files
smartservice_native/hdssd/huslib/Common.cpp
T
2026-05-20 03:08:08 +09:00

110 lines
1.6 KiB
C++

#include "Common.h"
unsigned int ELFHash(const char * str, unsigned int len)
{
unsigned int hash = 0;
unsigned int x = 0;
unsigned int i = 0;
for (i = 0; i < len; str++, i++)
{
hash = (hash << 4) + (*str);
if ((x = hash & 0xF0000000L) != 0)
{
hash ^= (x >> 24);
}
hash &= ~x;
}
return hash;
}
// Caution! : 0 ~ RAND_MAX
unsigned _GetRandNum(unsigned nMin, unsigned nMax)
{
if ((nMin == 0 && nMax == 0) ||
nMin > nMax)
{
//printf("[WARN] Invalide parameter.\n");
return 0;
}
if (nMax > RAND_MAX)
{
nMax = RAND_MAX;
//printf("[WARN] Invalide parameter.\n");
}
static bool bRandInit = false;
if (bRandInit == false)
{
bRandInit = true;
srand((unsigned)time(NULL));
}
unsigned nRandNum;
for (;;)
{
nRandNum = (unsigned)rand();
if (nRandNum >= nMin && nRandNum <= nMax)
break;
}
return nRandNum;
}
bool RealignPathDepth(const char * pszSrc, char * pszDst)
{
if (pszSrc == NULL || pszDst == NULL)
return false;
if (pszSrc[0] == '\0')
{
pszDst[0] = '\0';
return true;
}
const char * p1 = pszSrc;
char * p2 = pszDst;
do
{
if (p1[0] == '.' && p1[1] == '.' && p1[2] == '.')
{
do
{
if (*p1 == '/' || *p1 == '\\' || *p1 == '\0')
break;
*p2++ = *p1;
} while (p1++);
}
if (*p1 == '\0')
break;
if (p1[0] == '.' && p1[1] == '.' && (p1[2] == '/' || p1[2] == '\\'))
{
for (int i = 0; i < 2; i++)
{
while (p2 != pszDst && p2--)
{
if (*p2 == '/' || *p2 == '\\' || p2 == pszDst)
break;
}
}
p1 += 2;
}
*p2++ = *p1;
} while (p1++);
*p2 = '\0';
return true;
}