public.cpp

来自「通信BOSS计费方面的服务器源码,有很多经典的创意,值得借鉴,在UNIX上运行.」· C++ 代码 · 共 1,271 行 · 第 1/2 页

CPP
1,271
字号
#include "Public.h"

#include <userlog.h>

#include <sys/proc.h>

int GetInputParm(char *pstr,char *lparm[],int maxlen)

{

	char tch[2];

	long ltmp,len,pos;

	long i=0,j,k=-1;



	len=strlen(pstr);

	if(len<=0) return 0;



	while(1)

	{

		lparm[i]=(char *)malloc(maxlen);

		strcpy(lparm[i],NULL);

		for(j=k+1;j<len;j++)

		{

			if(pstr[j]!='\t')

			{

				sprintf(tch,"%c",pstr[j]);

				strcat(lparm[i],tch);

			}

			else

			{

				k=j;

				break;

			}

		}

		if(j>=len) break;

		i++;

	}



	pos=strlen(lparm[i])-1;

	if(lparm[i][pos]=='\n') lparm[i][pos]='\0';



	return i+1;

}



void GetProfileString(char *filename,char *areaname,char *keyname,char *keydata)

{

	FILE *fp;

	long flen;

	char *filedata,area[256],*areabegin,*areaend,*areadata;

	char *databegin,*dataend,*keybegin;

	if((fp=fopen(filename,"r"))==NULL)

	{

		printf("Cannot open the configure file %s\n",filename);

		exit(0);

	}

	fseek(fp,0L,SEEK_END);

	flen=ftell(fp);

	filedata=(char *)calloc(1,flen);

	fseek(fp,0L,SEEK_SET);

	fread(filedata,1,flen,fp);

	fclose(fp);

	sprintf(area,"[%s]",areaname);

	areabegin=strstr(filedata,area);

	areaend=strstr(areabegin+1,"[");

	if(areaend==NULL)

	{

		areaend=filedata+flen-1;

	}

	keybegin=strstr(areabegin,keyname);

	if(keybegin==NULL||keybegin>areaend)

	{

		return;

	}

	databegin=strstr(keybegin,"=");

	dataend=strstr(keybegin,"\n");

	if(dataend==NULL)

	{

		dataend=areaend;

	}

	while(*(databegin+1)==' ')

	{

		databegin++;

	}

	strncpy(keydata,databegin+1,dataend-databegin-1);

	keydata[dataend-databegin-1] = '\0';

	//MessageBox("",0,keydata);

}







int GetParmByAny(char *pstr,char *lparm[],char ch,int maxlen)

{

	char tch[2];

	long ltmp,len,pos;

	long i=0,j,k=-1;



	len=strlen(pstr);

	if(len<=0) return 0;



	while(1)

	{

		lparm[i]=(char *)malloc(maxlen);

		strcpy(lparm[i],NULL);

		for(j=k+1;j<len;j++)

		{

			if(pstr[j]!=ch)

			{

				sprintf(tch,"%c",pstr[j]);

				strcat(lparm[i],tch);

			}

			else

			{

				k=j;

				break;

			}

		}

		if(j==len) break;

		i++;

	}



	return i+1;

}





long GetRowCol(char *pstr,long *row,long *col)

{

	long len;

	long i,j,n=0,k=-1;



	len=strlen(pstr);

	if(len<=0) return 0;



	while(1)

	{

	   i=0;



	   while(1)

	   {

		for(j=k+1;pstr[j]!='\n'&&j<len;j++)

		{

			if(pstr[j]!='\t')

			{

				continue;

			}

			else

			{

				k=j;

				break;

			}

		}

		i++;

		n++;

		if(pstr[j]=='\n'||j>=len-1) break;

	  }



	  *row=*row+1;

	  if(*row==1) *col=i;

	  else if(*col!=i&&j<len) return -1;

	  if(j>=len-1) break;

	  k=j;

	}

	return n;

}





long GetMulRowData(char *pstr,char **lparm,long *row,long *col,int maxlen)

