您好,欢迎访问三七文档
Sampleprogram:groupingofrepetitivefunctionsonUserArchivesinonefunction.QUESTION:HowcanIreducetheCcodewithscriptsforaccessingUserArchive?ANSWER:IfyouprogramscriptsforaccessingUserArchive,thentherearelargevolumesofcodefortheadministrationtasks.ThiscodeisthesameforaccessingallUserArchives.Theexamplehereshowsanoptionforgroupingtheadministrationtaskstogetherinonefunction.Thescriptstructurelookslikethisforstandardprogrammingmethods:1.Generatingthesortingandfilteringconditions2.SettingupaconnectiontotheUserArchiveuaConnectuaQueryArchiveByNameiferror:return3.SettingthesortingandfilteringconditionsuaArchiveSetFilteruaArchiveSetSort4.OpeningthequeryuaArchiveOpen5.ActuallyworkingwiththequeryRead,write,delete,insertandsoon.6.ClearingdowntheconnectiontotheUserArchiveuaArchiveCloseuaReleaseArchiveuaDisconnectIfyouprogrameverythingwithfaultcheckinonefunction,thefunctionbecomestoolargeandunclear.Ifyoutakeacloselookatthelistabove,youwillbeabletoseethatonlypoints1and5changefromcasetocase.AlltheotherpointsremainthesamenomatterwhatstructuretheUserArchivehas.Youcansplittheabovestructureinto3functions.1.Thetask-relatedfunctiontakescareofpoint1.Itgeneratesthefiltersandsortingconditions.2.Ageneralfunctionthatencapsulatesalladministrationactions.ThisfunctionappliesforALLUserArchives.Thefunctionreceiveacallbackfunctionasparameter.Thiscallbackfunctioniscalledeitherforeachentryinthequeryorjustoncefortheentirearchive.3.Acallbackfunction,thisfunctionprocesspoint5.Thisdivisionmeansthatyoudon'thavetoworryabouttheadministrationactions.Youprogramjustonefunctionforquerygenerationandonearchive-relatedfunction,whichprocessesthearchiveentries.InthefollowingexampleweusethefunctionuaUtilityEnumRecords.Functiondescription:BOOLuaUtilityEnumRecords(Constchar*pszArchiveName,constchar*lpszFilter,constchar*lpszSort,BOOL(UserFunc)(UAHARCHIVE*phUA,void*pUserData),void*pUserData)Parameters:pszArchiveName:Pointertoazero-terminatedstring.ThestringcontainsthenameofthearchiveasitappearsintheUserArchiveeditor.lpszFilter:Pointertoazero-terminatedstring.Thestringcontainsthefilterconditionforthearchive.ThepointercanalsobeZERO.lpszSort:Pointertoazero-terminatedstring.Thestringcontainsthesortingconditionforthearchive.ThepointercanalsobeZERO.UserFunc:Nameofacallbackfunction.Thefunctioniscalledforeachentryifyoutransferafilter.Thefunctioniscalledoncefortheentirearchiveifyoudonottransferafilter.Thefunctionmustbeofthefollowingtype:BOOL(UserFunc)(UAHARCHIVE*phUA,void*pUserData).pUserData:Pointertouserdata.Thepointerisforwardedwithoutmodificationtothecallbackfunction.Returnvalue:ThefunctionreturnsthevalueTRUEif:nofaultshaveoccurredduringconnectionsetupandclear-down.thecallbackfunctionalwaysreturnsTRUE.OtherwisethevalueFALSEisreturned.Function:TheuaUtilityEnumRecordsfunctioncarriesoutthefollowingsteps:1.SetsupconnectiontotheUserArchive.2.Setsthefilterandsortingconditionsiftheyaretransferred.3.Opensthequery.4.Ifnofilterhasbeenset,thefunctioncallsthecallbackfunctionwiththehandletothearchive.Ifthefilterexists,thecallbackfunctioniscalledinaloopforeachentryinthequerywiththehandletoanentry.AssoonastheCBfunctionreturnsthevalueFALSE,theloopisterminated.ThefunctionuaUtilityEnumRecordsthenreturnstheFALSE.5.Afterthecallbackfunctionhasbeenprocessed,theconnectiontothearchiveissetup.YouseethecodeofthefunctioninthefollowingPDFdocument:uaUtilityEnumRecords_1.pdf(7KB)代码:#includeapdefap.hBOOLuaUtilityEnumRecords(constchar*pszArchiveName,constchar*lpszFilter,constchar*lpszSort,BOOL(UserFunc)(UAHARCHIVE*phUA,void*pUserData),void*pUserData){/*FunctioncreateaQueryforaUserArchivespecifiedbyFilterandsortcondition.FunctioncallsforeachRecordinQueryUserfunctionifFilterspecified.Ifnotfilterspezifiedfunctioncallsuserfunctiononlyonetime.ParameterhArchive:HandletoaUserArchivelpszFilter:PointertoZerroTerminatedstringwithFilterforUserArchive.IfPointeris'ntNULLfunctionmakenewQueryfromArchive,andcallsforEACHrecordUserFunction!IfPointerisNULL:FunctioncreateconectiontoUserArchivewhithoutfilter.InthisCaseuserfunctioniscalledonlyONETIME!!lpszSort:pointertozeroterminatedstringwithsortconditionlpFunc:PointertoUserFunction.pUserData:PointertoParameterStructforUserFunction.*/BOOLreturnCode=TRUE;constchar*funcName=uaUtilityEnumRecords;UAHCONNECThConnect;BOOLreturnCode=TRUE;//GetConnectiontoUserArchivesreturnCode=uaConnect(&hConnect);if(!returnCode||!hConnect){returnCode=FALSE;printf(%sfaultinuaConnect\r\n,funcName);}else{//GetHandletoActualArchive.UAHARCHIVEhArchive;if(!uaQueryArchiveByName(hConnect,pszArchiveName,&hArchive)){printf(%s:uaQueryArchiveError:%d\r\n,funcName,uaGetLastError());returnCode=FALSE;}else{if(lpszFilter)returnCode=uaArchiveSetFilter(hArchive,lpszFilter);if(lpszSort)returnCode=(returnCode&&uaArchiveSetSort(hArchive,lpszSort));if(!returnCode){printf(%s:uaArchiveSetFilter\r\n,funcName);}else{//OpenDatareturnedbyQueryif(uaArchiveOpen(hArchive)){//IfFilterspecified:GotoFirstRecordinQueryif(!lpszFilter)//ifnoneFilterspecified:calluserfunkctiononeTimeonly{if(!(returnCode=UserFunc(&hArchive,pUse
本文标题:grouping of repetitive functions on User Archives
链接地址:https://www.777doc.com/doc-5332862 .html