C 常用功能函数 整理

Tody Guo 2010-05-07 02:49:01

/*
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__ */

...全文
309 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
z569362161 2010-05-09
  • 打赏
  • 举报
回复
没有了,都让你开发了。
  • 打赏
  • 举报
回复
谢谢楼主,楼主辛苦了,
vanchristin 2010-05-09
  • 打赏
  • 举报
回复
楼主很 厉害,也很有心
我看好你哟
旭子 2010-05-09
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 simple1129 的回复:]

楼主学有所思,将来必成大器!
[/Quote]
其实楼主已经成大器了!!!
厉害啊呵呵,学习了!
w74839520 2010-05-08
  • 打赏
  • 举报
回复
有心人。。。
weilong8998 2010-05-08
  • 打赏
  • 举报
回复
支持,支持///
Tody Guo 2010-05-08
  • 打赏
  • 举报
回复
自己没事整理的完的,希望大家有什么新功能往里面加啊。谢谢啦。
regerfe 2010-05-08
  • 打赏
  • 举报
回复
LZ,加点注释好吗?
FDWolf 2010-05-08
  • 打赏
  • 举报
回复
支持楼主,努力学习
anew2010 2010-05-08
  • 打赏
  • 举报
回复
顶一下你了
simple1129 2010-05-08
  • 打赏
  • 举报
回复
楼主学有所思,将来必成大器!
ctwoz 2010-05-08
  • 打赏
  • 举报
回复
up......
somesky 2010-05-08
  • 打赏
  • 举报
回复
up,强烈支持!
mzlogin 2010-05-08
  • 打赏
  • 举报
回复
不错不错~~~支持下。
十八道胡同 2010-05-08
  • 打赏
  • 举报
回复
up
up
yuanzhang198711 2010-05-08
  • 打赏
  • 举报
回复
谢谢楼主的奉献精神!
hw19886200 2010-05-08
  • 打赏
  • 举报
回复
up!!!
哈北儿 2010-05-07
  • 打赏
  • 举报
回复
顶一下~
阿磊2013 2010-05-07
  • 打赏
  • 举报
回复
楼主是个有心人啊
zhangzhongke007 2010-05-07
  • 打赏
  • 举报
回复
楼主学有所思,将来必成大器!
加载更多回复(4)

69,364

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