All pastes #738818 Raw Edit

Untitled

public text v1 · immutable
·published 2007-10-16 17:50 UTC
rendered paste body
/* *
 * reader.c
 * Guillaume Coguiec <g.coguiec@gmail.com>
 * Created: Sat Oct 12, 2007 08:00AM
 * Last modified: Tue Oct 16, 2007 11:00AM
 */
 
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <unistd.h>
 
 #define CLEFS      "essai.txt"     // Fichier statique
 #define SIZE_B     1024            // Taille max du buffer
 #define SIZE_I     32              // Taille max de l'index
 #define SIZE_F     256             // Taille max du nom fichier

 #define TRUE       1
 #define FALSE      0
 #define DEBUG      0
 
 typedef int desc;

/* *
 * Structure Fichier 
 */
 typedef struct
 {
    char filename[SIZE_F];  // Nom du Fichier
    char buffer[SIZE_B];    // Buffer contenant le texte
    int index[SIZE_I];      // Index des lignes
    int nblignes;           // Nombre de Lignes
    desc handle;            // Ressource de l'ouverture du fichier
 } Fichier;

/* *
 * Fonction initFichier()
 */
 void initFichier(Fichier *fic)
 {
    fic->nblignes = 0;
    fic->index[0] = 0;
    return;
 }

/* *
 * Fonction strlenth
 */
 int strlenth(char *str)
 {
    int p = 0;
    while(str[p] != '\0') p++;
    return p;
 }
    
/* *
 * Fonction afficherPhrases
 */
 int afficherPhrases(Fichier *fic)
 {
    int i = 0, k = 0, j = 0;
   
    printf("-- Affichage du fichier par ligne:\n"); 
    if( fic->handle > 0 )
    {
        for(i = 0 ; i < sizeof(fic->buffer) ; i++)
        {
            if ( (fic->buffer[i]) == '\n' )
            { 
                fic->nblignes += 1;
                fic->index[ fic->nblignes ] = i;
            }
        }

        #if DEBUG
        printf("\n ## Nombre de lignes : %d \n", fic->nblignes);
        #endif
             
        for(k = 0 ; k < strlenth(fic->buffer) ; k++)
        {
            for(j = 0 ; j < (fic->nblignes) ; j++)
            { 
                if ( fic->index[j] == k )
                { 
                    if ( j == 0) printf(" %d: ", j+1);
                    else  if ( j>0 && j<9 ) printf("\n %d:", j+1);
                    else  printf("\n%d:", j+1);
                }   
                if ( (fic->buffer[k+1]) == '\n' )
                fic->buffer[k+1] = ' ';    
            }
            printf("%c", fic->buffer[k]);
        }
        printf("\n");
        return TRUE;
    }
    else return FALSE;
 }

/* *
 * Fonction obtenirPhrases
 */
 int obtenirPhrases(Fichier *fic)
 {
    if ( (fic->handle = open(CLEFS, O_RDONLY)) < 0 )
    {
        printf("! Erreur : Ouverture du fichier %s impossible !\n", CLEFS);
        switch(errno)
        {
            case EACCES : 
                printf("! Permission refuse ! \n");
                break;

            case ENOENT:
                printf("! Fichier introuvable ! \n");
                break;

            case EMFILE:
                printf("! Handle indisponible ! \n");
                break;
        }
        return FALSE;
    }
    else
    {
        printf("-- Ouverture du fichier %s \n", CLEFS);
        if ( read(fic->handle, fic->buffer, sizeof(fic->buffer) ) > 0 ) return TRUE;
        else return FALSE;   
    }    
 }

/* *
 * Fonction requetePhrase
 */
 int requetePhrase(Fichier *fic)
 {
    int choix, s = 0;
    printf("-- Entrer le N de la phrase  afficher :\n");
    scanf("%d", &choix);
    
    if ( (choix > (fic->nblignes)) || (choix <= 0) )
    {
        printf("! Choix impossible !\n");
        return FALSE;
    }
    else
    {
        printf("-- Affichage de la phrase %d :\n", choix);
        printf("%d:", choix);
        for(s = fic->index[choix-1] ; s <= fic->index[choix] ; s++)
        {
            printf("%c", fic->buffer[s]);
        }
        printf("\n");

    }
 }

/* *
 * Fonction fermFichier
 */
 int fermFichier(Fichier *fic)
 {
    if ( close(fic->handle) == 0 ) return TRUE;
    else return FALSE;
 }

/* *
 * Main
 */
 int main(int argc, char *argv[])
 {
    Fichier fichier;
    initFichier(&fichier);
    if ( obtenirPhrases(&fichier) )
    {
        if ( !fermFichier(&fichier) )
        return FALSE;
 
        if ( afficherPhrases(&fichier) )
        { 
          if ( requetePhrase(&fichier) ) 
          return TRUE;
        }  
    }
    return FALSE;
 }