PennMUSH Community

Changeset 1243

Show
Ignore:
Timestamp:
09/05/08 01:24:43 (3 months ago)
Author:
walker
Message:

Making baseconv() support up to base64.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • 1.8.3/branches/devel/game/txt/hlp/pennfunc.hlp

    r1240 r1243  
    580580 
    581581  Converts <number>, which is in base <from base> into base <to base>. 
    582   The bases can be between 2 (binary) and 36.  
     582  The bases can be between 2 (binary) and 64, inclusive. 
     583 
     584  All bases over 64 use base64 url string: 
     585 
     586  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" 
     587 
     588  But using base64 as a 'from' will also understand + as 61 and / as 62. 
    583589& BEEP() 
    584590  beep([<number>]) 
  • 1.8.3/branches/devel/src/funmath.c

    r1177 r1243  
    11081108FUNCTION(fun_log) 
    11091109{ 
    1110   NVAL num, base; 
     1110  NVAL num; 
     1111  NVAL base = 10.0; 
    11111112  bool base_is_e = false; 
    11121113 
     
    11291130  if (nargs == 2) { 
    11301131    if (!is_number(args[1])) { 
    1131       if (args[1][0] == 'e' && args[1][1] == '\0') 
     1132      if (args[1][0] == 'e' && args[1][1] == '\0') { 
    11321133        base_is_e = true; 
    1133       else { 
     1134      } else { 
    11341135        safe_str(T(e_nums), buff, bp); 
    11351136        return; 
     
    17751776} 
    17761777 
     1778extern char from_base_64[256]; 
     1779extern char to_base_64[]; 
     1780 
     1781extern char from_base_36[256]; 
     1782extern char to_base_36[]; 
     1783 
    17771784FUNCTION(fun_baseconv) 
    17781785{ 
    17791786  long n; 
     1787  int m; 
    17801788  int from, to; 
    1781   char *end; 
     1789  char *ptr; 
     1790  char numbuff[BUFFER_LEN], *nbp; 
     1791   
     1792 
     1793  // Base 36 by default. 
     1794  char *frombase = from_base_36; 
     1795  char *tobase = to_base_36; 
    17821796 
    17831797  if (!(is_integer(args[1]) && is_integer(args[2]))) { 
     
    17861800  } 
    17871801 
    1788   from = strtol(args[1], NULL, 10); 
    1789   to = strtol(args[2], NULL, 10); 
    1790  
    1791   if (from < 2 || from > 36) { 
     1802  from = parse_integer(args[1]); 
     1803  to = parse_integer(args[2]); 
     1804 
     1805  if (from < 2 || from > 64) { 
    17921806    safe_str(T("#-1 FROM BASE OUT OF RANGE"), buff, bp); 
    17931807    return; 
    17941808  } 
    17951809 
    1796   if (to < 2 || to > 36) { 
     1810  if (from > 36) { 
     1811    frombase = from_base_64; 
     1812  } 
     1813 
     1814  if (to < 2 || to > 64) { 
    17971815    safe_str(T("#-1 TO BASE OUT OF RANGE"), buff, bp); 
    17981816    return; 
    17991817  } 
    18001818 
    1801   n = strtol(trim_space_sep(args[0], ' '), &end, from); 
    1802  
    1803   if (*end != '\0') { 
    1804     safe_str(T("#-1 MALFORMED NUMBER"), buff, bp); 
    1805     return; 
    1806   } 
    1807  
    1808   format_long(n, buff, bp, BUFFER_LEN, to); 
     1819  if (to > 36) { 
     1820    tobase = to_base_64; 
     1821  } 
     1822 
     1823  // Parse it. 
     1824  ptr = trim_space_sep(args[0], ' '); 
     1825  n = 0; 
     1826  while (ptr && *ptr) { 
     1827    n *= from; 
     1828    if (frombase[(unsigned char) *ptr] >= 0) { 
     1829      n += frombase[(unsigned char) *ptr]; 
     1830      ptr++; 
     1831    } else { 
     1832      safe_str(T("#-1 MALFORMED NUMBER"), buff, bp); 
     1833      return; 
     1834    } 
     1835  } 
     1836 
     1837  // Handle the 0-case. (And quickly handle < to_base case, too!) 
     1838  if (n < to) { 
     1839    safe_chr(tobase[(unsigned char) n], buff, bp); 
     1840    return; 
     1841  } 
     1842 
     1843  nbp = numbuff; 
     1844 
     1845  // This comes out backwards. 
     1846  while (n > 0) { 
     1847    m = n % to; 
     1848    n = n / to; 
     1849    safe_chr(tobase[(unsigned char) m], numbuff, &nbp); 
     1850  } 
     1851 
     1852  // Reverse back onto buff. 
     1853  nbp--; 
     1854  while (nbp >= numbuff) { 
     1855    safe_chr(*nbp, buff, bp); 
     1856    nbp--; 
     1857  } 
    18091858} 
    18101859 
  • 1.8.3/branches/devel/src/tables.c

    r1239 r1243  
    2121  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 
    2222}; 
     23 
     24char from_base_64[256] = { 
     25  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
     26  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
     27  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63, 
     28  52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, 
     29  -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 
     30  15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63, 
     31  -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 
     32  41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, 
     33  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
     34  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
     35  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
     36  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
     37  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
     38  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
     39  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
     40  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 
     41}; 
     42 
     43char to_base_64[] = 
     44    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; 
     45 
     46char from_base_36[256] = { 
     47  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
     48  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
     49  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
     50  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, 
     51  -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 
     52  25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, 
     53  -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 
     54  25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, 
     55  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
     56  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
     57  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
     58  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
     59  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
     60  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
     61  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
     62  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 
     63}; 
     64 
     65char to_base_36[] = "0123456789abcdefghijklmnopqrstuvwxyz"; 
    2366 
    2467char active_table[256] = {