Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * This file is part of LibParserUtils.
  3.  * Licensed under the MIT License,
  4.  *                http://www.opensource.org/licenses/mit-license.php
  5.  * Copyright 2007 John-Mark Bell <jmb@netsurf-browser.org>
  6.  */
  7.  
  8. #include <ctype.h>
  9. #include <stdbool.h>
  10. #include <stddef.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14.  
  15. #include <assert.h>
  16.  
  17. #include "charset/aliases.h"
  18. #include "utils/utils.h"
  19.  
  20. /* Bring in the aliases tables */
  21. #include "aliases.inc"
  22.  
  23. typedef struct {
  24.         size_t slen;
  25.         const char *s;
  26. } lengthed_string;
  27.  
  28.  
  29. #define IS_PUNCT_OR_SPACE(x)                    \
  30.         (!(((x) >= 'A' && (x) <= 'Z') ||        \
  31.            ((x) >= 'a' && (x) <= 'z') ||        \
  32.            ((x) >= '0' && (x) <= '9')))
  33.  
  34.  
  35. static int parserutils_charset_alias_match(const void *a, const void *b)
  36. {
  37.         lengthed_string *s = (lengthed_string *)a;
  38.         parserutils_charset_aliases_alias *alias = (parserutils_charset_aliases_alias*)b;
  39.         size_t key_left = s->slen;
  40.         size_t alias_left = alias->name_len;
  41.         const char *s_alias = alias->name;
  42.         const char *s_key = s->s;
  43.         int cmpret;
  44.        
  45.         while ((key_left > 0) && (alias_left > 0)) {
  46.                 while ((key_left > 0) && IS_PUNCT_OR_SPACE(*s_key)) {
  47.                         key_left--; s_key++;
  48.                 }
  49.                
  50.                 if (key_left == 0)
  51.                         break;
  52.                
  53.                 cmpret = tolower(*s_key) - *s_alias;
  54.                
  55.                 if (cmpret != 0) {
  56.                         return cmpret;
  57.                 }
  58.                
  59.                 key_left--;
  60.                 s_key++;
  61.                 alias_left--;
  62.                 s_alias++;
  63.         }
  64.        
  65.         while ((key_left > 0) && IS_PUNCT_OR_SPACE(*s_key)) {
  66.           key_left--; s_key++;
  67.         }
  68.        
  69.         return key_left - alias_left;
  70. }
  71.  
  72. /**
  73.  * Retrieve the canonical form of an alias name
  74.  *
  75.  * \param alias  The alias name
  76.  * \param len    The length of the alias name
  77.  * \return Pointer to canonical form or NULL if not found
  78.  */
  79. parserutils_charset_aliases_canon *parserutils__charset_alias_canonicalise(
  80.                 const char *alias, size_t len)
  81. {
  82.         parserutils_charset_aliases_alias *c;
  83.         lengthed_string s;
  84.        
  85.         s.slen = len;
  86.         s.s = alias;
  87.  
  88.         c = (parserutils_charset_aliases_alias*)bsearch(&s,
  89.                 &charset_aliases[0],
  90.                 charset_aliases_count,
  91.                 sizeof(parserutils_charset_aliases_alias),
  92.                 parserutils_charset_alias_match);
  93.        
  94.         if (c == NULL)
  95.                 return NULL;
  96.        
  97.         return c->canon;
  98. }
  99.  
  100. /**
  101.  * Retrieve the MIB enum value assigned to an encoding name
  102.  *
  103.  * \param alias  The alias to lookup
  104.  * \param len    The length of the alias string
  105.  * \return The MIB enum value, or 0 if not found
  106.  */
  107. uint16_t parserutils_charset_mibenum_from_name(const char *alias, size_t len)
  108. {
  109.         parserutils_charset_aliases_canon *c;
  110.  
  111.         if (alias == NULL)
  112.                 return 0;
  113.  
  114.         c = parserutils__charset_alias_canonicalise(alias, len);
  115.         if (c == NULL)
  116.                 return 0;
  117.  
  118.         return c->mib_enum;
  119. }
  120.  
  121. /**
  122.  * Retrieve the canonical name of an encoding from the MIB enum
  123.  *
  124.  * \param mibenum The MIB enum value
  125.  * \return Pointer to canonical name, or NULL if not found
  126.  */
  127. const char *parserutils_charset_mibenum_to_name(uint16_t mibenum)
  128. {
  129.         int i;
  130.         parserutils_charset_aliases_canon *c;
  131.        
  132.         for (i = 0; i < charset_aliases_canon_count; ++i) {
  133.                 c = &canonical_charset_names[i];
  134.                 if (c->mib_enum == mibenum)
  135.                         return c->name;
  136.         }
  137.        
  138.         return NULL;
  139. }
  140.  
  141. /**
  142.  * Detect if a parserutils_charset is Unicode
  143.  *
  144.  * \param mibenum  The MIB enum to consider
  145.  * \return true if a Unicode variant, false otherwise
  146.  */
  147. bool parserutils_charset_mibenum_is_unicode(uint16_t mibenum)
  148. {
  149.         return MIBENUM_IS_UNICODE(mibenum);
  150. }
  151.