资源预览内容
第1页 / 共9页
第2页 / 共9页
第3页 / 共9页
第4页 / 共9页
第5页 / 共9页
第6页 / 共9页
第7页 / 共9页
第8页 / 共9页
第9页 / 共9页
亲,该文档总共9页全部预览完了,如果喜欢就下载吧!
资源描述
Sample program: grouping of repetitive functions on User Archives in one function.QUESTION:How can I reduce the C code with scripts for accessing User Archive? ANSWER:If you program scripts for accessing User Archive, then there are large volumes of code for the administration tasks. This code is the same for accessing all User Archives. The example here shows an option for grouping the administration tasks together in one function. The script structure looks like this for standard programming methods: 1. Generating the sorting and filtering conditions 2. Setting up a connection to the User ArchiveuaConnectuaQueryArchiveByNameif error: return 3. Setting the sorting and filtering conditionsuaArchiveSetFilteruaArchiveSetSort 4. Opening the queryuaArchiveOpen 5. Actually working with the queryRead, write, delete, insert and so on. 6. Clearing down the connection to the User ArchiveuaArchiveCloseuaReleaseArchiveuaDisconnect If you program everything with fault check in one function, the function becomes too large and unclear.If you take a close look at the list above, you will be able to see that only points 1 and 5 change from case to case.All the other points remain the same no matter what structure the User Archive has. You can split the above structure into 3 functions. 1. The task-related function takes care of point 1. It generates the filters and sorting conditions. 2. A general function that encapsulates all administration actions. This function applies for ALL User Archives. The function receive a callback function as parameter. This callback function is called either for each entry in the query or just once for the entire archive. 3. A callback function, this function process point 5. This division means that you dont have to worry about the administration actions. You program just one function for query generation and one archive-related function, which processes the archive entries. In the following example we use the function uaUtilityEnumRecords. Function description: BOOL uaUtilityEnumRecords( Const char* pszArchiveName ,const char* lpszFilter,const char* lpszSort,BOOL ( UserFunc )( UAHARCHIVE* phUA, void* pUserData ),void* pUserData ) Parameters: pszArchiveName: Pointer to a zero-terminated string. The string contains the name of the archive as it appears in the User Archive editor. lpszFilter: Pointer to a zero-terminated string. The string contains the filter condition for the archive. The pointer can also be ZERO. lpszSort: Pointer to a zero-terminated string. The string contains the sorting condition for the archive. The pointer can also be ZERO. UserFunc: Name of a callback function. The function is called for each entry if you transfer a filter. The function is called once for the entire archive if you do not transfer a filter. The function must be of the following type:BOOL ( UserFunc )( UAHARCHIVE* phUA, void* pUserData ). pUserData: Pointer to user data. The pointer is forwarded without modification to the callback function. Return value: The function returns the value TRUE if: no faults have occurred during connection setup and clear-down. the callback function always returns TRUE. Otherwise the value FALSE is returned. Function:The uaUtilityEnumRecords function carries out the following steps: 1. Sets up connection to the User Archive. 2. Sets the filter and sorting conditions if they are transferred. 3. Opens the query. 4. If no filter has been set, the function calls the callback function with the handle to the archive.If the filter exists, the callback function is called in a loop for each entry in the query with the handle to an entry. As soon as the CB function returns the value FALSE, the loop is terminated. The function uaUtilityEnumRecords then returns the FALSE. 5. After the callback function has been processed, the connection to the archive is set up. You see the code of the function in the following PDF document:uaUtilityEnumRecords_1.pdf ( 7 KB ) 代码:#include apdefap.hBOOL uaUtilityEnumRecords( const char* pszArchiveName , const char* lpszFilter, const char* lpszSort,BOOL ( UserFunc ) ( UAHARCHIVE* phUA, void* pUserData ),void* pUserData )/*Function create a Query for a User Archive specified by Filter and sort condition. Function calls for eachRecord in Query User function if Filter specified. If not filter spezified function calls user function only onetime.ParameterhArchive: Handle to a User ArchivelpszFilter: Pointer to Zerro Terminated string with Filter for User Archive. If Pointer isnt NULL functionmake new Query from Archive, and calls for EACH record User Function !If Pointer is NULL: Function create conection to User Archive whithout filter.In this Case user function is called only ONE TIME !lpszSort: pointer to zeroterminated string with sort conditionlpFunc: Pointer to Us
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号