READAF4DIR


Basé sur le tutorial de Scott Klement ou comment manipuler l'IFS


le but du jeux étant de :

  1. récupérer tous les fichiers de certains répertoires (lecture d'une directory) pour traiter tous ceux au format HTML : ce source
  2. de lire ensuite le contenu de chaque fichier par le pgm TRT_FILE afin de stocker en base de données la liste des mots composant cette page html

 

Nous utilisons cet utilitaire "maison", afin d'indexer tous les mots de toutes les pages composant nos cours pour proposer une recherche rapide par mot-clé.

 

Cette fonction d'indexation et de recherche était auparavant intégrée à DG1 (Apache sur System i), mais elle disparait en V7R1.

 




|

     H DFTACTGRP(*NO) ACTGRP(*NEW) BNDDIR('QC2LE') BNDDIR('IFSTEXT')

     D/copy IFSEBOOK/QRPGLESRC,IFSIO_H      D/copy IFSEBOOK/QRPGLESRC,ERRNO_H      D/copy IFSEBOOK/QRPGLESRC,IFSTEXT_H
     D trt_dir         PR            10I 0      D   curdir                    1024A   varying const
     D STDIN           C                   CONST(0)      D STDOUT          C                   CONST(1)      D STDERR          C                   CONST(2)
     D S_ISDIR         PR             1N      D   mode                        10U 0 value
     D curr            s           1024A      D curdir          s           1024A   varying      D line            S           1024A      D len             S             10I 0
     D cmd             PR                  ExtPgm('QCMDEXC')
    D  command                     200A   const      D  length                       15P 5 const
      /FREE

       trt_dir('/af4dir/courshtm');        trt_dir('/af4dir/AF4SRC');        trt_dir('/af4dir/AF4SRCT');        trt_dir('/af4dir/AF4TOOL');
       *inlr = *on;        return;
       //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++        //  prints all of the files in the directory to STDOUT and execute trtfile //        //  if a subdirectory is found, this procedure will call //        //  itself (recursively) to process that directory. //        //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++       /END-FREE      P trt_dir         B
     D trt_dir         PI            10I 0      D   curdir                    1024A   varying const
     D trt_file        PR                  EXTPGM('TRTFILE')      D   curfile                   1024A    const
     D mystat          S                   like(statds)      D dir             S               *      D line            S           1024A      D len             S             10I 0      D filename        S           1024A   varying      D fnwithdir       S           1024A   varying      D err             S             10I 0
      //******************************************       // open the current directory:       //******************************************       /FREE        dir = opendir(curdir);        if dir = *NULL;          err = errno;          line = 'opendir(): ' +

|
              %str(strerror(err)) +
             ', errno=' + %trim(%editc(err:'L'));
         len = %len(%trimr(line));
         writeline(STDERR: %addr(line): len);
         if err = EACCES;
           return 0;
         else;
           return -1;
         endif;
       endif;

       p_dirent = readdir(dir);
       dow p_dirent <> *NULL;
         filename = %subst(d_name:1:d_namelen);          fnwithdir = curdir + '/' + filename;
         if filename<>'.' and filename<>'..';
           if stat(fnwithdir: %addr(mystat))<0;              line = 'stat(): ' +
            %str(strerror(errno));              len = %len(%trimr(line));              writeline(STDERR: %addr(line): len); //log si lancé depuis QSH              return -1;            endif;

           p_statds = %addr(mystat);            if S_ISDIR(st_mode);              if trt_dir(fnwithdir) < 0;                return -1;              endif;            else;              len = %len(%trimr(filename));
// on ne traite que les fichiers html              if (len > 4 AND (                  %subst(filename : len -3 : 4) = '.htm'  OR                  %subst(filename : len -3 : 4) = '.HTM')                 ) OR                 (len > 5 AND (                  %subst(filename : len -4 : 5) = '.html' OR                  %subst(filename : len -4 : 5) = '.HTML')                 );
                 line = fnwithdir;                   trt_file(line); // indexation des mots de ce fichier
             ENDIF;            endif;
         endif;
         p_dirent = readdir(dir);        enddo;
       closedir(dir);
       return 0;       /END-FREE      P                 E

|
 
      //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++       //  This tests a file mode to see if a file is a directory.        //        // Here is the C code we're trying to duplicate:       //      #define _S_IFDIR    0040000                                       */       //      #define S_ISDIR(mode) (((mode) & 0370000) == _S_IFDIR)       //       // 1) ((mode) & 0370000) takes the file's mode and performs a       //      bitwise AND with the octal constant 0370000.  In binary,       //      that constant looks like: 00000000000000011111000000000000       //      The effect of this code is to turn off all bits in the       //      mode, except those marked with a '1' in the binary bitmask.       //       // 2) ((result of #1) == _S_IFDIR)  What this does is compare       //      the result of step 1, above with the _S_IFDIR, which       //      is defined to be the octal constant 0040000.  In decimal,       //      that octal constant is 16384.       //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++      P S_ISDIR         B      D S_ISDIR         PI             1N      D   mode                        10U 0 value
     D                 DS      D  dirmode                1      4U 0      D  byte1                  1      1A      D  byte2                  2      2A      D  byte3                  3      3A      D  byte4                  4      4A
     // Turn off bits in the mode, as in step (1) above.       /FREE        dirmode = mode;
      /END-FREE      c                   bitoff    x'FF'         byte1      c                   bitoff    x'FE'         byte2      c                   bitoff    x'0F'         byte3      c                   bitoff    x'FF'         byte4
      // Compare the result to 0040000, and return true or false.       /FREE        if dirmode = 16384;          return *On;        else;          return *Off;        endif;       /END-FREE      P                 E
      /DEFINE ERRNO_LOAD_PROCEDURE       /COPY IFSEBOOK/QRPGLESRC,ERRNO_H




©AF400