home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 106 / EnigmaAmiga106CD.iso / www / afc / afc-dir / dirlist_source.lha / Source / DirList.e next >
Text File  |  1999-02-20  |  13KB  |  496 lines

  1. /*
  2.     $VER: DirList V3.11 - (C)Amiga Foundation Classes
  3.  
  4.     Written  By: Fabio Rotondo
  5.  
  6.     V2.10  - Added setmode() which can be used to select operating modes
  7.              via tags. (Modes will grow as time goes by)
  8.  
  9.              At the moment this flags (TRUE/FALSE) are present
  10.              DIR_COMPLETEPATH - Stores the complete path of the file/dir
  11.              DIR_MARKDIR      - Marks dir with the leading (DIR) prefix
  12.  
  13.  
  14.     V3.00  - Restructured code.
  15.  
  16.      -->   - EDefObj Compliant.
  17.  
  18.     V3.10  - Now DirList supports resourceTracker
  19.  
  20.     V3.11  - Recompiled to support the new StringNode
  21. */
  22.  
  23. OPT PREPROCESS
  24.  
  25. #define DO_DIRLIST_MOD
  26.  
  27. #ifdef DO_DIRLIST_MOD
  28. OPT MODULE
  29. OPT EXPORT
  30. #endif
  31.  
  32. MODULE 'afc/stringnode', 'dos/dos',
  33.        'afc/resourceTracker'
  34.  
  35.  
  36. CONST DIRLIST_BASE = $00030000
  37.  
  38. ENUM DIRTAG_COMPLETEPATH=DIRLIST_BASE, DIRTAG_MARKDIR
  39.  
  40. ENUM DLERR_NOMEMORY=DIRLIST_BASE,
  41.      DLERR_INIT, DLERR_LOCK, DLERR_NOINIT, DLERR_NODIR, DLERR_EXAMINE
  42.  
  43. CONST DIRLIST_VERSION = 3
  44. CONST DIRLIST_REVISION = 11
  45.  
  46. -> /// main docs
  47. /*
  48. @node "Main" "Amiga Foundation Classes Module: DirList"
  49.  
  50.  
  51.  
  52.                   ** DirList - Original By Fabio Rotondo **
  53.  
  54.                       * Part of Amiga Foundation Classes *
  55.  
  56.                           --- NO LONGER SUPPORTED ---
  57.  
  58.         @{" Introduction " link "introduction" }             @{" Author's Infos " link "author" }      @{" Amiga Foundation Classes " LINK "afc.guide/main"}
  59.  
  60.  
  61.    Requires: @{" NodeMaster      " LINK "NodeMaster.guide/main"}
  62.              @{" StringNode      " LINK "StringNode.guide/main"}
  63.              @{" resourceTracker " LINK "resourceTracker.guide/main"}
  64.  
  65.    Base: $0003
  66.  
  67.  
  68.    NOTE: DirList inherits completely @{" StringNode " LINK "stringnode.guide/main"} so, please, refer
  69.          to this class for further documentation.
  70.  
  71.    NEW COMMANDS                          BRIEF DESCRIPTION
  72.    -------------------------------------------------------------------------
  73.    @{" dirlist(resTracker=NIL)         " link "DirList" } Initailizes the object
  74.    @{" dirname()                       " link "dirname" } Returns current dir name
  75.    @{" read(dirs, files, match)        " link "read" } Scans a Directory
  76.    @{" setattrs(tags)                  " link "setmode" } Set objects behaviours
  77.    @{" setdir(path)                    " link "setdir" } Set Directory to read
  78.    @{" version()                       " link "version"} Returns class version and revision
  79.  
  80.    @{" Tags Table                      " link "Tags" } Possible DirList's Tags
  81.  
  82.    @{" Errors Table                    " LINK "ErrorTable"} Error Table
  83. @endnode
  84.  
  85. @node "Introduction" "Introduction"
  86.  
  87.     INTRODUCTION
  88.  
  89.  
  90.     DirList Object is something I created for my own needs.
  91.  
  92.     Now  it  is grown to a powerful and stable object, so I decided to share it
  93. with all the rest of Amiga Foundation Classes community.
  94.  
  95.     Main features are:
  96.  
  97.         * Accept AmigaDOS Pattern Matching.
  98.  
  99.         * Very fast scanning algorithms.
  100.  
  101.         * Easy to use.
  102.  
  103.         * Relys on StringNode Object. (See StringNode_oo)
  104.  
  105.  
  106. NEW V2.10 Features:
  107.  
  108.         * Added a new method called tagset():
  109.           With this method you will be able to change some DirList
  110.           behaviours via tags (at the moment, just two different tags
  111.           are implemented, but many others will do as time goes by)
  112.  
  113.         * Now dirname() returns also the "/" when needed.
  114.  
  115.         * Example program now working ;)
  116.  
  117.         * New examples added.
  118.  
  119. NEW V3.00 Features:
  120.  
  121.         * Restructured code.
  122.  
  123.         * Part of the Amiga Foundation Classes
  124.  
  125. NEW V3.10 Features:
  126.  
  127.         * Added resourceTracker Support.
  128. @endnode
  129.  
  130. @node author "Author(s) Info"
  131.  
  132.     Original By:  Fabio Rotondo  (fsoft@intercom.it)
  133.  
  134.     E Version By: Fabio Rotondo
  135.  
  136.  
  137.  
  138.     Address:
  139.  
  140.             Fabio Rotondo
  141.             C.so Vercelli 9
  142.             28100 Novara
  143.             ITALY
  144.  
  145.             e-mail: fsoft@intercom.it
  146.                     Fabio.Rotondo@deagostini.it
  147.  
  148.             Phone:  (ITA) - (0)321 -   459676  (home)
  149.                     (ITA) - (0)321 -   424272  (office)
  150.                     (ITA) - (0)338 -  7336477  (GSM Phone)
  151.  
  152.             Fax:    (ITA) - (0)321 -   424560
  153.  
  154.             Web:    http://www.intercom.it/~fsoft               (my home page)
  155.  
  156.                     http://www.intercom.it/~fsoft/ablast.html   (Amiga Blast Home Page)
  157. @endnode
  158.  
  159. @node Tags "Amiga Foundation Classes Modules: DirList/Tag Table"
  160.  
  161.  
  162.     TAG NAME        | VALUES (DEFAULT)| DESCRIPTION
  163. +-------------------+-----------------+-----------------------------------
  164.  TAGDIR_MARKDIR     |TRUE/FALSE(TRUE) | If TRUE, DirList will add the
  165.                     |                 | leading "(DIR) " prefix to dir names.
  166.                     |                 |
  167. TAGDIR_COMPLETEPATH |TRUE/FALSE(FALSE)| If TRUE, DirList will add complete path
  168.                     |                 | to the dirs and files read.
  169.                     |                 |
  170.  
  171. Please, see also @{" StringNode/Tags Table" LINK "StringNode.guide/StringNode_TagsTable"}
  172. @endnode
  173.  
  174. @node ErrorTable "Amiga Foundation Classes Modules: DirList / Error Table "
  175. ERROR VALUE         | DESCRIPTION
  176. --------------------+------------------------------------------------------
  177.     $0000           | No memory.
  178.     $0001           | Initialization failed.
  179.     $0002           | Could not lock desired dir (ie. dir not found)
  180.     $0003           | You are trying to use a class which failed to init.
  181.     $0004           | You are trying to read "nothing" (ie: you haven't set a valid dir)
  182.     $0005           | Examine() call failed.
  183.  
  184. Please, see also @{" StringNode/Error Table" LINK "StringNode.guide/StringNode_ErrorTable"}
  185.  
  186. @endnode
  187.  
  188. */
  189. -> ///
  190.  
  191. OBJECT dirlist OF stringnode
  192.   PRIVATE
  193.   dirname
  194.   doslock
  195.   dosobj:PTR TO fileinfoblock
  196.   init
  197.  
  198.   adddir  :CHAR
  199.   allpath :CHAR
  200.  
  201.   resTracker:PTR TO resourceTracker
  202. ENDOBJECT
  203.  
  204. -> /// dirlist() OF dirlist
  205. /*
  206. @node "DirList" "Amiga Foundation Classes Modules: DirList/dirlist()"
  207.  
  208.            NAME: dirlist(resTracker=NIL:PTR TO resourceTracker)
  209.  
  210.     DESCRIPTION: Use this command to initailize the object.
  211.  
  212.           INPUT: resTracker       - (Optional) Pointer to a valid resourceTracker class.
  213.  
  214.         RESULTS: NONE.
  215.  
  216.        SEE ALSO: @{" StringNode/stringnode() " LINK "StringNode.guide/StringNode_stringnode"}
  217.                  @{" resourceTracker         " LINK "resourceTracker.guide/main"}
  218. @endnode
  219.  
  220. */
  221. PROC dirlist(resTracker=NIL:PTR TO resourceTracker) OF dirlist
  222.   self.stringnode(NIL, resTracker)
  223.   self.dirname:=NIL
  224.   self.doslock:=NIL
  225.   self.dosobj:=AllocDosObject(DOS_FIB,0)
  226.   IF self.dosobj = NIL THEN Raise(DLERR_INIT)   -> DirList Initialization Error
  227.   self.init:=TRUE
  228.  
  229.   self.adddir  := TRUE
  230.   self.allpath := FALSE
  231.  
  232.   self.resTracker:=resTracker
  233.   IF resTracker THEN resTracker.add(self, {dirlist_clean_proc})
  234. ENDPROC
  235. -> ///
  236.  
  237.  
  238. PROC end() OF dirlist
  239.   SUPER self.end()
  240.   IF self.doslock
  241.     UnLock(self.doslock)
  242.   ENDIF
  243.   IF self.dosobj
  244.     FreeDosObject(DOS_FIB, self.dosobj)
  245.   ENDIF
  246.   IF self.dirname
  247.     DisposeLink(self.dirname)
  248.   ENDIF
  249.  
  250.   IF self.resTracker THEN self.resTracker.remove(self)
  251. ENDPROC
  252.  
  253. PROC dirlist_clean_proc(dirl:PTR TO dirlist, info=NIL)
  254.   END dirl
  255. ENDPROC
  256.  
  257. -> /// setdir(txt:PTR TO CHAR) OF dirlist
  258. /*
  259. @node "setdir" "Amiga Foundation Classes Modules: DirList/setdir()"
  260.            NAME: setdir(path:PTR TO CHAR)
  261.  
  262.     DESCRIPTION: Use this command to select directory to scan.
  263.  
  264.           INPUT: path     - PTR TO CHAR. Directory name, FULL PATH!
  265.  
  266.         RESULTS: NONE.
  267.  
  268.        SEE ALSO: @{" dirname() " LINK "dirname"}
  269.                  @{" read()    " LINK "read"}
  270. @endnode
  271.  
  272. */
  273. PROC setdir(txt:PTR TO CHAR) OF dirlist
  274.   DEF mylock, c
  275.   
  276.   IF (mylock:=Lock(txt, ACCESS_READ)) = NIL THEN Raise(DLERR_LOCK)
  277.   self.doslock:=mylock
  278.   IF self.dirname
  279.     DisposeLink(self.dirname)
  280.     self.dirname:=NIL
  281.   ENDIF
  282.   self.dirname:=String(StrLen(txt)+1)
  283.  
  284.   c:=txt[StrLen(txt)-1]
  285.   StrCopy(self.dirname, txt)
  286.  
  287.   IF (c<>":") AND (c<>"/") THEN StrAdd(self.dirname, '/')
  288. ENDPROC
  289. -> ///
  290.  
  291. -> /// dirname() OF dirlist IS self.dirname
  292. /*
  293. @node dirname "Amiga Foundation Classes Modules: DirList/dirname()"
  294.            NAME: dirname()
  295.  
  296.     DESCRIPTION: This command returns the current dir name.
  297.  
  298.           INPUT: NONE.
  299.  
  300.         RESULTS: name       - A PTR TO CHAR containing current dir name.
  301.  
  302.        SEE ALSO: @{" setdir() " LINK "setdir"}
  303. @endnode
  304.  
  305. */
  306. PROC dirname() OF dirlist IS self.dirname
  307. -> ///
  308.  
  309. -> /// read(dirs=TRUE, files=TRUE, match=NIL:PTR TO CHAR) OF dirlist
  310. /*
  311. @node read "Amiga Foundation Classes Modules: DirList/read()"
  312.            NAME: read(dirs=TRUE, files=TRUE, match=NIL:PTR TO CHAR)
  313.  
  314.     DESCRIPTION: This command scans selected dir.
  315.  
  316.           INPUT: dirs       - (TRUE) If this flag is set to TRUE, DirList
  317.                               will memorize any directory it will encounter
  318.                               while scanning.
  319.  
  320.                  files      - (TRUE) Same as dirs flag, only with files.
  321.  
  322.                  match      - (NIL) This is a _VALID_ search pattern. This
  323.                               pattern will be CASE INSENSITIVE and it is a
  324.                               _STANDARD_ AmigaDOS pattern matching. (By the
  325.                               way, DirList *USES* AmigaDOS pattern matching,
  326.                               so it is FAST and you already should know how
  327.                               to use it!)
  328.  
  329.                               Ex. "#?sequ#?" is a valid pattern matching
  330.                                   string.
  331.  
  332.         RESULTS: NONE
  333.  
  334.        SEE ALSO: @{" setdir() " LINK "setdir"}
  335. @endnode
  336.  
  337. */
  338. PROC read(dirs=TRUE, files=TRUE, match=NIL:PTR TO CHAR) OF dirlist
  339.   DEF success=FALSE
  340.   DEF s
  341.   DEF pat=NIL:PTR TO CHAR
  342.  
  343.   IF match
  344.     pat :=String(StrLen(match)+2)
  345.     ParsePatternNoCase(match, pat, StrLen(match)+2)
  346.   ENDIF
  347.  
  348.   IF (s:=String(255)) = NIL THEN Raise(DLERR_NOMEMORY)
  349.   IF self.init=FALSE  THEN Raise(DLERR_NOINIT)
  350.   IF self.doslock=NIL THEN Raise(DLERR_NODIR)
  351.   IF (Examine(self.doslock, self.dosobj))=NIL THEN Raise(DLERR_EXAMINE)
  352.  
  353.   REPEAT
  354.     IF (success:=ExNext(self.doslock, self.dosobj))
  355.       StrCopy(s,'')
  356.       IF self.dosobj.direntrytype>0
  357.         IF dirs
  358.           IF self.adddir
  359.             StrCopy(s, '(DIR) ')
  360.           ENDIF
  361.           IF self.allpath THEN StrAdd(s, self.dirname)
  362.           IF pat
  363.             IF MatchPatternNoCase(pat, self.dosobj.filename)
  364.               StrAdd(s, self.dosobj.filename)
  365.               self.add(s)
  366.             ENDIF
  367.           ELSE
  368.             StrAdd(s, self.dosobj.filename)
  369.             self.add(s)
  370.           ENDIF
  371.         ENDIF
  372.       ELSE
  373.         IF files
  374.           IF self.allpath THEN StrCopy(s, self.dirname)
  375.           IF pat
  376.             IF MatchPatternNoCase(pat, self.dosobj.filename)
  377.               StrAdd(s, self.dosobj.filename)
  378.               self.add(s)
  379.             ENDIF
  380.           ELSE
  381.             StrAdd(s, self.dosobj.filename)
  382.             self.add(s)
  383.           ENDIF
  384.         ENDIF
  385.       ENDIF
  386.     ENDIF
  387.   UNTIL success=FALSE
  388.   DisposeLink(s)
  389. ENDPROC
  390. -> ///
  391.  
  392.  
  393.  
  394. -> /// setattrs(tags:PTR TO LONG) OF dirlist
  395. /*
  396. @node "setattrs" "Amiga Foundation Classes Modules: DirList/setattrs()"
  397.            NAME: setmode(tags:PTR TO LONG)
  398.  
  399.     DESCRIPTION: Use this command to change some of object's behaviours.
  400.                  This method will grow as time goes by. It is implemented
  401.                  in a very comfy way: taglists.
  402.  
  403.           INPUT: tags     - PTR TO LONG. Tags and their value.
  404.                             See @{" Tags Table " LINK "Tags"}
  405.  
  406.         RESULTS: NONE.
  407.  
  408.            NOTE: REMEMBER to ALWAYS end taglist with 0,0, otherwise
  409.                  strange things WILL happen.
  410.  
  411.                  You can pass to this method also StringNode's tags.
  412.                  They will be handled correctly.
  413.  
  414.        SEE ALSO: @{" dirname() " LINK "dirname"}
  415.                  @{" read()    " LINK "read"}
  416.  
  417.                  @{" StringNode/setattrs " LINK "StringNode.guide/StringNode_setattrs"}
  418. @endnode
  419.  
  420. */
  421. PROC setattrs(tags:PTR TO LONG) OF dirlist
  422.   DEF v:LONG, t:LONG
  423.  
  424.   WHILE (t:=Long(tags++))
  425.     v:=Long(tags++)
  426.     SELECT t
  427.       CASE DIRTAG_COMPLETEPATH
  428.         self.allpath :=v
  429.       CASE DIRTAG_MARKDIR
  430.         self.adddir  :=v
  431.       DEFAULT
  432.         SUPER self.setattrs([t,v,0,0])
  433.     ENDSELECT
  434.   ENDWHILE
  435. ENDPROC
  436. -> ///
  437.  
  438. -> /// version() OF dirlist IS DIRLIST_VERSION,DIRLIST_REVISION
  439. /*
  440. @node "version" "Amiga Foundation Classes Modules: DirList/version()"
  441.            NAME: version()
  442.  
  443.     DESCRIPTION: This method returns class version and revision.
  444.  
  445.           INPUT: NONE
  446.  
  447.         RESULTS: this method return TWO values: version and revision.
  448.  
  449.        SEE ALSO:
  450.  
  451. @endnode
  452.  
  453. */
  454. PROC version() OF dirlist IS DIRLIST_VERSION,DIRLIST_REVISION
  455. -> ///
  456.  
  457. #ifndef DO_DIRLIST_MOD
  458. PROC main()
  459.   DEF dl:PTR TO dirlist
  460.  
  461.   NEW dl.dirlist()
  462.  
  463.  
  464.   dl.setattrs([DIRTAG_COMPLETEPATH, TRUE,
  465.               DIRTAG_MARKDIR, TRUE,
  466.               0,0
  467.              ])
  468.  
  469.  
  470.  
  471.   dl.setdir('work:')
  472.   WriteF('Reading...\n')
  473.   dl.read(TRUE, TRUE)
  474.   WriteF('Sorting!\n')
  475.   dl.sort(TRUE)
  476.  
  477.   WriteF('Done!\n')
  478.  
  479.   IF dl.first()
  480.     REPEAT
  481.       WriteF('\s\n',dl.obj())
  482.     UNTIL dl.succ()=FALSE
  483.   ENDIF
  484.  
  485.   WriteF('DIR:\s\n', dl.dirname())
  486.  
  487.  
  488.   END dl
  489. ENDPROC
  490.  
  491.  
  492. #endif
  493.  
  494.  
  495.  
  496.