Source file: /~heha/hsn/unhtml.zip/unhtml.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __GNUC__
#include <unistd.h>
#endif

const char *tags[96]={
 "nbsp",  "iexcl", "cent",  "pound", "curren","yen",   "brvbar","sect",
 "uml",   "copy",  "ordf",  "laquo", "not",   "shy",   "reg",   "macr",
 "deg",   "plusmn","sup2",  "sup3",  "acute", "micro", "para",  "middot",
 "cedil", "sup1",  "ordm",  "raquo", "frac14","frac12","frac34","iquest",
 "Agrave","Aacute","Acirc", "Atilde","Auml",  "Aring", "AElig", "Ccedil",
 "Egrave","Eacute","Ecirc", "Euml",  "Igrave","Iacute","Icirc", "Iuml",
 "ETH",   "Ntilde","Ograve","Oacute","Ocirc", "Otilde","Ouml",  "times",
 "Oslash","Ugrave","Uacute","Ucirc", "Uuml",  "Yacute","THORN", "szlig",
 "agrave","aacute","acirc", "atilde","auml",  "aring", "aelig", "ccedil",
 "egrave","eacute","ecirc", "euml",  "igrave","iacute","icirc", "iuml",
 "eth",   "ntilde","ograve","oacute","ocirc", "otilde","ouml",  "divide",
 "oslash","ugrave","uacute","ucirc", "uuml",  "yacute","thorn", "yuml"};

void DoConvert(FILE*in,FILE*out) {
 char buf[8];
 char *bz;
 int c,i;
 for (bz=buf; bz<buf+7; c=EOF) {
  c=getc(in);
  if (c==EOF) break;
  if (c==';') {
   *bz='\0';
   for (i=0; i<96; i++) {
    if (strcmp(tags[i],buf)==0) {
     putc(i+160,out);
     return;
    }
   }
   break;
  }
  *bz++=c;		// abspeichern
 }
 putc('&',out);
 fwrite(buf,bz-buf,1,out);
 if (c!=EOF) putc(c,out);
}

void Process(FILE*in,FILE*out) {
 int c;

 while(1) {
  c=getc(in);
  if (c==EOF) break;
  if (c=='&') {
   DoConvert(in,out);
  }else{
   putc(c,out);
  }
 }
}


void ProcessFile(char*filename) {
 const char TmpName[]="TMP$$$";
 FILE*in,*out;

 in=fopen(filename,"r");
 if (!in) {
  printf("unhtml: can't open input file %s\n",filename);
  exit(1);
 }
 out=fopen(TmpName,"w");
 if (!out) {
  printf("unhtml: can't create temporary file %s\n",TmpName);
  exit(1);
 }
 Process(in,out);
 fclose(in);
 fclose(out);
 if (unlink(filename)) {
  printf("unhtml: can't delete file %s\n",filename);
  exit(1);
 }
 if (rename(TmpName,filename)) {
  printf("unhtml: can't rename file %s to %s\n",TmpName,filename);
  exit(1);
 }
}

int main(int argc, char**argv) {
 int i;

 if (argc<2) {
  Process(stdin,stdout);
 }else{
  for (i=1; i<argc; i++) {
   ProcessFile(argv[i]);
  }
 }
 return 0;
}
Detected encoding: ASCII (7 bit)2