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
#ifdef 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__ */
...全文
相关推荐
Linux C 常用库函数手册(最新整理)
Linux C 常用库函数手册是一份针对Linux平台下C语言编程的重要参考资料,它包含了丰富的函数库函数,是开发者学习和查阅C编程时的重要工具。
C语言 | C语言中 100个常用函数全面解析
掌握这100个函数可覆盖90%的C语言开发需求,建议结合实践项目加深理解。
常用函数整理
常用函数整理"这个主题意味着我们将探讨一个收集了各种常用功能的函数库,这些功能可能涵盖了多个编程领域。首先,函数库的一个关键优点是代码重用。
C/C++ 常用函数查找大全
C/C++ 常用函数查找大全"是一个非常实用的资源,它提供了详尽的函数参考信息,帮助开发者快速定位并理解所需的函数功能。
C语言代码自动整理工具.rar
描述中提到的“自动生成程序流程图”是一项重要功能,它利用特定的算法解析C语言代码,将程序逻辑转换为图形化的表示方式。


