first version of the reading cow
This commit is contained in:
134
src/C/reading_cow.c
Normal file
134
src/C/reading_cow.c
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#define MAX_TEXT 1024
|
||||||
|
|
||||||
|
void update() { printf("\033[H\033[J"); }
|
||||||
|
|
||||||
|
void gotoxy(int x, int y) { printf("\033[%d;%dH", y, x); }
|
||||||
|
|
||||||
|
void print_repeat(char c, int n) {
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
putchar(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void affiche_bulle(const char *texte) {
|
||||||
|
int len = strlen(texte);
|
||||||
|
|
||||||
|
putchar(' ');
|
||||||
|
print_repeat('_', len + 2);
|
||||||
|
putchar('\n');
|
||||||
|
|
||||||
|
printf("< %s >\n", texte);
|
||||||
|
|
||||||
|
putchar(' ');
|
||||||
|
print_repeat('-', len + 2);
|
||||||
|
putchar('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
void affiche_vache(char lettre_en_bouche) {
|
||||||
|
printf(" \\ ^__^\n");
|
||||||
|
|
||||||
|
if (lettre_en_bouche == '\0')
|
||||||
|
printf(" \\ (oo)\\_______\n");
|
||||||
|
else
|
||||||
|
printf(" \\ (%c%c)\\_______\n", lettre_en_bouche,
|
||||||
|
lettre_en_bouche);
|
||||||
|
|
||||||
|
printf(" (__)\\ )\\/\\\\\n");
|
||||||
|
printf(" ||----w |\n");
|
||||||
|
printf(" || ||\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void affiche_scene(const char *deja_lu, char lettre_en_bouche) {
|
||||||
|
update();
|
||||||
|
affiche_bulle(deja_lu);
|
||||||
|
affiche_vache(lettre_en_bouche);
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
|
|
||||||
|
void usage(const char *prog) {
|
||||||
|
fprintf(stderr, "Usage: %s [-i intervalle] fichier\n", prog);
|
||||||
|
fprintf(stderr, " ou: %s [-i intervalle] -\n", prog);
|
||||||
|
fprintf(stderr, "Le fichier '-' signifie lecture depuis stdin.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
double intervalle = 1.0;
|
||||||
|
char *nom_fichier = NULL;
|
||||||
|
FILE *f = NULL;
|
||||||
|
|
||||||
|
for (int i = 1; i < argc; i++) {
|
||||||
|
if (strcmp(argv[i], "-i") == 0) {
|
||||||
|
if (i + 1 >= argc) {
|
||||||
|
fprintf(stderr, "erreur : -i attend une valeur.\n");
|
||||||
|
usage(argv[0]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
intervalle = atof(argv[i + 1]);
|
||||||
|
if (intervalle < 0) {
|
||||||
|
fprintf(stderr, "erreur : l'intervalle doit etre >= 0.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
} else if (nom_fichier == NULL) {
|
||||||
|
nom_fichier = argv[i];
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "erreur : trop d'arguments.\n");
|
||||||
|
usage(argv[0]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nom_fichier == NULL) {
|
||||||
|
usage(argv[0]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strcmp(nom_fichier, "-") == 0) {
|
||||||
|
f = stdin;
|
||||||
|
} else {
|
||||||
|
f = fopen(nom_fichier, "r");
|
||||||
|
if (f == NULL) {
|
||||||
|
perror("erreur ouverture fichier");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char texte_lu[MAX_TEXT];
|
||||||
|
int taille = 0;
|
||||||
|
texte_lu[0] = '\0';
|
||||||
|
|
||||||
|
int c;
|
||||||
|
useconds_t pause_us =
|
||||||
|
(useconds_t)(intervalle * 1000000.0); // Car usleep est en ms.
|
||||||
|
|
||||||
|
while ((c = fgetc(f)) != EOF) {
|
||||||
|
if (taille >= MAX_TEXT - 1) {
|
||||||
|
fprintf(stderr, "erreur : texte trop long (max %d caracteres).\n",
|
||||||
|
MAX_TEXT - 1);
|
||||||
|
if (f != stdin)
|
||||||
|
fclose(f);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
affiche_scene(texte_lu, (char)c);
|
||||||
|
usleep(pause_us);
|
||||||
|
|
||||||
|
texte_lu[taille++] = (char)c;
|
||||||
|
texte_lu[taille] = '\0';
|
||||||
|
affiche_scene(texte_lu, '\0');
|
||||||
|
usleep(pause_us);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (f != stdin) {
|
||||||
|
fclose(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
gotoxy(1, 10);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user