Useful functions in c

3 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;
    }
}

Tags: ,

Categories:

Updated:

Leave a comment