Back to index

tetex-bin  3.0
enc.c
Go to the documentation of this file.
00001 /* enc.c */
00002 
00003 /************************************************************************
00004 
00005   Part of the dvipng distribution
00006 
00007   This program is free software; you can redistribute it and/or modify
00008   it under the terms of the GNU General Public License as published by
00009   the Free Software Foundation; either version 2 of the License, or
00010   (at your option) any later version.
00011 
00012   This program is distributed in the hope that it will be useful, but
00013   WITHOUT ANY WARRANTY; without even the implied warranty of
00014   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015   General Public License for more details.
00016 
00017   You should have received a copy of the GNU General Public License
00018   along with this program; if not, write to the Free Software
00019   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00020   02111-1307, USA.
00021 
00022   Copyright (C) 2002-2005 Jan-Åke Larsson
00023 
00024 ************************************************************************/
00025 
00026 #include "dvipng.h"
00027 
00028 struct encoding* encodingp=NULL;
00029 
00030 struct encoding* InitEncoding(char* encoding) 
00031 {
00032   char *pos,*max,*buf,*enc_file;
00033   int i;
00034   struct encoding* encp=NULL;
00035   struct filemmap fmmap;
00036   
00037 #ifdef HAVE_KPSE_ENC_FORMATS
00038   TEMPSTR(enc_file,kpse_find_file(encoding,kpse_enc_format,false));
00039 #else
00040   TEMPSTR(enc_file,kpse_find_file(encoding,kpse_tex_ps_header_format,false));
00041 #endif
00042   if (enc_file == NULL) {
00043     Warning("encoding file %s could not be found",encoding);
00044     return(NULL);
00045   }
00046   DEBUG_PRINT((DEBUG_FT|DEBUG_ENC),("\n  OPEN ENCODING:\t'%s'", enc_file));
00047   if (MmapFile(enc_file,&fmmap)) return(NULL);
00048   if ((encp = calloc(sizeof(struct encoding)+strlen(encoding)+1
00049                    +fmmap.size,1))==NULL) {
00050     Warning("cannot alloc space for encoding",enc_file);
00051     UnMmapFile(&fmmap);
00052     return(NULL);
00053   }
00054   encp->name=(char*)encp+sizeof(struct encoding);
00055   strcpy(encp->name,encoding);
00056   pos=fmmap.mmap;
00057   max=fmmap.mmap+fmmap.size;
00058   buf=encp->name+strlen(encoding)+1;
00059 #define SKIPCOMMENT(x) if (*x=='%') while (x<max && *x!='\n') x++;
00060   while(pos<max && *pos!='/') {
00061     SKIPCOMMENT(pos);
00062     pos++;
00063   }
00064   pos++;
00065   encp->charname[256]=buf;
00066   while(pos<max && *pos!='[' 
00067        && *pos!=' ' && *pos!='\t' && *pos!='\n' && *pos!='%') 
00068     *buf++=*pos++;
00069   *buf++='\0';
00070   DEBUG_PRINT(DEBUG_ENC,("\n  PS ENCODING '%s'",
00071                       encp->charname[256])); 
00072   while (pos < max && *pos!='[') {
00073     SKIPCOMMENT(pos);
00074     pos++;
00075   }
00076   while(pos<max && *pos!='/') {
00077     SKIPCOMMENT(pos);
00078     pos++;
00079   }
00080   i=0;
00081   while(pos<max && *pos!=']') {
00082     pos++;
00083     encp->charname[i++]=buf;
00084     while(pos<max && *pos!=' ' && *pos!='\t' && *pos!='\n' && *pos!='%') 
00085       *buf++=*pos++;
00086     *buf++='\0';
00087     DEBUG_PRINT(DEBUG_ENC,("\n  PS ENCODING %d '%s'",
00088                i-1,encp->charname[i-1])); 
00089     while(pos<max && *pos!='/' && *pos!=']') {
00090       SKIPCOMMENT(pos);
00091       pos++;
00092     }
00093   }
00094   UnMmapFile(&fmmap);
00095   return(encp);
00096 }
00097 
00098 
00099 struct encoding* FindEncoding(char* encoding) 
00100 {
00101   struct encoding *temp=encodingp;
00102 
00103   /* printf("{%s} \n",encoding); */
00104   while(temp!=NULL && strcmp(encoding,temp->name)!=0) 
00105     temp=temp->next;
00106   if (temp==NULL) {
00107     temp=InitEncoding(encoding);
00108     if (temp!=NULL) {
00109       temp->next=encodingp;
00110       encodingp=temp;
00111     }
00112   }
00113   return(temp);
00114 }
00115 
00116 void ClearEncoding(void)
00117 {
00118   struct encoding *temp=encodingp;
00119 
00120   while(temp!=NULL) {
00121     encodingp=encodingp->next;
00122     free(temp);
00123     temp=encodingp;
00124   }
00125 }