程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> TSDK.H 開發包

TSDK.H 開發包

編輯:關於C語言

T-ware Inc.
C 語言開發包

[cpp]
/*
    TSDK.H
 
    Definitions for Common functions and string functions.
 
    Copyright (c)  Tody 2010
    All Rights Reserved 
*/ 
 
#ifndef __TSDK_H__ 
#define __TSDK_H__ 
 
#include <string.h> 
#include <time.h> 
#include <ctype.h> 
 
#if __STDC__ 
# define _Cdecl 
#else 
# define _Cdecl  cdecl 
#endif 
 
#ifndef DOS 
# define SAY(x...)   printf(x) 
#else 
# define SAY(x)      printf(x) 
#endif 
 
#ifndef MAX 
# define MAX(a, b) ((a) < (b) ? (b) : (a)) 
#endif 
#ifndef MIN 
# define MIN(a, b) ((a) < (b) ? (a) : (b)) 
#endif 
 
#define FATAL(x) do { \ 
    printf("\n[-] PROGRAM ABORT : %s", x); \ 
    exit(1); \ 
  } while (0) 
 
#define VERSION "1.0.0" 
 
#define true    1 
#define false   0 
 
typedef char    bool; 
 
 
/* Function Declare */ 
bool  _Cdecl    copy_file   (char *scr_file, char *dst_file, int flag); 
bool  _Cdecl    file_exist  (char *file); 
 
char *_Cdecl    skip_spaces (const char *str); 
char *_Cdecl    strim       (char *p); 
int   _Cdecl    t_random    (int a, int b); 
bool  _Cdecl    log_save    (char *log_file, char *ErrMsg); 
 
bool  _Cdecl    is_leap_yaer(int year); 
int   _Cdecl    which_day   (int year, int mon, int day); 
 
bool  _Cdecl        is_little_endian( void ); 
 
/*******************************************************************
*       Remove leading whitespace string  
*******************************************************************/ 
char *skip_spaces(const char *str) 

        while (isspace(*str)) 
                ++str; 
        return (char *)str; 

 
/*******************************************************************
*       Remove Left and right blank of string  
*******************************************************************/ 
char *strim(char *s) 

        size_t size; 
        char *end; 
 
        s = skip_spaces(s); 
        size = strlen(s); 
        if (!size) 
                return s; 
 
        end = s + size - 1; 
        while (end >= s && isspace(*end)) 
                end--; 
        *(end + 1) = '\0'; 
 
        return s; 

 
/*******************************************************************
*       Random a number between a and b (not contain b)
*******************************************************************/ 
int t_random (int a, int b) 

    if (a >= b) return -1; 
    srand(time(NULL));  /* Seed is time */ 
    return (rand()%(b-a) + a);   

 
/*******************************************************************
*       Copy file from scr_file to des_file
*******************************************************************/ 
bool copy_file (char *scr_file, char *des_file, int flag) 

    FILE *fin,*fout; 
    char ch; 
     
    if (0 == flag  && file_exist(des_file) == 1)    return false;   
     
    if ((fin=fopen(scr_file, "rb")) == NULL) 
    { 
        return false; 
    } 
    if ((fout=fopen(des_file, "wb")) == NULL) 
    { 
        return false; 
    } 
 
    ch = fgetc(fin);  /* File copying... */ 
    while ( ch != EOF) 
    { 
        fputc(ch, fout); 
        ch = fgetc(fin);     
    } 
 
    fclose(fout); 
    fclose(fin); 
    return true; 

 
/*******************************************************************
*       Check file is exist or not
*******************************************************************/ 
bool file_exist (char *file) 

    return !access(file,0); /* File exist, access() return 0, else return -1 */ 

 
/*******************************************************************
*       Save Log to File
*******************************************************************/ 
bool log_save ( char *log_file, char *ErrMsg) 

    FILE *p; 
    p = fopen(log_file, "wb"); 
    if ( p == NULL ) return false; 
    fprintf(p, "[Err]: %s\n", ErrMsg);  /* Write ErrMsg to File. */ 
    fclose(p); 
    return true; 

 
/*******************************************************************
*       Is Leap Year
*******************************************************************/ 
 
bool is_leap_year ( int year ) 

    return ( ((year%100==0) && (year%400==0)) || ((year%100!=0) && (year%4==0)) ); 

 
/*******************************************************************
*       Which Day with Year+Mon+day Specified
*******************************************************************/ 
int which_day (int year, int mon, int day) 

    static int mdays[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 }; 
    int i, days = day; 
     
    for (i=0; i<mon; ++i) 
    { 
        days += mdays[i]; 
    } 
 
    if (mon>2) 
    { 
        if ( is_leap_year(year) ) ++days; 
    } 
    return days; 

 
/*******************************************************************
*      Check CPU endian type
*******************************************************************/ 
bool is_little_endian( void ) 

        union w{ 
                       int a; 
                       char b; 
        } c; 
 
        c.a = 1; 
 
        return ( c.b == 1 ); 

#endif /* __tsdk_h__ */ 


摘自  tody_guo的專欄
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved