T-ware Inc.
C 语言开发包
/* 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__ */