{

	char tch[2];

	long ltmp,len,pos;

	long i,j,n=0,k=-1;



	*row=0;*col=0;

	len=strlen(pstr);

	if(len<=0) return 0;



	while(1)

	{

	   i=0;

	   while(1)

	   {

		lparm[n]=(char *)malloc(maxlen);

		strcpy(lparm[n],NULL);

		for(j=k+1;pstr[j]!='\n'&&j<len;j++)

		{

			if(pstr[j]!='\t')

			{

				sprintf(tch,"%c",pstr[j]);

				strcat(lparm[n],tch);

			}

			else

			{

				k=j;

				break;

			}

		}



		pos=strlen(lparm[n])-1;

		if(lparm[n][pos]=='\r') lparm[n][pos]='\0';



		//userlog("lparm[%d]= %s",n,lparm[n]);



		i++;

		n++;

		if(pstr[j]=='\n'||j>=len-1) break;



	  }



	  *row=*row+1;

	  if(*row==1) *col=i;

	  else if(*col!=i&&j<len) return -1;

	  if(j>=len-1) break;

	  k=j;



	}



	return n;

}





void lpfree(char *pt[],int num)

{

	for(int i=0;i<num;i++)

		free(pt[i]);

}





int is_number(char *pStr)

{

    int i;

    for (i=0;i<strlen(pStr);i++)

        if ((pStr[i]>='0' && pStr[i]<='9') || (pStr[i] == '.'))

            ;

        else

            return FALSE;

    return TRUE;

}



char* ltrim(char * pStr)

{

    char lsTemp[1024],*p;

    int i,j,l;

    j = 0;

    l = strlen(pStr);

    p = pStr;

    for(i=0;i<l;i++)

    {

    	if( (*p)!= ' ') break;

    	p++;

    }

    strcpy(lsTemp,p);

    strcpy(pStr,lsTemp);

    return pStr;

}



char* rtrim(char *pStr)

{

    int i,l;

    l = strlen(pStr);

    for(i=l-1;i>=0;i--)

        if((pStr[i] == ' ') || (pStr[i] == '\r') ||(pStr[i] == '\n'))

        	 continue;

        else

        {

           pStr[i+1]= '\0';

            break;

         }



    return pStr;

}





int CallServAsynByStr(char *serv_name,char *instr,char *outstr[])

{

	char *sendbuf, *rcvbuf;

	long sendlen, rcvlen;

	int ret,retid;

	char lstmp[500];



	sendlen = strlen(instr);

	rcvlen = 0;





	if (tpinit((TPINIT *) NULL) == -1) {

		sprintf(lstmp, "中间件调用初始化失败!错误代码 = %d,请速与系统管理员联系!\n",tperrno);

		outstr[0]=(char *)malloc(strlen(lstmp));

		strcpy(outstr[0],lstmp);

		return -2;

	}	

		

	if((sendbuf = (char *) tpalloc("STRING", NULL, sendlen+1)) == NULL) {

		sprintf(lstmp,"tpalloc failed (IN),errno: %d",tperrno);

		outstr[0]=(char *)malloc(strlen(lstmp));

		strcpy(outstr[0],lstmp);

		tpterm();

		return -2;

	}



	if((rcvbuf = (char *) tpalloc("STRING", NULL, rcvlen)) == NULL) {

		sprintf(lstmp,"tpalloc failed (OUT),errno: %d",tperrno);

		outstr[0]=(char *)malloc(strlen(lstmp));

		strcpy(outstr[0],lstmp);

		tpfree(sendbuf);

		tpterm();

		return -2;

	}



	strcpy(sendbuf, instr);

	ret = tpacall(serv_name, (char *)sendbuf, 0, TPSIGRSTRT);

	if(ret == -1) {

		sprintf(lstmp,"tpacall failed,errno: %d",tperrno);

		outstr[0]=(char *)malloc(strlen(lstmp));

		strcpy(outstr[0],lstmp);

		tpfree(sendbuf);

		tpfree(rcvbuf);

		tpterm();

		return -2;

	}

	int ret2=-1;

	while(ret2==-1)

	{

	     ret2=tpgetrply(&ret,(char **)&rcvbuf,&rcvlen,TPGETANY);

		 if(ret2==-1)

		 {

		     if(tperrno==TPETIME)

			 {

				 ret2==0;

			     continue;

			 }

			 else

			 {

		         sprintf(lstmp,"tpgetrply failed,errno: %d",tperrno);

		         outstr[0]=(char *)malloc(strlen(lstmp));

		         strcpy(outstr[0],lstmp);

		         tpfree(sendbuf);

		         tpfree(rcvbuf);

				 tpcancel(ret);

		         tpterm();

		         return -3;

			 }

		 }

		 

	}

	//printf("%s\n",rcvbuf);

	outstr[0]=(char *)malloc(rcvlen);

	strcpy(outstr[0],rcvbuf);

	

	tpcancel(ret);



	retid=tpurcode;

	/* Free Buffers & Detach from System/T */

	tpfree(sendbuf);

	tpfree(rcvbuf);

	

	tpterm();

	return(retid);

}



