1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char *identifiant;
char *sequence;
} proteine;
typedef struct {
proteine *content;
int size;
int alloc_size;
} table_prot;
table_prot *create_table() {
table_prot *table = malloc(sizeof(table_prot));
#define INITIAL_SIZE 10
table->content = (proteine *)malloc(INITIAL_SIZE*sizeof(proteine));
table->size = 0;
table->alloc_size = INITIAL_SIZE;
return table;
}
void ajout_proteine( table_prot *table, proteine prot ) {
// Check enough space
if(table->size == table->alloc_size) {
// Augmente la taille allouée
table->alloc_size += INITIAL_SIZE;
table->content=realloc(table->content,(table->alloc_size)*sizeof(proteine));
}
// Initialise le nouvel élement
table->content[table->size]=prot;
// Enregistre le fait qu'on ait un de plus
table->size++;
}
// Récupère le ième élement
proteine *get_proteine(table_prot *table, int i) {
if(i<table->size) {
fprintf(stderr,"Error, out of bound : %d\n",i);
exit(-1);
}
return &(table->content[i]);
}
table_prot *read_prot( char *filename ) {
FILE *fp = fopen(filename,"r");
if(!fp) {
perror("impossible de lire le fichier");
exit(-1);
}
// Crée une table
table_prot *table = create_table();
proteine current_prot;
current_prot.identifiant = NULL;
current_prot.sequence = NULL;
#define BUF_SIZE 65000
char buffer[BUF_SIZE];
while(buffer == fgets(buffer,BUF_SIZE,fp)) {
#define ID_PATTERN "> identifiant "
if(strncmp(buffer,ID_PATTERN,strlen(ID_PATTERN)) == 0) {
// Cette ligne est un identifiant
if(current_prot.identifiant!=NULL) {
fprintf(stderr,"Erreur, proteine non terminé !\n");
exit(-1);
}
// Nouvelle proteine avec un nouvel identifiant
current_prot.identifiant = malloc(strlen(buffer)-strlen(ID_PATTERN));
current_prot.sequence = NULL;
strcpy(current_prot.identifiant, buffer+strlen(ID_PATTERN));
} else {
// On devrait avoir lu un identifiant !
if(current_prot.identifiant==NULL) {
fprintf(stderr,"Erreur, identifiant manquant !\n");
exit(-1);
}
// Taille initiale = 0
current_prot.sequence = malloc(1);
current_prot.sequence[0] = '\0';
// Cette ligne est une (partie de) sequence
char *c = buffer;
// Cherche la fin de la chaine
while(*c!='*' && *c !='\0') {
while(*c!='\n' && *c!='*' && *c !='\0') {
c++;
}
// Longueur de la sequence (sur la ligne en cours)
int length = c-buffer;
// Gère la mémoire
current_prot.sequence = realloc(current_prot.sequence,strlen(current_prot.sequence)+length+1);
// Copy la séquence
strncat(current_prot.sequence,buffer,length);
// Si fin de ligne, passe à la suivante
if(*c!='\n') {
fgets(buffer,BUF_SIZE,fp); // FIXME check error
c = buffer;
}
}
if(*c=='\0') {
fprintf(stderr,"Erreur tampon trop petit");
}
// Fin de séquence, on l'ajoute à la table
ajout_proteine( table, current_prot );
// Réinitialise
current_prot.identifiant = NULL;
current_prot.sequence = NULL;
}
}
return table;
}
int main() {
table_prot *table = read_prot("blabla.txt");
return 0;
} |
Partager