home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 337_01 / in_demo.c < prev    next >
C/C++ Source or Header  |  1991-01-14  |  5KB  |  188 lines

  1. /* Copyright (c) James L. Pinson 1990,1991  */
  2.  
  3. /**********************   IN-DEMO.C   ***************************/
  4.  
  5. #include "mydef.h"
  6. #include <stdio.h>
  7. #include <ctype.h>
  8.  
  9. #define NUMBER_FIELDS 12  /* the maximum number of fields */
  10.  
  11. /* for memory allocation */
  12. #if defined QUICKC
  13.  
  14. #include <malloc.h>
  15. #include <memory.h>
  16.  
  17. #endif
  18.  
  19. #if defined TURBOC
  20.  
  21. #include <alloc.h>
  22. #include <mem.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <dir.h>
  26.  
  27. #endif
  28.  
  29. /* function prototypes */
  30.  
  31. void help();
  32. int val_field( char *string,int length,int field_number);
  33.  
  34. int start(void)
  35. {
  36. extern struct screen_structure scr;
  37. extern struct window_structure w[];
  38.  
  39. int i;
  40. int helpwin, mainwin;  /* window handles */
  41.  
  42. /* make a help screen */
  43.  
  44. helpwin= win_make(1,20,78,5,STD_FRAME,"",scr.normal,scr.normal);
  45.  
  46. help();  /* display help */
  47.  
  48. /* make the main window */
  49. mainwin=win_make (1,1,78,17,TOP_FRAME,"",scr.normal,scr.normal);
  50.  
  51. demo();  /* do input demo */
  52.  
  53. /* remove windows */
  54.  
  55. win_delete(helpwin);
  56. win_delete(mainwin);
  57.  
  58. return(0);
  59. }
  60.  
  61.  
  62. demo()
  63. {
  64. extern struct screen_structure scr;
  65. extern struct window_structure w[];
  66.  
  67. int field_attr;
  68. int ret_code;
  69. char ch,ext;
  70.      /* make in_scrn large enough to include zero terminator */
  71. struct in_struc in_scrn[NUMBER_FIELDS+1]= {
  72. /*
  73. X   Y   Label name      Ptr   Length   Label-color   Field color */
  74.  
  75. 1,  1,  "First Name:"     ,NULL  ,10,  YELLOW,BLACK, BLACK,WHITE,
  76. 1,  2,  "Last Name :"     ,NULL  ,15,  YELLOW,BLACK, BLACK,WHITE,
  77. 40, 2,  "Middle init:"    ,NULL  ,1,   YELLOW,BLACK, BLACK,WHITE,
  78. 1,  4,  "Age:"            ,NULL  ,3,   CYAN,BLACK,   BLACK,CYAN,
  79. 12, 4,  "Sex:"            ,NULL  ,1,   CYAN,BLACK,   BLACK,CYAN,
  80. 25, 4,  "Date of Birth:"  ,NULL  ,10,  CYAN,BLACK,   BLACK,WHITE,
  81. 1,  7,  "Street address:" ,NULL  ,30,  GREEN,BLACK,  BLACK,GREEN,
  82. 1,  8,  "City :"          ,NULL  ,20,  GREEN,BLACK,  BLACK,GREEN,
  83. 1,  9,  "State:"          ,NULL  ,2,   GREEN,BLACK,  BLACK,GREEN,
  84. 12, 9,  "Zip:"            ,NULL  ,11,  GREEN,BLACK,  BLACK,GREEN,
  85. 1,  11, "Current Title:"  ,NULL  ,40,  WHITE,BLACK,  WHITE,RED,
  86. 1,  13, "Comments:"       ,NULL  ,160, WHITE,BLACK,  WHITE,RED,
  87. 0   /* terminator */
  88. };
  89. int i;
  90.  
  91. /* We must allocate enough memory for each field, allowing for the
  92.    '\0' terminator. */
  93.  
  94.    for (i= 0 ;i<NUMBER_FIELDS;i++){
  95.     in_scrn[i].ptr= malloc((in_scrn[i].length+1)*sizeof(char));
  96.     if(in_scrn[i].ptr==NULL) exit(1);
  97.     in_scrn[i].ptr[0]='\0';
  98.    }
  99.  
  100. /* call input function, pass it the input structure */
  101.  
  102. ret_code=input(in_scrn);
  103.  
  104. cls();
  105.  
  106. /* show that we really got data */
  107. if (ret_code==ESCAPE) print(1,1,"Escape pressed, no data entered");
  108.  else {
  109.   print (1,1, "The following data were returned from input():");
  110.   gotoxy(1,3);
  111.     for(i=0;i<NUMBER_FIELDS;i++) {
  112.       print(1,i+2,in_scrn[i].prompt);
  113.       print_here(in_scrn[i].ptr);
  114.     }
  115.    print (1,i+4, "Touch any key to quit");
  116.  }
  117.  
  118.     for(i=0;i<NUMBER_FIELDS;i++)
  119.      /* free memory if it has been allocated*/
  120.      if (in_scrn[i].ptr!=NULL) 
  121.          free(in_scrn[i].ptr); 
  122.  
  123. get_key(&ch,&ext);
  124. return(0);
  125. }
  126.  
  127.  
  128. void help()
  129. {
  130. int x=1, y=1;
  131.  
  132. print(x,y++,"\x19             Next Field          \x18\
  133.              Previous field");
  134. print(x,y++,"\x1b             Cursor left         \x1a\
  135.              Cursor right (text)");
  136. print(x,y++,"<Home>        beginning of field  <End>\
  137.          End of field (text)");
  138. print(x,y++,"<insert>      Toggle Insert       <Backspace>\
  139.    Backspace(destructive)");
  140. print(x,y++,"<Enter>       Next field          <Escape>   \
  141.    Exit (Abandon record)");
  142.  
  143. }
  144.  
  145. /* This function evaluates the data input and does the following
  146.    for each field.
  147.  
  148.    Field:         Action
  149.  
  150.   "First Name"   Trims leading spaces,capitalizes first letter.
  151.   "Last Name"    "     "       "      "   "           "     "
  152.   "Middle Init"  Capitalizes
  153.   "Age"          If age is <1 or >110 a re-edit is requested
  154.   "Sex"          If sex equals 'm' or 'f' then it is capitalized
  155.                  if it is any other letter, then re-edit.   */
  156.  
  157.  
  158. int val_field( char *string,int length,int field_number)
  159.  
  160. {
  161.  /* Note: In this demo "length" is not used. */
  162.  int age;
  163.  
  164.  switch (field_number){
  165.   /* trim leading spaces off field zero */
  166.   case 0: trim_left(string);
  167.           /* make first letter uppercase */
  168.           string[0]=toupper(string[0]); 
  169.           return(REDRAW);
  170.           /* trim leading spaces off field one */
  171.   case 1: trim_left(string);             
  172.           /* make first letter uppercase */
  173.           string[0]=toupper(string[0]);  
  174.           return (REDRAW);
  175.   case 2: string[0]=toupper(string[0]);
  176.           return(REDRAW);
  177.  
  178.   case 3: age=atoi(string);    /* convert string to integer */
  179.           if (age<0 || age>110) return(REDO);
  180.           break;
  181.   case 4: if (string[0]=='m'){string[0]='M';return(REDRAW);}
  182.           if (string[0]=='f'){ string[0]='F'; return(REDRAW);}
  183.           if(string[0]!='M' && string[0]!='F')return (REDO);
  184.           break;
  185.  }
  186. return (OK);
  187. }
  188.