File:  [local] / rpl / rpliconv / rpliconv.c
Revision 1.4: download - view: text, annotated - select for diffs - revision graph
Mon May 24 16:29:27 2010 UTC (13 years, 11 months ago) by bertrand
Branches: MAIN
CVS tags: HEAD
Remplacement du code EOF par '\0'.

    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                     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: 
  111:     unsigned long           drapeau;
  112:     unsigned long           i;
  113:     unsigned long           j;
  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;
  280:         drapeau = 0;
  281: 
  282:         for(i = 1; drapeau == 0; i++)
  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: 
  291:             fflush(stdin);
  292: 
  293:             for(j = 0; (j < d_LONGUEUR) && (drapeau == 0); j++)
  294:             {
  295:                 if ((fichier_source[(d_LONGUEUR * (i - 1)) + j] = getc(stdin))
  296:                         == (unsigned char) EOF)
  297:                 {
  298:                     fichier_source[(d_LONGUEUR * (i - 1)) + j] = 0;
  299:                     drapeau = 1;
  300:                 }
  301:             }
  302:         }
  303:     }
  304: 
  305:     if ((fichier_destination = reencodage(fichier_source, encodage_source,
  306:             encodage_destination)) == NULL)
  307:     {
  308:         free(fichier_source);
  309:         free(fichier_destination);
  310: 
  311:         fprintf(stderr, "Conversion error\n");
  312:         return(-1);
  313:     }
  314: 
  315:     free(fichier_source);
  316: 
  317:     while((ios = fprintf(stdout, "%s", fichier_destination)) < 0)
  318:     {
  319:         if ((errno != EINTR) && (errno != 0))
  320:         {
  321:             free(fichier_destination);
  322:             perror("fprintf(stdout, ...)");
  323:             return(-1);
  324:         }
  325:     }
  326: 
  327:     free(fichier_destination);
  328: 
  329:     return(0);
  330: }
  331: 
  332: // vim: ts=4

CVSweb interface <joel.bertrand@systella.fr>