int CallServByStr(char *serv_name,char *instr,char *outstr[])

{

	char *sendbuf, *rcvbuf;

	long sendlen, rcvlen;

	int ret,retid;

	char lstmp[500];

/*        time_t first_time;

        struct tm *newtime;

        char *mybuf;

        char lssql[500];

        long ll_pid;*/

       



	sendlen = strlen(instr);

	rcvlen = 0;



	/* Allocate STRING buffers for the request and the reply */

	/* Allocate STRING buffers for the request and the reply */

/*	time(&first_time);

	newtime=localtime(&first_time);

	mybuf=asctime(newtime);

	strcpy(lssql,"client_init_begin:");

	strcat(lssql,mybuf);

	ll_pid=getpid();

	userlog("%d:%s",ll_pid,lssql);*/

	if (tpinit((TPINIT *) NULL) == -1) {

		sprintf(lstmp, "中间件调用初始化失败!错误代码 = %d,请速与系统管理员联系!\n",tperrno);

		outstr[0]=(char *)malloc(strlen(lstmp));

		strcpy(outstr[0],lstmp);

		return -2;

	}	

/*	time(&first_time);

	newtime=localtime(&first_time);

	mybuf=asctime(newtime);

	strcpy(lssql,"client_tpinit_end:");

	strcat(lssql,mybuf);

	ll_pid=getpid();

	userlog("%d:%s",ll_pid,lssql);

*/		

	if((sendbuf = (char *) tpalloc("STRING", NULL, sendlen+1)) == NULL) {

		sprintf(lstmp,"tpalloc failed (IN),errno: %d",tperrno);

		outstr[0]=(char *)malloc(strlen(lstmp));

		strcpy(outstr[0],lstmp);

		tpterm();

		return -2;

	}



	if((rcvbuf = (char *) tpalloc("STRING", NULL, rcvlen)) == NULL) {

		sprintf(lstmp,"tpalloc failed (OUT),errno: %d",tperrno);

		outstr[0]=(char *)malloc(strlen(lstmp));

		strcpy(outstr[0],lstmp);

		tpfree(sendbuf);

		tpterm();

		return -2;

	}



	strcpy(sendbuf, instr);

/*

        time(&first_time);

	newtime=localtime(&first_time);

	mybuf=asctime(newtime);

	strcpy(lssql,"client_tpcall_begin:");

	strcat(lssql,mybuf);

	ll_pid=getpid();

	userlog("%d:%s",ll_pid,lssql);

*/	

	ret = tpcall(serv_name, (char *)sendbuf, 0, (char **)&rcvbuf, &rcvlen, (long)0);

 /*       

        time(&first_time);

	newtime=localtime(&first_time);

	mybuf=asctime(newtime);

	strcpy(lssql,"client_tpcall_end:");

	strcat(lssql,mybuf);

	ll_pid=getpid();

	userlog("%d:%s",ll_pid,lssql);

	*/

	if(ret == -1) {

		sprintf(lstmp,"tpcall failed,errno: %d",tperrno);

		outstr[0]=(char *)malloc(strlen(lstmp));

		strcpy(outstr[0],lstmp);

		tpfree(sendbuf);

		tpfree(rcvbuf);

		tpterm();

		return -2;

	}

	

	//printf("%s\n",rcvbuf);

	outstr[0]=(char *)malloc(rcvlen);

	strcpy(outstr[0],rcvbuf);

	//userlog("outstr[0]:%s,%d\n",outstr[0],strlen(outstr[0]));

	//printf("outstr[0]:%s,%d\n",outstr[0],strlen(outstr[0]));

	

	retid=tpurcode;

	/* Free Buffers & Detach from System/T */

	tpfree(sendbuf);

	tpfree(rcvbuf);

/*	

	time(&first_time);

	newtime=localtime(&first_time);

	mybuf=asctime(newtime);

	strcpy(lssql,"client_tpterm_begin:");

	strcat(lssql,mybuf);

	ll_pid=getpid();

	userlog("%d:%s",ll_pid,lssql);

*/	

	tpterm();

/*		

        time(&first_time);

	newtime=localtime(&first_time);

	mybuf=asctime(newtime);

	strcpy(lssql,"client_tpterm_end");

	strcat(lssql,mybuf);

	ll_pid=getpid();

	userlog("%d:%s",ll_pid,lssql);

	*/

	return(retid);

}





