home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 17 / CD_ASCQ_17_101194.iso / dos / prg / vidal / c / view.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-25  |  13.5 KB  |  484 lines

  1. /*****************************************/
  2. /* interface texte pour acces au fichier */
  3. /*           ascii                       */
  4. /*       vidal charles 1/6/94            */
  5. /*      modele Large pour la compilation */
  6. /*****************************************/
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <dir.h>
  11. #include <dos.h>
  12. #include <conio.h>
  13. #include <stdlib.h>
  14. #include <alloc.h>
  15. #include <sys\stat.h>
  16. #include <io.h>
  17.  
  18.  
  19. /****************************************************/
  20.  
  21.  
  22. #define ScrSize    23     /* Nombre de lignes de texte par écran */
  23. #define TabSize    5      /* Cinq espaces par tabulation */
  24. #define EscKey     27     /* Codes clavier étendus */
  25. #define PgUp       73
  26. #define PgDn       81
  27. #define Home       71
  28. #define EndKey     79
  29. #define UpKey      72
  30. #define DnKey      80
  31. #define RiKey      77
  32. #define LeKey      75
  33. #define Alt_R      19     /* Pour la recherche */
  34. #define Screen     (*ScreenPtr)
  35. #define TabKey     9
  36. #define CR         10     /* Retour chariot (Entrée) */
  37. #define Enter      13
  38. #define F4         62
  39. #define F10        68
  40. #define NO_CODE         -1
  41. #define SCREENWIDE      320 /* dimensions de l'écran en mode 13 */
  42. #define SCREENDEEP      200
  43. #define STEP            32  /* taille d'un pas de déplacement */
  44. #define MaxBufSize 65520L /* Taille maximum : à peu près 64K */
  45. #define HOME            0x4700 /* codes de contrôle du curseur */
  46. #define CURSOR_UP       0x4800
  47. #define CURSOR_LEFT     0x4b00
  48. #define CURSOR_RIGHT    0x4d00
  49. #define END             0x4f00
  50. #define CURSOR_DOWN     0x5000
  51. #define BYTE unsigned char
  52. #define largest_code    4095            /* Plus grand code possible */
  53. #define table_size      5003            /* dimensions de la table */
  54.  
  55. /**********************************************/
  56.  
  57.  
  58. struct Texel                      // Structure utilisée pour
  59. { char Ch;                        // accéder directement à l'écran
  60.   unsigned char Attr;             // Attribut du caractère
  61. };
  62.  
  63. typedef struct Texel ScreenArea[25][80];
  64. ScreenArea far *ScreenPtr;
  65.  
  66. typedef struct liste{
  67.            char nom[14];
  68.            char rep_or_nom;
  69.            liste *suivant;
  70.            };
  71.  
  72. /**********************************************/
  73.  
  74. char dirc[100];
  75. int x=0,y=0,longeur_liste,no_liste=0,fin=1,fin2=1;
  76. BYTE code_buffer[259];
  77. int oldcode[table_size];        /* la table */
  78. int currentcode[table_size];
  79. BYTE newcode[table_size];
  80. int code_size;
  81. int clear_code;
  82. int eof_code;
  83. char far *BufPtr;char Ch,numero;
  84. int LinePtr[3000]; /* Index des débuts de lignes dans BufPtr */
  85. int Attr, I, Lc, Bot, End,end,flag;
  86. unsigned int NoBytes;
  87. FILE *InFile;
  88. int page=0;
  89.  
  90. /*************************/
  91. // affiche fichier texte
  92. /*************************/
  93. void DisplayFStat(char *fname, FILE *InFile, struct ffblk *ffblk)
  94.  
  95. /* Afficher la première ligne d'état contenant les informations sur le
  96.    fichier : nom, taille, date et heure */
  97. { int Col;
  98.   struct ftime ft;
  99.   char AtStr[8];
  100.  
  101.   textbackground(7);           /* Texte noir sur fond blanc */
  102.   textcolor(0);
  103.  
  104.   gotoxy(3,1);                 /* Afficher le nom du fichier */
  105.   printf("Fichier: %s",fname);
  106.   gotoxy(30,1);
  107. /*  getftime(fileno(InFile),&ft);
  108.   printf("Date: %02u-%02u-%04u",ft.ft_month,
  109.      ft.ft_day,ft.ft_year+1980);*/
  110.   gotoxy(48,1);                /* Afficher la taille */
  111.   printf("Taille: %ld",ffblk->ff_fsize);
  112.  
  113.   if (ffblk->ff_attrib == FA_RDONLY) /* Déterminer les attributs */
  114.     strcpy(AtStr,"R");
  115.   else
  116.     strcpy(AtStr,"R-W");
  117.   if (ffblk->ff_attrib == FA_HIDDEN) strcat(AtStr,"-H");
  118.   if (ffblk->ff_attrib == FA_SYSTEM) strcat(AtStr,"-S");
  119.   gotoxy(63,1);
  120.   printf("Attr: %s",AtStr);      /* Afficher les attributs */
  121.   for (Col=0; Col<80; Col++)   /* Put status bar in reverse video */
  122.      Screen[0][Col].Attr = 112;
  123. }
  124. void ShowScreenTexte(void)
  125. /* Affiche un écran contenant 13 lignes du fichier */
  126. { int Row, Col, TLc, I, Tp;
  127.  
  128.   TLc = Lc;       /* Commencer avec l'index de la ligne courante */
  129.   for (Row=0; Row<ScrSize && Row<=End; Row++)
  130.   { Tp = LinePtr[TLc];
  131.     for (Col=0; Col<80 && Tp<NoBytes && BufPtr[Tp] != CR; Tp++)
  132.     { if (BufPtr[Tp] == TabKey)     /* Recherche d'une tabulation */
  133.     for (I=0; I<TabSize && Col<80; I++)
  134.       /* Remplacer la tabulation par des espaces */
  135.       Screen[Row+1][Col++].Ch = ' ';
  136.       else Screen[Row+1][Col++].Ch = BufPtr[Tp];
  137.     }
  138.     for (; Col<80; Col++) Screen[Row+1][Col].Ch = ' ';
  139.     TLc++;
  140.   }
  141.   /* Remplir l'écran si le fichier n'a pas assez de lignes pour
  142.   garnir l'écran*/
  143.   for (; Row<ScrSize; Row++)
  144.     for (Col=0; Col<80; Col++)
  145.       Screen[Row+1][Col].Ch = ' ';
  146. }
  147. void ProcessInput2(char Ch)
  148. /* le flag permet de savoir si on est dans filereach ou non */
  149. /* Traiter les frappes au clavier */
  150. {char unite[80],*S,*U,SearchStr2[80],commande[80];
  151.  char Col;
  152.  switch (Ch)
  153.   { case PgUp:  if (Lc-ScrSize > 0) Lc -= ScrSize;
  154.         else Lc = 0;
  155.         ShowScreenTexte();
  156.         break;
  157.     case PgDn:  if (Lc+ScrSize < Bot && Bot >= ScrSize) Lc += ScrSize;
  158.         else Lc = Bot;
  159.         ShowScreenTexte();
  160.         break;
  161.     case UpKey: if (Lc > 0)
  162.         { Lc--;
  163.           ShowScreenTexte();
  164.         }
  165.         break;
  166.     case DnKey: if (Lc < Bot && Bot >= ScrSize)
  167.         { Lc++;
  168.           ShowScreenTexte();
  169.         }
  170.         break;
  171.     case Home:  Lc = 0;
  172.         ShowScreenTexte();
  173.         break;
  174.     case EndKey:if (Bot >= ScrSize)
  175.         { Lc = Bot;
  176.           ShowScreenTexte();
  177.         }
  178.         break;
  179.     case EscKey:fin2=0;end=0;break; /* Rien pour l'instant */
  180.   }
  181. }
  182. int Aff_texte(char *com)
  183. { struct ffblk ffblk;
  184.   char nombre[5];
  185.   int DosError;
  186.   fin2=1;
  187.   clrscr();
  188.   //ScreenPtr = SelectMonitor();
  189.     DosError = findfirst(com, &ffblk, 0);
  190.   if (DosError != 0)
  191.   { printf("Fichier introuvable \n");
  192.     getch();//exit(1);
  193.   }
  194.   if ((InFile=fopen(com,"r")) == NULL)
  195.   {   /* Ouverture du fichier */
  196.     printf("Ouverture du fichier %s impossible.\n", com);
  197.     getch();//exit(1);
  198.   }
  199.   /* Allouer la mémoire pour le buffer du fichier */
  200.   if (ffblk.ff_fsize >= MaxBufSize)
  201.   {  printf("Fichier trop grand : %s\n", com);
  202.     getch();//exit(1);
  203.   }
  204.   if ((BufPtr=(char far *) farmalloc(ffblk.ff_fsize)) == NULL)
  205.   { printf("Pas assez de mémoire\n");
  206.     getch();//exit(1);
  207.   }
  208.   DisplayFStat(com, InFile, &ffblk);
  209. //  DisplayCommands();
  210.   Lc = 0;
  211.   LinePtr[Lc++] = 0;      /* Initialiser le tableau des index de ligne */
  212.   NoBytes =read(fileno(InFile), BufPtr, ffblk.ff_fsize);
  213.   Lc = 0;
  214.   LinePtr[Lc++] = 0;      /* Initialiser le tableau des index de ligne */
  215.   for (I=0; I<NoBytes; I++)
  216.     if (BufPtr[I] == CR)
  217.       LinePtr[Lc++] = I + 1;
  218.  
  219.   fclose(InFile);
  220.   if (Lc > ScrSize)       /* Définir l'index pouvant afficher */
  221.     Bot = Lc - ScrSize;   /* le dernier écran entier du fichier */
  222.   else Bot = 0;
  223.   End = Lc - 1;           /* Sauvegarder l'index de la ligne de la fin */
  224.   Lc = 0;                 /* Définir l'index de la ligne du haut */
  225.   ShowScreenTexte();
  226.   do
  227.   {
  228.    ProcessInput2(getch()); /* Exécution ! */
  229.   } while (fin2);
  230.   flag=1;
  231.   farfree(BufPtr);
  232.   textbackground(0);      /* Restaurer l'écran */
  233.   textcolor(7);
  234.   clrscr();
  235.   return 0;
  236. }
  237.  
  238. /**************************/
  239. //  liste chainée
  240. /**************************/
  241.  
  242. void free_list(liste *l)
  243. { if (l==NULL) return;
  244.   free_list(l->suivant);free((liste *) l);}
  245.  
  246. void insert(liste *l,char nom[14],char rep_or_nom)
  247. { if (l->suivant==NULL)
  248.     {if ((l->suivant=(liste *) malloc(sizeof(liste)))==NULL) return;
  249.       strcpy(l->suivant->nom,nom);
  250.       l->suivant->rep_or_nom=rep_or_nom;
  251.       l->suivant->suivant=NULL;}
  252.     else insert(l->suivant,nom,rep_or_nom);}
  253.  
  254. liste *Page(liste *l,int offset)
  255. {liste *tempo;
  256.  int i;
  257. tempo=l;
  258.  for(i=0;i<offset;i++,tempo=tempo->suivant);
  259.  return(tempo);
  260. }
  261. /*void aff_list(liste *l)
  262. {if (l==NULL) return;
  263.     else {printf("%s\n",l->nom);
  264.      aff_list(l->suivant);}}*/
  265.  
  266. /*****************************/
  267. // affichage texte du directory
  268. /*****************************/
  269. int dir(liste *l,char dir2[90],char *filtre1,char *filtre2)
  270. /* filtre et insert le coutenu d'un repertoire*/
  271. {
  272.   int nombre=0;
  273.   char nomf[14],tempo[90];
  274.   struct ffblk f1;
  275.   strcpy(tempo,dir2);
  276.   strcat(tempo,"*.*");
  277.    if (findfirst(tempo,&f1,FA_DIREC)==0)
  278.     {if (strcmp(f1.ff_name,"."))
  279.            {
  280.            strcpy(nomf,f1.ff_name);
  281.            strncat(nomf,"           ",13-strlen(f1.ff_name));
  282.            insert(l,nomf,f1.ff_attrib);
  283.            nombre++;
  284.            }
  285.      while (findnext(&f1)==0)
  286.        { if ((!strcmp(strchr(f1.ff_name,'.'),filtre1) ||
  287.           !strcmp(strchr(f1.ff_name,'.'),filtre2))
  288.          || filtre1==NULL
  289.          || f1.ff_attrib==FA_DIREC)
  290.       {
  291.        strcpy(nomf,f1.ff_name);
  292.        strncat(nomf,"           ",13-strlen(f1.ff_name));
  293.        insert(l,nomf,f1.ff_attrib);
  294.        nombre++;
  295.        }
  296.        }
  297.     }
  298. return(nombre);
  299. }
  300.  
  301. ScreenArea far *SelectMonitor(void)
  302. /* Déterminer le type de carte graphique installée et renvoyer
  303.    adresse de la mémoire vidéo */
  304. { union REGS Regs;
  305.   unsigned Segment, Offset;
  306.  
  307.   Regs.h.ah = 15;
  308.   int86(0x10, &Regs, &Regs);
  309.   //if (Regs.h.al == 7)
  310.   //  Segment = 0xB000;       /* Monochrome */
  311.   /*else*/ Segment = 0xB800;    /* Graphique */
  312.   Offset = Regs.h.bh * (unsigned)0x1000;  /* Calculer la page vidéo */
  313.   return (ScreenArea *)(((long)Segment << 16) | (long)Offset);
  314. }
  315.  
  316.  
  317. void DisplayCommands(void)
  318. /* Afficher la barre de commande sur la dernière ligne de l'écran */
  319. { int Col;
  320.  
  321.   gotoxy(2,25);
  322.   printf(" <Esc=Quit>   ");
  323.   for (Col=0; Col<80; Col++) Screen[24][Col].Attr = 112;
  324. }
  325.  
  326. void ShowScreen(liste *l,int n)
  327. {
  328. static char x,y;
  329. if (l==NULL || n==100 ) return;
  330.  else {x=(n%5)*14+1;y=n/5+3;
  331.        gotoxy(x,y);
  332.        if (l->rep_or_nom==FA_DIREC) putchar('/');
  333.      else putchar(' ');
  334.        printf("%s",l->nom);
  335.        ShowScreen(l->suivant,n+1);
  336.       }
  337.  }
  338. void init_ecran(char *nom,int page)
  339. {char Col;
  340.   ScreenPtr = SelectMonitor();
  341.   textbackground(BLACK);           /* Texte noir sur fond blanc */
  342.   clrscr();
  343.   gotoxy(3,1);                 /* Afficher le nom du fichier */
  344.   printf("    %s    ",nom);
  345.   gotoxy(63,1);
  346.   printf("Page : %d ",page/100);
  347.    for (Col=0; Col<80; Col++) Screen[0][Col].Attr = 112;
  348.   DisplayCommands();
  349. }
  350. void aff_case_choisit(int n,char attr)
  351. {static char Col,x,y;
  352.  x=(n%5)*14;y=n/5+2;
  353. for (Col=x; Col<x+14; Col++) Screen[y][Col].Attr = attr;
  354. }
  355. char *element(liste *l,int no_liste,int n)
  356. {if (l==NULL || no_liste==n ) return(l->nom);
  357.  else element(l->suivant,no_liste,n+1);
  358. }
  359. char dir_or_nom(liste *l,int no_liste,int n)
  360. {if (l==NULL || no_liste==n ) return(l->rep_or_nom);
  361.  else dir_or_nom(l->suivant,no_liste,n+1);
  362. }
  363.  
  364. void changement_rep(liste *l)
  365. {
  366. free_list(l);
  367. strcpy(l->nom,".    ");
  368. l->suivant=NULL;
  369. l->rep_or_nom=FA_DIREC;
  370. longeur_liste=dir(l,dirc,NULL,".PCX");
  371. init_ecran(dirc,page);
  372. no_liste=0;
  373. ShowScreen(l,0);
  374. aff_case_choisit(no_liste,112);
  375. }
  376. /******************************/
  377. /****** handle Event **********/
  378. /******************************/
  379.  
  380. void ProcessInput(liste *l,char Ch)
  381. /* Traiter les frappes au clavier */
  382. {char dir[100],tempo[120];
  383.  char i;
  384.  FILE *fp;
  385.  switch (Ch)
  386.   { case UpKey: if (no_liste-5>=page)
  387.             {aff_case_choisit(no_liste-page,7);no_liste-=5;
  388.              aff_case_choisit(no_liste-page,112);}
  389.         break;
  390.     case DnKey: if (no_liste+5<=longeur_liste && no_liste+5<page+100)
  391.             {aff_case_choisit(no_liste-page,7);no_liste+=5;
  392.             aff_case_choisit(no_liste-page,112);}
  393.         break;
  394.     case LeKey: if (no_liste>page)
  395.           {aff_case_choisit(no_liste-page,7);no_liste--;
  396.           aff_case_choisit(no_liste-page,112);}
  397.         break;
  398.     case RiKey: if (no_liste<longeur_liste && no_liste+1<page+100)
  399.              {aff_case_choisit(no_liste-page,7);no_liste++;
  400.              aff_case_choisit(no_liste-page,112);}
  401.         break;
  402.     case PgUp:  if (no_liste>=page)
  403.         {page-=100;no_liste=page;
  404.         init_ecran(dirc,page);
  405.         ShowScreen(Page(l,page),0);
  406.         aff_case_choisit(no_liste-page,112);}
  407.         break;
  408.     case PgDn:  if (longeur_liste>page+100)
  409.         {page+=100;no_liste=page;
  410.         init_ecran(dirc,page);
  411.         ShowScreen(Page(l,page),0);
  412.         aff_case_choisit(no_liste-page,112);}
  413.  
  414.         break;
  415. /*    case Home:  aff_case_choisit(no_liste,7);
  416.         no_liste=0;
  417.         aff_case_choisit(no_liste,112);
  418.         break;
  419.  /*     case EndKey:
  420.         break;
  421.     case Alt_R:
  422.         break;*/
  423.     case Enter : if (dir_or_nom(l,no_liste,0)!=FA_DIREC)
  424.           {strcpy(tempo,dirc);
  425.            strcat(tempo,element(l,no_liste,0));
  426.           if((fp=fopen(tempo,"rb")) == NULL)
  427.            perror("Erreur à l'ouverture du fichier");
  428.            else {
  429.            if (strncmp(strchr(tempo,'.'),".EXE",strlen(".EXE")) &&
  430.                strncmp(strchr(tempo,'.'),".COM",strlen(".COM")))
  431.            {Aff_texte(tempo);
  432.            init_ecran(dirc,page);
  433.            ShowScreen(Page(l,page),0);
  434.            aff_case_choisit(no_liste,112);
  435.            }
  436.           }
  437.          break;}
  438.            else
  439.         {
  440.          strcpy(dir,element(l,no_liste,0));
  441.          *strchr(dir,' ')=0;
  442.          if (!strcmp(dir,".")) break;
  443.          aff_case_choisit(no_liste,7);
  444.          if (!strcmp(dir,".."))
  445.            {char tag=0;
  446.             for (i=strlen(dirc);i>0;i--)
  447.              {if (dirc[i]=='\\')
  448.               {if (tag==1)
  449.                {dirc[i+1]=0;break;}
  450.              else tag=1;
  451.                }
  452.              }
  453.              changement_rep(l);
  454.              break;
  455.             }
  456.            else {strcat(dirc,dir);
  457.              strcat(dirc,"\\");
  458.              changement_rep(l);
  459.              break;
  460.              }
  461.          }break;
  462.     case EscKey: fin=0;break; /* sortie */
  463.    }
  464. }
  465.  
  466. main()
  467. {
  468. liste l;
  469. strcpy(l.nom,".    ");
  470. l.suivant=NULL;
  471. l.rep_or_nom=FA_DIREC;
  472. getcwd(dirc,100);
  473. if (strlen(dirc)>3) strcat(dirc,"\\");
  474. init_ecran(dirc,page);
  475. longeur_liste=dir(&l,dirc,NULL,".PCX");
  476. ShowScreen(&l,0);
  477. aff_case_choisit(no_liste,112);
  478. while(fin)
  479.   {if (kbhit())
  480.     ProcessInput(&l,getch()); /* Exécution ! */
  481.   }
  482. free_list(&l);
  483. clrscr();
  484. }