gil's pkm2

lr cpc stript

#include "lrs.h"

int getWorkSpace(char **lpWorkSpace,unsigned long ulSize) {
        int MAX_SIZE=1048576;

        ulSize=(ulSize>MAX_SIZE)?MAX_SIZE:ulSize;
        *lpWorkSpace=(char *)malloc(ulSize);
        memset(*lpWorkSpace,0,ulSize);
        return (ulSize);
}

Actions()
{
        char *lpDataWs=NULL,*lpTempDataWs=NULL,*lpWork,*lpTempWork,*lpSearchString,*lpTempSearchString,cEOF=0;
        char cMakeDigit=0,cFirstDigit=0,cBackSlash=0,cEnableWrite=0,cSkipQuote=0,cUpdateTempWork=0;
        char cSearchValid=0,cFoundDelimiter=0;
        char lpFileIn[128]={"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"};
        char lpFileOut[128]={"yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"};
        char *lzFileName2={"c:\\temp\\bufloc.inp"};
        int fp1=0,fp2=0,fp3,tfp=0,iBuf=0,iBuffer,BufSpaceAvailable=1,iRecv=0,iSend=0,NumElements=1;
        int iSearchStringLength=0,iSearchStringLength2,iMatchFound=0,iSearchOffset=0;
        unsigned long iFileSize,offset=0,uLine=1,uBufSize=0,uBufSize2=0,uBytesRead,uTotalBytesRead=0;
        unsigned long uTempBytesWritten=0;

        fp1=fopen(lzFileName2,"r");
        fseek(fp1,0,2);
        iFileSize=ftell(fp1);
        fseek(fp1,0,0);

        getWorkSpace(&lpWork,iFileSize);
        fgets(lpWork,iFileSize,fp1);
        memcpy(lpFileIn,lpWork,iFileSize);
        fgets(lpWork,iFileSize,fp1);
        memcpy(lpFileOut,lpWork,iFileSize);

        lpTempWork=lpFileIn;
        while (*(lpTempWork++)!=10);
        uBufSize=(lpTempWork-lpFileIn)-1;
        *lpTempWork=0;*(--lpTempWork)=0;

        lpTempWork=lpFileOut;
        while (*(lpTempWork++)!=10);
        uBufSize+=((lpTempWork-lpFileOut));
        *lpTempWork=0;*(--lpTempWork)=0;

        iFileSize-=(uBufSize+4);

        getWorkSpace(&lpTempSearchString,iFileSize);
        fgets(lpTempSearchString,iFileSize,fp1);
        iSearchStringLength2=(iFileSize-2)+1;
        fclose(fp1);
//lr_output_message("SearchStringLength2 %d",iSearchStringLength2);
        getWorkSpace(&lpSearchString,iFileSize);
        lpTempWork=lpTempSearchString;
        lpWork=lpSearchString;

        while (iSearchStringLength2&&(cFoundDelimiter<2)) {
                if (*lpTempSearchString==34) {
                        if (*(lpTempSearchString-1)==92) ;
                        else {
                                cSkipQuote=1;
                                cFoundDelimiter++;
                        }
                }
                if (cSkipQuote)
                        cSkipQuote=0;
                else {
                        if (cMakeDigit) {
                                if (cFirstDigit) {
                                        if ((tolower(*lpTempSearchString)>='a')&&(tolower(*lpTempSearchString)<='f'))
                                                switch (tolower(*lpTempSearchString)) {
                                                        case 'a':
                                                                *lpWork=10; break;
                                                        case 'b':
                                                                *lpWork=11; break;
                                                        case 'c':
                                                                *lpWork=12; break;
                                                        case 'd':
                                                                *lpWork=13; break;
                                                        case 'e':
                                                                *lpWork=14; break;
                                                        case 'f':
                                                                *lpWork=15; break;
                                                }
                                        else
                                                *lpWork=*lpTempSearchString-48;
                                        *lpWork<<=4;
                                        cFirstDigit=0;
                                }
                                else {
                                        if ((tolower(*lpTempSearchString)>='a')&&(tolower(*lpTempSearchString)<='f'))
                                                switch (tolower(*lpTempSearchString)) {
                                                        case 'a':
                                                                *lpWork^=10; break;
                                                        case 'b':
                                                                *lpWork^=11; break;
                                                        case 'c':
                                                                *lpWork^=12; break;
                                                        case 'd':
                                                                *lpWork^=13; break;
                                                        case 'e':
                                                                *lpWork^=14; break;
                                                        case 'f':
                                                                *lpWork^=15; break;
                                                }
                                        else
                                                *lpWork^=(*lpTempSearchString-48);
                                        cBackSlash=cMakeDigit=0;
                                        cUpdateTempWork=1;
                                }
                        }
                        else {
                                if ((!cBackSlash)&&(*lpTempSearchString==92))
                                        cBackSlash=1;
                                else
                                        if (cBackSlash&&(*lpTempSearchString==120)) {
                                                cMakeDigit=1; cFirstDigit=1;
                                        }
                                else {
                                        *lpWork=*lpTempSearchString;
                                        cUpdateTempWork=1;
                                }
                        }
                }
                if (cUpdateTempWork) {
//lr_output_message("SearchString %02x",*lpWork);
                        ++lpWork;
                        ++iSearchStringLength;
//lr_output_message("SearchStringLength %d",iSearchStringLength);
                        cUpdateTempWork=0;
                }
                ++lpTempSearchString;
                --iSearchStringLength2;
        }
        cMakeDigit=0;cFirstDigit=0;cBackSlash=0;cUpdateTempWork=0;
//lr_output_message("SearchStringLength %d",iSearchStringLength);
        free(lpWork);
        fp1=fopen("c:\\temp\\data.ws","rb");
//        fp1=fopen(lpFileIn,"rb");
        fp2=fopen("c:\\temp\\bufloc.out","w");
//        fp2=fopen(lpFileOut,"w");
        fp3=fopen("c:\\temp\\strloc.out","w");
//        fp3=fopen(lpFileOut2,"w");
        if (fp3)
                fprintf(fp3,"Searching for %s\r\n",lpSearchString);
        if (fp2) {
                fprintf(fp2,"Finding buffer locations for %s\r\n",lpFileIn);
        if (fp1) {                                                                        // must have valid file pointer first
                fseek(fp1,0,2);
                iFileSize=ftell(fp1);
                fseek(fp1,0,0);
                uBufSize=getWorkSpace(&lpDataWs,iFileSize);
                if (!lpDataWs) {
                        lr_output_message("Memory allocation failure - exiting");
                        cEOF=1;
                }
                uBufSize2=getWorkSpace(&lpTempDataWs,iFileSize);
                if (!lpTempDataWs) {
                        lr_output_message("Memory allocation failure - exiting");
                        cEOF=1;
                }
//                tfp=tmpfile();
tfp=fopen("c:\\temp\\bufbinary.out","w");
                while (!cEOF) {
                        uBytesRead=fread(lpDataWs, uBufSize, NumElements, fp1);
                        lpWork=lpDataWs;
                        lpTempWork=lpTempDataWs;
                        do {
                                switch (*lpWork) {
                                        case 10:
                                                if (*(lpWork-1)==13)
                                                        ++uLine;
                                                break;
                                        case 100:
                                                if (*(lpWork-1)=='n')
                                                        if (*(lpWork-2)=='e')
                                                                if (*(lpWork-3)=='s')
                                                                        fprintf(fp2,"send buf%d - line %d\r\n",iBuf++,uLine,iSend++);
                                                break;
                                        case 118:
                                                if (*(lpWork-1)=='c')
                                                        if (*(lpWork-2)=='e')
                                                                if (*(lpWork-3)=='r')
                                                                        fprintf(fp2,"recv buf%d - line %d\r\n",iBuf++,uLine,iRecv++);
                                                break;
                                        case 49:
                                                if (*(lpWork-1)=='-')
                                                        cEOF=1;
                                                break;
                                        case 34:
                                                if (*(lpWork-1)==9) {
                                                        cSkipQuote=cEnableWrite=1;
                                                        break;
                                                }
                                                if (*(lpWork-1)!=92)
                                                        cEnableWrite=0;
                                                break;
                                }
//lr_output_message("offset %lu\t%x\t%d\t%c",uTotalBytesRead,*lpWork,*lpWork,*lpWork);
                                if (cEnableWrite) {
                                        if (cSkipQuote)
                                                cSkipQuote=0;
                                        else {
                                                if (cMakeDigit) {
                                                        if (cFirstDigit) {
                                                                if ((tolower(*lpWork)>='a')&&(tolower(*lpWork)<='f'))
                                                                        switch (tolower(*lpWork)) {
                                                                                case 'a':
                                                                                        *lpTempWork=10; break;
                                                                                case 'b':
                                                                                        *lpTempWork=11; break;
                                                                                case 'c':
                                                                                        *lpTempWork=12; break;
                                                                                case 'd':
                                                                                        *lpTempWork=13; break;
                                                                                case 'e':
                                                                                        *lpTempWork=14; break;
                                                                                case 'f':
                                                                                        *lpTempWork=15; break;
                                                                        }
                                                                else
                                                                        *lpTempWork=*lpWork-48;
                                                                *lpTempWork<<=4;
                                                                cFirstDigit=0;
                                                        }
                                                        else {
                                                                if ((tolower(*lpWork)>='a')&&(tolower(*lpWork)<='f'))
                                                                        switch (tolower(*lpWork)) {
                                                                                case 'a':
                                                                                        *lpTempWork^=10; break;
                                                                                case 'b':
                                                                                        *lpTempWork^=11; break;
                                                                                case 'c':
                                                                                        *lpTempWork^=12; break;
                                                                                case 'd':
                                                                                        *lpTempWork^=13; break;
                                                                                case 'e':
                                                                                        *lpTempWork^=14; break;
                                                                                case 'f':
                                                                                        *lpTempWork^=15; break;
                                                                        }
                                                                else
                                                                        *lpTempWork^=(*lpWork-48);
                                                                cBackSlash=cMakeDigit=0;
                                                                cSearchValid=cUpdateTempWork=1;
                                                        }
                                                }
                                                else {
                                                        if ((!cBackSlash)&&(*lpWork==92))
                                                                cBackSlash=1;
                                                        else {
                                                                if (cBackSlash&&(*lpWork==120)) {
                                                                        cMakeDigit=1; cFirstDigit=1;
                                                                }
                                                                else {
                                                                        *lpTempWork=*lpWork;
                                                                        cSearchValid=cUpdateTempWork=1;
                                                                }
                                                        }
                                                }
                                                if (cSearchValid) {
                                                        if (*lpTempWork==*(lpSearchString+iSearchOffset)) {
//lr_output_message("TEMPWork %x\t%x",*lpTempWork,*((lpSearchString+iSearchOffset)-1));
//lr_output_message("TEMPWork %d\t%d",iSearchOffset,iSearchStringLength);
                                                                if (++iSearchOffset==iSearchStringLength) {
                                                                        iSearchOffset=0;
                                                                        fprintf(fp3,"\r\n%d).  Line %d",++iMatchFound,uLine);
                                                                }
                                                        }
                                                        else
                                                                if (iSearchOffset)                                        // partial string - reset
                                                                        iSearchOffset=0;
                                                        cSearchValid=0;
                                                }
                                                if (cUpdateTempWork) {
//lr_output_message("TEMPoffset %lu\t%x\t%d\t%c",uTempBytesWritten,*lpTempWork,*lpTempWork,*lpTempWork);
                                                        ++lpTempWork;
                                                        ++uTempBytesWritten;
                                                        cUpdateTempWork=0;
                                                }
                                                if (uTempBytesWritten==uBufSize2) {
                                                        fwrite(lpTempDataWs, uBufSize2, NumElements, tfp);
                                                        memset(lpTempDataWs,0,uBufSize2);
                                                        uTempBytesWritten=0;
                                                }
                                        }
                                }
                                lpWork++;
                                uTotalBytesRead++;
                        } while ((++offset<uBufSize)&&(!cEOF));
                        memset(lpDataWs,0,uBufSize);
                        offset=0;
                }
                if (uTempBytesWritten)
                        fwrite(lpTempDataWs, uBufSize2, NumElements, tfp);
                fprintf(fp2,"Total Send Buffers: %d\r\nTotal Receive Buffers: %d\r\n",iSend,iRecv);
                lr_output_message("Read %lu bytes",uTotalBytesRead);
                free(lpDataWs);
                fclose(fp1);
                fclose(fp2);
                fclose(fp3);
fclose(tfp);
        }}       
        return 0;
}

posted on 2009-07-29 22:04  gil's pkm2  阅读(139)  评论(0)    收藏  举报

导航