home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / OPDIRX.ZIP / EXTSORT.TXT next >
Encoding:
Text File  |  1990-02-24  |  9.3 KB  |  234 lines

  1.                   EXTENSION SORT FOR OPDIR.TPU
  2.                                or
  3.            THE SORT METHOD TURBOPOWER SORT OF FORGOT
  4.  
  5.                          Copyright 1990
  6.                    by Gil Yoder [73237,3103]
  7.          Portions Copyright 1989 by TurboPower Software
  8.            Permission granted to copy and distribute
  9.                  if such is done without profit
  10.  
  11.  
  12. Thank you TurboPower for a great new product!  Object
  13. Professional is great.  Although at first glance the packaged
  14. seemed unwieldy (my wife had to get a neighbor to help her carry
  15. it into the house ;->), after reading about a third of the
  16. printed documentation I am coming to the realization that
  17. implementing the features of this tool box will be much easier
  18. than its predecessor, Turbo Professional.
  19.  
  20. There are, however, just a few things one could wish for; not
  21. much, but a few.  For example OPDIR needs at least one new sort
  22. procedure for directory lists.  You include sorts based on name
  23. and extension; directory, name and extension; size, name and
  24. extension; and date, name and extension; but where are the
  25. procedures for sorting by directory, extension and name?  For
  26. picking filenames out of large directories this extra method of
  27. sorting is essential.
  28.  
  29. Really, I am not complaining!  In fact you said, "If you need to
  30. write a sort procedure that isn't already covered, study the
  31. sort routines in OPDIR.IN1"  (volume 1, page 4-246).  That is
  32. what I did.  The code that you have written is so well
  33. documented and easy to understand, I was able to prepare two new
  34. sort methods within a matter of about 30 minutes.  Below are the
  35. changes I have made to OPDIR.IN1 and OPDIR.PAS to implement
  36. sorting by extension and name, and sorting by directory,
  37. extension and name.
  38.  
  39. These four lines were added to OPDIR.PAS in the INTERFACE
  40. section to make the procedures accessible to the outside.  Add
  41. them along with the other Sort procedures (search for
  42. "SortName").
  43.  
  44.   procedure SortExt(DirPtr : DirListPtr);
  45.     {-Sort alphabetically by extension, then by name}
  46.   procedure SortDirExt(DirPtr : DirListPtr);
  47.     {-Sort directories first, then alphabetically by extension and name}
  48.  
  49. These two procedures are added along with two local functions to
  50. OPDIR.IN1.  If you keep the order given here, you can put them
  51. just about anywhere in the file, but it would be wise to search
  52. for the sort routines and place these routines with those.
  53. (Again search for "SortName.")
  54.  
  55.   function LessExt(var X, Y : DirRec) : Boolean;
  56.     {-Sort ordering -- alphabetically by extension then name}
  57.   var
  58.     Xdrive : Boolean;
  59.     Ydrive : Boolean;
  60.   begin
  61.     Xdrive := (X.Attr = diDriveAttr);
  62.     Ydrive := (Y.Attr = diDriveAttr);
  63.     if Xdrive = Ydrive then
  64.       if ByteFlagIsSet(X.Attr, Directory) then
  65.         LessExt := (X.Name < Y.Name)
  66.       else
  67.         LessExt := (JustExtension(X.Name)+X.Name <
  68.           JustExtension(Y.Name)+Y.Name)
  69.     else
  70.       LessExt := Xdrive;
  71.   end;
  72.  
  73.   procedure SortExt(DirPtr : DirListPtr);
  74.     {-Sort alphabetically by name, then by extension}
  75.   begin
  76.     with DirPtr^ do begin
  77.       diLess := LessExt;
  78.       diQuickSort(1, pkItems);
  79.     end;
  80.   end;
  81.  
  82.   function LessDirExt(var X, Y : DirRec) : Boolean;
  83.     {-Sort directories first, then alphabetically by extension and name}
  84.   var
  85.     Xdir : Boolean;
  86.     Ydir : Boolean;
  87.     Xdrive : Boolean;
  88.     Ydrive : Boolean;
  89.   begin
  90.     Xdrive := (X.Attr = diDriveAttr);
  91.     Ydrive := (Y.Attr = diDriveAttr);
  92.     if Xdrive = Ydrive then begin
  93.       Xdir := ByteFlagIsSet(X.Attr, Directory);
  94.       Ydir := ByteFlagIsSet(Y.Attr, Directory);
  95.       if Xdir = YDir then
  96.         LessDirExt := LessExt(X, Y)
  97.       else
  98.         LessDirExt := Xdir;
  99.     end else
  100.       LessDirExt := Xdrive;
  101.   end;
  102.  
  103.   procedure SortDirExt(DirPtr : DirListPtr);
  104.     {-Sort directories first, then alphabetically by extension and name}
  105.   begin
  106.     with DirPtr^ do begin
  107.       diLess := LessDirExt;
  108.       diQuickSort(1, pkItems);
  109.     end;
  110.   end;
  111.  
  112. [I can't take credit for these routines, because they are only
  113. slight variations of the routines already supplied with OPRO.]
  114.  
  115. After adding these routines to OPDIR, I modified DESKPOP to take
  116. advantage of the new sort methods.  Two of DESKPOP's source
  117. files need changing.  DESKPOP.ICD must be changed on one line.
  118. Change line 11 from this...
  119.  
  120.   DirSortType     = (dstName, dstDate, dstSize, dstDos);
  121.  
  122. ... to this...
  123.  
  124.   DirSortType     = (dstName, dstExt, dstDate, dstSize, dstDos);
  125.  
  126. If you want to make Extension sorting the default, change line
  127. 57 from this...
  128.  
  129.   CurDirSortType  : DirSortType = dstName; {sort method}
  130.  
  131. ... to this...
  132.  
  133.   CurDirSortType  : DirSortType = dstExt; {sort method}
  134.  
  135. DESKMAIN.IN1 needs to be changed in a couple of places.  Change
  136. line 353 from this...
  137.  
  138.       'Name  ', 'Date  ', 'Size  ', 'MS-DOS');
  139.  
  140. ... to this...
  141.  
  142.       'Name  ', 'Ext   ', 'Date  ', 'Size  ', 'MS-DOS');
  143.  
  144. ... and then add this at line 568
  145.  
  146.         dstExt  : SetSortOrder(SortDirExt);
  147.  
  148. Make these few changes to the files mentioned and compile with
  149. TPC.  You will need about 520k memory, and should specify /L to
  150. link to disk.  Otherwise all your changes will be for nought.
  151.  
  152. What else could a programmer want?  Well so far this is my wish
  153. list:
  154.  
  155. 1. A multiple choice pick list that returns choices in a linked
  156.    list or array rather than a BitSet.  BitSets are efficient
  157.    for small lists, but the larger the pick list the less
  158.    efficient BitSets become.  I have an application requiring
  159.    multiple choices from a list of over 12,000 items!  That
  160.    would require a structure containing over 1,500 bytes. That's
  161.    a lot of data to reference 1 to 20 items.
  162. 2. A quick incremental search for large pick lists (same
  163.    project).  This will probably be easy to program, given
  164.    objects and the excellent design of OPRO.  If you can assume
  165.    (or determine) that a list is in alphabetical order, for a normal
  166.    incremental search there is way too much overhead in
  167.    comparing every item with the search string.  A much faster
  168.    approach is to compare a mid point with the search string.
  169.    If there is no match there, the list can be divided in half
  170.    and the half that could contain the string could be searched
  171.    in the same way.  Eventually the string would be found, or
  172.    else no other portion of the list could be divided.  If the
  173.    string is found, then each string one less than that could be
  174.    compared with the string, until no match was found.  The new
  175.    item would be the last string found that matched the search
  176.    string.  I implemented this method with TurboPower, and
  177.    found that I could do an incremental search in an
  178.    alphabetical list of 12,500 words and update the bar cursor
  179.    as fast as anyone could type.
  180. 3. A larger index.  Six pages for three volumes of material as
  181.    rich as you have is just a little conservative.
  182.  
  183. All the same Object Professional is a great package; it's hard
  184. to imagine how you produced it in the short time that you did
  185. (documentation problems notwithstanding).  Thank you, and keep
  186. up the good work.
  187.  
  188. NOTE:  Except for small changes the code listed in this article
  189. is taken from code found in the Object Professional package.
  190. The copyright (c) 1989 to the code found here belongs to TurboPower
  191. Software, P.O. Box 66747, Scotts Valley, CA 95066.  CompuServe
  192. 76004,2611.  The people at TurboPower have graciously granted
  193. permission to place this file at the disposal of their customers
  194. by means of CompuServe or any other avenue.
  195.  
  196.  
  197.                           POST SCRIPT
  198.  
  199. Because of the similarity between the procedures found here and
  200. those found in OPDIR, before uploading this file I sent it to
  201. TurboPower to ask for their permission to make it available to
  202. others.  Kim Kokkonen replied...
  203.  
  204. >> Gil - nice job on your "article." I don't have any problem
  205. >> with your posting it on PCVENB or elsewhere. One
  206. >> suggestion: your new sorts for OPDIR can just as easily be
  207. >> implemented without changing any of our source files. I'd
  208. >> suggest that you just write a standalone unit that
  209. >> includes your new functions for sorting. The DESKPOP
  210. >> changes, of course, would be slightly modified.
  211. >>
  212. >> Regarding your suggestions: 1) it would be easy to
  213. >> post-process the OPPICK selected bitset to turn it into a
  214. >> linked list and then dispose of the bitset. For
  215. >> performance reasons, OPPICK must have indexed access to
  216. >> the elements of the bitset while it is processing, so a
  217. >> linked list is not a good choice internally. 2) the
  218. >> searching functions for the pick list were designed to be
  219. >> extensible. I think that you could write a separate unit
  220. >> using the same kind of methodology you did for OPDIR
  221. >> sorting that would provide the kind of incremental search
  222. >> you want. 3) I agree that the index could be expanded.
  223. >> Someday when we have time we'll go through and beef it up
  224. >> and create a text file that people can use.
  225.  
  226. Comments:  The suggestion to place the procedures and functions
  227. in a standalone unit is a good one.  As far as possible it is
  228. best not to modify TurboPower's routines unless official changes
  229. are released.  OpDirX.pas implements both of the preceeding
  230. sorts without changing OpDir.  The changes detailed above for
  231. DESKPOP.ICD and DESKMAIN.IN1 are still necessary.  In addition
  232. DESKMAIN.PAS needs to add OPDIRX in its "uses" statement.
  233.  
  234. Enjoy.