Back to index

texmacs  1.0.7.15
p_in.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdio_ext.h>
00003 
00004 int hex(int c, int *n)
00005 { if ((c>='0')&&(c<='9')) { *n=c-'0'; return 1; }
00006   else if ((c>='a')&&(c<='f')) { *n=c-'a'+10; return 1; }
00007   else if ((c>='A')&&(c<='F')) { *n=c-'A'+10; return 1; }
00008   else return 0;
00009 }
00010 
00011 int main(int argc, char **argv)
00012 { FILE *in,*out; int c,c2,c3,n,n2,ok=1;
00013   if (argc>2)
00014   { in=fopen(argv[2],"r");
00015     if (!in)
00016     { fprintf(stderr,"cannot read from %s\n",argv[2]); exit(1); }
00017   }
00018   else
00019     { in=stdin;
00020     if (argc>1)
00021     { out=fopen(argv[1],"w");
00022       if (!out)
00023       {fprintf(stderr,"cannot write to %s\n",argv[1]); exit(1); }
00024     }
00025     else out=stdout;
00026   }
00027   while (1)
00028   { c=fgetc(in);
00029     if (c==EOF) break;
00030     else if (c=='[')
00031     { c=fgetc(in);
00032       if (c=='[') fputc('[',out);
00033       else if (hex(c,&n))
00034       { c2=fgetc(in);
00035        if (hex(c2,&n2))
00036         { n=16*n+n2; c3=fgetc(in);
00037          if (c3==']') fputc(n,out);
00038          else { fprintf(stderr,"error: [%c%c%c\n",c,c2,c3); ok=0; }
00039         }
00040        else { fprintf(stderr,"error: [%c%c\n",c,c2); ok=0; }
00041       }
00042       else { fprintf(stderr,"error: [%c\n",c); ok=0; }
00043     }
00044     else if (c==']')
00045     { c=fgetc(in);
00046       if (c==']') fputc(']',out);
00047       else { fprintf(stderr,"error: ]%c\n",c); ok=0; }
00048     }
00049     else if (c=='\n')
00050       if (ok) fflush(out); else { __fpurge(out); ok=1; }
00051     else if ((c>=' ')&&(c<=0x7e)) fputc(c,out);
00052   }
00053 }