Useful functions in c

4 minute read

Some functions can be useful to copy and paste. The following code snippets are some functions that I used before.

#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <sys/stat.h>

int UpperCase(char *psInStr);
int LowerCase(char *psInStr);
int TrimRightSpace(char *psInStr);
char* GetDateStr(char *psDate);
char* GetTimeStr(char *psTime);
char* TimeToDateStr(time_t pTime, char* psBuff);
char* TimeToTimeStr(time_t pTime, char* psBuff);
time_t GetTimeFromStr(char *psTimeStr);

/*-------------------------------------------------------------------------
 * Function     : UpperCase
 * Description  : 
 * Input param  : InStr
 * Output param :
 * Return value : 0(Success), -1(Fail)
 *-------------------------------------------------------------------------*/
int UpperCase(char *psInStr)
{
    int liIdx, liStrLen;
    if (psInStr == NULL) return -1;

    liStrLen = strlen(psInStr);
    for (liIdx=0; liIdx < liStrLen; liIdx++)
    {
        if (psInStr[liIdx] >= 'a' && psInStr[liIdx] <= 'z')
            psInStr[liIdx] = psInStr[liIdx] - ('a' - 'A');
    }
    return 0;

} /* End of UpperCase */

/*-------------------------------------------------------------------------
 * Function     : LowerCase
 * Description  : 
 * Input param  : InStr
 * Output param :
 * Return value : 0(Success), -1(Fail)
 *-------------------------------------------------------------------------*/
int LowerCase(char *psInStr)
{
    int liIdx, liStrLen;
    if (psInStr == NULL) return -1;

    liStrLen = strlen(psInStr);
    for (liIdx=0; liIdx < liStrLen; liIdx++)
    {
        if (psInStr[liIdx] >= 'A' && psInStr[liIdx] <= 'Z')
            psInStr[liIdx] = psInStr[liIdx] + ('a' - 'A');
    }
    return 0;

} /* End of LowerCase */

/*-------------------------------------------------------------------------
 * Function     : TrimRightSpace
 * Description  : 
 * Input param  : 
 * Output param :
 * Return value : success(size), 0(error)
 *-------------------------------------------------------------------------*/
int TrimRightSpace(char *psInStr)
{
    int liIdx, InStrLen;
    if (psInStr == NULL) return -1;

    InStrLen = strlen(psInStr);
    for (liIdx=InStrLen-1; liIdx>= 0; liIdx=liIdx-1)
    {
        if ( !isspace((int)psInStr[liIdx]) )
        {
            if (liIdx== (InStrLen-1))
            {
                return (liIdx+1);
            }
            else
            {
                (psInStr[liIdx+1] = NULL);
                return (liIdx+1);
            }
        }
    }
    psInStr[0] = NULL;
    return 0;

} /* End of TrimRightSpace */

/*-------------------------------------------------------------------------
 * Function     : GetDateStr
 * Description  : Get Date String
 * Input param  : 
 * Output param :
 * Return value : char*
 *-------------------------------------------------------------------------*/
char* GetDateStr(char *psDate)
{
    time_t lTime;
    struct tm *tp;

    time(&lTime);
    tp = localtime(&lTime);

    sprintf(psDate, "%04d%02d%02d", tp->tm_year+1900, tp->tm_mon+1, tp->tm_mday);

    return psDate;

} /* End of GetDateStr */

/*-------------------------------------------------------------------------
 * Function     : GetTimeStr
 * Description  : Get Time String
 * Input param  : 
 * Output param :
 * Return value : char*
 *-------------------------------------------------------------------------*/
char* GetTimeStr(char *psTime)
{
    time_t lTime;
    struct tm *tp;

    time(&lTime);
    tp = localtime(&lTime);

    sprintf(psTime, "%02d%02d%02d", tp->tm_hour, tp->tm_min, tp->tm_sec);

    return psTime;

} /* End of GetTimeStr */

/*-------------------------------------------------------------------------
 * Function     : TimeToDateStr
 * Description  : Convert Date to String
 * Input param  : pTime, psBuff
 * Output param :
 * Return value : psBuff pointer
 *-------------------------------------------------------------------------*/
char* TimeToDateStr(time_t pTime, char* psBuff)
{
    struct tm *tp;
    tp = localtime(&pTime);

    sprintf(psBuff, "%04d%02d%02d",
                     tp->tm_year+1900, tp->tm_mon+1, tp->tm_mday);

    return psBuff;
} /* End of TimeToDateStr */

/*-------------------------------------------------------------------------
 * Function     : TimeToTimeStr
 * Description  : Get Time String from Time
 * Input param  : pTime, psBuff
 * Output param :
 * Return value : psBuff pointer
 *-------------------------------------------------------------------------*/
