#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "hashtable.h" extern int optind; extern char *optarg; typedef struct { char **array; int size; int allocSize; struct hashtable *h; } ARR; static unsigned int hashKey1(void* item) { unsigned int hash = 0; int c; char *p = (char *)item; while ( c = *p++ ) hash = ((hash << 5) + hash) + c; return hash; } static unsigned int hashKey2(void* item) { unsigned int hash = 0; int c; char *p = (char *)item; while ( c = *p++ ) hash = c + (hash << 6) + (hash << 16) - hash; return hash; } //#if sizeof(short)==2 //#define getint16(x) *((short *)x) //#else #define getint16(x) (((x)[0] << 8) | (x)[1]) //#endif static unsigned int hashKey3(void* item) { char *p = (char *)item; int len = strlen(item), tmp; unsigned int hash = len; int rem; if (len <0 || item NULL) return 0; rem = len & 3; len >>= 2; /* Main loop */ for (;len > 0; len--) { hash += (p[0] << 8) | p[1]; tmp = (((p[2] << 8) | p[3]) << 11) ^ hash; hash = (hash << 16) ^ tmp; p += 2; hash += hash >> 11; } /* Handle end cases */ switch (rem) { case 3: hash += (p[0] << 8) | p[1]; hash ^= hash << 16; hash ^= p[2] << 18; hash += hash >> 11; break; case 2: hash += (p[0] << 8) | p[1]; hash ^= hash << 11; hash += hash >> 17; break; case 1: hash += p[0]; hash ^= hash << 10; hash += hash >> 1; } /* Force "avalanching" of final 127 bits */ hash ^= hash << 3; hash += hash >> 5; hash ^= hash << 4; hash += hash >> 17; hash ^= hash << 25; hash += hash >> 6; return hash; } static unsigned int hashKey4(void* item) { char *p = (char *)item; const unsigned int m = 0x5bd1e995; const int r = 24; long seed = 0xc58f1a7b; int len = strlen(item), tmp; if ( len == 0 ) return 0; unsigned int hash = seed ^ len; int rem = len & 3; // mod 4 len >>= 2; // div 4 for ( ; len > 0; len-- ) { unsigned int k = *((short *)p); k *= m; k ^= k >> r; k *= m; hash *= m; hash ^= k; p += sizeof(short); } switch ( rem ) { case 3: hash ^= *((short *)p); hash ^= (*((short *)(p+2))) << 16; hash *= m; break; case 2: hash ^= *((short *)p); hash *= m; break; case 1: hash ^= *((short *)p); hash *= m; break; default: break; } // Do a few final mixes of the hash to ensure the last few // bytes are well-incorporated. hash ^= hash >> 13; hash *= m; hash ^= hash >> 15; return hash; } static int compareKey(void* k1, void* k2) { //return !strcmp(k1, k2); char *p1 k1, *p2 k2; while ( *p1 && *p1 == *p2 ) p1++, p2++; return *p1 == *p2 ? 1 : 0; } unsigned int (*hashKey)(void* item); void initArray(ARR* a, int sz, int hsz) { a->size = 0; a->allocSize = sz; a->array = (char **) malloc(sizeof(char *) * sz); a->h = create_hashtable(hsz, hashKey, compareKey); } void sortie(char *msg, int nret) { fprintf(stderr, msg); exit(nret); } long findOrAddArray(ARR *arr, char *item) { long * pi = (long *)hashtable_search(arr->h, item); if ( pi ) return *pi; long i = arr->size; int n = strlen(item); char * key = malloc(n + 1 + sizeof(long)); strcpy(key, item); pi = (long *)(&key[n + 1]); *pi = i; if ( arr->size == arr->allocSize ) { arr->allocSize *= 2; char ** p = (char **) malloc(sizeof(char *) * arr->allocSize); memcpy(p, arr->array, sizeof(char *) * arr->size); free(arr->array); arr->array = p; } arr->array[i] = key; arr->size++; hashtable_insert(arr->h, key, pi); return i; } void outBuf(FILE *fp, char *t, char **s, int n) { fputs(t, fp); while ( n-- > 0 ) { fputc(' ', fp); fputs(*s++, fp); } fputc('\n', fp); } static void usage(const char *name) { fprintf(stderr, "usage: %s [option]... Fichier_Entrée\n\n", name); fprintf(stderr, "options:\n"); fprintf(stderr, " -h, --help affiche ce message\n"); fprintf(stderr, " -v, --verbose version bavarde vers stderr\n\n"); } main(int argc, char *argv[]) { int opt; int verbose = 0; const char *prgName = argv[0]; hashKey = hashKey1; while ( (opt = getopt(argc, argv, "hk:v")) != EOF ) { switch ( opt ) { case 'v': verbose = 1; break; case 'k': switch ( atoi(optarg) ) { case 2: hashKey = hashKey2; break; case 3: hashKey = hashKey3; break; case 4: hashKey = hashKey4; break; default: hashKey = hashKey1; break; } break; case 'h': usage(prgName); exit(0); break; default: usage(prgName); exit(-1); break; } } argv += optind; argc -= optind; if ( argc <= 0 ) sortie("il manque le fichier à traiter.", 1); char *inFile = argv[0]; char *outFile = NULL; if ( argc > 1 ) outFile = argv[1]; /*************************************/ ARR gsm; initArray(&gsm, 500, 1000); long i, j; int fd; if ( !(fd = open(inFile,O_RDONLY)) ) sortie("erreur d'ouverture", 1); struct stat st; stat(inFile, &st); unsigned long fsz = st.st_size; if ( fsz == 0 ) sortie("la taille du fichier est nulle", 1); if ( verbose ) fprintf(stderr, "size = %d\n", fsz); char *bufFile = malloc(st.st_size); if ( !bufFile ) sortie("Erreur malloc bufFile\n", 2); /*************************************/ int nr; char *pbf, *p; int n = 1; long nl = 0; long ncur = 0; for (pbf = bufFile; (nr=read(fd, pbf, 4096)) > 0; pbf += nr ) { for (p=pbf; p n*fsz ) { fprintf(stderr, "lecture %d%%\r", n); fflush(stderr); n += 1; } } *pbf = 0; if ( pbf>bufFile && pbf[-1] ) nl++; close(fd); if ( verbose ) fprintf(stderr, "lecture 100%% NL=%d\r\n", nl); if ( nl == 0 ) sortie("Fichier vide\n", 4); /*************************************/ char **row = (char **)malloc(nl*sizeof(char *)); if ( !row ) sortie("Erreur malloc row\n", 2); long *vgsm = (long *)malloc(nl*sizeof(long)); if ( !vgsm ) sortie("Erreur malloc vgsm\n", 2); char **prow = row; long *pvgsm = vgsm; pbf = bufFile; while ( *pbf ) { *pvgsm = 0; *prow = NULL; i = strlen(pbf)+1; if ( p = strtok(pbf, " ") ) { *pvgsm = findOrAddArray(&gsm, p); if ( (p=strtok(NULL, " ")) && strtok(NULL, " ") ) *prow = p; // pointe sur tag } prow++; pvgsm++; pbf += i; } if ( gsm.size == 0 ) sortie("Pas de gsm\n", 3); /*************************************/ size_t szRes = gsm.size * sizeof(char *); char **res = (char **)malloc(szRes); if ( !res ) sortie("Erreur malloc res\n", 2); // ajouter ci-dessous : je recopierai res0 dans res pour éter une grosse boucle if ( !res0 ) sortie("Erreur malloc res0\n", 2); char *zero = "0"; // plus besoin d' if/else sur les pointeurs nuls for (i=0; i<gsm.size; i++) res0[i] = zero; FILE *fpo = stdout; if ( outFile ) fpo = fopen(outFile, "w"); if ( !fpo ) { fprintf(stderr, "%s: erreur d'ouverture, utilisation de stdout\n", outFile); fpo = stdout; } fprintf(fpo, "etiquette"); for (j=0; j<gsm.size; j++) fprintf(fpo, " %s", gsm.array[j]); fprintf(fpo, "\n"); char **irow; char *curTag; //tag en cours long *ivgsm = vgsm; char *prevTag = NULL; for (i=0, irow = row; i < nl && !(prevTag = *irow); i++, irow++) ; // memset(res, 0, gsm.size*sizeof(long)); // remise à 0 memcpy(res, res0, szRes); // remise à n = 1; int pfreq = strlen(prevTag)+1; for (i=0, irow = row; i < nl; i++, irow++, ivgsm++) { if ( curTag = *irow ) { if ( strcmp(curTag, prevTag) ) { fprintf(fpo, "%s", prevTag); for (j=0; j<gsm.size; j++) fprintf(fpo, " %d", res[j]); fprintf(fpo, "\n"); // Réinitialisation prevTag = curTag; pfreq = strlen(prevTag) + 1; //memset(res, 0, gsm.size*sizeof(long)); // remise à 0 memcpy(res, res0, szRes); // remise à } // res[*ivgsm] = atol(curTag + pfreq); res[*ivgsm] = curTag + pfreq; } if ( verbose && i*100.0 > n*nl ) { fprintf(stderr, "écriture %d%%\r", n); fflush(stderr); n += 1; } } if ( nl > 0 ) { /*fprintf(fpo, "%s", curTag); for (j=0; j<gsm.size; j++) fprintf(fpo, " %d", res[j]); fprintf(fpo, "\n"); */ outBuf(fpo, prevTag, res, gsm.size); } fclose(fpo); if ( verbose ) fprintf(stderr, "écriture 100%%\r\n"); /*************************************/ hashtable_destroy(gsm.h, 0); free(gsm.array); free(res); free(bufFile); exit(0); }
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "hashtable.h" extern int optind; extern char *optarg; typedef struct { char **array; int size; int allocSize; struct hashtable *h; } ARR; static unsigned int hashKey(void* item) { unsigned int hash = 0; int c; char *p = (char *)item; while ( c = *p++ ) { //hash = ((hash << 5) + hash) + c; hash = c + (hash << 6) + (hash << 16) - hash; } return hash; } static int compareKey(void* k1, void* k2) { //return !strcmp(k1, k2); char *p1 k1, *p2 k2; while ( *p1 && *p1 == *p2 ) p1++, p2++; return *p1 == *p2 ? 1 : 0; } void initArray(ARR* a, int sz, int hsz) { a->size = 0; a->allocSize = sz; a->array = (char **) malloc(sizeof(char *) * sz); a->h = create_hashtable(hsz, hashKey, compareKey); } void sortie(char *msg, int nret) { fprintf(stderr, msg); exit(nret); } long findOrAddArray(ARR *arr, char *item) { long * pi = (long *)hashtable_search(arr->h, item); if ( pi ) return *pi; long i = arr->size; int n = strlen(item); char * key = malloc(n + 1 + sizeof(long)); strcpy(key, item); pi = (long *)(&key[n + 1]); *pi = i; if ( arr->size == arr->allocSize ) { arr->allocSize *= 2; char ** p = (char **) malloc(sizeof(char *) * arr->allocSize); memcpy(p, arr->array, sizeof(char *) * arr->size); free(arr->array); arr->array = p; } arr->array[i] = key; arr->size++; hashtable_insert(arr->h, key, pi); return i; } void outBuf(FILE *fp, char *t, char **s, int n) { fputs(t, fp); while ( n-- > 0 ) { fputc(' ', fp); fputs(*s++, fp); } fputc('\n', fp); } static void usage(const char *name) { fprintf(stderr, "usage: %s [option]... Fichier_Entrée\n\n", name); fprintf(stderr, "options:\n"); fprintf(stderr, " -h, --help affiche ce message\n"); fprintf(stderr, " -v, --verbose version bavarde vers stderr\n\n"); } int main(int argc, char *argv[]) { int opt; int verbose = 0; const char *prgName = argv[0]; while ( (opt = getopt(argc, argv, "hv")) != EOF ) { switch ( opt ) { case 'v': verbose = 1; break; case 'h': usage(prgName); exit(0); break; default: usage(prgName); exit(-1); break; } } argv += optind; argc -= optind; if ( argc <= 0 ) sortie("il manque le fichier à traiter.", 1); char *inFile = argv[0]; char *outFile = NULL; if ( argc > 1 ) outFile = argv[1]; /*************************************/ ARR gsm; initArray(&gsm, 2000, 1000); long i; int fd; if ( !(fd = open(inFile,O_RDONLY)) ) sortie("erreur d'ouverture", 1); struct stat st; stat(inFile, &st); unsigned long fsz = st.st_size; if ( fsz == 0 ) sortie("la taille du fichier est nulle", 1); if ( verbose ) fprintf(stderr, "size = %d\n", fsz); char *bufFile = malloc(st.st_size); if ( !bufFile ) sortie("Erreur malloc bufFile\n", 2); /************************ Lecture ***************************/ time_t t0, t1, t2, t3; int nr; char *pbf, *p; int n = 1; long nl = 0; long ncur = 0; double cx = 100.0/fsz; time(&t0); for (pbf = bufFile; (nr=read(fd, pbf, 4096)) > 0; pbf += nr ) { for (p=pbf; p n ) { fprintf(stderr, "\rLecture %d%%", n); fflush(stderr); n = 1 + cx*ncur; } } *pbf = 0; if ( pbf>bufFile && pbf[-1] ) nl++; close(fd); time(&t1); if ( verbose ) fprintf(stderr, "\rLecture 100%%, NL=%d %ds\n", nl, t1-t0); if ( nl == 0 ) sortie("Fichier vide\n", 4); /************************ Traitement ***************************/ char **row = (char **)malloc(nl*sizeof(char *)); if ( !row ) sortie("Erreur malloc row\n", 2); long *vgsm = (long *)malloc(nl*sizeof(long)); if ( !vgsm ) sortie("Erreur malloc vgsm\n", 2); char **prow = row; long *pvgsm = vgsm; pbf = bufFile; while ( *pbf ) { *pvgsm = 0; *prow = NULL; i = strlen(pbf)+1; if ( p = strtok(pbf, " ") ) { *pvgsm = findOrAddArray(&gsm, p); if ( (p=strtok(NULL, " ")) && strtok(NULL, " ") ) *prow = p; // pointe sur tag } prow++; pvgsm++; pbf += i; } if ( gsm.size == 0 ) sortie("Pas de gsm\n", 3); time(&t2); /************************ Ecriture ***************************/ size_t szRes = gsm.size * sizeof(long); char **res = (char **)malloc(szRes); if ( !res ) sortie("Erreur malloc res\n", 2); char **res0 = (char **)malloc(szRes); if ( !res0 ) sortie("Erreur malloc res0\n", 2); char zero[] = "0"; for (i=0; i<gsm.size; i++) res0[i] = zero; memcpy(res, res0, szRes); // remise à 0 FILE *fpo = stdout; if ( outFile ) fpo = fopen(outFile, "w"); if ( !fpo ) { fprintf(stderr, "%s: erreur d'ouverture, utilisation de stdout\n", outFile); fpo = stdout; } outBuf(fpo, "etiquette", gsm.array, gsm.size); char **irow; char *curTag; //tag en cours long *ivgsm = vgsm; char *prevTag = NULL; for (i=0, irow = row; i < nl && !(prevTag = *irow); i++, irow++) ; n = 1; cx = 100.0/nl; int pfreq = strlen(prevTag)+1; for (i=0, irow = row; i < nl; i++, irow++, ivgsm++) { if ( curTag = *irow ) { if ( strcmp(curTag, prevTag) ) { outBuf(fpo, prevTag, res, gsm.size); // Réinitialisation prevTag = curTag; pfreq = strlen(prevTag) + 1; memcpy(res, res0, szRes); // remise à 0 } res[*ivgsm] = curTag + pfreq; } if ( verbose && cx*i > n ) { fprintf(stderr, "\rEcriture %d%%", n); fflush(stderr); n = 1 + i*cx; } } if ( nl > 0 ) { outBuf(fpo, prevTag, res, gsm.size); } time(&t3); if ( verbose ) fprintf(stderr, "\rEcriture 100%%, %ds\n", t3-t2); /************************ Terminé ***************************/ if ( verbose ) fprintf(stderr, "Temp total: %d'%02d\n", (t3-t0)/60, (t3-t0)%60); hashtable_destroy(gsm.h, 0); free(gsm.array); free(res); free(res0); free(bufFile); exit(0); }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionvoid outBuf(int fd, char *t, char **s, int n) { int nc = 0; char *buf = malloc(n*15); char *p = buf; while ( *p++ = *t++) nc++; while ( n-- > 0 ) { p[-1] = ' '; nc++; t = *s++; while ( *p++ = *t++) nc++; } p[-1] = '\n'; nc++; *p = 0; write(fd, buf, nc); free(buf); }
int main(int argc, char *argv[]) { int opt; int verbose = 0; const char *prgName = argv[0]; while ( (opt = getopt(argc, argv, "hv")) != EOF ) { switch ( opt ) { case 'v': verbose = 1; break; case 'h': usage(prgName); exit(0); break; default: usage(prgName); exit(-1); break; } } argv += optind; argc -= optind; if ( argc <= 0 ) sortie("il manque le fichier à traiter.", 1); char *inFile = argv[0]; char *outFile = NULL; if ( argc > 1 ) outFile = argv[1]; /*************************************/ ARR gsm; initArray(&gsm, 2000, 1000); long i; int fd; if ( !(fd = open(inFile,O_RDONLY)) ) sortie("erreur d'ouverture", 1); struct stat st; stat(inFile, &st); unsigned long fsz = st.st_size; if ( fsz == 0 ) sortie("la taille du fichier est nulle", 1); if ( verbose ) fprintf(stderr, "size = %d\n", fsz); char *bufFile = malloc(st.st_size); if ( !bufFile ) sortie("Erreur malloc bufFile\n", 2); /************************ Lecture ***************************/ time_t t0, t1, t2, t3; int nr; char *pbf, *p; int n = 1; long nl = 0; long ncur = 0; double cx = 100.0/fsz; time(&t0); for (pbf = bufFile; (nr=read(fd, pbf, 4096)) > 0; pbf += nr ) { for (p=pbf; p n ) { fprintf(stderr, "\rLecture %d%%", n); fflush(stderr); n = 1 + cx*ncur; } } *pbf = 0; if ( pbf>bufFile && pbf[-1] ) nl++; close(fd); time(&t1); if ( verbose ) fprintf(stderr, "\rLecture 100%%, NL=%d %ds\n", nl, t1-t0); if ( nl == 0 ) sortie("Fichier vide\n", 4); /************************ Traitement ***************************/ char **row = (char **)malloc(nl*sizeof(char *)); if ( !row ) sortie("Erreur malloc row\n", 2); long *vgsm = (long *)malloc(nl*sizeof(long)); if ( !vgsm ) sortie("Erreur malloc vgsm\n", 2); char **prow = row; long *pvgsm = vgsm; pbf = bufFile; while ( *pbf ) { *pvgsm = 0; *prow = NULL; i = strlen(pbf)+1; if ( p = strtok(pbf, " ") ) { *pvgsm = findOrAddArray(&gsm, p); if ( (p=strtok(NULL, " ")) && strtok(NULL, " ") ) *prow = p; // pointe sur tag } prow++; pvgsm++; pbf += i; } if ( gsm.size == 0 ) sortie("Pas de gsm\n", 3); time(&t2); if ( verbose ) fprintf(stderr, "\rTraitement 100%%, %ds\n", t2-t1); /************************ Ecriture ***************************/ size_t szRes = gsm.size * sizeof(long); char **res = (char **)malloc(szRes); if ( !res ) sortie("Erreur malloc res\n", 2); char **res0 = (char **)malloc(szRes); if ( !res0 ) sortie("Erreur malloc res0\n", 2); char zero[] = "0"; for (i=0; i<gsm.size; i++) res0[i] = zero; memcpy(res, res0, szRes); // remise à 0 int fdo = 1; if ( outFile ) fdo = open(outFile, O_RDONLY); if ( fdo < 0 ) { fprintf(stderr, "%s: erreur d'ouverture, utilisation de stdout\n", outFile); fdo = 1; } outBuf(fdo, "etiquette", gsm.array, gsm.size); char **irow; char *curTag; //tag en cours long *ivgsm = vgsm; char *prevTag = NULL; for (i=0, irow = row; i < nl && !(prevTag = *irow); i++, irow++) ; n = 1; cx = 100.0/nl; int pfreq = strlen(prevTag)+1; for (i=0, irow = row; i < nl; i++, irow++, ivgsm++) { if ( curTag = *irow ) { if ( strcmp(curTag, prevTag) ) { outBuf(fdo, prevTag, res, gsm.size); // Réinitialisation prevTag = curTag; pfreq = strlen(prevTag) + 1; memcpy(res, res0, szRes); // remise à 0 } res[*ivgsm] = curTag + pfreq; } if ( verbose && cx*i > n ) { fprintf(stderr, "\rEcriture %d%%", n); fflush(stderr); n = 1 + i*cx; } } if ( nl > 0 ) { outBuf(fdo, prevTag, res, gsm.size); } time(&t3); if ( verbose ) fprintf(stderr, "\rEcriture 100%%, %ds\n", t3-t2); /************************ Terminé ***************************/ if ( verbose ) fprintf(stderr, "Temp total: %d'%02d\n", (t3-t0)/60, (t3-t0)%60); hashtable_destroy(gsm.h, 0); free(gsm.array); free(res); free(res0); free(bufFile); exit(0); }
static unsigned int hashKey1(void* item) { unsigned int hash = 0; int c; char *p = (char *)item; p += 3; // <<<<<<<<<<<<<<<<<<<<< while ( c = *p++ ) hash = c + (hash << 6) + (hash << 16) - hash; return hash; }
fdo = open(outFile, O_WRONLY|O_CREAT|O_TRUNC);
long findOrAddArray(ARR *arr, char *item) { long * pi = (long *)hashtable_search(arr->h, item + 3); .... hashtable_insert(arr->h, key + 3, pi); return i; }
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "hashtable.h" extern int optind; extern char *optarg; typedef struct { char **array; int size; int allocSize; struct hashtable *h; } ARR; static unsigned int hashKey(void* item) { unsigned int hash = 0; int c; char *p = (char *)item; while ( c = *p++ ) { //hash = ((hash << 5) + hash) + c; hash = c + (hash << 6) + (hash << 16) - hash; } return hash; } static int compareKey(void* k1, void* k2) { //return !strcmp(k1, k2); char *p1 k1, *p2 k2; while ( *p1 && *p1 == *p2 ) p1++, p2++; return *p1 == *p2 ? 1 : 0; } void initArray(ARR* a, int sz, int hsz) { a->size = 0; a->allocSize = sz; a->array = (char **) malloc(sizeof(char *) * sz); a->h = create_hashtable(hsz, hashKey, compareKey); } void sortie(char *msg, int nret) { fprintf(stderr, msg); exit(nret); } long findOrAddArray(ARR *arr, char *item) { long * pi = (long *)hashtable_search(arr->h, item); if ( pi ) return *pi; long i = arr->size; int n = strlen(item); char * key = malloc(n + 1 + sizeof(long)); strcpy(key, item); pi = (long *)(&key[n + 1]); *pi = i; if ( arr->size == arr->allocSize ) { arr->allocSize *= 2; char ** p = (char **) malloc(sizeof(char *) * arr->allocSize); memcpy(p, arr->array, sizeof(char *) * arr->size); free(arr->array); arr->array = p; } arr->array[i] = key; arr->size++; hashtable_insert(arr->h, key, pi); return i; } void outBuf(int fd, char *t, char **s, int n) { int nc = 0; char *buf = malloc(n*15); char *p = buf; while ( *p++ = *t++) nc++; while ( n-- > 0 ) { p[-1] = ' '; nc++; t = *s++; while ( *p++ = *t++) nc++; } p[-1] = '\n'; nc++; *p = 0; write(fd, buf, nc); free(buf); } static void usage(const char *name) { fprintf(stderr, "usage: %s [option]... Fichier_Entrée\n\n", name); fprintf(stderr, "options:\n"); fprintf(stderr, " -h, --help affiche ce message\n"); fprintf(stderr, " -v, --verbose version bavarde vers stderr\n\n"); } int main(int argc, char *argv[]) { int opt; int verbose = 0; const char *prgName = argv[0]; while ( (opt = getopt(argc, argv, "hv")) != EOF ) { switch ( opt ) { case 'v': verbose = 1; break; case 'h': usage(prgName); exit(0); break; default: usage(prgName); exit(-1); break; } } argv += optind; argc -= optind; if ( argc <= 0 ) sortie("il manque le fichier àraiter.", 1); char *inFile = argv[0]; char *outFile = NULL; if ( argc > 1 ) outFile = argv[1]; /*************************************/ ARR gsm; initArray(&gsm, 2000, 1000); long i; int fd; if ( !(fd = open(inFile,O_RDONLY)) ) sortie("erreur d'ouverture", 1); struct stat st; stat(inFile, &st); unsigned long fsz = st.st_size; if ( fsz == 0 ) sortie("la taille du fichier est nulle", 1); if ( verbose ) fprintf(stderr, "size = %d\n", fsz); char *bufFile = malloc(st.st_size); if ( !bufFile ) sortie("Erreur malloc bufFile\n", 2); /************************ Lecture ***************************/ time_t t0, t1, t2, t3; int nr; char *pbf, *p; int n = 1; long nl = 0; long ncur = 0; double cx = 100.0/fsz; time(&t0); for (pbf = bufFile; (nr=read(fd, pbf, 4096)) > 0; pbf += nr ) { for (p=pbf; p n ) { fprintf(stderr, "\rLecture %d%%", n); fflush(stderr); n = 1 + cx*ncur; } } *pbf = 0; if ( pbf>bufFile && pbf[-1] ) nl++; close(fd); time(&t1); if ( verbose ) fprintf(stderr, "\rLecture 100%%, NL=%d %ds\n", nl, t1-t0); if ( nl == 0 ) sortie("Fichier vide\n", 4); /************************ Traitement ***************************/ char **row = (char **)malloc(nl*sizeof(char *)); if ( !row ) sortie("Erreur malloc row\n", 2); long *vgsm = (long *)malloc(nl*sizeof(long)); if ( !vgsm ) sortie("Erreur malloc vgsm\n", 2); char **prow = row; long *pvgsm = vgsm; pbf = bufFile; while ( *pbf ) { *pvgsm = 0; *prow = NULL; i = strlen(pbf)+1; if ( p = strtok(pbf, " ") ) { *pvgsm = findOrAddArray(&gsm, p); if ( (p=strtok(NULL, " ")) && strtok(NULL, " ") ) *prow = p; // pointe sur tag } prow++; pvgsm++; pbf += i; } if ( gsm.size == 0 ) sortie("Pas de gsm\n", 3); time(&t2); if ( verbose ) fprintf(stderr, "\rTraitement 100%%, %ds\n", t2-t1); /************************ Ecriture ***************************/ size_t szRes = gsm.size * sizeof(long); char **res = (char **)malloc(szRes); if ( !res ) sortie("Erreur malloc res\n", 2); char **res0 = (char **)malloc(szRes); if ( !res0 ) sortie("Erreur malloc res0\n", 2); char zero[] = "0"; for (i=0; i<gsm.size; i++) res0[i] = zero; memcpy(res, res0, szRes); // remise à int fdo = 1; if ( outFile ) fdo = open(outFile, O_RDONLY); if ( fdo < 0 ) { fprintf(stderr, "%s: erreur d'ouverture, utilisation de stdout\n", outFile); fdo = 1; } outBuf(fdo, "etiquette", gsm.array, gsm.size); char **irow; char *curTag; //tag en cours long *ivgsm = vgsm; char *prevTag = NULL; for (i=0, irow = row; i < nl && !(prevTag = *irow); i++, irow++) ; n = 1; cx = 100.0/nl; int pfreq = strlen(prevTag)+1; for (i=0, irow = row; i < nl; i++, irow++, ivgsm++) { if ( curTag = *irow ) { if ( strcmp(curTag, prevTag) ) { outBuf(fdo, prevTag, res, gsm.size); // Réitialisation prevTag = curTag; pfreq = strlen(prevTag) + 1; memcpy(res, res0, szRes); // remise à } res[*ivgsm] = curTag + pfreq; } if ( verbose && cx*i > n ) { fprintf(stderr, "\rEcriture %d%%", n); fflush(stderr); n = 1 + i*cx; } } if ( nl > 0 ) { outBuf(fdo, prevTag, res, gsm.size); } time(&t3); if ( verbose ) fprintf(stderr, "\rEcriture 100%%, %ds\n", t3-t2); /************************ Terminé**************************/ if ( verbose ) fprintf(stderr, "Temp total: %d'%02d\n", (t3-t0)/60, (t3-t0)%60); hashtable_destroy(gsm.h, 0); free(gsm.array); free(res); free(res0); free(bufFile); exit(0); }
int fdo = 1; if ( outFile ) // FAUX: fdo = open(outFile, O_RDONLY); fdo = open(outFile, O_WRONLY|O_CREAT|O_TRUNC); if ( fdo < 0 ) { fprintf(stderr, "%s: erreur d'ouverture, utilisation de stdout\n", outFile); fdo = 1; } outBuf(fdo, "etiquette", gsm.array, gsm.size);