home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 January / Pcwk0198.iso / Zadarmo / Convert / SRC / CPL.C next >
C/C++ Source or Header  |  1996-01-31  |  9KB  |  302 lines

  1. /*
  2.                                ConvPL by kravietZ
  3.                                       v2.01
  4.  
  5.           Uniwersalny konwerter standardow polskich znakow, freeware.
  6.  
  7.           Kontakt: Pawel Krawczyk <kravietz@pipeta.chemia.pk.edu.pl>
  8.                                   <2:486/18.4@fidonet>
  9.  
  10.           Najnowsza wersja:
  11.           ftp://pipeta.chemia.pk.edu.pl/pub/convpl
  12.  
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <fcntl.h>
  19.  
  20. #ifdef __MSDOS__
  21. #include <io.h>
  22. #include <sys\stat.h>
  23. #include <dos.h>
  24. #pragma warn -pia
  25. #pragma warn -sus
  26. #undef __GNUC__     /* DJGPP #definiuje obydwie zmienne */
  27. #endif
  28.  
  29. #ifdef __GNUC__
  30. #include <sys/file.h>
  31. #include <sys/stat.h>
  32. #define O_BINARY
  33. #endif
  34.  
  35. #include "polish.h"
  36. #include "disph.h"
  37. #define BUF_SIZE (20*1024)  /* bufor 20K */
  38. #define TEMP_FILE "tempxx.cpl"
  39. #define TO_CRLF 1
  40. #define TO_LF 2
  41.  
  42. typedef unsigned char ubyte;
  43.  
  44. static char ibuf[BUF_SIZE], obuf[BUF_SIZE];
  45. static int ibuf_ptr = 0, obuf_ptr = 0, ibuf_lim = 0;
  46. static int f_in,f_out,opt_mode=0;
  47.  
  48. short int ver_lo=1;
  49. short int ver_hi=2;
  50.  
  51. #ifdef __MSDOS__ /* to do przyszlych zastosowan... */
  52. const char *f_ext_check[14],*bin_ext_lst=".exe .com .gif .jpg";
  53. #endif
  54.  
  55. int indexx(ubyte ch, ubyte *ctab)
  56. /*
  57.     zwraca indeks znaku ch w stringu okreslonym przez wskaznik ctab
  58.     lub -1 jesli nie znajdzie znaku; indeksy zaczynaja sie od 0;
  59. */
  60. { int poss=0;
  61. while((ch != *(ctab+poss) && (*(ctab+poss) != '\0'))) poss++;
  62. if(*(ctab+poss) == '\0') return(-1);
  63.  else
  64. return(poss);
  65. }; /* postab */
  66.  
  67. ubyte getxx (void)
  68. {
  69.   if (ibuf_ptr >= ibuf_lim) {               /* odebrane wszystkie? */
  70.     ibuf_lim = read(f_in, ibuf, BUF_SIZE);  /* czytaj z pliku */
  71.     if (ibuf_lim == 0) return -1;           /* nie ma wiæcej w pliku */
  72.     ibuf_ptr = 0;                           /* inicjuj wskaªnik znaków */
  73.   }
  74.   return ibuf[ibuf_ptr++];                  /* daj znak, zwiæksz wskaªnik */
  75. }
  76.  
  77. void putxx (ubyte ch)
  78. {
  79.   if (obuf_ptr >= BUF_SIZE) {                /* bufor peÆny? */
  80.     write(f_out, obuf, obuf_ptr);           /* zapisz bufor do pliku */
  81.     obuf_ptr = 0;                           /* inicjuj wskaªnik znaków */
  82.   }
  83.   obuf[obuf_ptr++] = ch;                    /* znak do bufora, zwiæksz wsk. */
  84. }
  85.  
  86. int ConvFile(char *cf_fname,char *cf_fnameo,char st1,char st2)
  87. /*  
  88.     czyta z nazwy cf_fname
  89.     zapisuje do nazwy cf_fnameo
  90.     st1 = std zrodlowy,
  91.     st2 = docelowy
  92.     parametr z zewnatrz: opt_mode (konwersja koncow linii)
  93. */
  94. {
  95.     ubyte *in_tab, *out_tab;
  96.     int ps1;
  97.     char ch1;
  98.     long flen;
  99.  
  100. #ifndef NO_LINE_CNT
  101.     long int n_line=0;  /* numer linii */
  102. #endif
  103.  
  104. switch(st1)     /* najpierw kontrola poprawnosci standardow */
  105.     {
  106. /*  drobny trick umozliwiajacy przekodowanie u*x <-> MSDOS bez
  107.     zmiany standardu polskich liter; szczegoly w cpl.txt */
  108.  
  109.     case '0': in_tab=p_Asc; st2='0'; break;
  110.     case '1': in_tab=p_Maz; break;
  111.     case '2': in_tab=p_FMaz; break;
  112.     case '3': in_tab=p_IBML; break;
  113.     case '4': in_tab=p_ISOL; break;
  114.     case '5': in_tab=p_DHN; break;
  115.     case '6': in_tab=p_CSK; break;
  116.     case '7': in_tab=p_Cyf; break;
  117.     case '8': in_tab=p_IEA; break;
  118.     case '9': in_tab=p_Log; break;
  119.     case 'a': in_tab=p_Mic; break;
  120.     case 'b': in_tab=p_Ami; break;
  121.     case 'c': in_tab=p_CPJ; break;
  122.     case 'd': in_tab=p_FAT; break;
  123.     case 'e': in_tab=p_Win3; break;
  124.     case 'f': in_tab=p_XJP; break;
  125.     case 'g': in_tab=p_Ven; break;
  126.     case 'h': in_tab=p_Mac; break;
  127.     case 'i': in_tab=p_TeX; break;
  128.     case 'j': in_tab=p_Cor; break;
  129.     default: fprintf(stderr,"\7\b\b\b\b\bnieznany standard wejsciowy \"%c\".\n",st1); return(255);
  130.     } /* switch st1 */
  131.  
  132. switch(st2)
  133.     {
  134.     case '0': out_tab=p_Asc; break;
  135.     case '1': out_tab=p_Maz; break;
  136.     case '2': out_tab=p_FMaz; break;
  137.     case '3': out_tab=p_IBML; break;
  138.     case '4': out_tab=p_ISOL; break;
  139.     case '5': out_tab=p_DHN; break;
  140.     case '6': out_tab=p_CSK; break;
  141.     case '7': out_tab=p_Cyf; break;
  142.     case '8': out_tab=p_IEA; break;
  143.     case '9': out_tab=p_Log; break;
  144.     case 'a': out_tab=p_Mic; break;
  145.     case 'b': out_tab=p_Ami; break;
  146.     case 'c': out_tab=p_CPJ; break;
  147.     case 'd': out_tab=p_FAT; break;
  148.     case 'e': out_tab=p_Win3; break;
  149.     case 'f': out_tab=p_XJP; break;
  150.     case 'g': out_tab=p_Ven; break;
  151.     case 'h': out_tab=p_Mac; break;
  152.     case 'i': out_tab=p_TeX; break;
  153.     case 'j': out_tab=p_Cor; break;
  154.     default: fprintf(stderr,"\7\b\b\b\b\bnieznany standard wyjsciowy \"%c\".\n",st2); return(255);
  155.     } /* switch st2 */
  156.  
  157.     fprintf(stderr,"\n%s:      ",cf_fname);
  158.  
  159.     f_in=open(cf_fname,O_RDONLY|O_BINARY);
  160.  
  161.     if( (f_in != -1) || !strcmp(cf_fname,"stdin") )
  162.     {
  163.  
  164.     f_out=open(cf_fnameo,O_CREAT|O_TRUNC|O_RDWR
  165.              |O_BINARY|S_IFREG|S_IREAD|S_IWRITE);
  166.  
  167.     if( (f_out != -1) || !strcmp(cf_fnameo,"stdout") )
  168.     {
  169.  
  170. if(!strcmp(cf_fname,"stdin"))
  171.     {
  172.     fprintf(stderr,"\n");
  173.         while((ch1=fgetc(stdin)) != EOF) switch(opt_mode)
  174.         {
  175. case TO_CRLF: { if(ch1==10) { fputc(13,stdout); fputc(10,stdout); } else { if( (ps1=indexx(ch1,in_tab)) != -1 ) fputc(*(out_tab+ps1),stdout); else fputc(ch1,stdout); }; break; }
  176. case TO_LF: { if(ch1==13) { break; } else {if( (ps1=indexx(ch1,in_tab)) != -1 ) fputc(*(out_tab+ps1),stdout); else fputc(ch1,stdout); }; break; }
  177. default: { if( (ps1=indexx(ch1,in_tab)) != -1 ) fputc(*(out_tab+ps1),stdout); else fputc(ch1,stdout); break; }
  178.          } /* switch */
  179.     return(0);
  180.     } /* if(stdin) */
  181.  
  182. while((ch1=getxx()) != -1)
  183.     {   /* opt_mode = 0 jesli nie crlf, 1,2 jesli konw. crlf */
  184.     switch(opt_mode) {
  185. case TO_CRLF: { if(ch1==10) { putxx(13); putxx(10);
  186.       #ifndef NO_LINE_CNT
  187.     n_line++; fprintf(stderr,"\b\b\b\b\b%-5u",n_line);
  188.       #endif
  189.      } else { if( (ps1=indexx(ch1,in_tab)) != -1 ) putxx(*(out_tab+ps1)); else putxx(ch1); }; break; }
  190. case TO_LF: { if(ch1==13) {
  191.       #ifndef NO_LINE_CNT
  192.         n_line++; fprintf(stderr,"\b\b\b\b\b%-5u",n_line);
  193.       #endif
  194.        break; } else { if( (ps1=indexx(ch1,in_tab)) != -1 ) putxx(*(out_tab+ps1)); else putxx(ch1); }; break; }
  195. default: { if( (ps1=indexx(ch1,in_tab)) != -1 ) putxx(*(out_tab+ps1)); else putxx(ch1);
  196.  
  197.       #ifndef NO_LINE_CNT
  198.         n_line++; fprintf(stderr,"\b\b\b\b\b%-5u",n_line);
  199.       #endif
  200.  
  201.       break; }
  202.     } /* switch */
  203. }; /* while */
  204.  
  205. /* zrzucamy bufor do pliku */
  206.  
  207.     if (obuf_ptr < BUF_SIZE) { write(f_out, obuf, obuf_ptr); }
  208.  
  209.        close(f_out);
  210.      } /* open(f_out) */
  211.      else { perror(cf_fnameo); exit(1); }
  212.  
  213.        close(f_in);
  214.        fprintf(stderr,"ok.\n");
  215.      } /* open(f_in) */
  216. else { perror(""); exit(1); }
  217.  
  218. return(0);
  219.  
  220. } /* convfile */
  221.  
  222. /*                             -+- MAIN -+-               */
  223.  
  224. int main(argc, argv)
  225. int argc;
  226. char **argv;
  227. {
  228.  
  229. char swt[4];
  230. char *p_swt=swt;
  231. char st_in,st_out;
  232.  
  233. char *valid1="0123456789abcdefghijlmxyzSL\0";
  234. char *valid2="0123456789abcdefghij-=\0";
  235.  
  236. fprintf(stderr,"\n[ConvPL] by kravietZ, v%u.%.2u konwerter standardow polskich \
  237. liter. Freeware.\n",ver_hi,ver_lo);
  238.  
  239. if(argc>1)
  240. {
  241.  
  242. /* sprawdz rozne mniej lub bardziej standardowe opcje */
  243. if(!strcmp(argv[1],"-?") || !strcmp(argv[1],"--help") ||
  244.    !strcmp(argv[1],"-h") || !strcmp(argv[1],"/?")) { DispH(); exit(1); }
  245.  
  246. if(!strcmp(argv[1],"-V") || strstr(argv[1],"--ver"))
  247.     { fprintf(stdout,"%u.%u",ver_hi,ver_lo); exit(0); }
  248.  
  249. if(!strcmp(argv[1],"-S") || strstr(argv[1],"--stand"))
  250.      { DispS(); exit(0); }
  251.  
  252. if(!strcmp(argv[1],"-L") || strstr(argv[1],"--lic"))
  253.     { DispL(); exit(0); }
  254.  
  255. /* sprawdz konwersje koncow linii [ uzywam wlasnego index() ]*/
  256. if(indexx('=',argv[1]) != -1) opt_mode=TO_CRLF;
  257. if(indexx('-',argv[1]) != -1) opt_mode=TO_LF;
  258.  
  259. /* sprawdzanie makr */
  260. switch(*argv[1])
  261. {
  262.     case 'l':   strncpy(p_swt,"13",2); break;
  263.     case 'm':   strncpy(p_swt,"31",2); break;
  264.     case 'x':   strncpy(p_swt,"10",2); break;
  265.     case 'y':   strncpy(p_swt,"30",2); break;
  266.     case 'z':   strncpy(p_swt,"21",2); break;
  267. /* te 2 zachowane dla kompatybilnosci z poprzednimi wersjami */
  268.     case 'S':   DispS(); exit(1); /* lista standardow */
  269.     case 'L':   DispL(); exit(1); /* licencja uzytkownika */
  270.      default:   strcpy(p_swt,argv[1]);
  271. }; /* sprawdzanie makr */
  272.  
  273. st_in=swt[0];
  274. st_out=swt[1];
  275.  
  276. /*  wszystkie ew. przypisania zostaly dokonane; teraz sprawdzamy
  277.     poprawnosc argumentow */
  278.  
  279. if( (indexx(st_in,valid1) == -1) || (indexx(st_out,valid2) == -1))
  280. { fprintf(stderr,"Zly parametr \"%c%c\". Polecenie: cpl -?  wyswietli \
  281. liste dostepnych opcji.\n",st_in,st_out); exit(1); }
  282.  
  283. if(argc==2) { ConvFile("stdin","stdout",st_in,st_out ); unlink("stdout"); exit(0);  }
  284.   else
  285.    if(argc==3) { ConvFile(argv[2],TEMP_FILE,st_in,st_out );
  286.                  unlink(argv[2]);
  287.                  rename(TEMP_FILE,argv[2]);
  288. /*               exit(0); */
  289.                 }
  290.     else if(argc>3) { ConvFile(argv[2],argv[3],st_in,st_out );
  291. /*                    exit(0); */
  292.                     }
  293. }
  294.       else
  295. {
  296.     DispH();
  297.     return(1);
  298. } /* argc<2 */
  299.  
  300. return(0);
  301. } /* main */
  302.