home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a006 / 1.ddi / CSAMP.ZIP / SEARCH.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-11  |  4.9 KB  |  194 lines

  1. /*       Copyright (c) 1990 by Borland International, Inc.        */
  2.  
  3. #include <stdio.h>
  4. #include <pxengine.h>
  5. #include <string.h>
  6. #define SEARCHMAIN
  7. #define CASESENSITIVE 1
  8. #define NETDIR ""
  9. #define NETTYPE NOTONNET
  10.  
  11. /* prototypes for functions */
  12. int pxPartialSearch(TABLEHANDLE tblHandle, char *SearchString,
  13.                     FIELDHANDLE fldHandle, int mode, int Case);
  14. #ifdef SEARCHMAIN
  15. int AddRecords(char *TblName);
  16. void main(void);
  17. #endif
  18.  
  19. /*
  20. **  Function:       pxPartialSearch
  21. **
  22. **  Parameters:
  23. **
  24. **                tblHandle            table to search
  25. **
  26. **                SearchString         string to locate
  27. **
  28. **                fldHandle            field to search
  29. **        
  30. **                mode                 SEARCHFIRST or SEARCHNEXT 
  31. **
  32. **                Case                 CASESENSITIVE or !CASESENSITIVE 
  33. **
  34. **   Returns:
  35. **
  36. **                PXSUCCESS if record found
  37. **
  38. **                PXERR_RECNOTFOUND if record not found
  39. **
  40. */
  41. int pxPartialSearch(TABLEHANDLE tblHandle,char *SearchString, 
  42.                     FIELDHANDLE fldHandle,int mode,int Case) 
  43.   int pxErr; 
  44.   char buf[BUFSIZ]; 
  45.   char buf2[BUFSIZ]; 
  46.   RECORDHANDLE recHandle; 
  47.   LOCKHANDLE lckHandle; 
  48.   int found = 0;
  49.  
  50.   /* Make a copy of the search string, switch to uppercase if case-insensitive
  51.      search */
  52.   strcpy(buf,SearchString);
  53.   if (Case != CASESENSITIVE)
  54.     strupr(buf);
  55.  
  56.   /* Lock the table so no changes can occur while we search */
  57.   if ((pxErr = PXNetTblLock(tblHandle,WL)) != PXSUCCESS)
  58.     return(pxErr);
  59.  
  60.   /* save current record in case we need to return if string not found*/
  61.   if ((pxErr = PXNetRecLock(tblHandle,&lckHandle)) == PXSUCCESS)
  62.   {
  63.     if (mode == SEARCHFIRST)
  64.           pxErr = PXRecFirst(tblHandle);
  65.       else
  66.           pxErr = PXRecNext(tblHandle);
  67.   }
  68.  
  69.   if (pxErr == PXSUCCESS)
  70.     if ((pxErr = PXRecBufOpen(tblHandle,&recHandle)) == PXSUCCESS)
  71.     do
  72.     {
  73.       if ((pxErr =PXRecGet(tblHandle,recHandle)) == PXSUCCESS)
  74.         if ((pxErr = PXGetAlpha(recHandle,fldHandle,BUFSIZ,buf2)) == PXSUCCESS)
  75.     {
  76.             if (Case != CASESENSITIVE)
  77.                 strupr(buf2);
  78.  
  79.             if (strstr(buf2,buf))
  80.             found = 1;
  81.         else
  82.           pxErr = PXRecNext(tblHandle);
  83.         }
  84.       }
  85.       while (!found && pxErr == PXSUCCESS);
  86.  
  87.   if (!found)  /* move to original position */
  88.     PXNetRecGotoLock(tblHandle,lckHandle);
  89.  
  90.   PXNetRecUnlock(tblHandle,lckHandle);
  91.   PXNetTblUnlock(tblHandle,WL);
  92.  
  93.   if (found)
  94.     return(PXSUCCESS);
  95.   else
  96.     return(pxErr == PXERR_ENDOFTABLE ? PXERR_RECNOTFOUND : pxErr);
  97. }
  98.  
  99.  
  100. #ifdef SEARCHMAIN
  101. char *Data[] = {"Paradox","Engine","Leveraging","The",
  102.                 "Power","Of","Paradox!"}; 
  103.  
  104. #define NUMBEROFELEMENTS sizeof(Data)/sizeof(char*)
  105.  
  106. /*
  107. **  Function:     AddRecords
  108. **
  109. **  Parameters:   
  110. **
  111. **              TblName         Name of table to append records to
  112. **
  113. **  Return Value:
  114. **
  115. **              PXSUCCESS if successful
  116. **              Appropriate PXERR_... if unsuccessful
  117. */
  118. int AddRecords(char *TblName)
  119. {
  120.   TABLEHANDLE tblHandle;
  121.   RECORDHANDLE recHandle;
  122.   int pxErr;
  123.   int i;
  124.  
  125.   if ((pxErr = PXTblOpen(TblName,&tblHandle,0,0)) == PXSUCCESS)
  126.     if ((pxErr = PXRecBufOpen(tblHandle,&recHandle)) == PXSUCCESS)
  127.       for (i=0;i<NUMBEROFELEMENTS && pxErr == PXSUCCESS;++i)
  128.         if ((pxErr = PXPutAlpha(recHandle,1,Data[i])) == PXSUCCESS)
  129.           pxErr = PXRecAppend(tblHandle,recHandle);
  130.  
  131.   if (pxErr == PXSUCCESS)
  132.     if ((pxErr = PXRecBufClose(recHandle)) == PXSUCCESS)
  133.       pxErr = PXTblClose(tblHandle);
  134.   return(pxErr);
  135. }
  136.  
  137. char *fields[] = {"Field 1","Field 2"};
  138. char *types[] = {"A20","A20"};
  139. #define TblSize sizeof(fields)/sizeof(char *)
  140. FIELDHANDLE f[] = {1};
  141.  
  142. /*
  143. **  Function:    MakeTable
  144. **
  145. **  Parameters:
  146. **
  147. **               TblName          Name of table to create
  148. **
  149. **  Return Value:
  150. **
  151. **               PXSUCCESS if successful
  152. **               Appropriate PXERR_... if unsuccessful
  153. */
  154. int MakeTable(char *TblName)
  155. {
  156.   int pxErr;
  157.  
  158.   if ((pxErr = PXTblCreate(TblName,TblSize,fields,types)) == PXSUCCESS)
  159.     if ((pxErr = PXKeyAdd(TblName,1,f,PRIMARY)) == PXSUCCESS)
  160.       pxErr = AddRecords(TblName);
  161.  
  162.   return(pxErr);
  163. }
  164.  
  165. void main(void)
  166. {
  167.   TABLEHANDLE tblHandle;
  168.   RECORDHANDLE recHandle;
  169.   char buf[BUFSIZ];
  170.   int pxErr;
  171.  
  172.   if ( (pxErr = PXNetInit(NETDIR,NETTYPE,DEFUSERNAME)) == PXSUCCESS)
  173.     if ( (pxErr = MakeTable("TABLE")) == PXSUCCESS)
  174.       if ((pxErr = PXTblOpen("TABLE",&tblHandle,0,0)) == PXSUCCESS)
  175.         pxErr = PXRecBufOpen(tblHandle,&recHandle);
  176.   if (pxErr)
  177.   {
  178.     printf("Error: %s\n",PXErrMsg(pxErr));
  179.     return;
  180.   }
  181.  
  182.   if ((pxErr = pxPartialSearch(tblHandle,"aging",1,SEARCHFIRST, CASESENSITIVE)) == PXSUCCESS)
  183.   {
  184.     PXRecGet(tblHandle,recHandle);
  185.     PXGetAlpha(recHandle,1,BUFSIZ,buf);
  186.     printf("Found [%s]\n",buf);
  187.   }
  188.   else
  189.     printf("SEARCH: %s\n",PXErrMsg(pxErr));
  190. }
  191. #endif
  192.  
  193.