home *** CD-ROM | disk | FTP | other *** search
/ Games 1995 to 2000 / Games.iso / SexTetris / PROFESS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-16  |  17.7 KB  |  515 lines

  1. #include "microlib.h"
  2.  
  3. typedef struct {
  4.     char deleted;
  5.     char code[7];
  6.     char descr[40];
  7.     } REC;
  8.  
  9. static int display_fields(REC *);
  10. static int prepare_fields(REC *);
  11. static int delete(D4DATA *, X4FILTER);
  12. static int edit(D4DATA *, REC *);
  13. static int append(D4DATA *, REC *cr);
  14.  
  15. int professions()
  16. {
  17.     REC *cr;
  18.     int topics_wref;        /* Window Reference. */
  19.  
  20.     int count, i;           /* Counters. */
  21.  
  22.     int seek_val;           /* Returned Values. */
  23.     int read_val;
  24.     int key_val;
  25.  
  26.     int code_len;           /* Length of frequently used fields ... */
  27.     int descr_len;
  28.  
  29.     char tmp_code[sizeof(cr->code) + 1];       /* Temp storage of CODE field entry (with NULL) */
  30.     char tmp_descr[sizeof(cr->descr) + 1];     /* Temp storage of DESCR (with NULL) */
  31.  
  32.     F4FIELD *fields[3];         /* filtering... */
  33.     F4FIELD *field_ptr;
  34.  
  35.     D4DATA *db_positions;
  36.  
  37.     X4FILTER filter;
  38.     FILTER_INFO_BASIC info;
  39.  
  40.     /* Allocate memory for my record buffer                     */
  41.     if ((cr = (REC *) malloc(sizeof(REC))) == NULL ) {
  42.         w4display(mes[MT], mes[ME+1], (char *) 0);
  43.         w4exit(1);
  44.     }
  45.  
  46.     code_len = sizeof(cr->code);
  47.     descr_len = sizeof(cr->descr);
  48.  
  49.     /* Define, activate and clear Data Entry screen.    */
  50.     topics_wref = w4define(7, 5, 12, 66);
  51.     w4popup();
  52.     w4memory();
  53.     w4border(SINGLE, custom[MAIN_N]);
  54.     w4title(0, 1, mes[MT_PROFESSIONS], custom[MAIN_N]);
  55.     w4attribute(custom[MAIN_N]);
  56.     w4activate(topics_wref);
  57.     w4clear(0);
  58.  
  59.  
  60.     /* Load screen from disk and get field coordinates */
  61.     if (screen_file(screens(SCR_PROFESSIONS), 19, 78))
  62.         w4exit(1);
  63.     if (get_input_positions(screens(SCR_PROFESSIONS)))
  64.         w4exit(1);
  65.  
  66.     /* Open Databases and select the proper Tags... */
  67.     db_positions = d4open(&set, files(DBF_PROFESSIONS));
  68.     d4tag_select(db_positions, d4tag(db_positions, "CODE_T"));
  69.  
  70.     g4attribute (custom[MAIN_I]);       /* Set input attribute.     */
  71.  
  72.     /* Main append, scan, edit and delete loop.         */
  73.     for(;;) {                                       /* ==L1==   */
  74.  
  75.         /* Blank my record buffer and display it.                   */
  76.         memset(cr, (int) ' ', sizeof(REC));
  77.  
  78.         display_fields(cr);
  79.  
  80.         statline(mes[MS]);
  81.         helpline(mes[MH]);
  82.         g4delimiter("[]");
  83.  
  84.         /* Get code or any other user input.                    */
  85.         read_val =0;
  86.         while (read_val != ESC && read_val != F5 && read_val != RETURN\
  87.                                && read_val != TAB && read_val != F1) {
  88.  
  89.             g4(srow[1], scol[1], cr->code);
  90.             g4picture("W999999");
  91.             read_val = g4read();
  92.         }
  93.  
  94.         /* If Function Keys             */
  95.         if (read_val == ESC)
  96.             break;  /* Out of ==L1==    */
  97.  
  98.         if (read_val == F1) {
  99.             help("CASE_QUERY");
  100.             continue;
  101.         }
  102.  
  103.         if (read_val == F5) {
  104.             append(db_positions, cr);
  105.             continue;
  106.         }
  107.  
  108.         /* If ENTER was pressed and CODE field is blank...  */
  109.         if (!memcmp(cr->code + 1, space(code_len - 1), code_len - 1)) {
  110.             statline(mes[MS+7]);
  111.             helpline(mes[MH+22]);
  112.             read_val = 0;
  113.             while (read_val != RETURN && read_val != ESC && read_val != TAB ) {
  114.                 g4(srow[2], scol[2], cr->descr); g4width(descr_len, descr_len);
  115.                 read_val = g4read();
  116.                 if (read_val == F1)
  117.                     help("CASE_QUERY");
  118.             }
  119.  
  120.             if (!memcmp(cr->descr, space(descr_len), descr_len))
  121.                 continue;
  122.  
  123.             memcpy(tmp_descr, cr->descr, descr_len);
  124.             tmp_descr[descr_len] = '\0';
  125.  
  126.             d4tag_select(db_positions, d4tag(db_positions, "NAME"));
  127.             seek_val = d4seek(db_positions, tmp_descr);
  128.             d4tag_select(db_positions, d4tag(db_positions, "CODE_T"));
  129.             d4unlock_index(db_positions);
  130.             if (seek_val == r4eof || seek_val == r4after) {
  131.                 w4display("", mes[ME+2], (char *) 0);
  132.                 continue; /* ==L1== */
  133.             }
  134.         }
  135.         else {      /* If CODE field is not empty...        */
  136.             // Zerofill and show the entered CODE.
  137.             zerofill(cr->code + 1, code_len - 1);
  138.             w4attribute(custom[MAIN_I]);
  139.             w4num(srow[1], scol[1], cr->code, code_len);
  140.             w4attribute(custom[MAIN_N]);
  141.  
  142.             /* Copy code and add NULL. */
  143.             memcpy(tmp_code, cr->code, code_len);
  144.             tmp_code[code_len] = '\0';
  145.  
  146.             seek_val = d4seek(db_positions, (char *)&tmp_code);
  147.             d4unlock_index(db_positions);
  148.             if (seek_val == r4eof || seek_val == r4after) {
  149.                 w4display("", mes[ME+2], (char *) 0);
  150.                 continue; // ==L1==
  151.             }
  152.         }
  153.  
  154.         // If CODE or SURNAMEKEY was found display the record and let the
  155.         // user to skip through the records, edit, delete, change to
  156.         // the second screen or ask for help.
  157.  
  158.         memcpy(cr, (REC *) d4record(db_positions), sizeof(REC));
  159.  
  160.         helpline(mes[MH+33]);   // Show new available keys.
  161.         statline(mes[MS+8]);
  162.  
  163.         // Clear delimiters from CODE and SURNAME field
  164.         w4num(srow[1], scol[1] -1, space(code_len +2), code_len +2);
  165.         w4num(srow[2], scol[2] -1, space(descr_len +2), descr_len +2);
  166.  
  167.         display_fields(cr);
  168.  
  169.         for (;;) {   /* ==L2== */
  170.             key_val = g4char();
  171.             if (key_val != PGUP && key_val != PGDN && key_val != ESC &&
  172.                 key_val != F1 && key_val != RETURN &&
  173.                 key_val != F2 && key_val != F3 && key_val != F6 &&
  174.                 key_val != F7 && key_val != F8 && key_val != F4)
  175.                 continue;
  176.  
  177.             if (key_val == PGUP) {
  178.                 if (d4skip(db_positions, -1L) == r4bof)
  179.                     d4bottom(db_positions);
  180.             }
  181.  
  182.             if (key_val == PGDN) {
  183.                 if (d4skip(db_positions, 1L) == r4eof)
  184.                     d4top(db_positions);
  185.             }
  186.  
  187.             if (key_val == F1) {
  188.                 help("CASE_ROLL");
  189.                 continue;
  190.             }
  191.  
  192.             if (key_val == ESC)
  193.                 break; /* ==L2 ==*/
  194.  
  195.             if (key_val == F2) {
  196.                 edit(db_positions, cr);
  197.                 display_fields(cr);
  198.             }
  199.  
  200.             if (key_val == RETURN) {
  201.                 display_fields(cr);
  202.             }
  203.  
  204.             if (key_val == F6) {
  205.                 helpline(mes[MH+33]);   /* Show new available keys. */
  206.                 statline(mes[MS+8]);
  207.                 key_val = RETURN;
  208.             }
  209.  
  210.             if (key_val == F7) {
  211.                 helpline(mes[MH+33]);   /* Show new available keys. */
  212.                 statline(mes[MS+8]);
  213.                 continue;
  214.             }
  215.  
  216.             if (key_val == F4) {
  217.                 continue;
  218.             }
  219.  
  220.             if (key_val == F8) {
  221.                 delete(db_positions, filter);
  222.             }
  223.  
  224.             memcpy(cr, (REC *) d4record(db_positions), sizeof(REC));
  225.             if (key_val != RETURN && key_val != F2) {
  226.                 display_fields(cr);
  227.             }
  228.         }   /* ==L2==  */
  229.  
  230.     }       /* ==L1==   */
  231.  
  232.     statline(mes[MS+2]);
  233.     helpline(mes[MH+3]);
  234.     d4close_all(&set);
  235.  
  236.     free(cr);
  237.     w4deactivate(topics_wref);
  238.     w4close(topics_wref);
  239.     /* Return the menu item to be selected. */
  240.     return (2);
  241. }
  242.  
  243. /************************************************************************/
  244. /* Display Fields                                                       */
  245. /************************************************************************/
  246. static int display_fields(cr)
  247. REC *cr;
  248. {
  249.     w4attribute(custom[MAIN_I]);
  250.     w4num(srow[1],  scol[1],  cr->code,       sizeof(cr->code));
  251.     w4num(srow[2],  scol[2],  cr->descr,     sizeof(cr->descr));
  252.  
  253.     w4attribute(custom[MAIN_N]);
  254.  
  255.     return (1);
  256. }
  257.  
  258. /************************************************************************/
  259. /* Prepare Gets.                                                        */
  260. /************************************************************************/
  261. static int prepare_fields(cr)
  262. REC *cr;
  263. {
  264.     w4attribute(custom[MAIN_I]);
  265.     w4num(srow[1], scol[1], cr->code, sizeof(cr->code));
  266.     w4attribute(custom[MAIN_N]);
  267.  
  268.     g4(srow[2],  scol[2],  cr->descr); g4width(40,40);
  269.  
  270.     return (1);
  271. }
  272.  
  273. /************************************************************************/
  274. /* Append a record                                                      */
  275. /************************************************************************/
  276. static int append(dbfile, cr)
  277. D4DATA *dbfile;
  278. REC *cr;
  279. {
  280.     REC *rec_buffer;
  281.     D4DATA *dbcodes;                /* File to get last code.                */
  282.     int count;                      /* Counter.                              */
  283.     int read_val;                   /* Returned value.                       */
  284.  
  285.     char old_code[7];
  286.     char tmp_code[7];               /* Temporary storage of CODE field       */
  287.                                     /* because when I clear my buffer I lose */
  288.                                     /* the next available code.              */
  289.     int code_len;                   /* Length of frequently used fields ...  */
  290.     int descr_len;
  291.     int someone_used_the_code;
  292.  
  293.     someone_used_the_code = FALSE;
  294.     code_len = sizeof(cr->code);
  295.  
  296.     d4tag_select(dbfile , d4tag(dbfile, "CODE_T"));
  297.  
  298.     /* Main Append Loop (where next code number is given etc.). */
  299.     for (;;) {   /* ==L1== */
  300.         /* Read the last code used from CODES file.         */
  301.         dbcodes = d4open(&set, files(DBF_CODES));
  302.         d4tag_select(dbcodes, d4tag(dbcodes, "CODE_T"));
  303.         d4lock_file(dbcodes);
  304.         d4lock_index(dbcodes);
  305.         if (d4seek(dbcodes, "W") != 0 ) {
  306.             d4close_all(&set);
  307.             w4display(mes[MT+1], mes[ME+9], (char *) 0);
  308.             w4exit(1);
  309.         }
  310.         memcpy(cr->code, f4str(d4field(dbcodes, "CODE")), code_len);
  311.         memcpy(old_code, cr->code, code_len);
  312.         old_code[7]='\0';
  313.         d4close(dbcodes);
  314.  
  315.         /*  The character field is converted to numeric ,       */
  316.         /* the value is increased by 1 and then the result      */
  317.         /* is converted again to character.                         */
  318.         /* At the numeric-->character conversion I use a        */
  319.         /* negative length in order to "zerofill" the result. */
  320.         c4ltoa(c4atol(cr->code+1, code_len-1) + 1, cr->code+1, -6);
  321.  
  322.         /* Save the next available code.    */
  323.         memcpy(tmp_code, cr->code, code_len);
  324.         tmp_code[7]='\0';
  325.  
  326.         /* Show next available code, clear buffer, restore the next */
  327.         /* available code. Show the screen file and prepare gets.   */
  328.  
  329.         w4attribute(custom[MAIN_I]);
  330.         w4num(srow[1], scol[1], cr->code, code_len);
  331.         w4attribute(custom[MAIN_N]);
  332.         memset(cr, (int) ' ', sizeof(REC));
  333.         memcpy(cr->code, tmp_code, code_len);
  334.  
  335.         if (get_input_positions(screens(SCR_PROFESSIONS)))
  336.             w4exit(1);
  337.         prepare_fields(cr);
  338.  
  339.         /* Entry Loop   */
  340.         for (;;) {
  341.             helpline(mes[MH+4]);
  342.             statline(mes[MS+4]);
  343.             read_val = g4read();
  344.             switch (read_val) {
  345.                 case ESC :
  346.                     if (warning(mes[MH+6]))
  347.                         return(0);
  348.                     break;
  349.  
  350.                 case F1 :
  351.                     help("CASE_APPEND");
  352.                     break;
  353.  
  354.                 case F10 :
  355.                     dbcodes = d4open(&set, files(DBF_CODES));
  356.                     d4tag_select(dbcodes, d4tag(dbcodes, "CODE_T"));
  357.                     d4lock_file(dbcodes);
  358.                     d4lock_index(dbcodes);
  359.                     if( d4seek(dbcodes, old_code) != 0 ) {
  360.                         if( d4seek(dbcodes, "W") != 0 ) {
  361.                             d4close_all(&set);
  362.                             w4display(mes[MT+1], mes[ME+9], (char *) 0);
  363.                             w4exit(1);
  364.                         }
  365.                         memcpy(cr->code, f4str(d4field(dbcodes, "CODE")), code_len);
  366.                         c4ltoa(c4atol(cr->code+1, code_len-1) + 1, cr->code+1, -6);
  367.                         memcpy(tmp_code, cr->code, code_len);
  368.                         tmp_code[7]='\0';
  369.                         f4assign(d4field(dbcodes, "CODE"), tmp_code);
  370.                         d4close(dbcodes);
  371.                         someone_used_the_code = TRUE;
  372.                     }
  373.                     else { /* If CODE is still the same. */
  374.                         f4assign(d4field(dbcodes, "CODE"), tmp_code);
  375.                         d4close(dbcodes);
  376.                     }
  377.  
  378.                     //for(count = 0; count <= descr_len -1; count ++)
  379.                     //    cr->lname[count] = gtoupper(cr->lname[count]);
  380.                     rec_buffer = (REC *) d4record(dbfile);
  381.                     d4append_start(dbfile, 0);
  382.                     d4blank(dbfile);
  383.                     memcpy(rec_buffer, cr, sizeof(REC));
  384.                     d4append(dbfile);
  385.                     d4flush_all(dbfile);
  386.                     d4unlock_all_data(dbfile);
  387.                     d4unlock_index(dbfile);
  388.                     if (someone_used_the_code) {
  389.                         w4display("", mes[ME+10], mes[ME+11], tmp_code, (char *) 0);
  390.                         someone_used_the_code = FALSE;
  391.                     }
  392.                     if (screen_file(screens(SCR_PROFESSIONS), 19, 78))
  393.                         w4exit(1);
  394.                     return (1);
  395.                 default :
  396.                     prepare_fields(cr);
  397.                     break;
  398.             }
  399.         }
  400.  
  401.     }   /* ==L1== */
  402. }
  403.  
  404. /************************************************************************/
  405. /* Edit a record                                                        */
  406. /************************************************************************/
  407. static int edit(dbfile, cr)
  408. D4DATA *dbfile;
  409. REC *cr;
  410. {
  411.     REC *rec_buffer;
  412.     D4DATA *db_chistory;
  413.     D4DATA *db_credit;
  414.  
  415.     int read_val;       /* Returned Value. */
  416.     int count;          /* Counter.          */
  417.     //int descr_len;       /* Length of frequently used field ...  */
  418.  
  419.     //descr_len = sizeof(cr->descr);
  420.  
  421.     prepare_fields(cr);
  422.  
  423.     /* Main Edit Loop...                    */
  424.     for (;;) {               /* ==L1==   */
  425.         helpline(mes[MH+4]);
  426.         statline(mes[MS+4]);
  427.  
  428.         read_val = g4read();
  429.         if (read_val == ESC) {
  430.             if (warning(mes[MH+17]))
  431.                 break;
  432.             }
  433.         else if (read_val == F1)
  434.             help("CASE_EDIT");
  435.  
  436.         else if (read_val == F10) {
  437.             /* If client not the same */
  438.             //if (memcmp(tmp_client, cr->client, 7)) {
  439.             //    db_chistory = d4open(&set, files(27));
  440.             //    d4tag_select(db_chistory, d4tag(db_chistory, "CASE_T"));
  441.             //    /* If that person is found in a case do not delete the record */
  442.             //    if(d4seek(db_chistory, f4str(d4field(dbfile, "CODE"))) == 0) {
  443.             //        d4close(db_chistory);
  444.             //        w4display("", mes[ME+33], mes[ME+34], " ", mes[MH+16], (char*) 0);
  445.             //        prepare_fields(cr);
  446.             //        continue;
  447.             //        }
  448.             //    d4close(db_chistory);
  449.             //    db_credit = d4open(&set, files(30));
  450.             //    d4tag_select(db_credit, d4tag(db_credit, "CASE_T"));
  451.             //    /* If that person is found in a case do not delete the record */
  452.             //    if(d4seek(db_credit, f4str(d4field(dbfile, "CODE"))) == 0) {
  453.             //        d4close(db_credit);
  454.             //        w4display("", mes[ME+33], mes[ME+34], " ", mes[MH+16], (char*) 0);
  455.             //        prepare_fields(cr);
  456.             //        continue;
  457.             //        }
  458.             //    d4close(db_credit);
  459.             //    }
  460.             if (warning(mes[MH+7])) {
  461.                 //for (count = 0; count <= descr_len -1; count ++)
  462.                 //    cr->surnamekey[count] = gtoupper(cr->surname[count]);
  463.                 rec_buffer = (REC *) d4record(dbfile);
  464.                 d4blank(dbfile); /* Set "record changed" flag to true. */
  465.                 memcpy(rec_buffer, cr, sizeof(REC));
  466.                 d4flush_all(dbfile);
  467.                 d4unlock_all_data(dbfile);
  468.                 d4unlock_index(dbfile);
  469.                 break;
  470.                 }
  471.             }
  472.  
  473.         prepare_fields(cr);
  474.  
  475.         } /* ==L1==*/
  476.  
  477.     helpline(mes[MH+33]);
  478.     statline(mes[MS+8]);
  479.  
  480.     return (1);
  481. }
  482.  
  483. /************************************************************************/
  484. /* Delete a record                                                      */
  485. /************************************************************************/
  486. static int delete(dbfile, filter)
  487. D4DATA *dbfile;
  488. X4FILTER filter;
  489. {
  490.     D4DATA* db_persons;
  491.  
  492.     /* If that case is found in a chistory or credit DBF do not delete the record */
  493.     // locate the supervisor code, if any, and blank it out
  494.     // in the persons.dbf
  495.     //db_persons = d4open(&set, files(27));
  496.     //d4tag_select(db_persons, d4tag(db_persons, "CASE_T"));
  497.     //if (d4seek(db_persons, f4str(d4field(dbfile, "CODE"))) == 0) {
  498.     //    d4close(db_pe);
  499.     //    w4display("", mes[ME+29], mes[ME+31], " ", mes[MH+16], (char*) 0);
  500.     //    return(0);
  501.     //}
  502.     //d4close(db_persons);
  503.  
  504.     if (warning(mes[MH+5])) {
  505.  
  506.         d4delete(dbfile);
  507.         d4flush_all(dbfile);
  508.         d4unlock_all_data(dbfile);
  509.         d4unlock_index(dbfile);
  510.         d4unlock_index(dbfile);
  511.     }
  512.  
  513.     return (1);
  514. }
  515.