home *** CD-ROM | disk | FTP | other *** search
/ Prima Shareware 3 / DuCom_Prima-Shareware-3_cd1.bin / PROGRAMO / C / TPXCLS / TEST.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-11  |  6.6 KB  |  333 lines

  1.  
  2. // text.cpp
  3. //----------------------------------------------------------------------------
  4. #include "test.hpp"
  5. //----------------------------------------------------------------------------
  6.  
  7. main()
  8. {
  9.     register b;
  10.  
  11.     init_px_engine();
  12.     
  13.     init_px_file();
  14.  
  15.     add_records();
  16.     
  17.     display_records();
  18.         
  19.     search_primary();
  20.  
  21.     search_secondary();
  22.         
  23. }
  24.  
  25. //----------------------------------------------------------------------------
  26.  
  27. int TMYPXFILE :: create_data_file()
  28. {
  29.   register b;
  30.  
  31.   b=init_descriptors(7,1,1);
  32.   if(b)
  33.     return(b);
  34.  
  35.   add_field("S","rec_num");
  36.   add_field("A30","name");
  37.   add_field("A40","address");
  38.   add_field("A20","city");
  39.   add_field("A2","state");
  40.   add_field("A9","zip");
  41.   add_field("$","net_income");
  42.  
  43.     add_secondary_index(7);
  44.     
  45.   b=init_data_file();
  46.     
  47.   return(b);
  48.  
  49. }
  50.  
  51. //----------------------------------------------------------------------------
  52.  
  53. void init_px_engine()
  54. {
  55.     register b;
  56.  
  57.   b=PXInit();
  58.   if(b){
  59.     printf("\n\n%s\n\n",PXErrMsg(b));;
  60.     exit(1);
  61.   }
  62.  
  63.     MyPXFile.set_data_buf((char *)&MyPXFile.data);
  64.   MyPXFile.set_name("MYPXFILE");
  65.  
  66.     atexit(kill_px_engine);
  67.     
  68. }
  69.  
  70. //----------------------------------------------------------------------------
  71.  
  72. void init_px_file()
  73. {
  74.     register b;
  75.   int eflag;
  76.     
  77.     b=MyPXFile.pxtblexist(&eflag);
  78.   if(!b && eflag){
  79.       b=MyPXFile.pxtbldelete();        
  80.       if(b){
  81.             printf("\n\n%s\n\n",PXErrMsg(b));;
  82.             exit(1);
  83.         }
  84.     }
  85.   
  86.     mkdir("DATA");
  87.  
  88.   b=MyPXFile.create_data_file();
  89.     if(b){
  90.         printf("\n\n%s\n\n",PXErrMsg(b));;
  91.         exit(1);
  92.     }
  93.  
  94. }
  95.  
  96. //----------------------------------------------------------------------------
  97.  
  98. void add_records()
  99. {
  100.     register b;
  101.  
  102.     b=MyPXFile.OpenTable();            
  103.     if(b){
  104.         printf("\n\n%s\n\n",PXErrMsg(b));
  105.         exit(1);
  106.     }
  107.     
  108.     MyPXFile.data.rec_num=1;
  109.     strcpy(MyPXFile.data.name,"Mr. John and Janet Jones");
  110.     strcpy(MyPXFile.data.address,"123 Main St");
  111.     strcpy(MyPXFile.data.city,"Anytown");
  112.     strcpy(MyPXFile.data.state,"NY");
  113.     strcpy(MyPXFile.data.zip,"111110000");
  114.     MyPXFile.data.net_income=45000.00;
  115.     
  116.   b=MyPXFile.InsertRecord();
  117.     if(b){
  118.         printf("\n\n%s\n\n",PXErrMsg(b));;
  119.         exit(1);
  120.     }
  121.     
  122.     MyPXFile.data.rec_num++;
  123.     strcpy(MyPXFile.data.name,"Mr. Art and Ann Able");
  124.     strcpy(MyPXFile.data.address,"121 Baker St");
  125.     strcpy(MyPXFile.data.city,"Anytown");
  126.     strcpy(MyPXFile.data.state,"NY");
  127.     strcpy(MyPXFile.data.zip,"111110000");
  128.     MyPXFile.data.net_income=33000.00;
  129.     
  130.   b=MyPXFile.InsertRecord();
  131.     if(b){
  132.         printf("\n\n%s\n\n",PXErrMsg(b));;
  133.         exit(1);
  134.     }
  135.     
  136.     MyPXFile.data.rec_num++;
  137.     strcpy(MyPXFile.data.name,"Mr. Bert Broke");
  138.     strcpy(MyPXFile.data.address,"10 1/2 Main St");
  139.     strcpy(MyPXFile.data.city,"Anytown");
  140.     strcpy(MyPXFile.data.state,"NY");
  141.     strcpy(MyPXFile.data.zip,"111110000");
  142.     MyPXFile.data.net_income=400.00;
  143.     
  144.   b=MyPXFile.InsertRecord();
  145.     if(b){
  146.         printf("\n\n%s\n\n",PXErrMsg(b));;
  147.         exit(1);
  148.     }
  149.     
  150.     MyPXFile.data.rec_num++;
  151.     strcpy(MyPXFile.data.name,"Ms. Harley Davidson");
  152.     strcpy(MyPXFile.data.address,"69 Main St");
  153.     strcpy(MyPXFile.data.city,"Anytown");
  154.     strcpy(MyPXFile.data.state,"NY");
  155.     strcpy(MyPXFile.data.zip,"111110000");
  156.     MyPXFile.data.net_income=110000.00;
  157.     
  158.   b=MyPXFile.InsertRecord();
  159.     if(b){
  160.         printf("\n\n%s\n\n",PXErrMsg(b));;
  161.         exit(1);
  162.     }
  163.  
  164.     b=MyPXFile.CloseTable();    
  165.     if(b){
  166.         printf("\n\n%s\n\n",PXErrMsg(b));;
  167.         exit(1);
  168.     }
  169.     
  170. }
  171.  
  172. //----------------------------------------------------------------------------
  173.  
  174. void display_records()
  175. {
  176.     register a,b;
  177.     
  178.     b=MyPXFile.OpenTable();    
  179.     if(b){
  180.         printf("\n\n%s\n\n",PXErrMsg(b));;
  181.         exit(1);
  182.     }
  183.         
  184.     b=MyPXFile.GetFirstRecord();
  185.     if(b){
  186.         printf("\n\n%s\n\n",PXErrMsg(b));;
  187.         exit(1);
  188.     }
  189.     
  190.     display_record();
  191.     
  192.     for(a=1;a<MyPXFile.total_records;a++){
  193.     
  194.         
  195.     b=MyPXFile.GetNextRecord();
  196.       if(b){
  197.           printf("\n\n%s\n\n",PXErrMsg(b));;
  198.           exit(1);
  199.       }
  200.  
  201.         display_record();
  202.                 
  203.     }
  204.         
  205.   b=MyPXFile.CloseTable();    
  206.     if(b){
  207.         printf("\n\n%s\n\n",PXErrMsg(b));;
  208.         exit(1);
  209.     }
  210.  
  211. }
  212.  
  213. //----------------------------------------------------------------------------
  214.  
  215. void search_primary()
  216. {
  217.   register b;
  218.  
  219.     b=MyPXFile.OpenTable();    
  220.     if(b){
  221.         printf("\n\n%s\n\n",PXErrMsg(b));;
  222.         exit(1);
  223.     }
  224.  
  225.     printf("\n\nSearch primary index for exact match\n");
  226.     printf("------------------------------------\n\n");
  227.     
  228.     for(;;){
  229.  
  230.         for(;;){
  231.             
  232.           printf("\n\n\nEnter record number (1-4, 0=Exit) :");
  233.           scanf("%d",&MyPXFile.data.rec_num);
  234.     
  235.             if(MyPXFile.data.rec_num >= 0 && MyPXFile.data.rec_num <= 4)
  236.                 break;
  237.             
  238.         }
  239.             
  240.       if(!MyPXFile.data.rec_num)
  241.           return;    
  242.         
  243.         b=MyPXFile.SearchRecord("rec_num",1,NULL,SEARCHFIRST,LOAD_RECORDS);
  244.         if(b){
  245.             printf("\n\n%s\n\n",PXErrMsg(b));;
  246.             exit(1);
  247.         }
  248.  
  249.         display_record();
  250.         
  251.     }
  252.  
  253.     b=MyPXFile.CloseTable();    
  254.     if(b){
  255.         printf("\n\n%s\n\n",PXErrMsg(b));;
  256.         exit(1);
  257.     }
  258.  
  259. }
  260.  
  261. //----------------------------------------------------------------------------
  262.  
  263. void search_secondary()
  264. {
  265.   register b;
  266.  
  267.     b=MyPXFile.OpenTable();    
  268.     if(b){
  269.         printf("\n\n%s\n\n",PXErrMsg(b));;
  270.         exit(1);
  271.     }
  272.  
  273.     printf("\n\nSearch secondary index for closest match\n");
  274.     printf("----------------------------------------\n\n");
  275.     
  276.     for(;;){
  277.  
  278.         for(;;){
  279.             
  280.           printf("\n\n\nEnter net income (0=Exit) : $");
  281.           scanf("%lf",&MyPXFile.data.net_income);
  282.     
  283.             if(MyPXFile.data.net_income >= 0)
  284.                 break;
  285.             
  286.         }
  287.             
  288.       if(!MyPXFile.data.net_income)
  289.           return;    
  290.         
  291.         b=MyPXFile.FindRecord("net_income",NULL,CLOSESTRECORD,LOAD_RECORDS);
  292.         if(b){
  293.             printf("\n\n%s\n\n",PXErrMsg(b));;
  294.             exit(1);
  295.         }
  296.  
  297.         display_record();
  298.         
  299.     }
  300.  
  301.     b=MyPXFile.CloseTable();    
  302.     if(b){
  303.         printf("\n\n%s\n\n",PXErrMsg(b));;
  304.         exit(1);
  305.     }
  306.  
  307. }
  308.  
  309. //----------------------------------------------------------------------------
  310.  
  311. void display_record()
  312. {
  313.     printf("\nRecord # %d\n   %s\n   %s\n   %s, %s %s\n  Net:$%7.2lf\n\n",
  314.                                                    MyPXFile.data.rec_num,
  315.                                                    MyPXFile.data.name,
  316.                                                    MyPXFile.data.address,
  317.                                                    MyPXFile.data.city,
  318.                                                    MyPXFile.data.state,
  319.                                                    MyPXFile.data.zip,
  320.                                                    MyPXFile.data.net_income
  321.                                                                                                 );
  322.  
  323. }
  324.                                                                                         
  325. //----------------------------------------------------------------------------
  326.  
  327. void kill_px_engine()
  328. {
  329.   PXExit();
  330. }
  331.  
  332. //----------------------------------------------------------------------------
  333.