[BACK]Return to chars.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Diff for /mandoc/chars.c between version 1.4 and 1.23

version 1.4, 2009/09/22 23:15:58 version 1.23, 2010/07/18 12:10:08
Line 1 
Line 1 
 /*      $Id$ */  /*      $Id$ */
 /*  /*
  * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>   * Copyright (c) 2009 Kristaps Dzonsons <kristaps@bsd.lv>
  *   *
  * Permission to use, copy, modify, and distribute this software for any   * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above   * purpose with or without fee is hereby granted, provided that the above
Line 14 
Line 14 
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF   * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.   * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */   */
   #ifdef HAVE_CONFIG_H
   #include "config.h"
   #endif
   
 #include <assert.h>  #include <assert.h>
 #include <err.h>  #include <stdio.h>
 #include <stdlib.h>  #include <stdlib.h>
 #include <string.h>  #include <string.h>
   
   #include "mandoc.h"
 #include "chars.h"  #include "chars.h"
   
 #define PRINT_HI         126  #define PRINT_HI         126
Line 28  struct ln {
Line 33  struct ln {
         struct ln        *next;          struct ln        *next;
         const char       *code;          const char       *code;
         const char       *ascii;          const char       *ascii;
         const char       *html;  
         size_t            codesz;  
         size_t            asciisz;          size_t            asciisz;
         size_t            htmlsz;          int               unicode;
         int               type;          int               type;
 #define CHARS_CHAR       (1 << 0)  #define CHARS_CHAR       (1 << 0)
 #define CHARS_STRING     (1 << 1)  #define CHARS_STRING     (1 << 1)
 #define CHARS_BOTH       (0x03)  #define CHARS_BOTH       (CHARS_CHAR | CHARS_STRING)
 };  };
   
 #define LINES_MAX         300  #define LINES_MAX         370
   
 #define CHAR(w, x, y, z, a, b) \  #define CHAR(in, ch, chsz, code) \
         { NULL, (w), (y), (a), (x), (z), (b), CHARS_CHAR },          { NULL, (in), (ch), (chsz), (code), CHARS_CHAR },
 #define STRING(w, x, y, z, a, b) \  #define STRING(in, ch, chsz, code) \
         { NULL, (w), (y), (a), (x), (z), (b), CHARS_STRING },          { NULL, (in), (ch), (chsz), (code), CHARS_STRING },
 #define BOTH(w, x, y, z, a, b) \  #define BOTH(in, ch, chsz, code) \
         { NULL, (w), (y), (a), (x), (z), (b), CHARS_BOTH },          { NULL, (in), (ch), (chsz), (code), CHARS_BOTH },
   
 static  struct ln lines[LINES_MAX] = {  #define CHAR_TBL_START    static struct ln lines[LINES_MAX] = {
   #define CHAR_TBL_END      };
   
 #include "chars.in"  #include "chars.in"
 };  
   
 struct  tbl {  struct  tbl {
         enum chars        type;          enum chars        type;
Line 58  struct tbl {
Line 62  struct tbl {
   
 static  inline int        match(const struct ln *,  static  inline int        match(const struct ln *,
                                 const char *, size_t, int);                                  const char *, size_t, int);
 static  const char       *find(struct tbl *, const char *,  static  const struct ln  *find(struct tbl *, const char *, size_t, int);
                                 size_t, size_t *, int);  
   
   
 void  void
Line 89  chars_init(enum chars type)
Line 92  chars_init(enum chars type)
          * (they're in-line re-ordered during lookup).           * (they're in-line re-ordered during lookup).
          */           */
   
         if (NULL == (tab = malloc(sizeof(struct tbl))))          tab = malloc(sizeof(struct tbl));
                 err(1, "malloc");          if (NULL == tab) {
         tab->type = type;                  perror(NULL);
                   exit(EXIT_FAILURE);
           }
   
         htab = calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln **));          htab = calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln **));
         if (NULL == htab)          if (NULL == htab) {
                 err(1, "malloc");                  perror(NULL);
                   exit(EXIT_FAILURE);
           }
   
         for (i = 0; i < LINES_MAX; i++) {          for (i = 0; i < LINES_MAX; i++) {
                 hash = (int)lines[i].code[0] - PRINT_LO;                  hash = (int)lines[i].code[0] - PRINT_LO;
Line 111  chars_init(enum chars type)
Line 118  chars_init(enum chars type)
         }          }
   
         tab->htab = htab;          tab->htab = htab;
           tab->type = type;
         return(tab);          return(tab);
 }  }
   
   
   /*
    * Special character to Unicode codepoint.
    */
   int
   chars_spec2cp(void *arg, const char *p, size_t sz)
   {
           const struct ln *ln;
   
           ln = find((struct tbl *)arg, p, sz, CHARS_CHAR);
           if (NULL == ln)
                   return(-1);
           return(ln->unicode);
   }
   
   
   /*
    * Reserved word to Unicode codepoint.
    */
   int
   chars_res2cp(void *arg, const char *p, size_t sz)
   {
           const struct ln *ln;
   
           ln = find((struct tbl *)arg, p, sz, CHARS_STRING);
           if (NULL == ln)
                   return(-1);
           return(ln->unicode);
   }
   
   
   /*
    * Special character to string array.
    */
 const char *  const char *
 chars_a2ascii(void *arg, const char *p, size_t sz, size_t *rsz)  chars_spec2str(void *arg, const char *p, size_t sz, size_t *rsz)
 {  {
           const struct ln *ln;
   
         return(find((struct tbl *)arg, p, sz, rsz, CHARS_CHAR));          ln = find((struct tbl *)arg, p, sz, CHARS_CHAR);
           if (NULL == ln)
                   return(NULL);
   
           *rsz = ln->asciisz;
           return(ln->ascii);
 }  }
   
   
   /*
    * Reserved word to string array.
    */
 const char *  const char *
 chars_a2res(void *arg, const char *p, size_t sz, size_t *rsz)  chars_res2str(void *arg, const char *p, size_t sz, size_t *rsz)
 {  {
           const struct ln *ln;
   
         return(find((struct tbl *)arg, p, sz, rsz, CHARS_STRING));          ln = find((struct tbl *)arg, p, sz, CHARS_STRING);
           if (NULL == ln)
                   return(NULL);
   
           *rsz = ln->asciisz;
           return(ln->ascii);
 }  }
   
   
 static const char *  static const struct ln *
 find(struct tbl *tab, const char *p, size_t sz, size_t *rsz, int type)  find(struct tbl *tab, const char *p, size_t sz, int type)
 {  {
         struct ln        *pp, *prev;          struct ln        *pp, *prev;
         struct ln       **htab;          struct ln       **htab;
         int               hash;          int               hash;
   
         assert(p);          assert(p);
         assert(sz > 0);          if (0 == sz)
                   return(NULL);
   
         if (p[0] < PRINT_LO || p[0] > PRINT_HI)          if (p[0] < PRINT_LO || p[0] > PRINT_HI)
                 return(NULL);                  return(NULL);
Line 156  find(struct tbl *tab, const char *p, size_t sz, size_t
Line 213  find(struct tbl *tab, const char *p, size_t sz, size_t
         if (NULL == (pp = htab[hash]))          if (NULL == (pp = htab[hash]))
                 return(NULL);                  return(NULL);
   
         if (NULL == pp->next) {  
                 if ( ! match(pp, p, sz, type))  
                         return(NULL);  
   
                 if (CHARS_HTML == tab->type) {  
                         *rsz = pp->htmlsz;  
                         return(pp->html);  
                 }  
                 *rsz = pp->asciisz;  
                 return(pp->ascii);  
         }  
   
         for (prev = NULL; pp; pp = pp->next) {          for (prev = NULL; pp; pp = pp->next) {
                 if ( ! match(pp, p, sz, type)) {                  if ( ! match(pp, p, sz, type)) {
                         prev = pp;                          prev = pp;
Line 180  find(struct tbl *tab, const char *p, size_t sz, size_t
Line 225  find(struct tbl *tab, const char *p, size_t sz, size_t
                         htab[hash] = pp;                          htab[hash] = pp;
                 }                  }
   
                 if (CHARS_HTML == tab->type) {                  return(pp);
                         *rsz = pp->htmlsz;  
                         return(pp->html);  
                 }  
                 *rsz = pp->asciisz;  
                 return(pp->ascii);  
         }          }
   
         return(NULL);          return(NULL);
Line 198  match(const struct ln *ln, const char *p, size_t sz, i
Line 238  match(const struct ln *ln, const char *p, size_t sz, i
   
         if ( ! (ln->type & type))          if ( ! (ln->type & type))
                 return(0);                  return(0);
         if (ln->codesz != sz)          if (strncmp(ln->code, p, sz))
                 return(0);                  return(0);
         return(0 == strncmp(ln->code, p, sz));          return('\0' == ln->code[(int)sz]);
 }  }

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.23

CVSweb