home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_10 / 9n10093a < prev    next >
Text File  |  1979-12-31  |  3KB  |  126 lines

  1. /* 
  2.     listing 6 - search.c (sample program)
  3. */
  4.  
  5. #include <stdio.h>
  6. #include "common.h" /* shared by all files */
  7.  
  8. /* 
  9.    Initialization of the tran_record file. This
  10.    would normally be done through file input. 
  11. */
  12.  
  13. struct record tran_record[] = {
  14.     0,  0,  0.0, "zero",
  15.     1, 11, 11.1, "one",
  16.     2, 22, 22.2, "two",    
  17.     3, 33, 33.3, "three",
  18.     4, 44, 44.4, "four",    
  19. };
  20.  
  21. struct field_definition definitions[] = {
  22.   "id",     INT,    &tran_record[0].id,
  23.   "number", INT,    &tran_record[0].number,
  24.   "price",  FLOAT,  &tran_record[0].price,
  25.   "code",   STRING, &tran_record[0].code,
  26.   "last",   NULL,   NULL,
  27. };
  28.  
  29. main()
  30. {
  31.  
  32.   /* loop control */
  33.   
  34.   int i;                
  35.  
  36.   /* holds definition type */    
  37.  
  38.   char user_keyword[10];
  39.  
  40.   /* ascii string search key */
  41.  
  42.   char user_value[100];        
  43.  
  44.   /* transaction_size of the transaction array */
  45.  
  46.   int transaction_size;        
  47.  
  48.     
  49.   /* calculate size of a transaction */
  50.  
  51.   transaction_size = 
  52.     sizeof(tran_record)/sizeof(tran_record[0]);
  53.  
  54.   /* set up the test to search for integer id */
  55.  
  56.   strcpy (user_value, "0");
  57.   strcpy (user_keyword, "id");
  58.   printf ("\nuser_value = %s\n", user_value);
  59.   printf ("user_keyword = %s\n", user_keyword);
  60.   if ( (type_check(definitions, tran_record, 
  61.     transaction_size, 
  62.     user_keyword, user_value) != SUCCEED) )
  63.     printf ("Error: search failed\n");
  64.  
  65.   /* set up the test to search for integer number */
  66.  
  67.   strcpy (user_value, "11");
  68.   strcpy (user_keyword, "number");
  69.   printf ("\nuser_value = %s\n", user_value);
  70.   printf ("user_keyword = %s\n", user_keyword);
  71.   if ( (type_check(definitions, tran_record, 
  72.     transaction_size, 
  73.     user_keyword, user_value) != SUCCEED) )
  74.     printf ("Error: search failed\n");
  75.  
  76.   /* set up the test to search for float */
  77.  
  78.   strcpy (user_value, "33.3");
  79.   strcpy (user_keyword, "price");
  80.   printf ("\nuser_value = %s\n", user_value);
  81.   printf ("user_keyword = %s\n", user_keyword);
  82.   if ( (type_check(definitions, tran_record, 
  83.     transaction_size, 
  84.     user_keyword, user_value) != SUCCEED) )
  85.     printf ("Error: search failed\n");
  86.  
  87.   /* set up the test to search for string */
  88.  
  89.   strcpy (user_value,"two");
  90.   strcpy (user_keyword, "code");
  91.   printf ("\nuser_value = %s\n", user_value);
  92.   printf ("user_keyword = %s\n", user_keyword);
  93.   if ( (type_check(definitions, tran_record, 
  94.     transaction_size, 
  95.     user_keyword, user_value) != SUCCEED) )
  96.     printf ("Error: search failed\n");
  97.  
  98.   /* set up the test to return 'No match' */
  99.  
  100.   strcpy (user_value, "99");
  101.   strcpy (user_keyword, "number");
  102.   printf ("\nuser_value = %s\n", user_value);
  103.   printf ("user_keyword = %s\n", user_keyword);
  104.   if ( (type_check(definitions, tran_record, 
  105.     transaction_size, 
  106.     user_keyword, user_value) != SUCCEED) )
  107.     printf ("Error: search failed\n");
  108.  
  109.   /* 
  110.      Generate invalid search, 
  111.      CHAR is not define as a keyword 
  112.   */
  113.  
  114.   strcpy (user_value, "99");
  115.   strcpy (user_keyword, "char");
  116.   printf ("\nuser_value = %s\n", user_value);
  117.   printf ("user_keyword = %s\n", user_keyword);
  118.   if ( (type_check(definitions, tran_record, 
  119.     transaction_size, 
  120.      user_keyword, user_value) != SUCCEED) )
  121.     printf ("Error: search failed\n");
  122.  
  123.   exit(0);
  124.  
  125. }
  126.