Files
ocs/mss/pps/src/main/ppsfunc.c
2025-03-03 11:01:26 +08:00

224 lines
4.7 KiB
C

/*
**
** function.c
** Created at 2000-05-29
** Functions for data conversion, tools.
**
*/
#include "../include/includes.h"
/*
** Fill string 'c' into last area of 'str'.
*/
/* duplicate with public module 2006-09-25*/
/*
void
StringCat(char *str, char *c, int slen)
{
if (strlen(str) >= slen) return;
while (strlen(str) < slen)
strcat(str, c);
str[slen] = 0;
}
*/
/*
** Cut string while match character c.
*/
/*
void
StringCut(char *str, int c)
{
if (toupper(*str) == toupper(c)) *str=0;
while (str && *str++)
if (toupper(*str) == toupper(c)) {
*str = 0;
break;
}
}
*/
void Bstr2Hstr(char *cptr)
{
char s[12] = "";
unsigned int i, j = 0;
while (cptr && *cptr && strchr("01", *cptr)) {
i = *cptr++ - '0';
j <<= 1;
j |= (i & 0x01);
}
sprintf(s, "%x", j);
cptr = (char *) s;
}
void bitstring(char *str, long byze, int biz, int strwid)
{
int i, j;
j = strwid - (biz + (biz >> 2)- (biz % 4 ? 0 : 1));
for (i = 0; i < j; i++)
*str++ = ' ';
while (--biz >= 0) {
*str++ = ((byze >> biz) & 1) + '0';
//
// if (!(biz % 4) && biz)
// *str++ = ' ';
//
}
*str = '\0';
}
/*
** Binary string convert to int.
*/
unsigned int Bstr2Int(char *cptr)
{
unsigned int i, j = 0;
while (cptr && *cptr && strchr("01", *cptr)) {
i = *cptr++ - '0';
j <<= 1;
j |= (i & 0x01);
}
return(j);
}
/*
** Hex string convert to int.
*/
unsigned int Hstr2Int(char *cptr)
{
unsigned int i, j = 0;
while (cptr && *cptr && isxdigit(*cptr)) {
i = *cptr++ - '0';
if (9 < i)
i -= 7;
j <<= 4;
j |= (i & 0x0f);
}
return(j);
}
/* Convert data from ASCII form to left-aligned compact BCD form. */
/*
void AsciiToBcd (BYTE *bcd_buf, const char *ascii_buf, int len)
{
int i;
char ch;
for (i=0; i<len; i++)
{
ch = ascii_buf[i];
if (ch>='a') ch -= 'a' - 10;
else if (ch>='A') ch -= 'A' - 10;
else ch -= '0';
if (i & 1) *(bcd_buf++) |= ch & 0x0f;
else *bcd_buf = ch << 4;
}
}
*/
/* Convert data from left-aligned compact BCD form to ASCII form. */
/*
void BcdToAscii (char *ascii_buf, const BYTE *bcd_buf, int len)
{
int i;
char ch;
for (i=0; i<len; i++) {
if (i & 1) ch = *(bcd_buf++) & 0x0f;
else ch = *bcd_buf >> 4;
ascii_buf[i] = ch + ((ch > 9)? 'A'-10 : '0');
}
ascii_buf[i] = '\0';
}
*/
/* Convert data from ASCII form to right-aligned compact BCD form. */
/*
void AsciiToRbcd (BYTE *bcd_buf, const char *ascii_buf, int len)
{
int i;
char ch;
if (len > 0) memset(bcd_buf ,0,len/2);
for (i=0; i<len; i++) {
ch = ascii_buf[i];
if (ch>='a') ch -= 'a' - 10;
else if (ch>='A') ch -= 'A' - 10;
else ch -= '0';
if ((len - i) & 1) *(bcd_buf++) |= ch & 0x0f;
else *bcd_buf = ch << 4;
}
}
*/
/* Convert data from right-aligned compact BCD form to ASCII form. */
void RbcdToAscii (char *ascii_buf, const BYTE *bcd_buf, int len)
{
int i;
char ch;
for (i=0; i<len; i++) {
if ((len - i) & 1) ch = *(bcd_buf++) & 0x0f;
else ch = *bcd_buf >> 4;
ascii_buf[i] = ch + ((ch > 9)? 'A'-10 : '0');
}
ascii_buf[i] = '\0';
}
/* Convert number to compact BCD, the number must in the range from 0 to 99. */
BYTE IntToBcd (int n)
{
return ((n / 10) << 4) | (n % 10);
}
/* Convert number from compact BCD. */
int BcdToInt (BYTE n)
{
return (n >> 4) * 10 + (n & 0x0f);
}
/* Copy charaters, then append a null character to array pointed by dst. */
void CopyToString (char *dst, const char *src, int len)
{
if (len) memcpy(dst, src, len);
dst[len] = '\0';
}
/* duplicate with public module 2006-09-25*/
/*
void gettime(time_buf)
char *time_buf;
{
struct tm *t,tt;
long curr_time;
t=&tt;
curr_time=time((long *)0);
t=localtime(&curr_time);
sprintf(time_buf,"%02d:%02d:%02d",t->tm_hour,t->tm_min,t->tm_sec);
time_buf[8]=0;
}
*/
const char * getdaytime(char *daytime_buf)
{
struct tm *t,tt;
long curr_time;
t=&tt;
curr_time=time((long *)0);
t=localtime(&curr_time);
sprintf(daytime_buf,"%04d%02d%02d%02d%02d%02d",
t->tm_year+1900, t->tm_mon+1, t->tm_mday, t->tm_hour,
t->tm_min, t->tm_sec);
daytime_buf[14] = 0;
return daytime_buf;
}