📄 misc.c
字号:
/******************************************************************************
Copyright(C) 2005,2006 Frank ZHANG
All Rights Reserved.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA.
******************************************************************************/
/******************************************************************************
* Authors : Frank ZHANG (openmgcp@gmail.com)
* Description :
*
*
* Date of creation : 04/15/2005
*
*
* History :
* 2005/04/15 Frank ZHANG : - Creation
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/timeb.h>
#include "debg.h"
#include "misc.h"
/***************************************************************************
* Function : Itoa
*
* Description : Conver the integer value to string.
*
* Input parameters : val - Integer value
*
* Output parameters :
*
* Return value : String.
*
* Comments :
*
* History :
* 2005/04/15 : Creation
*
* Date : April 15 2005, Frank Zhang
**************************************************************************/
char * Itoa(int val)
{ char *p; int flg = 0; if( val < 0 )
{
flg++;
val= -val;
}
p = Ultoa((unsigned long)val);
if(flg)
*--p = '-';
return p;
}/***************************************************************************
* Function : Ultoa
*
* Description : Conver the integer value to string.
*
* Input parameters : val - Integer value
*
* Output parameters :
*
* Return value : String.
*
* Comments :
*
* History :
* 2005/04/15 : Creation
*
* Date : April 15 2005, Frank Zhang
**************************************************************************/
static char buf[12];
char* Ultoa(unsigned long val)
{ char *p; p = buf + sizeof(buf); *--p = '\0'; do { *--p = (char)('0' + (val%10));
val/=10;
}
while(val); return p;}
/***************************************************************************
* Function : StrClone
*
* Description : Copy src string to des string, if des string is not
* NULL, free it firstly.
*
* Input parameters : Src - source string
* Des - pointer of destination string
*
* Output parameters : Des - destination string
*
* Return value : NULL - if source is NULL
* Destination string - if source is not NULL.
*
* Comments :
*
* History :
* 2005/04/15 : Creation
*
* Date : April 15 2005, Frank Zhang
**************************************************************************/
char* StrClone(char **Des, const char *Src)
{
if (*Des != NULL)
{
free(*Des);
*Des = NULL;
}
if (Src != NULL)
{
*Des = (char *)malloc(strlen(Src)+1);
Assert(*Des);
strcpy(*Des, Src);
}
return *Des;
}
/***************************************************************************
* Function : StrCaseCmp
*
* Description : Compare two strings without case sensitive
*
* Input parameters : s1 - string 1
* s2 - string 2
*
* Output parameters : None
*
* Return value : 0 - Srings are same
* Other value - Srtings are different
*
* Comments :
*
* History :
* 2005/04/15 : Creation
*
* Date : April 15 2005, Frank Zhang
**************************************************************************/
int StrCaseCmp(const char *s1,const char *s2)
{
unsigned int i;
unsigned int StrLen;
char Value1, Value2;
Assert(s1);
Assert(s2);
/* Check the length */
StrLen = strlen(s1);
if(StrLen != strlen(s2) )
return -1;
for(i = 0; i < StrLen; i++)
{
if((s1[i] >= 'A') && (s1[i] <= 'Z'))
Value1 = s1[i] + 'a' - 'A';
else Value1 = s1[i];
if((s2[i] >= 'A') && (s2[i] <= 'Z'))
Value2 = s2[i] + 'a' - 'A';
else Value2 = s2[i];
if (Value1 != Value2)
return -1;
}
return 0;
}
/***************************************************************************
* Function : AllUpperCase
*
* Description : Check if letters in sting are all upper case
*
* Input parameters : String - string
*
* Output parameters : None
*
* Return value : None 0 if string is capital, otherwise 0.
*
* Comments :
*
* History :
* 2005/04/15 : Creation
*
* Date : April 15 2005, Frank Zhang
**************************************************************************/
int AllUpperCase(const char *String)
{
while (*String)
{
if (!(*String >= 'A' && *String <= 'Z')
&& !(*String >= '0' && *String <= '9')
&& *String != '_' && *String != '-')
return 0;
String++;
}
return -1;
}
/***************************************************************************
* Function : Strdup
*
* Description : Dup a string and return its pointer
*
* Input parameters : s1 - src string
*
* Output parameters : None
*
* Return value : The new string pointer
*
* Comments :
*
* History :
* 2005/04/15 : Creation
*
* Date : April 15 2005, Frank Zhang
**************************************************************************/
char *Strdup(const char *s1)
{
size_t length;
char *newStr;
Assert(s1);
length = strlen(s1);
newStr = (char*)malloc(length+1);
if (newStr == NULL)
return NULL;
else
{
strcpy(newStr, s1);
return newStr;
}
}
int Get_Time_Ext(char * pch_time, int ilen)
{
struct timeb ttimeb;
struct tm *temp_tm = NULL;
char pch_year[5] = {0};
char pch_month[3] = {0};
char pch_day[3] = {0};
char pch_hour[3] = {0};
char pch_minute[3] = {0};
char pch_second[3] = {0};
if (pch_time == NULL || ilen < 24)
{
return FALSE;
}
memset(&ttimeb, 0, sizeof(ttimeb));
if (ftime(&ttimeb) != 0)
{
printf("\r\n ftime( ... ) fail \r\n");
return FALSE;
}
if ((temp_tm = (struct tm *)localtime(&ttimeb.time)) == NULL)
{
printf("\r\n localtime( ... ) fail \r\n");
return FALSE;
}
sprintf(pch_year, "%d", temp_tm->tm_year + 1900);
if (temp_tm->tm_mon < 9)
{
sprintf(pch_month + 1, "%d", temp_tm->tm_mon + 1);
pch_month[0] = '0';
}
else
{
sprintf(pch_month, "%d", temp_tm->tm_mon + 1);
}
if (temp_tm->tm_mday < 10)
{
sprintf(pch_day + 1, "%d", temp_tm->tm_mday);
pch_day[0] = '0';
}
else
{
sprintf(pch_day, "%d", temp_tm->tm_mday);
}
if (temp_tm->tm_hour < 10)
{
sprintf(pch_hour + 1, "%d", temp_tm->tm_hour);
pch_hour[0] = '0';
}
else
{
sprintf(pch_hour, "%d", temp_tm->tm_hour);
}
if (temp_tm->tm_min < 10)
{
sprintf(pch_minute + 1, "%d", temp_tm->tm_min);
pch_minute[0] = '0';
}
else
{
sprintf(pch_minute, "%d", temp_tm->tm_min);
}
if (temp_tm->tm_sec < 10)
{
sprintf(pch_second + 1, "%d", temp_tm->tm_sec);
pch_second[0] = '0';
}
else
{
sprintf(pch_second, "%d", temp_tm->tm_sec);
}
sprintf(pch_time, "%s:%s:%s:%d@%s.%s.%s",
pch_hour, pch_minute, pch_second, ttimeb.millitm, pch_year, pch_month, pch_day);
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -