home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / xbase / compiler / doobject / dbflib.do < prev    next >
Text File  |  1991-07-22  |  4KB  |  181 lines

  1. /*
  2.     DBFLIB.DO
  3.     This is a collection of methods for handling DBASE files
  4.     Includes:
  5.         Dbffile::displayStructure(self)
  6.         Dbffile::copyStructure(self)
  7.         Dbffile::list(self)
  8.         Dbffile::edit(self)
  9. */
  10. #include "keydef.do"
  11. #include "colors.do"
  12.  
  13. /*
  14.     Dbffile::displayStructure(self)
  15.     this methods prints a description of the structure of a DBASE
  16.     file similar to that in the DBASE DISPLAY STRUCTURE command
  17. */
  18. method Dbffile::displayStructure(self)
  19. {
  20.     ? "Structure for\t\t",name(self);
  21.     ? "Last udpate:\t\t",date(self);
  22.     ? "Number of records:\t",numRecords(self);
  23.     fields = structure(self);
  24.     ? "FIELD\t\t\t\TYPE\tLEN\tDEC";
  25.     for(i=1;i<numFields(self);i=i+1) {
  26.         f = at(fields,i);
  27.         ? format(at(f,1),"%-24s"),at(f,2),"\t",
  28.             at(f,3),"\t",at(f,4);
  29.     }    
  30. }
  31.  
  32. /*
  33.     Dbffile::copyStructure(self,newname)
  34.     this method copys the structure of self to a new database
  35.     with name of "newname"
  36.     only the structure of self is copied; no records are copied
  37. */
  38. method Dbffile::copyStructure(self,newname)
  39. {
  40.     l = structure(self);
  41.     createDbf(newname,l);
  42. }
  43.  
  44. /*
  45.     Dbffile::list(self)
  46.     this method lists the records in a Dbffile to the screen
  47. */
  48. method Dbffile::list(self)
  49. {
  50.     top(self);
  51.     fields = structure(self);
  52.     newline = set("newline");
  53.     set("newline","");
  54.     while(!eof(self)) {
  55.         for(i=1;i<numFields(self);i=i+1) {
  56.             f = at(fields,i);
  57.             name = at(f,1);
  58.             ? eval(asId(name))," ";
  59.         }
  60.         ? "\n";
  61.         skip(self,1L);
  62.     }
  63.     set("newline",newline);
  64. }
  65.  
  66. /*
  67.     The following methods implement a DBASE field editor using the 
  68.     dObject Window SAY, GET, and READ methods. 
  69.     first, define a new class called MYFIELD that inherits the behavior of
  70.     Field, but adds a prompt that is displayed at the bottom of the form
  71. */
  72. inherit(Field,Myfield,["prompt"]);
  73.  
  74. /*
  75.     define a method to get the prompt field from a Myfield variable
  76. */
  77. method Myfield::prompt(self)
  78. {
  79.     return(slot(self,"prompt"));
  80. }
  81.  
  82. /*
  83.     this method will execute whenever the DOWN key is pressed
  84.     from within a form
  85. */
  86. method Window::myNextField(self)
  87. {
  88.     n = num(currentField(self));
  89.     if(n < len(fields)) {
  90.         f = at(fields,n+1);
  91.         say(promptWindow,0,0,prompt(f),80,112);
  92.         gotoField(self,nextField(self));
  93.     }
  94.  
  95. }
  96.  
  97. /*
  98.     this method will execute whenever the UP key is pressed
  99.     from within a form
  100. */
  101. method Window::myPrevField(self)
  102. {
  103.     n = num(currentField(self));
  104.     if(n > 1) {
  105.         f = at(fields,n-1);
  106.         say(promptWindow,0,0,prompt(f),80,112);
  107.         gotoField(self,prevField(self));
  108.     }
  109. }
  110.  
  111. /*
  112.     this method implements a callable text editor for the contents of
  113.     a field
  114. */
  115. method Window::myEdit(self)
  116. {
  117.     f = currentField(self);
  118.     s = eval(asId(name(f)));
  119.     if(class(s) == "String") {
  120.         w = new(Window,1,31,3,name(f),5,5,10,40);
  121.         display(s);
  122.         remove(w);
  123.         gotoField(self,f);
  124.     }
  125. }
  126.  
  127. /*
  128.     edit a Dbffile using a full screen editor
  129. */
  130. method Dbffile::edit(self)
  131. {
  132. /*
  133.     make a window to run the editor in
  134. */
  135.     w = new(Window,1,FWHITE+LIGHTBLUE,FWHITE+LIGHTBLUE,
  136.         name(self),0,0,24,80);
  137.  
  138. /*
  139.     create the DBASE form based on field definitions within self
  140. */            
  141.     clearGets(w);
  142.     sayExp(w,0,1,#name(self),len(name(self)),FWHITE+LIGHTBLUE);
  143.     sayExp(w,0,20,#"Record=",7,FWHITE+LIGHTBLUE);
  144.     sayExp(w,0,30,#recno(self),7,FWHITE+LIGHTBLUE);
  145.     
  146.     l = structure(self);
  147.     fields = [];
  148.  
  149. /*
  150.     for each field in the DBF file, create:
  151.         a SAY field with the name of the DBF field
  152.         a GET field right next to it to read its value
  153. */
  154.     for(i=1;i<=numFields(self);i=i+1) {
  155.         field_def = at(l,i);
  156.         fname = at(field_def,1); % name is first element in field def
  157.         len = at(field_def,3);
  158.         if(len > 40) len = 40;
  159.         say(w,i,1,fname,10,31);
  160.         fields = append(fields,new(Myfield,w,i,15,fname,len,112,
  161.             "Enter the "+asUpper(trim(fname))+ " field"));
  162.  
  163.     }
  164.  
  165. /*
  166.     execute the form 
  167. */
  168.     top(self);
  169.     promptWindow = new(Window,80,112,0,"",24,0,1,80);
  170.     
  171.     onKey(w,DOWN,#myNextField(w));
  172.     onKey(w,UP,#myPrevField(w));
  173.     onKey(w,F8,#myEdit(w));
  174.  
  175.     say(promptWindow,0,0,prompt(at(fields,1)),80,112);
  176.  
  177.     read(w);
  178.     remove(w);
  179.     remove(promptWindow);
  180. }
  181.