home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 41 / IOPROG_41.ISO / soft / c++ / CDBFILE.ZIP / TestDBF.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-10  |  5.0 KB  |  197 lines

  1. /****************************************************************************/
  2. /*******    TestDBF.cpp : sample application for the CDBFile object  ********/
  3. /****************************************************************************/
  4. /*
  5.   
  6.     Copyright (C) 1997 HervΘ GOURMELON, ENSSAT
  7.     gourmelon@enssat.fr
  8.     http://www.enssat.fr
  9.  
  10.     This program is free software; you can redistribute it and/or modify
  11.     it under the terms of the GNU General Public License as published by
  12.     the Free Software Foundation; either version 1, or (at your option)
  13.     any later version.
  14.  
  15.     This program is distributed in the hope that it will be useful,
  16.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.     GNU General Public License for more details.
  19.  
  20.     You should have received a copy of the GNU General Public License
  21.     along with this program (COPYING.TXT); if not, write to the Free Software
  22.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24.  
  25.  
  26. #include <string.h>
  27. #include <stdio.h>
  28. #include <conio.h>
  29. #include <stdlib.h>
  30. #include "cdbfile.h" 
  31.  
  32.  
  33. // A simple function that displays the raw contents of the data, truncating
  34. // the length of the displayed records to 75 characters. An index Ind is passed
  35. // as a parameter, which indicates from which record the list should be
  36. // displayed.
  37. void  Display(CDBFile *Dbase, unsigned long Ind)
  38. {
  39.     int i; 
  40.     char Page[23][80];
  41.  
  42.     printf("*o:OpenFile; r/R:load one/all records; e:edit; d:del; s:sort; x:exit; i:index *\n");
  43.     Dbase->GetAtRecord(Ind);
  44.     for (i=0; i<23; i++)
  45.     {    
  46.         Dbase->DumpCurrentContents(75, Page[i]);
  47.         printf("%d\t%s\n", Dbase->GetRecordNum(), Page[i]);
  48.         Dbase->GetNextRecord();
  49.     }
  50. }
  51.  
  52.  
  53. void main()
  54. {
  55. CDBFile Base;
  56. unsigned long Index=1;
  57.  
  58.  
  59. char c=' ';
  60.  
  61. printf("TestDBF version 1.0a, Copyright (C) 1997  Herve GOURMELON \n");
  62. printf("TestDBF comes with ABSOLUTELY NO WARRANTY; it is licensed \n");
  63. printf("under the GNU Public License.  This is free software, and \n");
  64. printf("you are welcome to  redistribute  it under certain condi- \n");
  65. printf("tions; see the 'COPYING' file for details, or the website \n");
  66. printf("http://www.fsf.org. \n");
  67. printf("Strike SPACE to continue...\n");
  68. c=(char)getch();
  69.  
  70.  
  71.  
  72. while ((c!='x')||(c!='q'))
  73. {
  74.     switch(c)
  75.     {
  76.     case 'o':    // Opening the file
  77.     {
  78.          char Path[256];
  79.         printf("Type in the complete path to the file :");
  80.         scanf("%s", Path);
  81.         if (Base.OpenFile(Path)==FALSE) exit(1);
  82.         else Display(&Base,Index);
  83.         break;
  84.     }
  85.     case 'r':    // Reading one record from the file; loading it into memory
  86.     if (Base.IsOpen()==FALSE) break;
  87.     else
  88.     {
  89.         unsigned long RecNum;
  90.         printf("Type the number of the record within the file : ");
  91.         scanf("%d", &RecNum);
  92.         Base.LoadRecord(RecNum);
  93.         Display(&Base, Index);
  94.     }
  95.     break;
  96.     case 'R':    // Loading the whole file into the memory
  97.     if (Base.IsOpen()==FALSE) break;
  98.     else
  99.     {
  100.         unsigned long RecNum;
  101.         RecNum=Base.LoadFileToMemory();
  102.         Display(&Base, Index);
  103.     }
  104.     break;
  105.     case 'e':     // Editing a record
  106.     {
  107.         unsigned short i, Fields;
  108.         unsigned long RecNum;
  109.         printf("Type the number of the record within the file : ");
  110.         scanf("%d", &RecNum);
  111.         Base.GetAtRecord(RecNum);
  112.         Fields=Base.GetFieldCount();
  113.         for (i=1; i<=Fields; i++)
  114.         {
  115.             switch(Base.GetFieldType(i))
  116.             {
  117.             case 'N':
  118.                 if (Base.GetFieldDecCount(i)==0)
  119.                 {
  120.                     long Res1;
  121.                     printf("Enter the new value (int) for field %d : ", i);
  122.                     scanf("%ld", &Res1);
  123.                     Base.SetFieldValue(i, (void*)&Res1);
  124.                     break;
  125.                 }
  126.                 else
  127.                 {
  128.                     double Res1;
  129.                     printf("Enter the new value (float) for field %d : ", i);
  130.                     scanf("%lf", &Res1);
  131.                     Base.SetFieldValue(i, (void*)&Res1);
  132.                     break;
  133.                 }
  134.             case 'C':
  135.             default :
  136.                 char Res2[256];
  137.                 printf("Enter the new value (string, 255 char max.) : ");
  138.                 scanf("%s", Res2);
  139.                 Base.SetFieldValue(i, (void*)&Res2);
  140.                 break;
  141.             }
  142.         }
  143.         Display(&Base, RecNum);
  144.         break;
  145.     }
  146.     case 'd' :    // Delete a record
  147.     {
  148.         unsigned long RecNum;
  149.         printf("Type the number of the record within the file : ");
  150.         scanf("%d", &RecNum);
  151.         Base.GetAtRecord(RecNum);
  152.         Base.DeleteCurrentRec();
  153.         Display(&Base, RecNum);
  154.         break;
  155.     }
  156.     case 's' :    // Sort all records
  157.     {
  158.         unsigned short Crit1;
  159.         printf("Type the number of the criterium field : ");
  160.         scanf("%d", &Crit1);
  161.  
  162.         Base.SortOn(Crit1/*, Crit2*/);
  163.         Display(&Base, Index);
  164.         break;
  165.     }
  166.     case 'i' :     // Displaying from record #index
  167.         printf("Enter the display index : ");
  168.         scanf("%lu", &Index);
  169.         Display(&Base, Index);
  170.         break;
  171.     case 'w' :     // write modified records (only if FullFileInMemory==FALSE)
  172.         Base.WriteModified();
  173.         break;
  174.     case 'W' :     // save the entire file as... (if FullFileInMemory==TRUE)
  175.     {
  176.          char Path[256];
  177.         printf("Type in the complete path to the file :");
  178.         scanf("%s", Path);
  179.         Base.WriteAllToFile(Path);
  180.         break;
  181.     }
  182.  
  183.     case ' ' :
  184.         Display(&Base, Index);
  185.         break;
  186.     case 'x' : exit(0);
  187.     }// end switch
  188.     c=(char)getch();
  189. }//end while
  190.  
  191. }
  192.             
  193.             
  194.         
  195.  
  196.  
  197.