util.c
来自「打魔兽战网的都知道他是什么」· C语言 代码 · 共 720 行 · 第 1/2 页
C
720 行
/* * Copyright (C) 1998 Mark Baysinger (mbaysing@ucsd.edu) * Copyright (C) 1998,1999,2000,2001 Ross Combs (rocombs@cs.nmsu.edu) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */#include "common/setup_before.h"#include <stdio.h>#ifdef HAVE_STDDEF_H# include <stddef.h>#else# ifndef NULL# define NULL ((void *)0)# endif#endif#ifdef STDC_HEADERS# include <stdlib.h>#else# ifdef HAVE_MALLOC_H# include <malloc.h># endif#endif#include "compat/strtoul.h"#ifdef HAVE_STRING_H# include <string.h>#else# ifdef HAVE_STRINGS_H# include <strings.h># endif#endif#ifdef TIME_WITH_SYS_TIME# include <sys/time.h># include <time.h>#else# ifdef HAVE_SYS_TIME_H# include <sys/time.h># else# include <time.h># endif#endif#include "compat/strcasecmp.h"#include "compat/strncasecmp.h"#include "common/xalloc.h"#include <ctype.h>#include "common/util.h"#include "common/setup_after.h"extern int strstart(char const * full, char const * part){ size_t strlen_part; int compare_result; if (!full || !part) return 1; strlen_part = strlen(part); compare_result = strncasecmp(full,part,strlen_part); /* If there is more than the command, make sure it is separated */ if (compare_result!=0) return compare_result; else if (full[strlen_part]!=' ' && full[strlen_part]!='\0') return 1; else return compare_result;}#define DEF_LEN 64#define INC_LEN 16extern char * file_get_line(FILE * fp){ static char * line = NULL; static unsigned int len = 0; unsigned int pos = 0; int prev_char,curr_char; // use file_get_line with NULL argument to clear the buffer if (!(fp)) { len = 0; if ((line)) xfree((void *)line); line = NULL; return NULL; } if (!(line)) { line = xmalloc(DEF_LEN); len = DEF_LEN; } prev_char = '\0'; while ((curr_char = fgetc(fp))!=EOF) { if (((char)curr_char)=='\r') continue; /* make DOS line endings look Unix-like */ if (((char)curr_char)=='\n') { if (pos<1 || ((char)prev_char)!='\\') break; pos--; /* throw away the backslash */ prev_char = '\0'; continue; } prev_char = curr_char; line[pos++] = (char)curr_char; if ((pos+1)>=len) { len += INC_LEN; line = xrealloc(line,len); } } if (curr_char==EOF && pos<1) /* not even an empty line */ { return NULL; } line[pos] = '\0'; return line;}extern char * strreverse(char * str){ unsigned int len; char temp; char *start, *end; if (!str) return NULL; len = strlen(str); for (start=str,end=str+len-1;start<end;start++,end--) { temp = *end; *end = *start; *start = temp; } return str;}extern int str_to_uint(char const * str, unsigned int * num){ unsigned int val; unsigned int pval; char * pos; if (!str || !num) return -1; for (pos=(char *)str; *pos==' ' || *pos=='\t'; pos++); if (*pos=='+') pos++; val = 0; for (; *pos!='\0'; pos++) { pval = val; val *= 10; if (val/10!=pval) /* check for overflow */ return -1; pval = val; if (isdigit(*pos)) val += *pos - '0'; else return -1; if (val<pval) /* check for overflow */ return -1; } *num = val; return 0;}extern int str_to_ushort(char const * str, unsigned short * num){ unsigned short val; unsigned short pval; char * pos; if (!str || !num) return -1; for (pos=(char *)str; *pos==' ' || *pos=='\t'; pos++); if (*pos=='+') pos++; val = 0; for (; *pos!='\0'; pos++) { pval = val; val *= 10; if (val/10!=pval) /* check for overflow */ return -1; pval = val; if (isdigit(*pos)) val += *pos - '0'; else return -1; if (val<pval) /* check for overflow */ return -1; } *num = val; return 0;}/* This routine assumes ASCII like control chars. If len is zero, it will print all characters up to the first NUL, otherwise it will print exactly that many characters. */int str_print_term(FILE * fp, char const * str, unsigned int len, int allow_nl){ unsigned int i; if (!fp) return -1; if (!str) return -1; if (len==0) len = strlen(str); for (i=0; i<len; i++) if ((str[i]=='\177' || (str[i]>='\000' && str[i]<'\040')) && (!allow_nl || (str[i]!='\r' && str[i]!='\n'))) fprintf(fp,"^%c",str[i]+64); else fputc((int)str[i],fp); return 0;}extern int str_get_bool(char const * str){ if (!str) return -1; if (strcasecmp(str,"true")==0 || strcasecmp(str,"yes")==0 || strcasecmp(str,"on")==0 || strcmp(str,"1")==0) return 1; if (strcasecmp(str,"false")==0 || strcasecmp(str,"no")==0 || strcasecmp(str,"off")==0 || strcmp(str,"0")==0) return 0; return -1;}extern char const * seconds_to_timestr(unsigned int totsecs){ static char temp[256]; int days; int hours; int minutes; int seconds; days = totsecs/(24*60*60); hours = totsecs/(60*60) - days*24; minutes = totsecs/60 - days*24*60 - hours*60; seconds = totsecs - days*24*60*60 - hours*60*60 - minutes*60; if (days>0) sprintf(temp,"%d day%s %d hour%s %d minute%s %d second%s", days,days==1 ? "" : "s", hours,hours==1 ? "" : "s", minutes,minutes==1 ? "" : "s", seconds,seconds==1 ? "" : "s"); else if (hours>0) sprintf(temp,"%d hour%s %d minute%s %d second%s", hours,hours==1 ? "" : "s", minutes,minutes==1 ? "" : "s", seconds,seconds==1 ? "" : "s"); else if (minutes>0) sprintf(temp,"%d minute%s %d second%s", minutes,minutes==1 ? "" : "s", seconds,seconds==1 ? "" : "s"); else sprintf(temp,"%d second%s.", seconds,seconds==1 ? "" : "s"); return temp;}extern int clockstr_to_seconds(char const * clockstr, unsigned int * totsecs){ unsigned int i,j; unsigned int temp; if (!clockstr) return -1; if (!totsecs) return -1; for (i=j=temp=0; j<strlen(clockstr); j++) { switch (clockstr[j]) { case ':': temp *= 60; temp += strtoul(&clockstr[i],NULL,10); i = j+1; break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': break; default: return -1; } } if (i<j) { temp *= 60; temp += strtoul(&clockstr[i],NULL,10); } *totsecs = temp; return 0;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?