char* TimeToTimeStr(time_t pTime, char* psBuff)
{
    struct tm *tp;
    tp = localtime(&pTime);

    sprintf( psBuff, "%04d%02d%02d-%02d:%02d:%02d",
            tp->tm_year+1900, tp->tm_mon+1, tp->tm_mday,
            tp->tm_hour, tp->tm_min, tp->tm_sec );
    return psBuff;
} /* End of TimeToTimeStr */

/*-------------------------------------------------------------------------
 * Function     : GetTimeFromStr
 * Description  : Convert time string to time value(yyyymmddHHMMSS)
 * Input param  : void
 * Output param : 
 * Return value : time
 *-------------------------------------------------------------------------*/
time_t GetTimeFromStr(char *psTimeStr)
{
    char lsYear[4+1];
    char lsMon [2+1];
    char lsMday[2+1];
    char lsHour[2+1];
    char lsMin[2+1];
    char lsSec[2+1];

    time_t ctime;
    struct tm *tp;

    time(&ctime);
    tp = localtime(&ctime);

    if (strlen(psTimeStr) < 14)
        return 0;

    memcpy(lsYear, psTimeStr +  0, 4); lsYear[4] = 0x00;
    memcpy(lsMon , psTimeStr +  4, 2); lsMon [2] = 0x00;
    memcpy(lsMday, psTimeStr +  6, 2); lsMday[2] = 0x00;
    memcpy(lsHour, psTimeStr +  8, 2); lsHour[2] = 0x00;
    memcpy(lsMin , psTimeStr + 10, 2); lsMin [2] = 0x00;
    memcpy(lsSec , psTimeStr + 12, 2); lsSec [2] = 0x00;

    tp->tm_year = atoi(lsYear) - 1900;
    tp->tm_mon  = atoi(lsMon) - 1;
    tp->tm_mday  = atoi(lsMday);
    tp->tm_hour = atoi(lsHour);
    tp->tm_min  = atoi(lsMin );
    tp->tm_sec  = atoi(lsSec );

    return mktime(tp);
}

/*-------------------------------------------------------------------------
 * Function     : RTrim
 * Description  : Trimming
 * Input param  : psStr, piLen
 * Output param : 
 * Return value : void
 *-------------------------------------------------------------------------*/
void   RTrim(char *psStr, int piLen) {

    int  i = 0;

    for ( i = piLen-1 ; i >= 1; i--)
    {
        if ( psStr[i]==' ' ||  psStr[i]=='\t' || psStr[i]=='\r' || psStr[i]=='\n' || psStr[i]=='')
            psStr[i] = 0x00;
        else
            break;
    }
}
/*-------------------------------------------------------------------------
 * Function     : getFepTimeDiff
 * Description  : get time difference
 * Input param  : mmddHHMMSS
 * Output param : 
 * Return value : seconds
 *-------------------------------------------------------------------------*/
int getFepTimeDiff(char *mmddHHMMSS)
{
    int rtn = 0;
    time_t ctime; // Current time
    time_t rtime; // Received time
    struct tm *ctp;
    struct tm *rtp;

    time(&ctime);
    rtime = ctime;

    ctp = localtime(&ctime);
    rtp = localtime(&rtime);

    char mm[2+1];
    char dd[2+1];
    char HH[2+1];
    char MM[2+1];
    char SS[2+1];

    memset(mm, 0x00, sizeof(mm));
    memset(dd, 0x00, sizeof(dd));
    memset(HH, 0x00, sizeof(HH));
    memset(MM, 0x00, sizeof(MM));
    memset(SS, 0x00, sizeof(SS));

    memcpy(mm, &mmddHHMMSS[0], 2); 
    memcpy(dd, &mmddHHMMSS[2], 2); 
    memcpy(HH, &mmddHHMMSS[4], 2); 
    memcpy(MM, &mmddHHMMSS[6], 2); 
    memcpy(SS, &mmddHHMMSS[8], 2); 

    if (mm[0]<=' '||dd[0]<=' '||HH[0]<=' '||MM[0]<=' '||SS[0]<=' ')
        return rtn;

    rtp->tm_mon  = atoi(mm)-1;
    rtp->tm_mday = atoi(dd);
    rtp->tm_hour = atoi(HH);
    rtp->tm_min  = atoi(MM);
    rtp->tm_sec  = atoi(SS);

    // when change year: 1231235959 -> 0101000000
    if (rtp->tm_mon > ctp->tm_mon) 
        rtp->tm_year -= 1;
    rtime = mktime(rtp);

    rtn = ctime - rtime;
    return rtn;
}

Tags: ,

Categories:

Updated:

Leave a comment