File:  [local] / rpl / rpliconv / rpliconv.c
Revision 1.7: download - view: text, annotated - select for diffs - revision graph
Fri Aug 6 15:32:55 2010 UTC (13 years, 8 months ago) by bertrand
Branches: MAIN
CVS tags: rpl-4_1_9, rpl-4_1_8, rpl-4_1_7, rpl-4_1_6, rpl-4_1_5, rpl-4_1_4, rpl-4_1_35, rpl-4_1_34, rpl-4_1_33, rpl-4_1_32, rpl-4_1_31, rpl-4_1_30, rpl-4_1_3, rpl-4_1_29, rpl-4_1_28, rpl-4_1_27, rpl-4_1_26, rpl-4_1_25, rpl-4_1_24, rpl-4_1_23, rpl-4_1_22, rpl-4_1_21, rpl-4_1_20, rpl-4_1_2, rpl-4_1_19, rpl-4_1_18, rpl-4_1_17, rpl-4_1_16, rpl-4_1_15, rpl-4_1_14, rpl-4_1_13, rpl-4_1_12, rpl-4_1_11, rpl-4_1_10, rpl-4_1_1, rpl-4_1_0, rpl-4_0_24, rpl-4_0_22, rpl-4_0_21, rpl-4_0_20, rpl-4_0_19, rpl-4_0_18, rpl-4_0, HEAD
Cohérence

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

CVSweb interface <joel.bertrand@systella.fr>