Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5.  
  6. /* Basic signs over 127 and lower than 256*/
  7. const char *umap[][2] =
  8. {
  9.     {"#x80","€"},
  10.     {"#x81"," "},
  11.     {"#x82","‚"},
  12.     {"#x83","ƒ"},
  13.     {"#x84","„"},
  14.     {"#x85","…"},
  15.     {"#x86","†"},
  16.     {"#x87","‡"},
  17.     {"#x88","ˆ"},
  18.     {"#x89","‰"},
  19.     {"#x8A","Š"},
  20.     {"#x8B","‹"},
  21.     {"#x8C","Œ"},
  22.     {"#x8D"," "},
  23.     {"#x8E","Ž"},
  24.     {"#x8F"," "},
  25.     {"#x90"," "},
  26.     {"#x91","‘"},
  27.     {"#x92","’"},
  28.     {"#x93","“"},
  29.     {"#x94","”"},
  30.     {"#x95","•"},
  31.     {"#x96","–"},
  32.     {"#x97","—"},
  33.     {"#x98","˜"},
  34.     {"#x99","™"},
  35.     {"#x9A","š"},
  36.     {"#x9B","›"},
  37.     {"#x9C","œ"},
  38.     {"#x9D"," "},
  39.     {"#x9E","ž"},
  40.     {"#x9F","Ÿ"},
  41.     {"nbsp",""},
  42.     {"iexcl","¡"},
  43.     {"cent","¢"},
  44.     {"pound","£"},
  45.     {"curren","¤"},
  46.     {"yen","¥"},
  47.     {"brvbar","¦"},
  48.     {"sect","§"},
  49.     {"uml","¨"},
  50.     {"copy","©"},
  51.     {"ordf","ª"},
  52.     {"laquo","«"},
  53.     {"not","¬"},
  54.     {"shy","­"},
  55.     {"reg","®"},
  56.     {"macr","¯"},
  57.     {"deg","°"},
  58.     {"plusmn","±"},
  59.     {"sup2","²"},
  60.     {"sup3","³"},
  61.     {"acute","´"},
  62.     {"micro","µ"},
  63.     {"para","¶"},
  64.     {"middot","·"},
  65.     {"cedil","¸"},
  66.     {"sup1","¹"},
  67.     {"ordm","º"},
  68.     {"raquo","»"},
  69.     {"frac14","¼"},
  70.     {"frac12","½"},
  71.     {"frac34","¾"},
  72.     {"iquest","¿"},
  73.     {"Agrave","À"},
  74.     {"Aacute","Á"},
  75.     {"Acirc","Â"},
  76.     {"Atilde","Ã"},
  77.     {"Auml","Ä"},
  78.     {"Aring","Å"},
  79.     {"AElig","Æ"},
  80.     {"Ccedil","Ç"},
  81.     {"Egrave","È"},
  82.     {"Eacute","É"},
  83.     {"Ecirc","Ê"},
  84.     {"Euml","Ë"},
  85.     {"Igrave","Ì"},
  86.     {"Iacute","Í"},
  87.     {"Icirc","Î"},
  88.     {"Iuml","Ï"},
  89.     {"ETH","Ð"},
  90.     {"Ntilde","Ñ"},
  91.     {"Ograve","Ò"},
  92.     {"Oacute","Ó"},
  93.     {"Ocirc","Ô"},
  94.     {"Otilde","Õ"},
  95.     {"Ouml","Ö"},
  96.     {"times","×"},
  97.     {"Oslash","Ø"},
  98.     {"Ugrave","Ù"},
  99.     {"Uacute","Ú"},
  100.     {"Ucirc","Û"},
  101.     {"Uuml","Ü"},
  102.     {"Yacute","Ý"},
  103.     {"THORN","Þ"},
  104.     {"szlig","ß"},
  105.     {"agrave","à"},
  106.     {"aacute","á"},
  107.     {"acirc","â"},
  108.     {"atilde","ã"},
  109.     {"auml","ä"},
  110.     {"aring","å"},
  111.     {"aelig","æ"},
  112.     {"ccedil","ç"},
  113.     {"egrave","è"},
  114.     {"eacute","é"},
  115.     {"ecirc","ê"},
  116.     {"euml","ë"},
  117.     {"igrave","ì"},
  118.     {"iacute","í"},
  119.     {"icirc","î"},
  120.     {"iuml","ï"},
  121.     {"eth","ð"},
  122.     {"ntilde","ñ"},
  123.     {"ograve","ò"},
  124.     {"oacute","ó"},
  125.     {"ocirc","ô"},
  126.     {"otilde","õ"},
  127.     {"ouml","ö"},
  128.     {"divide","÷"},
  129.     {"oslash","ø"},
  130.     {"ugrave","ù"},
  131.     {"uacute","ú"},
  132.     {"ucirc","û"},
  133.     {"uuml","ü"},
  134.     {"yacute","ý"},
  135.     {"thorn","þ"},
  136.     {"yuml","ÿ"},
  137.     {"#x2500","─"},
  138.     {"#x2502","│"},
  139.     {"#x250C","┌"},
  140.     {"#x2510","┐"},
  141.     {"#x2514","└"},
  142.     {"#x2518","┘"},
  143.     {"#x251C","├"},
  144.     {"#x2524","┤"},
  145.     {"#x252C","┬"},
  146.     {"#x2534","┴"},
  147.     {"#x253C","┼"},
  148.     {"#x3B1","α"},
  149.     {"#x3B2","β"},
  150.     {"#x3B3","γ"},
  151.     {"#x394","Δ"},
  152.     {"#x2211","∑"},
  153.     {"#x221A","√"},
  154.     {"#x222B","∫"},
  155.     {"#x2248","≈"},
  156.     {"#x2260","≠"},
  157.     {"#x2264","≤"},
  158.     {"#x2265","≥"},
  159.     {"#x2190","←"},
  160.     {"#x2191","↑"},
  161.     {"#x2192","→"},
  162.     {"#x2193","↓"},
  163.     {"#x2194","↔"},
  164.     {"#x2195","↕"},
  165.     {"#x2022","•"},
  166.     {"#x2044","⁄"},
  167.     {"#x2030","‰"},
  168.     {"#x2122","™"},
  169.     {"#x20AC","€"},
  170.     {NULL}
  171. };
  172.  
  173. char* strip_html_unicode(const char * string)
  174. {
  175.     if(string == NULL) return NULL;
  176.  
  177.     char *p   = (char*)string;
  178.  
  179.     // Be safe.
  180.     size_t cpy_len = strlen(string);
  181.     char *cpy = malloc(cpy_len + 1);
  182.     memset(cpy,0,cpy_len+1);
  183.  
  184.     int x = 0;
  185.     bool is_uc = false;    
  186.  
  187.     while(*p)
  188.     {
  189.         char *sub = " ";
  190.         is_uc = false;
  191.        
  192.         if(*p == '&')
  193.         {
  194.             int uit = 0;
  195.             char uit_buf[8];
  196.             char *p2 = p;
  197.  
  198.             // Skip silly "unwarned result" warning
  199.             p2 = p2 + sizeof(char);
  200.            
  201.             while(*p2 && *p2 != ';')
  202.             {
  203.                 uit_buf[uit++] = *p2++;
  204.             }  
  205.             uit_buf[uit] = 0;
  206.  
  207.             if(*p2 == ';' && uit != 0)
  208.             {
  209.                 int uma = 0;
  210.                 while(umap[uma] && umap[uma][0])
  211.                 {
  212.                     if(!strcmp(umap[uma][0],uit_buf))
  213.                     {
  214.                         sub = (char*)umap[uma][1];
  215.                         is_uc = true;
  216.                         p = p2;
  217.                         break;
  218.                     }
  219.                     uma++;
  220.                 }          
  221.             }
  222.         }  
  223.    
  224.         if(is_uc == false) cpy[x++] = *p;      
  225.         else  while(*sub) cpy[x++] = *sub++;
  226.  
  227.         p = p + sizeof(char);
  228.     }
  229.     cpy[x] = '\0';
  230.     return cpy;
  231. }
  232.  
  233.  
  234. int main(int argc, char *argv[])
  235. {
  236.     /* Some human readable formula should be printed. */
  237.     const char *testme  = "f(x) -> &#x2211; &#x222B; &#x221A;x+1  [ &ETH; = { |R0 } ]  (&#x394; &#x2248; 0,11 +/-)";
  238.     char *result = strip_html_unicode(testme); 
  239.    
  240.     puts(result);
  241.    
  242.     if(result) free(result);
  243.     return EXIT_SUCCESS;
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement