home *** CD-ROM | disk | FTP | other *** search
- /*****************************************/
- /* interface texte pour acces au fichier */
- /* ascii */
- /* vidal charles 1/6/94 */
- /* modele Large pour la compilation */
- /*****************************************/
-
- #include <stdio.h>
- #include <string.h>
- #include <dir.h>
- #include <dos.h>
- #include <conio.h>
- #include <stdlib.h>
- #include <alloc.h>
- #include <sys\stat.h>
- #include <io.h>
-
-
- /****************************************************/
-
-
- #define ScrSize 23 /* Nombre de lignes de texte par écran */
- #define TabSize 5 /* Cinq espaces par tabulation */
- #define EscKey 27 /* Codes clavier étendus */
- #define PgUp 73
- #define PgDn 81
- #define Home 71
- #define EndKey 79
- #define UpKey 72
- #define DnKey 80
- #define RiKey 77
- #define LeKey 75
- #define Alt_R 19 /* Pour la recherche */
- #define Screen (*ScreenPtr)
- #define TabKey 9
- #define CR 10 /* Retour chariot (Entrée) */
- #define Enter 13
- #define F4 62
- #define F10 68
- #define NO_CODE -1
- #define SCREENWIDE 320 /* dimensions de l'écran en mode 13 */
- #define SCREENDEEP 200
- #define STEP 32 /* taille d'un pas de déplacement */
- #define MaxBufSize 65520L /* Taille maximum : à peu près 64K */
- #define HOME 0x4700 /* codes de contrôle du curseur */
- #define CURSOR_UP 0x4800
- #define CURSOR_LEFT 0x4b00
- #define CURSOR_RIGHT 0x4d00
- #define END 0x4f00
- #define CURSOR_DOWN 0x5000
- #define BYTE unsigned char
- #define largest_code 4095 /* Plus grand code possible */
- #define table_size 5003 /* dimensions de la table */
-
- /**********************************************/
-
-
- struct Texel // Structure utilisée pour
- { char Ch; // accéder directement à l'écran
- unsigned char Attr; // Attribut du caractère
- };
-
- typedef struct Texel ScreenArea[25][80];
- ScreenArea far *ScreenPtr;
-
- typedef struct liste{
- char nom[14];
- char rep_or_nom;
- liste *suivant;
- };
-
- /**********************************************/
-
- char dirc[100];
- int x=0,y=0,longeur_liste,no_liste=0,fin=1,fin2=1;
- BYTE code_buffer[259];
- int oldcode[table_size]; /* la table */
- int currentcode[table_size];
- BYTE newcode[table_size];
- int code_size;
- int clear_code;
- int eof_code;
- char far *BufPtr;char Ch,numero;
- int LinePtr[3000]; /* Index des débuts de lignes dans BufPtr */
- int Attr, I, Lc, Bot, End,end,flag;
- unsigned int NoBytes;
- FILE *InFile;
- int page=0;
-
- /*************************/
- // affiche fichier texte
- /*************************/
- void DisplayFStat(char *fname, FILE *InFile, struct ffblk *ffblk)
-
- /* Afficher la première ligne d'état contenant les informations sur le
- fichier : nom, taille, date et heure */
- { int Col;
- struct ftime ft;
- char AtStr[8];
-
- textbackground(7); /* Texte noir sur fond blanc */
- textcolor(0);
-
- gotoxy(3,1); /* Afficher le nom du fichier */
- printf("Fichier: %s",fname);
- gotoxy(30,1);
- /* getftime(fileno(InFile),&ft);
- printf("Date: %02u-%02u-%04u",ft.ft_month,
- ft.ft_day,ft.ft_year+1980);*/
- gotoxy(48,1); /* Afficher la taille */
- printf("Taille: %ld",ffblk->ff_fsize);
-
- if (ffblk->ff_attrib == FA_RDONLY) /* Déterminer les attributs */
- strcpy(AtStr,"R");
- else
- strcpy(AtStr,"R-W");
- if (ffblk->ff_attrib == FA_HIDDEN) strcat(AtStr,"-H");
- if (ffblk->ff_attrib == FA_SYSTEM) strcat(AtStr,"-S");
- gotoxy(63,1);
- printf("Attr: %s",AtStr); /* Afficher les attributs */
- for (Col=0; Col<80; Col++) /* Put status bar in reverse video */
- Screen[0][Col].Attr = 112;
- }
- void ShowScreenTexte(void)
- /* Affiche un écran contenant 13 lignes du fichier */
- { int Row, Col, TLc, I, Tp;
-
- TLc = Lc; /* Commencer avec l'index de la ligne courante */
- for (Row=0; Row<ScrSize && Row<=End; Row++)
- { Tp = LinePtr[TLc];
- for (Col=0; Col<80 && Tp<NoBytes && BufPtr[Tp] != CR; Tp++)
- { if (BufPtr[Tp] == TabKey) /* Recherche d'une tabulation */
- for (I=0; I<TabSize && Col<80; I++)
- /* Remplacer la tabulation par des espaces */
- Screen[Row+1][Col++].Ch = ' ';
- else Screen[Row+1][Col++].Ch = BufPtr[Tp];
- }
- for (; Col<80; Col++) Screen[Row+1][Col].Ch = ' ';
- TLc++;
- }
- /* Remplir l'écran si le fichier n'a pas assez de lignes pour
- garnir l'écran*/
- for (; Row<ScrSize; Row++)
- for (Col=0; Col<80; Col++)
- Screen[Row+1][Col].Ch = ' ';
- }
- void ProcessInput2(char Ch)
- /* le flag permet de savoir si on est dans filereach ou non */
- /* Traiter les frappes au clavier */
- {char unite[80],*S,*U,SearchStr2[80],commande[80];
- char Col;
- switch (Ch)
- { case PgUp: if (Lc-ScrSize > 0) Lc -= ScrSize;
- else Lc = 0;
- ShowScreenTexte();
- break;
- case PgDn: if (Lc+ScrSize < Bot && Bot >= ScrSize) Lc += ScrSize;
- else Lc = Bot;
- ShowScreenTexte();
- break;
- case UpKey: if (Lc > 0)
- { Lc--;
- ShowScreenTexte();
- }
- break;
- case DnKey: if (Lc < Bot && Bot >= ScrSize)
- { Lc++;
- ShowScreenTexte();
- }
- break;
- case Home: Lc = 0;
- ShowScreenTexte();
- break;
- case EndKey:if (Bot >= ScrSize)
- { Lc = Bot;
- ShowScreenTexte();
- }
- break;
- case EscKey:fin2=0;end=0;break; /* Rien pour l'instant */
- }
- }
- int Aff_texte(char *com)
- { struct ffblk ffblk;
- char nombre[5];
- int DosError;
- fin2=1;
- clrscr();
- //ScreenPtr = SelectMonitor();
- DosError = findfirst(com, &ffblk, 0);
- if (DosError != 0)
- { printf("Fichier introuvable \n");
- getch();//exit(1);
- }
- if ((InFile=fopen(com,"r")) == NULL)
- { /* Ouverture du fichier */
- printf("Ouverture du fichier %s impossible.\n", com);
- getch();//exit(1);
- }
- /* Allouer la mémoire pour le buffer du fichier */
- if (ffblk.ff_fsize >= MaxBufSize)
- { printf("Fichier trop grand : %s\n", com);
- getch();//exit(1);
- }
- if ((BufPtr=(char far *) farmalloc(ffblk.ff_fsize)) == NULL)
- { printf("Pas assez de mémoire\n");
- getch();//exit(1);
- }
- DisplayFStat(com, InFile, &ffblk);
- // DisplayCommands();
- Lc = 0;
- LinePtr[Lc++] = 0; /* Initialiser le tableau des index de ligne */
- NoBytes =read(fileno(InFile), BufPtr, ffblk.ff_fsize);
- Lc = 0;
- LinePtr[Lc++] = 0; /* Initialiser le tableau des index de ligne */
- for (I=0; I<NoBytes; I++)
- if (BufPtr[I] == CR)
- LinePtr[Lc++] = I + 1;
-
- fclose(InFile);
- if (Lc > ScrSize) /* Définir l'index pouvant afficher */
- Bot = Lc - ScrSize; /* le dernier écran entier du fichier */
- else Bot = 0;
- End = Lc - 1; /* Sauvegarder l'index de la ligne de la fin */
- Lc = 0; /* Définir l'index de la ligne du haut */
- ShowScreenTexte();
- do
- {
- ProcessInput2(getch()); /* Exécution ! */
- } while (fin2);
- flag=1;
- farfree(BufPtr);
- textbackground(0); /* Restaurer l'écran */
- textcolor(7);
- clrscr();
- return 0;
- }
-
- /**************************/
- // liste chainée
- /**************************/
-
- void free_list(liste *l)
- { if (l==NULL) return;
- free_list(l->suivant);free((liste *) l);}
-
- void insert(liste *l,char nom[14],char rep_or_nom)
- { if (l->suivant==NULL)
- {if ((l->suivant=(liste *) malloc(sizeof(liste)))==NULL) return;
- strcpy(l->suivant->nom,nom);
- l->suivant->rep_or_nom=rep_or_nom;
- l->suivant->suivant=NULL;}
- else insert(l->suivant,nom,rep_or_nom);}
-
- liste *Page(liste *l,int offset)
- {liste *tempo;
- int i;
- tempo=l;
- for(i=0;i<offset;i++,tempo=tempo->suivant);
- return(tempo);
- }
- /*void aff_list(liste *l)
- {if (l==NULL) return;
- else {printf("%s\n",l->nom);
- aff_list(l->suivant);}}*/
-
- /*****************************/
- // affichage texte du directory
- /*****************************/
- int dir(liste *l,char dir2[90],char *filtre1,char *filtre2)
- /* filtre et insert le coutenu d'un repertoire*/
- {
- int nombre=0;
- char nomf[14],tempo[90];
- struct ffblk f1;
- strcpy(tempo,dir2);
- strcat(tempo,"*.*");
- if (findfirst(tempo,&f1,FA_DIREC)==0)
- {if (strcmp(f1.ff_name,"."))
- {
- strcpy(nomf,f1.ff_name);
- strncat(nomf," ",13-strlen(f1.ff_name));
- insert(l,nomf,f1.ff_attrib);
- nombre++;
- }
- while (findnext(&f1)==0)
- { if ((!strcmp(strchr(f1.ff_name,'.'),filtre1) ||
- !strcmp(strchr(f1.ff_name,'.'),filtre2))
- || filtre1==NULL
- || f1.ff_attrib==FA_DIREC)
- {
- strcpy(nomf,f1.ff_name);
- strncat(nomf," ",13-strlen(f1.ff_name));
- insert(l,nomf,f1.ff_attrib);
- nombre++;
- }
- }
- }
- return(nombre);
- }
-
- ScreenArea far *SelectMonitor(void)
- /* Déterminer le type de carte graphique installée et renvoyer
- adresse de la mémoire vidéo */
- { union REGS Regs;
- unsigned Segment, Offset;
-
- Regs.h.ah = 15;
- int86(0x10, &Regs, &Regs);
- //if (Regs.h.al == 7)
- // Segment = 0xB000; /* Monochrome */
- /*else*/ Segment = 0xB800; /* Graphique */
- Offset = Regs.h.bh * (unsigned)0x1000; /* Calculer la page vidéo */
- return (ScreenArea *)(((long)Segment << 16) | (long)Offset);
- }
-
-
- void DisplayCommands(void)
- /* Afficher la barre de commande sur la dernière ligne de l'écran */
- { int Col;
-
- gotoxy(2,25);
- printf(" <Esc=Quit> ");
- for (Col=0; Col<80; Col++) Screen[24][Col].Attr = 112;
- }
-
- void ShowScreen(liste *l,int n)
- {
- static char x,y;
- if (l==NULL || n==100 ) return;
- else {x=(n%5)*14+1;y=n/5+3;
- gotoxy(x,y);
- if (l->rep_or_nom==FA_DIREC) putchar('/');
- else putchar(' ');
- printf("%s",l->nom);
- ShowScreen(l->suivant,n+1);
- }
- }
- void init_ecran(char *nom,int page)
- {char Col;
- ScreenPtr = SelectMonitor();
- textbackground(BLACK); /* Texte noir sur fond blanc */
- clrscr();
- gotoxy(3,1); /* Afficher le nom du fichier */
- printf(" %s ",nom);
- gotoxy(63,1);
- printf("Page : %d ",page/100);
- for (Col=0; Col<80; Col++) Screen[0][Col].Attr = 112;
- DisplayCommands();
- }
- void aff_case_choisit(int n,char attr)
- {static char Col,x,y;
- x=(n%5)*14;y=n/5+2;
- for (Col=x; Col<x+14; Col++) Screen[y][Col].Attr = attr;
- }
- char *element(liste *l,int no_liste,int n)
- {if (l==NULL || no_liste==n ) return(l->nom);
- else element(l->suivant,no_liste,n+1);
- }
- char dir_or_nom(liste *l,int no_liste,int n)
- {if (l==NULL || no_liste==n ) return(l->rep_or_nom);
- else dir_or_nom(l->suivant,no_liste,n+1);
- }
-
- void changement_rep(liste *l)
- {
- free_list(l);
- strcpy(l->nom,". ");
- l->suivant=NULL;
- l->rep_or_nom=FA_DIREC;
- longeur_liste=dir(l,dirc,NULL,".PCX");
- init_ecran(dirc,page);
- no_liste=0;
- ShowScreen(l,0);
- aff_case_choisit(no_liste,112);
- }
- /******************************/
- /****** handle Event **********/
- /******************************/
-
- void ProcessInput(liste *l,char Ch)
- /* Traiter les frappes au clavier */
- {char dir[100],tempo[120];
- char i;
- FILE *fp;
- switch (Ch)
- { case UpKey: if (no_liste-5>=page)
- {aff_case_choisit(no_liste-page,7);no_liste-=5;
- aff_case_choisit(no_liste-page,112);}
- break;
- case DnKey: if (no_liste+5<=longeur_liste && no_liste+5<page+100)
- {aff_case_choisit(no_liste-page,7);no_liste+=5;
- aff_case_choisit(no_liste-page,112);}
- break;
- case LeKey: if (no_liste>page)
- {aff_case_choisit(no_liste-page,7);no_liste--;
- aff_case_choisit(no_liste-page,112);}
- break;
- case RiKey: if (no_liste<longeur_liste && no_liste+1<page+100)
- {aff_case_choisit(no_liste-page,7);no_liste++;
- aff_case_choisit(no_liste-page,112);}
- break;
- case PgUp: if (no_liste>=page)
- {page-=100;no_liste=page;
- init_ecran(dirc,page);
- ShowScreen(Page(l,page),0);
- aff_case_choisit(no_liste-page,112);}
- break;
- case PgDn: if (longeur_liste>page+100)
- {page+=100;no_liste=page;
- init_ecran(dirc,page);
- ShowScreen(Page(l,page),0);
- aff_case_choisit(no_liste-page,112);}
-
- break;
- /* case Home: aff_case_choisit(no_liste,7);
- no_liste=0;
- aff_case_choisit(no_liste,112);
- break;
- /* case EndKey:
- break;
- case Alt_R:
- break;*/
- case Enter : if (dir_or_nom(l,no_liste,0)!=FA_DIREC)
- {strcpy(tempo,dirc);
- strcat(tempo,element(l,no_liste,0));
- if((fp=fopen(tempo,"rb")) == NULL)
- perror("Erreur à l'ouverture du fichier");
- else {
- if (strncmp(strchr(tempo,'.'),".EXE",strlen(".EXE")) &&
- strncmp(strchr(tempo,'.'),".COM",strlen(".COM")))
- {Aff_texte(tempo);
- init_ecran(dirc,page);
- ShowScreen(Page(l,page),0);
- aff_case_choisit(no_liste,112);
- }
- }
- break;}
- else
- {
- strcpy(dir,element(l,no_liste,0));
- *strchr(dir,' ')=0;
- if (!strcmp(dir,".")) break;
- aff_case_choisit(no_liste,7);
- if (!strcmp(dir,".."))
- {char tag=0;
- for (i=strlen(dirc);i>0;i--)
- {if (dirc[i]=='\\')
- {if (tag==1)
- {dirc[i+1]=0;break;}
- else tag=1;
- }
- }
- changement_rep(l);
- break;
- }
- else {strcat(dirc,dir);
- strcat(dirc,"\\");
- changement_rep(l);
- break;
- }
- }break;
- case EscKey: fin=0;break; /* sortie */
- }
- }
-
- main()
- {
- liste l;
- strcpy(l.nom,". ");
- l.suivant=NULL;
- l.rep_or_nom=FA_DIREC;
- getcwd(dirc,100);
- if (strlen(dirc)>3) strcat(dirc,"\\");
- init_ecran(dirc,page);
- longeur_liste=dir(&l,dirc,NULL,".PCX");
- ShowScreen(&l,0);
- aff_case_choisit(no_liste,112);
- while(fin)
- {if (kbhit())
- ProcessInput(&l,getch()); /* Exécution ! */
- }
- free_list(&l);
- clrscr();
- }