char *GetSubStr(char *pstr,long start,long *pos,char ch)

{

	char *substr;

	char tch[2];

	long ltmp,len;

	long i=0;



	len=strlen(pstr);

	if(len<=0) 

	{

		*pos=0;

		return NULL;

	}

	substr=(char *)malloc(len);

	strcpy(substr,NULL);



	for(i=start+1;pstr[i]!=ch&&i<len;i++)

	{

		sprintf(tch,"%c",pstr[i]);

		strcat(substr,tch);

	}



	if(i==len)

	{

		*pos=i+1;

		return substr;

	}



	if(pstr[i]==ch)

	{

		*pos=i;

		return substr;

	}

	return substr;

}





char *itoch(char *tp,int i)

{

	strcpy(tp,"");

	sprintf(tp,"%d",i);

	return tp;

}



char *ltoch(char *tp,long i)

{

	strcpy(tp,"");

	sprintf(tp,"%ld",i);

	return tp;

}



char *ftoch(char *tp,char *fmt,double f)

{

	strcpy(tp,"");

	sprintf(tp,fmt,f);

	return tp;

}





void BeginPrint()

{

        /*fflush( stdin );

        fflush( stdout );

        fflush( stderr );

		printf( "\x1B4304");

        printf( "\x1b[5i" );

        printf( "\x1c\x78" "1" );*/

	    fflush( stdin );

        fflush( stdout );

        fflush( stderr );

        //printf( "\x1b[5i" );

        //printf( "\x1c\x78" "1" );  

		printf( "\x1b[5i");

		//printf( "\x1B\x2B\x2D");

		//printf( "\x1c\x53\x01\x02");

		printf( "\x1B");

		printf( "\x43");

		printf( "\x0");

		printf( "\x4");

}



void EndPrint()

{

       //printf("\x0C");

	   printf("\x1b[4i");

	   fflush( stdout );

}



//不自动进退纸打印初始化及结束操作

void BeginPrint_NOAUTO()

{

        fflush( stdin );

        fflush( stdout );

        fflush( stderr );

        printf( "\x1b[5i" );

        printf( "\x1c\x78" "1" );

}



void EndPrint_NOAUTO()

{

        printf("\x1b[4i");

        fflush( stdout );

}







//自动进退纸打印初始化及结束操作

/*void BeginPrint()

{

        fflush( stdin );

        fflush( stdout );

        fflush( stderr );

        

        printf( "\x1b[5i" );

        printf("\x1b\x43");

        printf("%c",0);

        printf("\x04");        

}



void EndPrint()

{

        

        printf("\x0c");

        printf("\x1b[4i");                       

        fflush( stdout );                

}*/

 



//函数:用于从字符串中导出某位置开始指定数量的字符串

void MidOfStr(char* outstr, char* instr, int pos, int count)

{

	int i;

	for(i = 0; i < count; i ++)

	{

		outstr[i] = instr[pos+i];

	}

	outstr[count] = '\0';

}





//转换人民币小写为大写

void DigitalToCapital(double number, int cntype, char* outstr)

{

	char numstr[12][4] = 

	{{""},{"零"},{"壹"},{"贰"},{"叁"},{"肆"},{"伍"},{"陆"},{"柒"},{"捌"},{"玖"},{"拾"}};

	char str1[20], str2[20];						//原始整数部分,原始小数部分

	char intstr[200], decstr[50];					//大写整数部分,大写小数部分

	char num[4][10];								//各个数位段,即每四位为一段

	char n[10][8];									//各位数字

	char number1[100], number2[100], number3[100];	//定义数字的三个数位段

	char strtemp[200];								//临时用字符串

	int pointpos;									//小数点位置,

	int	sign;										//正负号符号标志0-正,1-负



	char firstout[200];								//超过亿时递归得到的结果

	char temp8[1],temp7[1],temp6[1],temp5[1],temp4[1],

		 temp3[1],temp2[1],temp1[1], temp0[1];      //从千万位到小数位的位串

	int length;										//原始数字串长度

	int intlen;										//整数部分长度

	char* inistr;									//初始数字转换来的字符串

    

	int qxf_flag=0;



	if(number<0)

	{

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?