📄 getregfile.cpp
字号:
szNamePtr=EmboTrimRight(szNamePtr);
if(NULL==szNamePtr)
{
fRet=FALSE;
goto Get_RegValue_Return;
}
nszSize=_tcslen(szNamePtr);
if((1==nszSize)&&(_T('@')==*szNamePtr))
{
_tcscpy(regArgs.szName, _T("Default"));
// _tcscpy(regArgs.szName, _T("@"));
}
else if(
(3<=nszSize)
&&(_T('"')==szNamePtr[0])
&&(_T('"')==szNamePtr[nszSize-1])
)
{
_tcsncpy(regArgs.szName, &szNamePtr[1], nszSize-2);
}
//2.3 Set regArgs.ValueBuf from szValuePtr
szValuePtr=EmboTrimLeft(szValuePtr);
szValuePtr=EmboTrimRight(szValuePtr);
if(NULL==szValuePtr)
{
fRet=FALSE;
goto Get_RegValue_Return;
}
//
fRet=GetValueBuf(®Args, szValuePtr);
Get_RegValue_Return:
memset(regArgs.szTempValueLine, 0, sizeof(regArgs.szTempValueLine));
return fRet;
}
BOOL ProcessReg(RegValue_ARGS* pregArgs)
{
//do real registry here:
BOOL fRet=Get_RegKey();
//////////////////////////////
regArgs.curStatus=statusBlank;
regArgs.KeyOnlyDone+=1;
fRet=SetRegKey(®Args);
return fRet;
}
BOOL ProcessRegValue(RegValue_ARGS* pregArgs)
{
//do real registry here:
BOOL fRet=Get_RegKey();
fRet=Get_RegValue();
//////////////////////////////
regArgs.curStatus=statusValueDone;
//regArgs.pszTempKeyLine=szTempKeyLine;
//memset(regArgs.szTempKeyLine,NULL,sizeof(TCHAR)*MAX_PATH);
//So the reg value had been done, initial the buff(regArgs.szTempValueLine)
//and ptr(regArgs.pszTempValueLine) again.
regArgs.pszTempValueLine=regArgs.szTempValueLine;
int dddd=sizeof(regArgs.szTempValueLine);
memset(regArgs.szTempValueLine,NULL,sizeof(regArgs.szTempValueLine));
regArgs.ValueDone+=1;
return TRUE;
}
////////////////////////////////////////////////////////////////////////////
//Activate the driver at regArgs.path
void EmboActivateDevice(RegValue_ARGS* pregArgs)
{
TCHAR szDevKey[MAX_PATH]={0};
_tcscpy(szDevKey, regArgs.szPath);
if(HKEY_LOCAL_MACHINE==regArgs.dwRoot)
{
regArgs.hDevice=ActivateDeviceEx(szDevKey,NULL,0,NULL);
}
}
////////////////////////////////////////////////////////////////////////////
//Get ;@EmboCmd: following settings.
#define EmboRegCmd _T("@emboregcmd:") //_T("@EmboRegCmd:")
#define EmboRegOption _T("emboregoption") //_T("@EmboRegOption")
#define EmboRegActivate _T("emboregactivate") //EmboRegActivate
#define EmboRegDeactivate _T("emboregdeactivate") //EmboRegDeactivate
////////////////////////////////////////////////////////////////////////////
void GetEmboCmd(FILE_CONTENT* pcontent)
{
PTCHAR pszLine=content.pRawLinePtr;
PTCHAR pszBreak=NULL;
PTCHAR pszLine2=NULL;
TCHAR* stopstring=NULL;
_tcslwr(pszLine);
//Skip the first ';' find the next ';', and delete the tail.
pszBreak=_tcsstr(&pszLine[1], _T(";"));
if(NULL!=pszBreak)
{
*pszBreak=NULL;
pszLine=EmboTrimLeft(pszLine);
pszLine=EmboTrimRight(pszLine);
if(NULL==pszLine) return;
}
//So there do is EmboRegCmd,
pszBreak=_tcsstr(pszLine, EmboRegCmd);
if(NULL!=pszBreak)
{
pszLine=pszBreak+_tcslen(EmboRegCmd);
pszLine=EmboTrimLeft(pszLine);
pszLine=EmboTrimRight(pszLine);
if(NULL==pszLine) return;
//So there do is EmboRegCmd,
//(1), if EmboRegOption?
pszLine2=pszLine;
pszBreak=_tcsstr(pszLine2, EmboRegOption);
if(NULL!=pszBreak)
{
pszLine2=pszBreak+_tcslen(EmboRegOption);
pszLine2=EmboTrimLeft(pszLine2);
pszLine2=EmboTrimRight(pszLine2);
if((NULL==pszLine2)||(_T('=')!=*pszLine2)) return;
//Skip the '='
regArgs.dwRegOptions=_tcstoul(&pszLine2[1], &stopstring, 0x10);
return;
}
//(2), if EmboRegActivate?
pszLine2=pszLine;
pszBreak=_tcsstr(pszLine2, EmboRegActivate);
if(NULL!=pszBreak)
{
pszLine2=pszBreak+_tcslen(EmboRegActivate);
pszLine2=EmboTrimLeft(pszLine2);
pszLine2=EmboTrimRight(pszLine2);
if((NULL==pszLine2)||(_T('=')!=*pszLine2)) return;
//Skip the '='
regArgs.fActivateDevice=_tcstoul(&pszLine2[1], &stopstring, 0x10);
if(1==regArgs.fActivateDevice)
{
EmboActivateDevice(®Args);
}
return;
}
//(3), if EmboRegDeactivate?
pszLine2=pszLine;
pszBreak=_tcsstr(pszLine2, EmboRegDeactivate);
if(NULL!=pszBreak)
{
pszLine2=pszBreak+_tcslen(EmboRegDeactivate);
pszLine2=EmboTrimLeft(pszLine2);
pszLine2=EmboTrimRight(pszLine2);
if((NULL==pszLine2)||(_T('=')!=*pszLine2)) return;
//Skip the '='
regArgs.fDeactivateDevice=_tcstoul(&pszLine2[1], &stopstring, 0x10);
int hhhhhh=0;
return;
}
}
}
/////////////////////////////////////////////////////////////////////////
//Input is content.pRawLinePtr
//.......................................................................
//(1), for key: content.pRawLinePtr-->regArgs.szTempKeyLine
//by:
// _tcscpy(regArgs.szTempKeyLine, content.pRawLinePtr);
//.......................................................................
//(2), for value: content.pRawLinePtr-->regArgs.szTempValueLine
//by:
// regArgs.pszTempValueLine=regArgs.szTempValueLine;
// _tcscpy(regArgs.pszTempValueLine, content.pRawLinePtr);
// regArgs.pszTempValueLine+=_tcslen(content.pRawLinePtr);
//////////////////////////////////////////////////////////////////////////
int ProcTrueLine(FILE_CONTENT* pcontent)
{
DWORD dwPTLRetVal=statusError;
//Check inputs, it surely been checked before
if(NULL==content.pRawLinePtr)
{
dwPTLRetVal=statusError;
return dwPTLRetVal;
}
//取首字节:
TCHAR tchTemp=*content.pRawLinePtr;
if(NULL==tchTemp)
{
dwPTLRetVal=statusError;
return dwPTLRetVal;
}
PTCHAR pszTemp=NULL;
DWORD dwLineSize=_tcslen(content.pRawLinePtr);
//Case 0: It is wince reg file header "REGEDIT4"
if(0==(_tcsncmp(content.pRawLinePtr, _T("REGEDIT4"), _tcslen(_T("REGEDIT4")))))
{
dwPTLRetVal=statusBlank;
return statusBlank;
}
//Case 1: It is a remark line?
if(_T(';')==tchTemp)
{
dwPTLRetVal=statusBlank;
GetEmboCmd(&content);
return statusBlank;
}
/*
enum RegInputStatus
{
statusError=0,
statusBlank=1,
statusKey=2,
statusValue=4,
statusNeedContinue=8,
statusValueDone=16,
statusBlankWithEmboCmd=32
};
*/
if(_T('[')==tchTemp)
{
//So it is a Key line's first,
//delete the remarks:
pszTemp=_tcspbrk(content.pRawLinePtr, _T(";"));
if(NULL!=pszTemp)
{
*pszTemp=NULL;
}
//find the corresponding ']'.
pszTemp=_tcspbrk(content.pRawLinePtr, _T("]"));
if(NULL!=pszTemp)
{
//if there's Key/Value in regArgs, do it first.
if((statusKey ==regArgs.curStatus)
//||(statusValue ==regArgs.curStatus)
//||(statusNeedContinue ==regArgs.curStatus)
)
{
if(!ProcessReg(®Args))
{
dwPTLRetVal=statusError;
return dwPTLRetVal;
}
}
regArgs.pszTempKeyLine=regArgs.szTempKeyLine;
memset(regArgs.szTempKeyLine,NULL,sizeof(TCHAR)*MAX_PATH);
regArgs.pszTempValueLine=regArgs.szTempValueLine;
memset(regArgs.szTempValueLine,NULL,sizeof(TCHAR)*MAX_PATH);
*(pszTemp+1)=NULL;
_tcscpy(regArgs.szTempKeyLine, content.pRawLinePtr);
regArgs.curStatus=statusKey;
return 2;
}
else
{
return 0;
}
}
else if(_T('"')==tchTemp)
{
//If it's a Value continue line?
if(statusNeedContinue==regArgs.curStatus)
{
goto ItIsAContinueLine;
}
//So it maybe a Value line's first,
//There should be Key already. Or Error.
if(! ((statusKey==regArgs.curStatus)||(statusValueDone==regArgs.curStatus)) )
{
return 0;
}
//cut the ';' tail
pszTemp=_tcspbrk(content.pRawLinePtr, _T(";"));
if(NULL!=pszTemp)
{
*pszTemp=NULL;
}
EmboTrimRight(content.pRawLinePtr);
//if it has continue lines?
if(_T('\\')==content.pRawLinePtr[_tcslen(content.pRawLinePtr)-1])
{
content.pRawLinePtr[_tcslen(content.pRawLinePtr)-1]=NULL;
//I don't know if we should trim the tail? EmboTrimRight(content.pRawLinePtr);
regArgs.curStatus=statusNeedContinue;
}
//if it has continue lines with _T(',')?
else if(_T(',')==content.pRawLinePtr[_tcslen(content.pRawLinePtr)-1])
{
regArgs.curStatus=statusNeedContinue;
}
else
{
regArgs.curStatus=statusValue;//statusNeedContinue
}
_tcscpy(regArgs.pszTempValueLine, content.pRawLinePtr);
regArgs.pszTempValueLine+=_tcslen(content.pRawLinePtr);
//if no continue line following do it
if((statusValue==regArgs.curStatus))
{
if(!ProcessRegValue(®Args))
{
return 0;
}
}
return 3;
}
else if(_T('@')==tchTemp)
{
//So it is a Value line's first,
//There should be Key already. Or Error.
if(! ((statusKey==regArgs.curStatus)||(statusValueDone==regArgs.curStatus)) )
{
return 0;
}
//cut the ';' tail
pszTemp=_tcspbrk(content.pRawLinePtr, _T(";"));
if(NULL!=pszTemp)
{
*pszTemp=NULL;
}
EmboTrimRight(content.pRawLinePtr);
//if it has continue lines?
if(_T('\\')==content.pRawLinePtr[_tcslen(content.pRawLinePtr)-1])
{
content.pRawLinePtr[_tcslen(content.pRawLinePtr)-1]=NULL;
//EmboTrimRight(content.pRawLinePtr);
regArgs.curStatus=statusNeedContinue;
}
//if it has continue lines with _T(',')?
else if(_T(',')==content.pRawLinePtr[_tcslen(content.pRawLinePtr)-1])
{
regArgs.curStatus=statusNeedContinue;
}
else
{
regArgs.curStatus=statusValue;//statusNeedContinue
}
_tcscpy(regArgs.pszTempValueLine, content.pRawLinePtr);
regArgs.pszTempValueLine+=_tcslen(content.pRawLinePtr);
//if no continue line following do it
if((statusValue==regArgs.curStatus))
{
if(!ProcessRegValue(®Args))
{
return 0;
}
}
return 3;
}
else
{
ItIsAContinueLine:
//So it is a Continue line,
//There should be Value/needcontinue already. Or Error.
if(statusNeedContinue!=regArgs.curStatus)
{
return 0;
}
//cut the ';' tail
pszTemp=_tcspbrk(content.pRawLinePtr, _T(";"));
if(NULL!=pszTemp)
{
*pszTemp=NULL;
}
EmboTrimRight(content.pRawLinePtr);
//if it has continue lines with _T('\\')?
if(_T('\\')==content.pRawLinePtr[_tcslen(content.pRawLinePtr)-1])
{
content.pRawLinePtr[_tcslen(content.pRawLinePtr)-1]=NULL;
EmboTrimRight(content.pRawLinePtr);
regArgs.curStatus=statusNeedContinue;
}
//if it has continue lines with _T(',')?
else if(_T(',')==content.pRawLinePtr[_tcslen(content.pRawLinePtr)-1])
{
regArgs.curStatus=statusNeedContinue;
}
else
{
regArgs.curStatus=statusValue;//statusNeedContinue
}
_tcscpy(regArgs.pszTempValueLine, content.pRawLinePtr);
regArgs.pszTempValueLine+=_tcslen(content.pRawLinePtr);
//if no continue line following do it
if((statusValue==regArgs.curStatus))
{
if(!ProcessRegValue(®Args))
{
return 0;
}
}
return 4;
}
}
//int _tmain2 (TCHAR* szAddRegName)
int GetRegFile(TCHAR* szAddRegName)
{
::SYSTEMTIME currentTime;
::GetLocalTime(¤tTime);
g_AppLog.Write("Start on [%d/%d/%d], at [%d:%d:%d]\r\n"
"\r\n\tBuilt on %s at %s\r\n",
currentTime.wYear,
currentTime.wMonth,
currentTime.wDay,
currentTime.wHour,
currentTime.wMinute,
currentTime.wSecond,
__DATE__, __TIME__);
BOOL fMainRetVal=FALSE;
int i=0;
TCHAR *pszFind=NULL;
TCHAR *pszCmdLine=NULL;
TCHAR szFileTemp[MAX_PATH]={0};
/*
//(1)Begin: get arg from command line ////////
pCmdLine=&sCmdLine;
if(TRUE!=sCmdLine.SetArgC(argc))
{
fMainRetVal=FALSE;;
goto MainErrorReturn;
}
if(TRUE!=sCmdLine.SetArgV(argv))
{
fMainRetVal=FALSE;;
goto MainErrorReturn;
}
//(1)End:
//(2)Begin: get regfile from argv
//we should get from /a:\...\....reg or /A:\...\....reg
#define AddRegToken _T("/a:")
for(i=0; i<sCmdLine.nArgCount; i++)
{
pszCmdLine=sCmdLine.szArg[i];
_tcslwr(pszCmdLine);
//if it's AddRegToken _T("/a:")
pszFind=_tcsstr(pszCmdLine, AddRegToken);
if(NULL!=pszFind)
{
_tcscpy(szFileTemp, AddRegToken);
pszCmdLine+=_tcslen(szFileTemp);
pszCmdLine=EmboTrimLeft(pszCmdLine);
pszCmdLine=EmboTrimRight(pszCmdLine);
if(NULL!=pszCmdLine)
{
fFindAddReg=TRUE;
_tcscpy(szAddRegName, pszCmdLine);
break;
}
}
continue;
}
if(TRUE!=fFindAddReg)
{
fMainRetVal=FALSE;
goto MainErrorReturn;
}
//(2)End:
*/
//(3)Begin: Input one true Line
fileAddReg=_tfopen(szAddRegName, _T("r"));
if(NULL==fileAddReg)
{
fReadfile=FALSE;
g_AppLog.Write(_T("Can Not Found This File: %s ! \r\n"), szAddRegName);
fMainRetVal=FALSE;
goto MainErrorReturn;
}
content.lLine=0;
regArgs.curStatus=statusBlank;
do
{
//If the end reach?
fRetValEof=feof(fileAddReg);
if(0!=fRetValEof)
{
//We reach the end already
break;
}
content.lLine+=1;
//Begin to read one line
PTCHAR szPtr=NULL;
memset(content.RawLine, 0, sizeof(_TCHAR)*MAX_PATH);
szPtr=_fgetts(content.RawLine, MAX_PATH, fileAddReg);
//If there is reading error
fRetValErr=ferror(fileAddReg);
if((NULL==szPtr)||(0!=fRetValErr))
{
//Reading error!
fReadfile=FALSE;
break;
}
//make sure raw line size within MAX_PATH
DWORD nLineSize=_tcslen(szPtr);
if((MAX_PATH-1)<=nLineSize)
{
fReadfile=FALSE;
break;
}
//Trim both ends
szPtr=EmboTrimLeft(szPtr);
szPtr=EmboTrimRight(szPtr);
//If it is a blank line, continue.
if((NULL==szPtr)||(NULL==*szPtr))
{
continue;
}
/////////////////
//Begin:处理每个真行
//Set content.pRawLinePtr
content.pRawLinePtr=szPtr;
//Here, we get a non_blank line, data pointed by content.pRawLinePtr
dwRetVal=ProcTrueLine(&content);
if(statusError==dwRetVal)
{
fMainRetVal=FALSE;
break;
}
//End:处理每个真行
/////////////////
//If the end reach?
fRetValEof=feof(fileAddReg);
if(0!=fRetValEof)
{
//We reach the end already
break;
}
}while(1);
if(FALSE==fReadfile)
{
g_AppLog.Write(_T("Read %s Error at Line: %d\r\n"), szAddRegName, content.lLine);
}
if(NULL!=fileAddReg)fclose(fileAddReg);
//(3)End:
MainErrorReturn:
return fMainRetVal;
}
int _tmain (int argc, TCHAR* argv[])
{
TCHAR szFileName[MAX_PATH]={0};
TCHAR szTempargv[MAX_PATH]={0};
if(1<argc)
{
for(int nargc=1;nargc<argc;nargc++)
{
memset(szTempargv, 0, sizeof(szTempargv));
_tcscpy(szTempargv, argv[nargc]);
if(_tcslen(szTempargv)<=4)
continue;
if(0==_tcsnicmp(&szTempargv[_tcslen(szTempargv)-4], _T(".reg"), 4))
{
if(_T(':')==*argv[nargc]) //为了与之前版本兼容
{
_tcscpy(szFileName, argv[nargc]+1);
}
else
{
_tcscpy(szFileName, argv[nargc]);
}
break;
}
}
}
if(NULL==*szFileName)
{
_tcscpy(szFileName, _T("\\Embo.reg"));
}
DWORD dwTest=GetRegFile(szFileName);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -