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

  1. #include "microlib.h"
  2. #include <malloc.h>
  3.  
  4. #include "fxaddons.h"
  5. #define  SCR_FILE       SCR_NEWFORM
  6. #define  DBF_FILE       DBF_FORMS
  7.  
  8. extern IDDFILE iddForms;
  9. static IDDFILE *idd = &iddForms;
  10.  
  11. typedef struct {
  12.     char deleted;
  13.     char code[7];
  14.     char name[80];
  15.     char page[2];
  16.     char orient[7];
  17.     char header[7];
  18.     char file[7];
  19.     } REC;
  20.  
  21. typedef struct __info__ {
  22.     F4FIELD *field;
  23.     unsigned char find_info[100];
  24.     int     compare_len;
  25. } FORMS_FILTER_INFO;
  26.  
  27. int  S4CALL filter_forms(void *);
  28.  
  29. static int append(D4DATA *, REC *cr);
  30.  
  31. int newform()
  32. {
  33.     char *cr;
  34.     char *pszKey1;
  35.     char *pszKey2;
  36.     int  wref;
  37.  
  38.     int  count, i;
  39.  
  40.     int  seek_val;
  41.     int  read_val;
  42.     int  key_val;
  43.  
  44.     int  bDone2;
  45.     int  nCurrKey;
  46.  
  47.     // size of frequently used fields
  48.     // in every database field 1 is the access code of a record
  49.     // while field 2 is actually the major key of the database
  50.     int  nSizeofCode = idd->Field[1].nFileSize;
  51.     int  nSizeofName = idd->Field[2].nFileSize;
  52.     int  nSizeofRec;
  53.  
  54.     // temp fields
  55.     char *szTmpCode;
  56.     char *szTmpName;
  57.  
  58.     D4DATA *dbf;
  59.  
  60.     X4FILTER filter;
  61.     FORMS_FILTER_INFO info;
  62.  
  63.     // Allocate memory for my record buffer
  64.     ComputeRecordSize(idd, nSizeofRec);
  65.     if ((cr = (char *) malloc(nSizeofRec)) == NULL) {
  66.         w4display(mes[MT], mes[ME+1], (char *) 0);
  67.         w4exit(1);
  68.     }
  69.     pszKey1 = (char *)cr + 1;
  70.     pszKey2 = (char *)cr + 8;
  71.     szTmpCode = (char *) alloca(nSizeofCode + 1);
  72.     szTmpName = (char *) alloca(nSizeofName + 1);
  73.  
  74.     // Define, activate and clear Data Entry screen
  75.     wref = w4define(1, 0, 22, 79);
  76.     w4popup();
  77.     w4memory();
  78.     w4border(SINGLE, custom[MAIN_N]);
  79.     w4title(0, 1, mes[MT_NEWFORM], custom[MAIN_N]);
  80.     w4attribute(custom[MAIN_N]);
  81.     w4activate(wref);
  82.     w4clear(0);
  83.  
  84.     // Load screen from disk and get field coordinates
  85.     if (screen_file(screens(SCR_FILE), 22, 78))
  86.         w4exit(1);
  87.     if (get_input_positions(screens(SCR_FILE)))
  88.         w4exit(1);
  89.  
  90.     // Open Databases and select the proper Tags...
  91.     dbf = d4open(&set, files(DBF_FILE));
  92.     d4tag_select(dbf, d4tag(dbf, "CODE_T"));
  93.  
  94.     info.field = d4field(dbf, "CODE");
  95.     strcpy(info.find_info, "R");
  96.     info.compare_len = 1;
  97.     x4init(&filter, dbf, filter_forms, &info);
  98.  
  99.     g4attribute (custom[MAIN_I]);       // Set input attribute.
  100.  
  101.     // Main append, scan, edit and delete loop.
  102.     for (;;) { // ==L1==
  103.  
  104.         // Blank my record buffer and display it
  105.         memset(cr, (int) ' ', sizeof(REC));
  106.  
  107.         DisplayFields(idd, (char *) cr);
  108.  
  109.         statline(mes[MS]);
  110.         helpline(mes[MH]);
  111.         g4delimiter("[]");
  112.  
  113.         // Get code or any other user input.
  114.         read_val =0;
  115.         while (read_val != ESC && read_val != F5 && read_val != RETURN\
  116.                                 && read_val != TAB && read_val != F1) {
  117.  
  118.             g4(srow[1], scol[1], pszKey1);
  119.             g4picture("R999999");
  120.             read_val = g4read();
  121.         }
  122.  
  123.         // If Function Keys
  124.         if (read_val == ESC)
  125.             break;  // Out of ==L1==
  126.  
  127.         if (read_val == F1) {
  128.             help("CASE_QUERY");
  129.             continue;
  130.         }
  131.  
  132.         if (read_val == F5) {
  133.             append(dbf, (REC *)cr);
  134.             if (screen_file(screens(SCR_FILE), 22, 78))
  135.                 w4exit(1);
  136.             continue;
  137.         }
  138.  
  139.         // If ENTER was pressed and CODE field is blank...
  140.         if (!memcmp(pszKey1 + 1, space(nSizeofCode - 1), nSizeofCode - 1)) {
  141.             statline(mes[MS+7]);
  142.             helpline(mes[MH+22]);
  143.             read_val = 0;
  144.             while (read_val != RETURN && read_val != ESC && read_val != TAB) {
  145.                 g4(srow[2], scol[2], pszKey2);
  146.                 //g4width(nSizeofName, nSizeofName);
  147.                 g4width(nSizeofName, idd->Field[2].nCrtSize);
  148.                 read_val = g4read();
  149.                 if (read_val == F1)
  150.                     help("CASE_QUERY");
  151.             }
  152.  
  153.             if (!memcmp(pszKey2, space(nSizeofName), nSizeofName))
  154.                 continue;
  155.  
  156.             d4tag_select(dbf, d4tag(dbf, "NAME"));
  157.             memcpy(szTmpName, pszKey2, nSizeofName);
  158.             szTmpName[ nSizeofName ] = '\0';
  159.  
  160.             //seek_val = d4seek(dbf, szTmpName);
  161.             c4trim_n(szTmpName, nSizeofName + 1);
  162.             info.field = d4field(dbf, "TITLE1");
  163.             strcpy(info.find_info, szTmpName);
  164.             info.compare_len = strlen(szTmpName);
  165.  
  166.             seek_val = x4seek(&filter, szTmpName);
  167.             //d4tag_select(dbf, d4tag(dbf, "CODE_T"));
  168.             d4unlock_index(dbf);
  169.             if (seek_val == r4eof || seek_val == r4after) {
  170.                 w4display("", mes[ME+2], (char *) 0);
  171.  
  172.                 info.field = d4field(dbf, "CODE");
  173.                 strcpy(info.find_info, "R");
  174.                 info.compare_len = 1;
  175.  
  176.                 continue; // ==L1==
  177.             }
  178.         }
  179.         else {      // If CODE field is not empty...
  180.             // Zerofill and show the entered CODE.
  181.             zerofill(pszKey1 + 1, nSizeofCode - 1);
  182.             w4attribute(custom[MAIN_I]);
  183.             w4num(srow[1], scol[1], pszKey1, nSizeofCode);
  184.             w4attribute(custom[MAIN_N]);
  185.  
  186.             d4tag_select(dbf, d4tag(dbf, "CODE_T"));
  187.  
  188.             // Copy code and add NULL.
  189.             memcpy(szTmpCode, pszKey1, nSizeofCode);
  190.             szTmpCode[ nSizeofCode ] = '\0';
  191.  
  192.             info.field = d4field(dbf, "CODE");
  193.             strcpy(info.find_info, szTmpCode);
  194.             info.compare_len = strlen(szTmpCode);
  195.             //seek_val = d4seek(dbf, szTmpCode);
  196.             seek_val = x4seek(&filter, szTmpCode);
  197.             d4unlock_index(dbf);
  198.             if (seek_val == r4eof || seek_val == r4after) {
  199.                 w4display("", mes[ME+2], (char *) 0);
  200.                 continue; // ==L1==
  201.             }
  202.         }
  203.  
  204.         // If CODE or SURNAMEKEY was found display the record and let the
  205.         // user to skip through the records, edit, delete, change to
  206.         // the second screen or ask for help.
  207.         // ?
  208.         memcpy(cr, (char *) d4record(dbf), sizeof(REC));
  209.  
  210.         helpline(mes[MH+33]);   // Show new available keys.
  211.         statline(mes[MS+8]);
  212.  
  213.         // Clear delimiters from CODE and SURNAME field
  214.         // w4num(srow[1], scol[1] -1, space(nSizeofCode +2), nSizeofCode +2);
  215.         // w4num(srow[2], scol[2] -1, space(nSizeofName +2), nSizeofName +2);
  216.  
  217.         DisplayFields(idd, (char *)cr);
  218.  
  219.         bDone2 = FALSE;
  220.         while (!bDone2) {
  221.             nCurrKey = _kbdhit();
  222.             switch (nCurrKey) {
  223.  
  224.                 case VK_PGUP :
  225.                     if (d4skip(dbf, -1L) == r4bof)
  226.                         d4bottom(dbf);
  227.                     break;
  228.  
  229.                 case VK_PGDN :
  230.                     if (d4skip(dbf, 1L) == r4eof)
  231.                         d4top(dbf);
  232.                     break;
  233.  
  234.                 case VK_ESCAPE :
  235.                     bDone2 = TRUE;
  236.                     continue;
  237.                     //break; // ==L2 ==
  238.  
  239.                 case VK_F2 :
  240.                     nCurrKey = EditRecord(dbf, idd, (char *) cr);
  241.                     if (screen_file(screens(SCR_FILE), 22, 78))
  242.                         w4exit(1);
  243.                     if (nCurrKey == VK_ESCAPE) {
  244.                         bDone2 = TRUE;
  245.                         continue;
  246.                     }
  247.                     DisplayFields(idd, (char *) cr);
  248.                     continue;
  249.  
  250.                 //case VK_F8 :
  251.                 //    delete(dbf, filter);
  252.                 //    break;
  253.  
  254.                 default :
  255.                     continue;
  256.             }
  257.  
  258.             memcpy(cr, (char *) d4record(dbf), sizeof(REC));
  259.             DisplayFields(idd, (char *) cr);
  260.  
  261.         }
  262.     }       // ==L1==
  263.  
  264.     statline(mes[MS+2]);
  265.     helpline(mes[MH+3]);
  266.     d4close_all(&set);
  267.  
  268.     free(cr);
  269.     w4deactivate(wref);
  270.     w4close(wref);
  271.  
  272.     // Return the menu item to be selected.
  273.     return (2);
  274. }
  275.  
  276. /************************************************************************/
  277. /* Append a record                                                      */
  278. /************************************************************************/
  279. static int append(dbfile, cr)
  280. D4DATA *dbfile;
  281. REC *cr;
  282. {
  283.     REC *rec_buffer;
  284.     D4DATA *dbcodes;                /* File to get last code.                */
  285.     int count;                      /* Counter.                              */
  286.     int read_val;                   /* Returned value.                       */
  287.  
  288.     char old_code[7];
  289.     char tmp_code[7];               /* Temporary storage of CODE field       */
  290.                                     /* because when I clear my buffer I lose */
  291.                                     /* the next available code.              */
  292.     int nSizeofCode;                   /* Length of frequently used fields ...  */
  293.     int nSizeofName;
  294.     int someone_used_the_code;
  295.  
  296.     someone_used_the_code = FALSE;
  297.     nSizeofCode = sizeof(cr->code);
  298.  
  299.     d4tag_select(dbfile , d4tag(dbfile, "CODE_T"));
  300.  
  301.     /* Main Append Loop (where next code number is given etc.). */
  302.     for (;;) {   /* ==L1== */
  303.         /* Read the last code used from CODES file.         */
  304.         dbcodes = d4open(&set, files(DBF_CODES));
  305.         d4tag_select(dbcodes, d4tag(dbcodes, "CODE_T"));
  306.         d4lock_file(dbcodes);
  307.         d4lock_index(dbcodes);
  308.         if (d4seek(dbcodes, "R") != 0) {
  309.             d4close_all(&set);
  310.             w4display(mes[MT+1], mes[ME+9], (char *) 0);
  311.             w4exit(1);
  312.         }
  313.         memcpy(cr->code, f4str(d4field(dbcodes, "CODE")), nSizeofCode);
  314.         memcpy(old_code, cr->code, nSizeofCode);
  315.         old_code[7]='\0';
  316.         d4close(dbcodes);
  317.  
  318.         /*  The character field is converted to numeric ,       */
  319.         /* the value is increased by 1 and then the result      */
  320.         /* is converted again to character.                         */
  321.         /* At the numeric-->character conversion I use a        */
  322.         /* negative length in order to "zerofill" the result. */
  323.         c4ltoa45(c4atol(cr->code+1, nSizeofCode-1) + 1, cr->code+1, -6);
  324.  
  325.         /* Save the next available code.    */
  326.         memcpy(tmp_code, cr->code, nSizeofCode);
  327.         tmp_code[7]='\0';
  328.  
  329.         /* Show next available code, clear buffer, restore the next */
  330.         /* available code. Show the screen file and prepare gets.   */
  331.  
  332.         w4attribute(custom[MAIN_I]);
  333.         w4num(srow[1], scol[1], cr->code, nSizeofCode);
  334.         w4attribute(custom[MAIN_N]);
  335.         memset(cr, (int) ' ', sizeof(REC));
  336.         memcpy(cr->code, tmp_code, nSizeofCode);
  337.  
  338.         if (get_input_positions(screens(SCR_FILE)))
  339.             w4exit(1);
  340.         read_val = EditFields(idd, (char *)cr);
  341.  
  342.         /* Entry Loop   */
  343.         for (;;) {
  344.             helpline(mes[MH+4]);
  345.             statline(mes[MS+4]);
  346.             //read_val = g4read();
  347.             switch (read_val) {
  348.                 case ESC :
  349.                     if (warning(mes[MH+6]))
  350.                         return(0);
  351.                     break;
  352.  
  353.                 case F1 :
  354.                     help("CASE_APPEND");
  355.                     break;
  356.  
  357.                 case F10 :
  358.                     dbcodes = d4open(&set, files(DBF_CODES));
  359.                     d4tag_select(dbcodes, d4tag(dbcodes, "CODE_T"));
  360.                     d4lock_file(dbcodes);
  361.                     d4lock_index(dbcodes);
  362.                     if(d4seek(dbcodes, old_code) != 0) {
  363.                         if(d4seek(dbcodes, "R") != 0) {
  364.                             d4close_all(&set);
  365.                             w4display(mes[MT+1], mes[ME+9], (char *) 0);
  366.                             w4exit(1);
  367.                         }
  368.                         memcpy(cr->code, f4str(d4field(dbcodes, "CODE")), nSizeofCode);
  369.                         c4ltoa45(c4atol(cr->code+1, nSizeofCode-1) + 1, cr->code+1, -6);
  370.                         memcpy(tmp_code, cr->code, nSizeofCode);
  371.                         tmp_code[7]='\0';
  372.                         f4assign(d4field(dbcodes, "CODE"), tmp_code);
  373.                         d4close(dbcodes);
  374.                         someone_used_the_code = TRUE;
  375.                     }
  376.                     else { // If CODE is still the same.
  377.                         f4assign(d4field(dbcodes, "CODE"), tmp_code);
  378.                         d4close(dbcodes);
  379.                     }
  380.  
  381.                     //for(count = 0; count <= nSizeofName -1; count ++)
  382.                     //    cr->lname[count] = gtoupper(cr->lname[count]);
  383.                     rec_buffer = (REC *) d4record(dbfile);
  384.                     d4append_start(dbfile, 0);
  385.                     d4blank(dbfile);
  386.                     memcpy(rec_buffer, cr, sizeof(REC));
  387.                     d4append(dbfile);
  388.                     d4flush_all(dbfile);
  389.                     d4unlock_all_data(dbfile);
  390.                     d4unlock_index(dbfile);
  391.                     if (someone_used_the_code) {
  392.                         w4display("", mes[ME+10], mes[ME+11], tmp_code, (char *) 0);
  393.                         someone_used_the_code = FALSE;
  394.                     }
  395.                     if (screen_file(screens(SCR_FILE), 19, 78))
  396.                         w4exit(1);
  397.                     return (1);
  398.                 default :
  399.                     EditFields(idd, (char *)cr);
  400.                     break;
  401.             }
  402.         }
  403.     }   /* ==L1== */
  404. }
  405.  
  406. int S4CALL filter_forms(void *info_parm)
  407. {
  408.     FORMS_FILTER_INFO *info;
  409.  
  410.     info = (FORMS_FILTER_INFO *) info_parm;
  411.  
  412.     if (memcmp(f4ptr(info->field), info->find_info, info->compare_len))
  413.         return (r4ignore);
  414.  
  415.     return (r4keep);
  416. }
  417.