📄 apptool.c
字号:
* 功 能:取当前时间转化为字符串 * 返 回:无 * 参 数: * 编 写:LHZ * 日 期:2004-04-05 */void get_stime(char *sresult, char *stype){ time_t t_now; /*现在时间*/ char strtmp[50]; /*临时字符串*/ struct tm *ptm_temp; /*临时tm变量*/ time(&t_now); /*取得现在时间*/ ptm_temp = localtime(&t_now); if (strcmp(stype,"yyyy-mm-dd hh:mm:ss") == 0 || strcmp(stype,"YYYY-MM-DD HH:MM:SS") == 0) { strftime(strtmp,50,"%Y-%m-%d %H:%M:%S",ptm_temp); } else if (strcmp(stype,"yyyymmddhhmmss") == 0 || strcmp(stype,"YYYYMMDDHHMMSS") == 0) { strftime(strtmp,50,"%Y%m%d%H%M%S",ptm_temp); } else if (strcmp(stype,"yyyy-mm-dd") == 0 || strcmp(stype,"YYYY-MM-DD") == 0) { strftime(strtmp,50,"%Y-%m-%d",ptm_temp); } else if (strcmp(stype,"yyyy/mm/dd") == 0 || strcmp(stype,"YYYY/MM/DD") == 0) { strftime(strtmp,50,"%Y/%m/%d",ptm_temp); } else if (strcmp(stype,"yyyymmdd") == 0 || strcmp(stype,"YYYYMMDD") == 0) { strftime(strtmp,50,"%Y%m%d",ptm_temp); } else if (strcmp(stype,"hh:mm:ss") == 0 || strcmp(stype,"HH:MM:SS") == 0) { strftime(strtmp,50,"%H:%M:%S",ptm_temp); } else { strcpy(strtmp,"\n"); } strcpy(sresult,strtmp); return;}/****************以下为读取INI文件函数*******************//** * 功 能:获取指定域的KEY和val值 * 返 回:无 * 参 数: * 编 写:LHZ * 日 期:2004-04-05 */int iniSplit(char *cInKeyBuf, char **cKeyTag, char **cKeyVal){ long lCirTemp = 0; /*循环变量*/ long lStrlen = 0; /*字符串长度*/ long lKeyTagBegin = 0; /*KEY标题开始位置*/ long lKeyValBegin = 0; /*KEY值开始位置*/ DelSpecialChar(cInKeyBuf); /*传入字符串为空*/ if( 1 > (lStrlen = strlen((char *)cInKeyBuf)) ) { return 0; } /*搜寻KEY行开始位置:行的缩进用'\t'*/ for(lCirTemp = 0; lCirTemp < lStrlen; lCirTemp++) { if( '\t' != cInKeyBuf[lCirTemp] ) { break; } } /*空行*/ if( lStrlen <= lCirTemp ) { return 0; } /*没有KEY标题*/ if( '=' == cInKeyBuf[lCirTemp] ) { return -1; } /*KEY标题开始位置*/ lKeyTagBegin = lCirTemp; /*KEY标题和值之间通过"="联接*/ for(lCirTemp++; lCirTemp < lStrlen; lCirTemp++) { if( '=' == cInKeyBuf[lCirTemp] ) { break; } } /*KEY值为空*/ if( lStrlen <= lCirTemp ) { return -2; } /*"="开始位置*/ lKeyValBegin = lCirTemp; /*KEY值开始位置*/ for(lCirTemp++; lCirTemp < lStrlen; lCirTemp++) { if( '\t' != cInKeyBuf[lCirTemp] ) { break; } } /*分隔KEY标题和值*/ cInKeyBuf[lKeyValBegin] = '\0'; /*KEY标题*/ *cKeyTag = cInKeyBuf + lKeyTagBegin; /*KEY值*/ *cKeyVal = cInKeyBuf + lCirTemp; return 1;}/* * 功 能:取得INI文件中指定域的KEY值 * 返 回:无 * 参 数: * 编 写:LHZ * 日 期:2004-04-05 */char *GetInitKey(char *cInitFile, char *cSection, char *cInKeyTag, char *cOutKeyValue){ FILE *fp; /*文件指针*/ char cTempBuf1[MAX_LINE_BUF + 1] = ""; /*临时变量1*/ char cTempBuf2[MAX_LINE_BUF + 1] = ""; /*临时变量2*/ char *cKeyTag = NULL; /*KEY*/ char *cKeyVal = NULL; /*KEY对应值*/ long lLineNo = 0; /*行号*/ long lStrLen = 0; /*字符串长度*/ long lErrCode = 0; /*错误代码*/ long lSectionLineNo = 0; /*域行号*/ long lKeyLineNo = 0; /*KEY行号*/ /*校验传入参数*/ if ( !(cInitFile && cSection && cInKeyTag && cOutKeyValue) ) { return NULL; } /*打开配置文件*/ if((fp = fopen((char *)cInitFile, "r")) == NULL) { printf("\nOpen initial file failed.\n%s: No such file or directory.",cInitFile); return INI_ERR_OPEN_FILE; } /*查找域*/ while( !feof(fp) ) { lErrCode = INI_ERR_READ_FILE; /*读取每一行*/ if( NULL == fgets(cTempBuf1, MAX_LINE_BUF, fp) ) { goto InitEnd; } /*数据为空*/ if( ferror(fp) ) { goto InitEnd; } lLineNo++; /*添加结束符*/ cTempBuf1[strlen(cTempBuf1) - 1] = '\0'; lStrLen = strlen(DelSpecialChar(cTempBuf1)); /*注释行*/ if( 0 == lStrLen || ';' == cTempBuf1[0] || '#' == cTempBuf1[0] ) { continue; } lErrCode = INI_ERR_FILE_FORMAT; /*域标题结束标识符不正确*/ if( 2 < lStrLen && (SECTION_L == cTempBuf1[0] && SECTION_R != cTempBuf1[n-1]) ) { goto InitEnd; } /*取域标题内容*/ if( SECTION_L == cTempBuf1[0] || 0 < lStrLen - 2 ) { cTempBuf1[lStrLen - 1] = '\0'; /*当前行和需要搜寻的域标题内容一致*/ if( 0 == strcmp(cSection,cTempBuf1 + 1) ) { break; } } lErrCode = INI_SECTION_NOT_FOUND; /*搜寻完成没有找到需要搜寻的域*/ if ( feof(fp) ) { goto InitEnd; } } /*域行号*/ lSectionLineNo = lLineNo; /*查找KEY*/ while( !feof(fp) ) { lErrCode = INI_ERR_READ_FILE; /*域下内容为空*/ if( NULL == fgets(cTempBuf1, MAX_LINE_BUF, fp) ) { goto InitEnd; } /*文件错误*/ if( ferror(fp) ) { goto InitEnd; } lLineNo++; lKeyLineNo = lLineNo; cTempBuf1[strlen(cTempBuf1) - 1] = '\0'; /*当前行长度*/ lStrLen = strlen(DelSpecialChar(cTempBuf1)); /*当前行为空或已经注释*/ if( 0 == lStrLen || ';' == cTempBuf1[0] ) { continue; } lErrCode = INI_KEY_NOT_FOUND; /*已经进入下一个域*/ if ( '[' == cTempBuf1[0] ) { goto InitEnd; } /*读取续行数据*/ if ( '+' == cTempBuf1[lStrLen - 1] ) { cTempBuf1[lStrLen - 1] = '\0'; while( !feof(fp) ) { /*取续行*/ lErrCode = INI_ERR_READ_FILE; /*续行数据为空*/ if( NULL == fgets(cTempBuf2, MAX_LINE_BUF, fp) ) { goto InitEnd; } /*文件错误*/ if( ferror(fp) ) { goto InitEnd; } lLineNo++; cTempBuf2[strlen(cTempBuf2) - 1 ] = '\0'; /*续行长度*/ lStrLen = strlen(DelSpecialChar(cTempBuf2)); /*续行还有续行*/ if ( 0 < lStrLen && '+' == cTempBuf2[lStrLen - 1] ) { /*去掉加号*/ cTempBuf2[lStrLen - 1] = '\0'; /*行数据操过最大允许值*/ if ( MAX_LINE_BUF < (strlen(cTempBuf1) + lStrLen - 1) ) { goto InitEnd; } /*串续行数据*/ strncat(cTempBuf1, cTempBuf2, lStrLen - 1); continue; } /*行数据操过最大允许值*/ if ( MAX_LINE_BUF < (strlen(cTempBuf1)+ lStrLen - 1) ) { goto InitEnd; } /*续行没有续行,串续行数据*/ strncat(cTempBuf1, cTempBuf2, lStrLen-1); break; } } lErrCode = INI_ERR_FILE_FORMAT; /*取KEY名称、值*/ if ( 1 != iniSplit(cTempBuf1, &cKeyTag, &cKeyVal) ) { goto InitEnd; } DelSpecialChar(cKeyTag); /*取到的KEY标题和需要寻找的不一致*/ if ( 0 != strcmp(cKeyTag,cInKeyTag) ) { lErrCode = INI_KEY_NOT_FOUND; if ( feof(fp) ) { goto InitEnd; } continue; } /*KEY值*/ (void)strcpy(cOutKeyValue,cKeyVal); break; } printf("\n[Field Tag: %s,Key Tag: %s] = %s",cSection,cInKeyTag,cOutKeyValue); lErrCode = INI_OK;InitEnd: if( fp ) { fclose(fp); } if ( 0 > lErrCode ) { printf("\n[Field Tag: %s,Key Tag: %s]:ErrCode = %d, ErrMsg = %s", \ cSection,cInKeyTag,lErrCode,iniGetErr(lErrCode)); } return cOutKeyValue;}/** * 功 能:取读INI文件错误信息 * 返 回:无 * 参 数: * 编 写:LHZ * 日 期:2004-04-05 */const char *iniGetErr(int errno){ int iCir = 0; /*循环变量*/ const char *pErr = "Unknown errno.";/*错误信息*/ typedef struct iniErrList { int iErrNo; /*错误代码*/ const char *pErr; /*错误信息描述*/ } iniErrList; static struct iniErrList sErrList[] = { { 0 , "No error." }, { -10, "Open file error." }, { -11, "Read file error." }, { -12, "File format error."}, { -13, "Section not found."}, { -14, "Key not found." } }; for(iCir = 0; sErrList[iCir].pErr; iCir++) { if (sErrList[iCir].iErrNo == errno) { pErr = sErrList[iCir].pErr; break; } } return pErr;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -