Annotation of rpl/rpliconv/rpliconv.c, revision 1.5

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;
1.3       bertrand   61:        longueur_entree = strlen(buffer_entree);
1.1       bertrand   62:        pointeur = buffer_sortie;
                     63: 
                     64:        if ((ios = iconv(transcodage, (char **) &buffer_entree,
                     65:                &longueur_entree, (char **) &pointeur, &longueur_sortie))
                     66:                == (size_t) -1)
                     67:        {
1.3       bertrand   68:            // On autorise les erreurs EINVAL et E2BIG
1.1       bertrand   69:            if (errno == EILSEQ)
                     70:            {
                     71:                perror("iconv");
                     72:                free(buffer_sortie);
                     73:                return(NULL);
                     74:            }
                     75:        }
                     76: 
                     77:        tampon = (unsigned char *) chaine_sortie;
                     78:        (*pointeur) = 0;
                     79: 
                     80:        if ((chaine_sortie = malloc((strlen(tampon) + strlen(buffer_sortie) + 1)
                     81:                * sizeof(unsigned char))) == NULL)
                     82:        {
                     83:            return(NULL);
                     84:        }
                     85: 
                     86:        sprintf(chaine_sortie, "%s%s", tampon, buffer_sortie);
                     87:        free(tampon);
                     88:    } while((*buffer_entree) != 0);
                     89: 
                     90:    free(buffer_sortie);
                     91:    iconv_close(transcodage);
                     92: 
                     93:    return(chaine_sortie);
                     94: }
                     95: 
                     96: int
                     97: main(int argc, char *argv[])
                     98: {
                     99:    file                    *f_source;
                    100: 
                    101:    int                     ios;
                    102: 
                    103:    unsigned char           *encodage_destination;
                    104:    unsigned char           *encodage_source;
                    105:    unsigned char           *nom_fichier_source;
                    106:    unsigned char           option;
                    107:    unsigned char           *pointeur;
                    108:    unsigned char           *fichier_source;
                    109:    unsigned char           *fichier_destination;
                    110: 
1.3       bertrand  111:    unsigned long           drapeau;
1.1       bertrand  112:    unsigned long           i;
1.3       bertrand  113:    unsigned long           j;
1.1       bertrand  114:    unsigned long           nombre_caracteres;
                    115: 
                    116:    nom_fichier_source = NULL;
                    117:    encodage_destination = NULL;
                    118:    encodage_source = NULL;
                    119: 
                    120:    option = ' ';
                    121: 
                    122:    while((--argc) > 0)
                    123:    {
                    124:        if ((*(++argv))[0] == '-')
                    125:        {
                    126:            // Options
                    127: 
                    128:            while((option = *(++argv[0])) != 0)
                    129:            {
                    130:                switch(option)
                    131:                {
                    132:                    case 'f' :
                    133:                    {
                    134:                        if (encodage_source != NULL)
                    135:                        {
                    136:                            fprintf(stderr, "More than one 'from' option\n");
                    137:                            return(-1);
                    138:                        }
                    139: 
                    140:                        while(*(++argv[0]) == ' ');
                    141:                        argv[0]--;
                    142: 
                    143:                        if ((*(++argv[0])) != 0)
                    144:                        {
                    145:                            encodage_source = argv[0];
                    146:                        }
                    147:                        else if ((--argc) > 0)
                    148:                        {
                    149:                            argv++;
                    150:                            encodage_source = argv[0];
                    151:                        }
                    152:                        else
                    153:                        {
                    154:                            fprintf(stderr, "-f option waits for argument\n");
                    155:                            return(-1);
                    156:                        }
                    157: 
                    158:                        while(*(argv[0]) != 0)
                    159:                        {
                    160:                            argv[0]++;
                    161:                        }
                    162: 
                    163:                        argv[0]--;
                    164: 
                    165:                        break;
                    166:                    }
                    167: 
                    168:                    case 't' :
                    169:                    {
                    170:                        if (encodage_destination != NULL)
                    171:                        {
                    172:                            fprintf(stderr, "More than one 'from' option\n");
                    173:                            return(-1);
                    174:                        }
                    175: 
                    176:                        while(*(++argv[0]) == ' ');
                    177:                        argv[0]--;
                    178: 
                    179:                        if ((*(++argv[0])) != 0)
                    180:                        {
                    181:                            encodage_destination = argv[0];
                    182:                        }
                    183:                        else if ((--argc) > 0)
                    184:                        {
                    185:                            argv++;
                    186:                            encodage_destination = argv[0];
                    187:                        }
                    188:                        else
                    189:                        {
                    190:                            fprintf(stderr, "-f option waits for argument\n");
                    191:                            return(-1);
                    192:                        }
                    193: 
                    194:                        while(*(argv[0]) != 0)
                    195:                        {
                    196:                            argv[0]++;
                    197:                        }
                    198: 
                    199:                        argv[0]--;
                    200: 
                    201:                        break;
                    202:                    }
                    203: 
                    204:                    default :
                    205:                    {
                    206:                        fprintf(stderr, "Unknown option\n");
                    207:                        return(-1);
                    208:                    }
                    209:                }
                    210:            }
                    211:        }
                    212:        else
                    213:        {
                    214:            // Nom du fichier à convertir
                    215: 
                    216:            if (nom_fichier_source != NULL)
                    217:            {
                    218:                fprintf(stderr, "More than one file\n");
                    219:                return(-1);
                    220:            }
                    221: 
                    222:            nom_fichier_source = argv[0];
                    223:        }
                    224:    }
                    225: 
                    226:    if (encodage_source == NULL)
                    227:    {
                    228:        fprintf(stderr, "Source encodage not found\n");
                    229:        return(-1);
                    230:    }
                    231: 
                    232:    if (encodage_destination == NULL)
                    233:    {
                    234:        fprintf(stderr, "Destination encodage not found\n");
                    235:        return(-1);
                    236:    }
                    237: 
                    238:    if (nom_fichier_source != NULL)
                    239:    {
                    240:        // Lecture à partir d'un fichier
                    241: 
                    242:        if ((f_source = fopen(nom_fichier_source, "r")) == NULL)
                    243:        {
                    244:            fprintf(stderr, "Source file not found\n");
                    245:            return(-1);
                    246:        }
                    247: 
                    248:        nombre_caracteres = 0;
                    249: 
                    250:        while(getc(f_source) != EOF)
                    251:        {
                    252:            nombre_caracteres++;
                    253:        }
                    254: 
                    255:        if ((fichier_source = malloc((nombre_caracteres + 1) *
                    256:                sizeof(unsigned char))) == NULL)
                    257:        {
                    258:            fprintf(stderr, "Not enough memory\n");
                    259:            return(-1);
                    260:        }
                    261: 
                    262:        rewind(f_source);
                    263:        pointeur = fichier_source;
                    264: 
                    265:        for(i = 0; i < nombre_caracteres; i++)
                    266:        {
                    267:            (*pointeur) = getc(f_source);
                    268:            pointeur++;
                    269:        }
                    270: 
                    271:        (*pointeur) = 0;
                    272: 
                    273:        fclose(f_source);
                    274:    }
                    275:    else
                    276:    {
                    277:        // Lecture depuis stdin
                    278: 
                    279:        fichier_source = NULL;
1.3       bertrand  280:        drapeau = 0;
1.1       bertrand  281: 
1.3       bertrand  282:        for(i = 1; drapeau == 0; i++)
1.1       bertrand  283:        {
                    284:            if ((fichier_source = realloc(fichier_source,
                    285:                    ((d_LONGUEUR * i) + 1) * sizeof(unsigned char))) == NULL)
                    286:            {
                    287:                fprintf(stderr, "Not enough memory\n");
                    288:                return(-1);
                    289:            }
                    290: 
1.3       bertrand  291:            for(j = 0; (j < d_LONGUEUR) && (drapeau == 0); j++)
1.1       bertrand  292:            {
1.3       bertrand  293:                if ((fichier_source[(d_LONGUEUR * (i - 1)) + j] = getc(stdin))
                    294:                        == (unsigned char) EOF)
                    295:                {
1.4       bertrand  296:                    fichier_source[(d_LONGUEUR * (i - 1)) + j] = 0;
1.3       bertrand  297:                    drapeau = 1;
                    298:                }
1.1       bertrand  299:            }
                    300:        }
                    301:    }
                    302: 
                    303:    if ((fichier_destination = reencodage(fichier_source, encodage_source,
                    304:            encodage_destination)) == NULL)
                    305:    {
                    306:        free(fichier_source);
                    307:        free(fichier_destination);
                    308: 
                    309:        fprintf(stderr, "Conversion error\n");
                    310:        return(-1);
                    311:    }
                    312: 
                    313:    free(fichier_source);
                    314: 
                    315:    while((ios = fprintf(stdout, "%s", fichier_destination)) < 0)
                    316:    {
                    317:        if ((errno != EINTR) && (errno != 0))
                    318:        {
                    319:            free(fichier_destination);
                    320:            perror("fprintf(stdout, ...)");
                    321:            return(-1);
                    322:        }
                    323:    }
                    324: 
                    325:    free(fichier_destination);
                    326: 
                    327:    return(0);
                    328: }
                    329: 
                    330: // vim: ts=4

CVSweb interface <joel.bertrand@systella.fr>