Example: Creating your own telephone directory

These ILE C programs show how to create a user index for a telephone directory, how to insert entries into the telephone directory, and how to find an entry.

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.

To create a user index, use the following program, $USIDXCRT:


/********************************************************************/
/*  PROGRAM:  $USIDXCRT                                             */
/*                                                                  */
/*  LANGUAGE:  ILE C                                                */
/*                                                                  */
/*  DESCRIPTION:  THIS PROGRAM CREATES A USER INDEX NAMED "TESTIDX" */
/*   IN THE LIBRARY "QGPL".                                         */
/*                                                                  */
/*  APIs USED:  QUSCRTUI                                            */
/*                                                                  */
/********************************************************************/
#include <quscrtui.h>
main()
{
/********************************************************************/
/*    Set up the parameters to be used in the call to 'QUSCRTUI'    */
/********************************************************************/
char idx_name[]= "TESTIDX   QGPL      ";
char ext_atr[]= "TESTER    ";
char entry_lgth_att[] = "F";
int entry_lngth = 50;
char key_insert[] = "1";
int key_lngth = 15;
char imm_update[] = "0";
char optim[] = "0";
char auth[] = "*CHANGE   ";
char desc[] = "Description .....    ";
/********************************************************************/
/*  Call the 'QUSCRTUI' program to create the user index.           */
/********************************************************************/
  QUSCRTUI(idx_name,ext_atr,entry_lgth_att,entry_lngth,key_insert,
           key_lngth,imm_update,optim,auth,desc);
}
To compile the program that creates the user index, specify
CRTBNDC PGM(QGPL/$USIDXCRT) SRCFILE(QGPL/QCSRC)
.

To insert entries into the user index, use the following program $USIDXEX:


/********************************************************************/
/*  PROGRAM:  $USIDXEX                                              */
/*                                                                  */
/*  LANGUAGE:  ILE C                                                */
/*                                                                  */
/*  DESCRIPTION:  THIS PROGRAM USES A USER INDEX TO KEEP TRACK OF   */
/*    NAMES AND PHONE NUMBERS.  THERE ARE TWO OPERATIONS THAT ARE   */
/*    DEMONSTRATED IN THIS EXAMPLE.  THE FIRST IS THE INSERTION OF  */
/*    AN ENTRY INTO THE INDEX, AND SECONDLY THE FINDING OF A GIVEN  */
/*    INDEX ENTRY.                                                  */
/*    THE INDEX IS KEYED ON THE LAST NAME, THEREFORE ENTER AS MUCH  */
/*    OF THE NAME AS YOU KNOW AND THE PROGRAM WILL LIST ALL ENTRIES */
/*    MATCHING YOUR STRING (IN ALPHABETICAL ORDER).                 */
/*                                                                  */
/*  APIs USED:  NONE                                                */
/*                                                                  */
/********************************************************************/
#include <stdio.h>
#include <string.h>
#include <miindex.h>
#include <miptrnam.h>
#include <stdlib.h>
#include <ctype.h>

_SYSPTR index;
_IIX_Opt_List_T  ins_option_list;
_IIX_Opt_List_T  *fnd_option_list;
char Name_And_Num[50];
char In_Name[50];
char Out_Num[5000];
char response[1];
char name[35];
char number[15];
int  Ent_Found,count,start,length_of_entry;


/********************************************************************/
/* Procedure to copy 'cpylngth' elements of 'string2' into the      */
/* new string, 'string1';  starting at position 'strpos'.           */
/********************************************************************/

void strncpyn(string1,string2,strpos,cpylngth)
 char string1[],string2[];
 int strpos,cpylngth;
{
 int x = 0;
 while (x < cpylngth)
  string1[x++]=string2[strpos++];
} /*strncpyn*/


/********************************************************************/
/* Procedure to convert any string into uppercase, where applicable */
/********************************************************************/

void convert_case(string1)
 char string1[];
{
 int x = 0;
 while (x < (strlen(string1))) {
  string1[x] = toupper(string1[x]);
  x++;
 } /*while*/
} /*convert_case*/


main()
{
fnd_option_list = malloc(sizeof(_IIX_Opt_List_T)
                        +99*sizeof(_IIX_Entry_T));

/********************************************************************/
/*  Resolve to the index created in $USIDXCRT.                      */
/********************************************************************/

  index = rslvsp(_Usridx,"TESTIDX","QGPL",_AUTH_ALL);

/********************************************************************/
/*  Set up the insert option list                                   */
/********************************************************************/

  ins_option_list.Rule = _INSERT_REPLACE;
  ins_option_list.Arg_Length = 50;
  ins_option_list.Occ_Count = 1;
  ins_option_list.Entry[0].Entry_Length = 50;
  ins_option_list.Entry[0].Entry_Offset = 0;

/********************************************************************/
/*  Set up the find option list                                     */
/********************************************************************/

  fnd_option_list->Rule = _FIND_EQUALS;
  fnd_option_list->Occ_Count = 100;

/********************************************************************/
/*  Loop until the choice 'Q' is entered at the menu                */
/********************************************************************/

 while (1==1) {
  printf("\n\n***********************\n");
  printf("*   TELEPHONE INDEX   *\n");
  printf("***********************\n");
  printf("* 'A'  Add name & num *\n");
  printf("* 'L'  List a number  *\n");
  printf("* 'Q'  Quit index     *\n");
  printf("***********************\n");
  gets(response);
  if ((strncmp(response,"A",1)==0)||(strncmp(response,"a",1)==0))
   { printf("\nEnter name to add. ex(Last, First)\n");
     gets(name);
     convert_case(name);
     printf("\nEnter number to add. ex(999-9999)\n");
     gets(number);
     strcpy(name,strcat(name,"     "));
     strcpy(Name_And_Num,strcat(name,number));
     printf("\nName and number to add is => %s\n",Name_And_Num);
     insinxen(index,Name_And_Num,Integrated Netfinity Server_option_list);
   } /* if 'a'*/
  if ((strncmp(response,"L",1)==0)||(strncmp(response,"l",1)==0))
   {
     printf("\nEnter name to find. ex(Last, First)\n");
     gets(In_Name);
     convert_case(In_Name);
     fnd_option_list->Arg_Length = strlen(In_Name);
     fndinxen(Out_Num,index,fnd_option_list,In_Name);
     length_of_entry = fnd_option_list->Entry[0].Entry_Length;
     Ent_Found = fnd_option_list->Ret_Count;
     if (Ent_Found == 0)
       printf("\nName not found in index => %s\n",In_Name);
     else {
       if (Ent_Found > 1)  {
        printf("\n%d occurences found,\n",Ent_Found);
        count = 0;
        start = 0;
        while (count++ < Ent_Found) {
         printf("Name and number is => %s\n",Out_Num);
         start = start + length_of_entry;
         strncpyn(Out_Num,Out_Num,start,length_of_entry);
        } /* while */
       }else
        printf("\nName and number is => %s\n",Out_Num);
     }  /*else*/
   }  /*if 'l'*/
  if ((strncmp(response,"Q",1)==0)||(strncmp(response,"q",1)==0))
   { break; }
 }  /*while*/
} /*$USIDXEX*/
To create the ILE C program to insert entries into the user index, specify
CRTBNDC PGM(QGPL/$USIDXEX) SRCFILE(QGPL/QCSRC)
.