牛逼程序与君赏(2)(前面代码不具有代表性,现将剩余代码贴出来)
前面代码不具有代表性,现将剩余代码贴出来
- C/C++ code
// unpack.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "windows.h"#define LENGTH 24#define DLENGTH 24typedef char (__stdcall * DecompressMinute)(unsigned char *src,int srclen,unsigned char *dest,int *destlen); typedef char (__stdcall * DecompressDaily)(unsigned char *src,int srclen,unsigned char *dest,int *destlen); DecompressMinute pDecompressMinute; DecompressDaily pDecompressDaily;HANDLE hHeap;int DecompressDailyFile(char *src,char *dest){…… return result;}int DecompressMinuteFile(char *src,char *dest){……allexit: return result;}int main(int argc, char* argv[]){ WIN32_FIND_DATA wfd; HANDLE hf,hLogFile; BOOL result=TRUE; int i,j,len[128],len1; char logFile[MAX_PATH],sourcePath[MAX_PATH],destPath[MAX_PATH],sourceFile[MAX_PATH],destFile[MAX_PATH]; HINSTANCE hlib; SYSTEMTIME st; int fileCount=0,errFileCount=0; char errMsg[255]; DWORD dwByteWritten; DWORD dwStart;//start time hHeap=GetProcessHeap(); GetSystemTime(&st); GetCurrentDirectory(MAX_PATH,logFile); wsprintf(logFile,"%s\\%04d%02d%02d%02d%02d%02d.log",logFile,st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond); hLogFile=CreateFile(logFile,GENERIC_WRITE,FILE_SHARE_READ,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0); if(hLogFile==INVALID_HANDLE_VALUE) { printf("create log file %s error\n",logFile); goto allexit; } if(argc!=3) {printusage: printf("Usage: %s <Source> <Dest>\n\t<Source>\tDirectory to be decompressed\n\t<Dest>\tDirectory to save the decompressed files",argv[0]); goto allexit; } if((GetFileAttributes(argv[1])&FILE_ATTRIBUTE_DIRECTORY)==0) { printf("%s is not a directory\n",argv[1]); goto printusage; } if((GetFileAttributes(argv[2])&FILE_ATTRIBUTE_DIRECTORY)==0) { printf("%s is not a directory\n",argv[2]); goto printusage; } //load library if((hlib=LoadLibrary("comp2.dll"))==NULL) { printf("LoadLibrary error\n"); goto allexit; } //get decompress pointer pDecompressMinute=(DecompressMinute)GetProcAddress(hlib, "DecompressMinute"); if(pDecompressMinute==NULL) { printf("GetProcAddress DecompressMinute error\n"); goto freelib; } //get decompress pointer pDecompressDaily=(DecompressDaily)GetProcAddress(hlib, "DecompressDaily"); if(pDecompressDaily==NULL) { printf("GetProcAddress DecompressDaily error\n"); goto freelib; } dwStart = GetTickCount(); lstrcpy(sourcePath,argv[1]); j=lstrlen(sourcePath); if(sourcePath[j-1]=='\\') { sourcePath[--j]=0; } len[0]=j; lstrcpy(destPath,argv[2]); len1=lstrlen(destPath); if(destPath[len1-1]=='\\') { destPath[--len1]=0; } j=0;search: strcat(sourcePath,"\\*.*"); result=TRUE; if((hf=FindFirstFile(sourcePath,&wfd))==INVALID_HANDLE_VALUE) {//查找路径失败 printf("FindFirstFile %s Error\n",sourcePath); goto freelib; }process: if(!result) goto goup;//未找到,则pop i=lstrlen(wfd.cFileName); if(!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {//非目录,表示是文件,处理文件 //拼凑源文件全部名称=路径+文件名 lstrcpy(sourceFile,sourcePath); sourceFile[len[j]+1]=0;//printf("%s\n",sourceFile); lstrcat(sourceFile,wfd.cFileName); lstrcpy(destFile,destPath); destFile[len[j]+len1-len[0]]='\\'; destFile[len[j]+len1-len[0]+1]=0; lstrcat(destFile,wfd.cFileName);printf("%s\t\t\r",sourceFile); if((!lstrcmpi(&wfd.cFileName[i-4],".dfc"))) {//daily data destFile[i--+len[j]+len1-len[0]]='D'; destFile[i--+len[j]+len1-len[0]]='O'; destFile[i+len[j]+len1-len[0]]='E'; //printf("\n\nzlz%s %s %s %d\n\n",wfd.cFileName,sourceFile,destFile,result); if(DecompressDailyFile(sourceFile,destFile)==0) { wsprintf(errMsg,"decompress %s error\r\n",sourceFile); WriteFile(hLogFile,errMsg,lstrlen(errMsg),&dwByteWritten,0); printf(errMsg); sourceFile[len[j]+1+i]='E'; sourceFile[len[j]+2+i]='R'; sourceFile[len[j]+3+i]='R'; MoveFile(destFile,sourceFile); errFileCount++; } else { fileCount++; } } else { if((!lstrcmpi(&wfd.cFileName[i-4],".mfc"))) {//minute data destFile[i--+len[j]+len1-len[0]]='R'; destFile[i--+len[j]+len1-len[0]]='A'; destFile[i+len[j]+len1-len[0]]='B'; if(DecompressMinuteFile(sourceFile,destFile)==0) { wsprintf(errMsg,"decompress %s error\r\n",sourceFile); WriteFile(hLogFile,errMsg,lstrlen(errMsg),&dwByteWritten,0); printf(errMsg); sourceFile[len[j]+1+i]='E'; sourceFile[len[j]+2+i]='R'; sourceFile[len[j]+3+i]='R'; MoveFile(destFile,sourceFile); errFileCount++; } else { fileCount++; } } } goto nextsearch; } if((*((WORD *)(wfd.cFileName)))==0x2e) {//当前目录,不用处理 goto nextsearch; } if(wfd.dwFileAttributes &FILE_ATTRIBUTE_SYSTEM) {//系统目录,无法访问,不用处理 goto nextsearch; } if( ((*(DWORD *)(wfd.cFileName)) & 0x00ff2e2e)==0x2e2e) goto nextsearch;//上级目录,不用处理 //重新拼凑sourcePath路径和destPath路径 sourcePath[len[j]]='\\'; sourcePath[len[j]+1]=0; destPath[len[j]+len1-len[0]]='\\'; destPath[len[j]+len1-len[0]+1]=0; lstrcat(sourcePath,wfd.cFileName); lstrcat(destPath,wfd.cFileName); //create directory if(!CreateDirectory(destPath,NULL)) if(GetLastError()!=ERROR_ALREADY_EXISTS) {//////to be modified,处理错误 //创建目录失败,请检查磁盘是否可写 printf("create directory\t%s Error\r\n",destPath); } len[++j]=lstrlen(sourcePath); //显示目录树 //for(i=0;i<j;i++) // printf(" "); //printf("%s\t%d\n",wfd.cFileName,j); __asm push hf goto search;nextsearch: result=FindNextFile(hf,&wfd); goto process;goup://pop and go to upper directory //关闭FindClose FindClose(hf); if(j) { j--; __asm pop hf//出栈 goto nextsearch; } dwStart=GetTickCount() - dwStart; int hour,minute,second,millisecond ; hour=dwStart/(3600*1000); dwStart=dwStart%(3600*1000); minute=dwStart/(60*1000); dwStart=dwStart%(60*1000); second=dwStart/(1000); millisecond =dwStart%(1000); wsprintf(errMsg,"Time used:\t%d hours %d minutes %d seconds %d milliseconds\r\n",hour,minute,second,millisecond ); WriteFile(hLogFile,errMsg,lstrlen(errMsg),&dwByteWritten,0); printf(errMsg); wsprintf(errMsg,"File Processed:\t%d\r\n",fileCount); WriteFile(hLogFile,errMsg,lstrlen(errMsg),&dwByteWritten,0); printf(errMsg); wsprintf(errMsg,"Error File:\t%d\r\n",errFileCount); WriteFile(hLogFile,errMsg,lstrlen(errMsg),&dwByteWritten,0); printf(errMsg); wsprintf(errMsg,"Log File:\t%s\r\n",logFile); WriteFile(hLogFile,errMsg,lstrlen(errMsg),&dwByteWritten,0); printf(errMsg); CloseHandle(hLogFile);freelib: if(!FreeLibrary(hlib)) { printf("FreeLibrary error\n"); }allexit: return 0;}
[解决办法]
我看见了很多GOTO
-_-!
[解决办法]
先顶后看
[解决办法]
留名
[解决办法]
真是不想出来说了,楼主,你要知道,真理掌握在少数人手中,那人的代码不在于用了多少 goto, 我认为,在于他 goto 了多少地方,nextsearch,freelib, 这是不好的,
这又不是 goto 的错,
[解决办法]
这人写代码的风格和俺基本一样,说实话,我写的程序看起来会比他的好看一些。
俺不喜欢的就是就是诸如 --len 之类的代码。
以及 if语句中将常量放在==右边这种写法
俺用的goto可能比他少,不过俺的代码缩进会好多,当代码变的庞大的时候,并不比这种方法更好。
[解决办法]
楼主啊,有空来搞口水帖,不如去看《重构》学学如何维护、修改这类垃圾级代码。
[解决办法]
前面代码的goto确实没问题的,
这里确实有点问题,估计这个代码是汇编改的吧,或者以前写这个代码的人是个汇编程序员。
[解决办法]
程序作者原来是学BASIC的吧.
[解决办法]
与之前风格一致
[解决办法]