T-ware Inc.
C Language Development Kit
[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): ())
# 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) + );
}
/*************************************** ****************************
* 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 };
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;
Char B;
} C;
C. a = 1;
Return (c. B = 1 );
}
# Endif/* _ tsdk_h __*/
From tody_guo's column