Platon Technologies
not logged in Login Registration
EnglishSlovak
open source software development celebrating 10 years of open source development! Friday, October 16, 2026

Diff for libplaton/utils/strconv/strconv.c between version 1.2 and 1.3

version 1.2, 2002/12/04 18:48:33 version 1.3, 2003/12/05 18:47:07
Line 1 
Line 1 
 #include <stdio.h>  #include <stdio.h>
   #include <string.h>
   
 #include <platon/str/charsets.h>  #include <platon/str/charsets.h>
 #include <platon/str/strconv.h>  #include <platon/str/strconv.h>
   #include <platon/str/strplus.h>
   #include <platon/str/strctype.h>
   
 /* Compilation agains libplaton library:  /* Compilation agains libplaton library:
    gcc -pedantic -L ../../platon/ -I ../../ -o strconv strconv.c -lplaton     gcc -pedantic -L ../../platon/ -I ../../ -o strconv strconv.c -lplaton
Line 9 
Line 12 
   
 #define BUFSIZE 512  #define BUFSIZE 512
   
 int main(int argc, char **argv) {  struct charset_struct
   {
           char name[20];
           char *charset;
   };
   
   const struct charset_struct charsets[] = {
           {"ascii",                       charset_ascii           },
           {"plain",                       charset_ascii           },
           {"iso88592",            charset_iso88592        },
           {"iso8859-2",           charset_iso88592        },
           {"iso-8859-2",          charset_iso88592        },
           {"cp1250",                      charset_cp1250          },
           {"cp-1250",                     charset_cp1250          },
           {"windows1250",         charset_cp1250          },
           {"windows-1250",        charset_cp1250          },
           {"keybcs2",                     charset_keybcs2         },
           {"universum",           charset_universum       },
           {"koi8cz",                      charset_koi8cz          },
           {"pclatin1a",           charset_pclatin1a       },
           {"pclatin2",            charset_pclatin2        },
           {"*",                           NULL                            }
   };
   
   int main(int argc, char **argv)
   {
         FILE *f;          FILE *f;
         char buf[BUFSIZE];          char buf[BUFSIZE], *src_charset, *dest_charset;
         int i, count;          register int i, count;
   
         if (argc < 2) {          if (argc > 1 && ! strcmp(argv[1], "-l")) {
                 printf("Usage: %s <filename>\n", argv[0]);                  char *tmp_charset = NULL;
                 return 1;                  printf("List of available charsets and its aliases:\n");
                   for (i = 0; charsets[i].charset != NULL; i++) {
                           if (charsets[i].charset != tmp_charset) {
                                   printf("\n  ");
                                   tmp_charset = charsets[i].charset;
                           } else {
                                   printf(", ");
                           }
                           printf("%s", charsets[i].name);
                   }
                   printf("\n\n");
                   return 0;
         }          }
   
         if ((f = fopen(argv[1], "r+")) == NULL) {          if (argc < 4) {
                 printf("%s: Can't open file '%s'.\n", argv[0], argv[1]);                  fprintf(stderr, "Usage: %s [ -l ] <src_charset> <dest_charset> <filename>\n",
                                   argv[0]);
                 return 1;                  return 1;
         }          }
   
           /* Source charset initialization */
           src_charset = NULL;
           while (strchr(argv[1], '_') != NULL)
                   *(strchr(argv[1], '_')) = '-';
           argv[1] = strtolower(argv[1]);
           argv[1] = str_trim(argv[1]);
           for (i = 0; charsets[i].charset != NULL; i++) {
                   if (! strcmp(charsets[i].name, argv[1])) {
                           src_charset = charsets[i].charset;
                           break;
                   }
           }
           if (src_charset == NULL) {
                   fprintf(stderr, "%s: unknown source charset: %s\n", argv[1]);
                   return 2;
           }
   
           /* Destination charset initialization */
           dest_charset = NULL;
           while (strchr(argv[2], '_') != NULL)
                   *(strchr(argv[2], '_')) = '-';
           argv[2] = strtolower(argv[2]);
           argv[2] = str_trim(argv[2]);
           for (i = 0; charsets[i].charset != NULL; i++) {
                   if (! strcmp(charsets[i].name, argv[2])) {
                           dest_charset = charsets[i].charset;
                           break;
                   }
           }
           if (src_charset == NULL) {
                   fprintf(stderr, "%s: unknown destination charset: %s\n", argv[2]);
                   return 2;
           }
   
           if ((f = fopen(argv[3], "r+")) == NULL) {
                   fprintf(stderr, "%s: can't open file '%s'.\n", argv[0], argv[3]);
                   return 4;
           }
   
         i = -1;          i = -1;
         count = 0;          count = 0;
   
Line 32  int main(int argc, char **argv) {
Line 110  int main(int argc, char **argv) {
                 count = fread(buf, sizeof(char), BUFSIZE - 1, f);                  count = fread(buf, sizeof(char), BUFSIZE - 1, f);
                 buf[count] = '\0';                  buf[count] = '\0';
   
                 strconv(buf, charset_windows_1250, charset_iso_8859_2);                  strconv(buf, src_charset, dest_charset);
   
                 fseek(f, -count, SEEK_CUR);                  fseek(f, -count, SEEK_CUR);
   
                 if (count != fwrite(buf, sizeof(char), count, f)) {                  if (count != fwrite(buf, sizeof(char), count, f)) {
                         fclose(f);                          fclose(f);
                         printf("%s: Write error.\n", argv[0]);                          fprintf(stderr, "%s: write error\n", argv[0]);
                         return 1;                          return 128;
                 }                  }
   
                 i++;                  i++;
         } while (count == BUFSIZE -1);          } while (count == BUFSIZE - 1);
   
         fclose(f);          fclose(f);
   
         printf("%s: File '%s' with size %ld sucessfuly converted.\n",          printf("%s: file '%s' with size %ld successfully converted\n",
                                                         argv[0], argv[1],                                                          argv[0], argv[3],
                                                         (long) (i * (BUFSIZE - 1)) + (long) count);                                                          (long) (i * (BUFSIZE - 1)) + (long) count);
   
         return 0;          return 0;

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

Platon Group <platon@platon.org> http://platon.org/
Copyright © 2002-2006 Platon Group
Site powered by Metafox CMS
Go to Top