home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0090 - 0099 / ibm0090-0099 / ibm0099.tar / ibm0099 / SAMV11-1.ZIP / API / FINDQTH.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-23  |  5.5 KB  |  246 lines

  1. /*
  2.  * findqth.c
  3.  *
  4.  * This program takes a city and state from the command line, searches
  5.  * the sam database for matches, and displays matching records in a
  6.  * 1-line per record format.
  7.  *
  8.  * 
  9.  * The main purpose is to demostrate the API for SAM, and use this
  10.  * as a starting point for fancier extracts.
  11.  * findqth depends on the API to the sam db provided by SAMAPI.EXE.
  12.  *
  13.  * to compile with Turbo-C
  14.  *  tcc findqth.c samlib.c
  15.  *
  16.  * Other C compilers can be used, but a "pack structs" option may be
  17.  * required.
  18.  *
  19.  * Copyright 1991 by Tom Thompson, Athens, AL and by RT Systems
  20.  *                   [n4yos]
  21.  *
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <conio.h>
  28. #include <dos.h>
  29.  
  30. #include "samapi.h"            /* samapi interface spec */
  31.  
  32.     
  33. /*
  34.  * functions in samlib.c
  35.  */
  36.  
  37. int LocateSam(void);
  38. int CallSam(int cmd, void far *cmdbuf, void far *rspbuf);
  39. int SetCountrySam(int country);
  40.  
  41. /*
  42.  * decls for functions contained here
  43.  */
  44.  
  45. void display_info(datarec_t *d);
  46. long getnumrecs(void);
  47. long getdictcode(char *str, int dno);
  48.  
  49.  
  50. int canada = 0;        /* set to 1 if /v */
  51.  
  52. /***********************
  53.  * main
  54.  ***********************
  55.  */
  56.  
  57. main(int argc, char *argv[])
  58. {
  59.     int err, nocity, x;
  60.     long citycode, statecode, numrecs, numfound, rn;
  61.     cmdgetrecs_t sam_in;
  62.     rspdatarec_t sam_out;
  63.  
  64.     if (argc < 3)
  65.     {
  66.         printf("usage: FINDQTH [/v] city state\n");
  67.         printf("  use * for city to match any city\n");
  68.         printf("  /v to select Canada database\n");
  69.         printf("  If city name contains spaces, enclose the name in quotes.  Example\n");
  70.         printf("      FINDQTH \"NEW YORK\" NY\n");
  71.         exit(1);
  72.     }
  73.     if (LocateSam())
  74.     {
  75.         printf("*** SAMAPI not loaded\n");
  76.         exit(1);
  77.     }
  78.  
  79.     x = 1;
  80.     if (!stricmp(argv[1], "/v"))
  81.     {
  82.         if (SetCountrySam(1))        /* 0 is USA, 1 is Canada */
  83.         {
  84.             printf("Canadian Database not installed\n");
  85.             exit(1);
  86.         }
  87.         canada = 1;
  88.         x++;
  89.     }
  90.     if (!strcmp(argv[x], "*"))
  91.         nocity = 1;
  92.     else
  93.     {
  94.         nocity = 0;
  95.         citycode = getdictcode(argv[x], DnoCity);
  96.         if (citycode == -1)
  97.         {
  98.             printf("City %s not in database\n", argv[1]);
  99.             exit(1);
  100.         }
  101.     }
  102.  
  103.     statecode = getdictcode(argv[x+1], DnoState);
  104.     if (statecode == -1)
  105.     {
  106.         printf("State %s not in database\n", argv[x+1]);
  107.         exit(1);
  108.     }
  109.  
  110.     numrecs = getnumrecs();
  111.     numfound = 0;
  112.     printf("Searching %ld records for %s, %s, please wait\n",
  113.                                 numrecs, argv[x], argv[x+1]);
  114.     printf("Esc quits\n");
  115.     for (rn = 0; rn < numrecs; rn ++)
  116.     {
  117.         if (!((int) rn & 0xff))
  118.         {
  119.             if (kbhit() && 27 == getch())
  120.                 exit(1);
  121.         }
  122.         sam_in.packflags = 1+2+4+8+16+32+64;    /* don't unpack */
  123.         sam_in.index = rn;                /* record to get */
  124.         
  125.  
  126.         err = CallSam(SamGetRecordByCall, &sam_in, &sam_out);
  127.         if (err != 0)
  128.         {
  129.             printf("*** Unexpected SAMAPI error %d\n", err);
  130.             exit(1);
  131.         }
  132.         if ( (nocity || citycode == sam_out.d.CityCode)
  133.                     && statecode == sam_out.d.StateCode)
  134.         {
  135.             /* found a match, get again unpacked and display */
  136.             sam_in.packflags = 0;        /* do unpack */
  137.             err = CallSam(SamGetRecordByCall, &sam_in, &sam_out);
  138.             if (err != 0)
  139.             {
  140.                 printf("*** Unexpected SAMAPI error %d\n", err);
  141.                 exit(1);
  142.             }
  143.             display_info(&sam_out.d);
  144.             numfound++;
  145.         }
  146.     }
  147.     printf("%ld matches in %ld records\n", numfound, numrecs);
  148.     return 0;
  149. }
  150.  
  151. /******************
  152.  * display_info
  153.  ******************
  154.  *
  155.  * displays some of info in callsign data record in a 1 line format.
  156.  * Since we searched by city/state these are not displayed.
  157.  * The display format is:
  158.  * call class last   first  m  address_truncated  zip
  159.  * For canada, the format is:
  160.  * call last   first  m  address_truncated  postal code
  161.  *
  162.  * returns: nothing
  163.  */
  164.  
  165. void display_info(datarec_t *d)
  166. {
  167.     /* note, only first 29 characters of 35 character address displayed */
  168.     if (!canada)
  169.         printf("%s %s %-20.20s %-11.11s %s %-29.29s %s\n",
  170.                     d->Call, d->Class,
  171.                     d->LastName, d->FirstName, d->MidInitial,
  172.                     d->Address, d->Zip);
  173.     else
  174.         printf("%s %-20.20s %-11.11s %s %-29.29s %s\n",
  175.                     d->Call, 
  176.                     d->LastName, d->FirstName, d->MidInitial,
  177.                     d->Address, d->Zip);
  178. }
  179.  
  180. /******************
  181.  * getnumrecs
  182.  ******************
  183.  *
  184.  * calls samapi to get number of records in database
  185.  *
  186.  * returns: number of records in sam db
  187.  */
  188.  
  189. long getnumrecs(void)
  190. {
  191.     chdr_t in;            /* api command block */
  192.     rspnumrecs_t out;    /* api response buffer */
  193.  
  194.     CallSam(SamGetNumRecs, &in, &out);
  195.     return out.numrecs;
  196. }
  197.  
  198.  
  199. /******************
  200.  * getdictcode
  201.  ******************
  202.  *
  203.  * This calls sam api to get dictionary code for strings
  204.  * The reason for this is because it is sooo slow to have each
  205.  * data record fully unpacked.  We can retrieve data records
  206.  * with codes (instead of strings) for names, address, city, etc.
  207.  * and compare these codes to what we learned here.
  208.  *
  209.  * input: string and dictionary (field) number (see samapi.c)
  210.  *
  211.  * returns: dictionary code or -1 if string not in dictionary
  212.  */
  213.  
  214. long getdictcode(char *str, int dno)
  215. {
  216.     cmdgetdictcode_t in;    /* api command buffer */
  217.     rspgetdictcode_t out;    /* api response buffer */
  218.     int err;
  219.  
  220.     /*
  221.      * build the command block and call sam api
  222.      */
  223.  
  224.     in.dno = dno;
  225.     in.string[35] = 0;
  226.     strncpy(in.string, str, 35);
  227.  
  228.     err = CallSam(SamGetDictCode, &in, &out);
  229.  
  230.     /*
  231.      * check for unusual error
  232.      */
  233.  
  234.     if (err && err != SerrNotFound)
  235.     {
  236.         printf("Unexpected error %d in getdictcode\n", err);
  237.         exit(1);
  238.     }
  239.  
  240.     /*
  241.      * return code or -1 if not found
  242.      */
  243.  
  244.     return err ? -1 : out.dictcode;
  245. }
  246.