Annotation of rpl/rpliconv/rpliconv.c, revision 1.1
1.1 ! bertrand 1: /*
! 2: ================================================================================
! 3: RPLiconv
! 4: ================================================================================
! 5: */
! 6:
! 7: #include <stdlib.h>
! 8: #include <stdarg.h>
! 9: #include <string.h>
! 10: #include <errno.h>
! 11: #include <stdio.h>
! 12:
! 13: #include "iconv.h"
! 14:
! 15: #define file FILE
! 16:
! 17: #define d_LONGUEUR 1024
! 18:
! 19: unsigned char *
! 20: reencodage(unsigned char *chaine_entree,
! 21: unsigned char *codage_entree,
! 22: unsigned char *codage_sortie)
! 23: {
! 24: iconv_t transcodage;
! 25:
! 26: size_t ios;
! 27: size_t longueur_entree;
! 28: size_t longueur_sortie;
! 29:
! 30: unsigned char *buffer_entree;
! 31: unsigned char *buffer_sortie;
! 32: unsigned char *chaine_sortie;
! 33: unsigned char *pointeur;
! 34: unsigned char *tampon;
! 35:
! 36: if ((transcodage = iconv_open(codage_sortie, codage_entree)) ==
! 37: (iconv_t) -1)
! 38: {
! 39: perror("iconv_open");
! 40: return(NULL);
! 41: }
! 42:
! 43: buffer_entree = chaine_entree;
! 44: longueur_entree = strlen(chaine_entree);
! 45:
! 46: if ((chaine_sortie = malloc(sizeof(unsigned char))) == NULL)
! 47: {
! 48: return(NULL);
! 49: }
! 50:
! 51: chaine_sortie[0] = 0;
! 52:
! 53: if ((buffer_sortie = malloc((d_LONGUEUR + 1) * sizeof(char))) == NULL)
! 54: {
! 55: return(NULL);
! 56: }
! 57:
! 58: do
! 59: {
! 60: longueur_sortie = d_LONGUEUR;
! 61: pointeur = buffer_sortie;
! 62:
! 63: if ((ios = iconv(transcodage, (char **) &buffer_entree,
! 64: &longueur_entree, (char **) &pointeur, &longueur_sortie))
! 65: == (size_t) -1)
! 66: {
! 67: // On autorise les erreurs EINVAL et EILSEQ
! 68: if (errno == EILSEQ)
! 69: {
! 70: perror("iconv");
! 71: free(buffer_sortie);
! 72: return(NULL);
! 73: }
! 74: }
! 75:
! 76: tampon = (unsigned char *) chaine_sortie;
! 77: (*pointeur) = 0;
! 78:
! 79: if ((chaine_sortie = malloc((strlen(tampon) + strlen(buffer_sortie) + 1)
! 80: * sizeof(unsigned char))) == NULL)
! 81: {
! 82: return(NULL);
! 83: }
! 84:
! 85: sprintf(chaine_sortie, "%s%s", tampon, buffer_sortie);
! 86: free(tampon);
! 87: } while((*buffer_entree) != 0);
! 88:
! 89: free(buffer_sortie);
! 90: iconv_close(transcodage);
! 91:
! 92: return(chaine_sortie);
! 93: }
! 94:
! 95: int
! 96: main(int argc, char *argv[])
! 97: {
! 98: file *f_source;
! 99:
! 100: int ios;
! 101:
! 102: unsigned char *encodage_destination;
! 103: unsigned char *encodage_source;
! 104: unsigned char *nom_fichier_source;
! 105: unsigned char option;
! 106: unsigned char *pointeur;
! 107: unsigned char *fichier_source;
! 108: unsigned char *fichier_destination;
! 109:
! 110: unsigned long i;
! 111: unsigned long nombre_caracteres;
! 112:
! 113: nom_fichier_source = NULL;
! 114: encodage_destination = NULL;
! 115: encodage_source = NULL;
! 116:
! 117: option = ' ';
! 118:
! 119: while((--argc) > 0)
! 120: {
! 121: if ((*(++argv))[0] == '-')
! 122: {
! 123: // Options
! 124:
! 125: while((option = *(++argv[0])) != 0)
! 126: {
! 127: switch(option)
! 128: {
! 129: case 'f' :
! 130: {
! 131: if (encodage_source != NULL)
! 132: {
! 133: fprintf(stderr, "More than one 'from' option\n");
! 134: return(-1);
! 135: }
! 136:
! 137: while(*(++argv[0]) == ' ');
! 138: argv[0]--;
! 139:
! 140: if ((*(++argv[0])) != 0)
! 141: {
! 142: encodage_source = argv[0];
! 143: }
! 144: else if ((--argc) > 0)
! 145: {
! 146: argv++;
! 147: encodage_source = argv[0];
! 148: }
! 149: else
! 150: {
! 151: fprintf(stderr, "-f option waits for argument\n");
! 152: return(-1);
! 153: }
! 154:
! 155: while(*(argv[0]) != 0)
! 156: {
! 157: argv[0]++;
! 158: }
! 159:
! 160: argv[0]--;
! 161:
! 162: break;
! 163: }
! 164:
! 165: case 't' :
! 166: {
! 167: if (encodage_destination != NULL)
! 168: {
! 169: fprintf(stderr, "More than one 'from' option\n");
! 170: return(-1);
! 171: }
! 172:
! 173: while(*(++argv[0]) == ' ');
! 174: argv[0]--;
! 175:
! 176: if ((*(++argv[0])) != 0)
! 177: {
! 178: encodage_destination = argv[0];
! 179: }
! 180: else if ((--argc) > 0)
! 181: {
! 182: argv++;
! 183: encodage_destination = argv[0];
! 184: }
! 185: else
! 186: {
! 187: fprintf(stderr, "-f option waits for argument\n");
! 188: return(-1);
! 189: }
! 190:
! 191: while(*(argv[0]) != 0)
! 192: {
! 193: argv[0]++;
! 194: }
! 195:
! 196: argv[0]--;
! 197:
! 198: break;
! 199: }
! 200:
! 201: default :
! 202: {
! 203: fprintf(stderr, "Unknown option\n");
! 204: return(-1);
! 205: }
! 206: }
! 207: }
! 208: }
! 209: else
! 210: {
! 211: // Nom du fichier à convertir
! 212:
! 213: if (nom_fichier_source != NULL)
! 214: {
! 215: fprintf(stderr, "More than one file\n");
! 216: return(-1);
! 217: }
! 218:
! 219: nom_fichier_source = argv[0];
! 220: }
! 221: }
! 222:
! 223: if (encodage_source == NULL)
! 224: {
! 225: fprintf(stderr, "Source encodage not found\n");
! 226: return(-1);
! 227: }
! 228:
! 229: if (encodage_destination == NULL)
! 230: {
! 231: fprintf(stderr, "Destination encodage not found\n");
! 232: return(-1);
! 233: }
! 234:
! 235: if (nom_fichier_source != NULL)
! 236: {
! 237: // Lecture à partir d'un fichier
! 238:
! 239: if ((f_source = fopen(nom_fichier_source, "r")) == NULL)
! 240: {
! 241: fprintf(stderr, "Source file not found\n");
! 242: return(-1);
! 243: }
! 244:
! 245: nombre_caracteres = 0;
! 246:
! 247: while(getc(f_source) != EOF)
! 248: {
! 249: nombre_caracteres++;
! 250: }
! 251:
! 252: if ((fichier_source = malloc((nombre_caracteres + 1) *
! 253: sizeof(unsigned char))) == NULL)
! 254: {
! 255: fprintf(stderr, "Not enough memory\n");
! 256: return(-1);
! 257: }
! 258:
! 259: rewind(f_source);
! 260: pointeur = fichier_source;
! 261:
! 262: for(i = 0; i < nombre_caracteres; i++)
! 263: {
! 264: (*pointeur) = getc(f_source);
! 265: pointeur++;
! 266: }
! 267:
! 268: (*pointeur) = 0;
! 269:
! 270: fclose(f_source);
! 271: }
! 272: else
! 273: {
! 274: // Lecture depuis stdin
! 275:
! 276: fichier_source = NULL;
! 277:
! 278: for(i = 1;; i++)
! 279: {
! 280: if ((fichier_source = realloc(fichier_source,
! 281: ((d_LONGUEUR * i) + 1) * sizeof(unsigned char))) == NULL)
! 282: {
! 283: fprintf(stderr, "Not enough memory\n");
! 284: return(-1);
! 285: }
! 286:
! 287: if (fread(&(fichier_source[d_LONGUEUR * (i - 1)]),
! 288: sizeof(unsigned char), d_LONGUEUR, stdin) < d_LONGUEUR)
! 289: {
! 290: break;
! 291: }
! 292: }
! 293: }
! 294:
! 295: if ((fichier_destination = reencodage(fichier_source, encodage_source,
! 296: encodage_destination)) == NULL)
! 297: {
! 298: free(fichier_source);
! 299: free(fichier_destination);
! 300:
! 301: fprintf(stderr, "Conversion error\n");
! 302: return(-1);
! 303: }
! 304:
! 305: free(fichier_source);
! 306:
! 307: while((ios = fprintf(stdout, "%s", fichier_destination)) < 0)
! 308: {
! 309: if ((errno != EINTR) && (errno != 0))
! 310: {
! 311: free(fichier_destination);
! 312: perror("fprintf(stdout, ...)");
! 313: return(-1);
! 314: }
! 315: }
! 316:
! 317: free(fichier_destination);
! 318:
! 319: return(0);
! 320: }
! 321:
! 322: // vim: ts=4
CVSweb interface <joel.bertrand@systella.fr>