2015-12-04 14:47:52 +00:00
|
|
|
/**
|
2016-09-11 20:20:15 +00:00
|
|
|
* Author......: See docs/credits.txt
|
2015-12-04 14:47:52 +00:00
|
|
|
* License.....: MIT
|
|
|
|
*/
|
|
|
|
|
2016-09-05 19:47:26 +00:00
|
|
|
#include "common.h"
|
2016-09-16 15:01:18 +00:00
|
|
|
#include "types.h"
|
2016-09-05 19:47:26 +00:00
|
|
|
#include "shared.h"
|
2015-12-04 14:47:52 +00:00
|
|
|
|
2016-09-08 08:01:49 +00:00
|
|
|
u32 get_random_num (const u32 min, const u32 max)
|
|
|
|
{
|
|
|
|
if (min == max) return (min);
|
|
|
|
|
|
|
|
return ((rand () % (max - min)) + min);
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 mydivc32 (const u32 dividend, const u32 divisor)
|
2016-09-08 07:21:25 +00:00
|
|
|
{
|
2016-09-08 08:01:49 +00:00
|
|
|
u32 quotient = dividend / divisor;
|
2016-09-08 07:21:25 +00:00
|
|
|
|
2016-09-08 08:01:49 +00:00
|
|
|
if (dividend % divisor) quotient++;
|
2016-09-08 07:21:25 +00:00
|
|
|
|
2016-09-08 08:01:49 +00:00
|
|
|
return quotient;
|
|
|
|
}
|
2016-09-08 07:21:25 +00:00
|
|
|
|
2016-09-08 08:01:49 +00:00
|
|
|
u64 mydivc64 (const u64 dividend, const u64 divisor)
|
|
|
|
{
|
|
|
|
u64 quotient = dividend / divisor;
|
2016-09-08 07:21:25 +00:00
|
|
|
|
2016-09-08 08:01:49 +00:00
|
|
|
if (dividend % divisor) quotient++;
|
|
|
|
|
|
|
|
return quotient;
|
|
|
|
}
|
|
|
|
|
|
|
|
void naive_replace (char *s, const u8 key_char, const u8 replace_char)
|
|
|
|
{
|
|
|
|
const size_t len = strlen (s);
|
|
|
|
|
|
|
|
for (size_t in = 0; in < len; in++)
|
2016-09-08 07:21:25 +00:00
|
|
|
{
|
2016-09-08 08:01:49 +00:00
|
|
|
const u8 c = s[in];
|
2016-09-08 07:21:25 +00:00
|
|
|
|
2016-09-08 08:01:49 +00:00
|
|
|
if (c == key_char)
|
2016-09-08 07:21:25 +00:00
|
|
|
{
|
2016-09-08 08:01:49 +00:00
|
|
|
s[in] = replace_char;
|
2016-09-08 07:21:25 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-08 08:01:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void naive_escape (char *s, size_t s_max, const u8 key_char, const u8 escape_char)
|
|
|
|
{
|
|
|
|
char s_escaped[1024] = { 0 };
|
2016-09-08 07:21:25 +00:00
|
|
|
|
2016-09-08 08:01:49 +00:00
|
|
|
size_t s_escaped_max = sizeof (s_escaped);
|
2016-09-08 07:21:25 +00:00
|
|
|
|
2016-09-08 08:01:49 +00:00
|
|
|
const size_t len = strlen (s);
|
2016-09-08 07:21:25 +00:00
|
|
|
|
2016-09-08 08:01:49 +00:00
|
|
|
for (size_t in = 0, out = 0; in < len; in++, out++)
|
|
|
|
{
|
|
|
|
const u8 c = s[in];
|
2016-09-06 09:49:26 +00:00
|
|
|
|
2016-09-08 08:01:49 +00:00
|
|
|
if (c == key_char)
|
|
|
|
{
|
|
|
|
s_escaped[out] = escape_char;
|
2015-12-04 14:47:52 +00:00
|
|
|
|
2016-09-08 08:01:49 +00:00
|
|
|
out++;
|
|
|
|
}
|
2015-12-04 14:47:52 +00:00
|
|
|
|
2016-09-08 08:01:49 +00:00
|
|
|
if (out == s_escaped_max - 2) break;
|
2015-12-04 14:47:52 +00:00
|
|
|
|
2016-09-08 08:01:49 +00:00
|
|
|
s_escaped[out] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy (s, s_escaped, s_max - 1);
|
2016-09-06 09:49:26 +00:00
|
|
|
}
|
2015-12-04 14:47:52 +00:00
|
|
|
|
2016-09-20 10:32:39 +00:00
|
|
|
void hc_sleep_ms (const int msec)
|
2016-09-08 08:48:38 +00:00
|
|
|
{
|
|
|
|
#if defined (_WIN)
|
2016-09-20 10:32:39 +00:00
|
|
|
Sleep (msec);
|
|
|
|
#else
|
|
|
|
usleep (msec * 1000);
|
2016-09-08 08:48:38 +00:00
|
|
|
#endif
|
2016-09-20 10:32:39 +00:00
|
|
|
}
|
2016-09-08 08:01:49 +00:00
|
|
|
|
2016-09-20 10:32:39 +00:00
|
|
|
void hc_sleep (const int sec)
|
|
|
|
{
|
|
|
|
#if defined (_WIN)
|
|
|
|
Sleep (sec * 1000);
|
|
|
|
#else
|
2016-09-08 08:48:38 +00:00
|
|
|
sleep (sec);
|
2016-09-20 10:32:39 +00:00
|
|
|
#endif
|
2016-09-08 08:48:38 +00:00
|
|
|
}
|