electronicsguy

sscanf.cpp

Apr 6th, 2017
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.57 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2000-2002 Opsycon AB  (www.opsycon.se)
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 1. Redistributions of source code must retain the above copyright
  8.  *    notice, this list of conditions and the following disclaimer.
  9.  * 2. Redistributions in binary form must reproduce the above copyright
  10.  *    notice, this list of conditions and the following disclaimer in the
  11.  *    documentation and/or other materials provided with the distribution.
  12.  * 3. All advertising materials mentioning features or use of this software
  13.  *    must display the following acknowledgement:
  14.  *  This product includes software developed by Opsycon AB.
  15.  * 4. The name of the author may not be used to endorse or promote products
  16.  *    derived from this software without specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  19.  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28.  * SUCH DAMAGE.
  29.  *
  30.  */
  31.  
  32. #include "sscanf.h"
  33.  
  34. #define MAXLN 200
  35. // http://web.mit.edu/10.001/Web/Course_Notes/c_Notes/tips_printf.html
  36. /*alert (beep) \a
  37.   backslash \\
  38.   backspace \b
  39.   carriage return \r
  40.   double quote  \"
  41.   formfeed  \f
  42.   horizontal tab  \t
  43.   newline \n
  44.   null character  \0
  45.   single quote  \'
  46.   vertical tab  \v
  47.   question mark \?
  48. */
  49. #define ISSPACE " \t\n\r\f\v"
  50.  
  51. size_t strcspn (const char *p, const char *s){
  52.     int i, j;
  53.  
  54.     for (i = 0; p[i]; i++) {
  55.         for (j = 0; s[j]; j++) {
  56.             if (s[j] == p[i])
  57.                 break;
  58.         }
  59.         if (s[j])
  60.             break;
  61.     }
  62.     return (i);
  63. }
  64.  
  65. char * _getbase(char *p, int *basep){
  66.     if (p[0] == '0') {
  67.         switch (p[1]) {
  68.         case 'x':
  69.             *basep = 16;
  70.             break;
  71.         case 't': case 'n':
  72.             *basep = 10;
  73.             break;
  74.         case 'o':
  75.             *basep = 8;
  76.             break;
  77.         default:
  78.             *basep = 10;
  79.             return (p);
  80.         }
  81.         return (p + 2);
  82.     }
  83.     *basep = 10;
  84.     return (p);
  85. }
  86.  
  87. /*
  88.  *  _atob(vp,p,base)
  89.  */
  90. int _atob (uint32_t *vp, char *p, int base){
  91.     uint32_t value, v1, v2;
  92.     char *q, tmp[20];
  93.     int digit;
  94.  
  95.     if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
  96.         base = 16;
  97.         p += 2;
  98.     }
  99.  
  100.     if (base == 16 && (q = strchr (p, '.')) != 0) {
  101.         if (q - p > sizeof(tmp) - 1)
  102.             return (0);
  103.  
  104.         strncpy (tmp, p, q - p);
  105.         tmp[q - p] = '\0';
  106.         if (!_atob (&v1, tmp, 16))
  107.             return (0);
  108.  
  109.         q++;
  110.         if (strchr (q, '.'))
  111.             return (0);
  112.  
  113.         if (!_atob (&v2, q, 16))
  114.             return (0);
  115.         *vp = (v1 << 16) + v2;
  116.         return (1);
  117.     }
  118.  
  119.     value = *vp = 0;
  120.     for (; *p; p++) {
  121.         if (*p >= '0' && *p <= '9')
  122.             digit = *p - '0';
  123.         else if (*p >= 'a' && *p <= 'f')
  124.             digit = *p - 'a' + 10;
  125.         else if (*p >= 'A' && *p <= 'F')
  126.             digit = *p - 'A' + 10;
  127.         else
  128.             return (0);
  129.  
  130.         if (digit >= base)
  131.             return (0);
  132.         value *= base;
  133.         value += digit;
  134.     }
  135.     *vp = value;
  136.     return (1);
  137. }
  138.  
  139. /*
  140.  *  atob(vp,p,base)
  141.  *      converts p to binary result in vp, rtn 1 on success
  142.  */
  143. int atob(uint32_t *vp, char *p, int base){
  144.     uint32_t v;
  145.     if (base == 0)
  146.         p = _getbase (p, &base);
  147.     if (_atob (&v, p, base)) {
  148.         *vp = v;
  149.         return (1);
  150.     }
  151.     return (0);
  152. }
  153.  
  154. /*
  155.  *  vsscanf(buf,fmt,ap)
  156.  */
  157.  
  158. int vsscanf (const char *buf, const char *s, va_list ap){
  159.     uint32_t             count, noassign, width, base, lflag;
  160.     const char     *tc;
  161.     char           *t, tmp[MAXLN];
  162.  
  163.     count = noassign = width = lflag = 0;
  164.     while (*s && *buf) {
  165.     while (isspace (*s))
  166.         s++;
  167.     if (*s == '%') {
  168.         s++;
  169.         for (; *s; s++) {
  170.         if (strchr ("dibouxcsefg%", *s))
  171.             break;
  172.         if (*s == '*')
  173.             noassign = 1;
  174.         else if (*s == 'l' || *s == 'L')
  175.             lflag = 1;
  176.         else if (*s >= '1' && *s <= '9') {
  177.             for (tc = s; isdigit (*s); s++);
  178.             strncpy (tmp, tc, s - tc);
  179.             tmp[s - tc] = '\0';
  180.             atob (&width, tmp, 10);
  181.             s--;
  182.         }
  183.         }
  184.         if (*s == 's') {
  185.         while (isspace (*buf))
  186.             buf++;
  187.         if (!width)
  188.             width = strcspn (buf, ISSPACE);
  189.         if (!noassign) {
  190.             strncpy (t = va_arg (ap, char *), buf, width);
  191.             t[width] = '\0';
  192.         }
  193.         buf += width;
  194.         } else if (*s == 'c') {
  195.         if (!width)
  196.             width = 1;
  197.         if (!noassign) {
  198.             strncpy (t = va_arg (ap, char *), buf, width);
  199.             t[width] = '\0';
  200.         }
  201.         buf += width;
  202.         } else if (strchr ("dobxu", *s)) {
  203.         while (isspace (*buf))
  204.             buf++;
  205.         if (*s == 'd' || *s == 'u')
  206.             base = 10;
  207.         else if (*s == 'x')
  208.             base = 16;
  209.         else if (*s == 'o')
  210.             base = 8;
  211.         else if (*s == 'b')
  212.             base = 2;
  213.         if (!width) {
  214.             if (isspace (*(s + 1)) || *(s + 1) == 0)
  215.             width = strcspn (buf, ISSPACE);
  216.             else
  217.             width = strchr (buf, *(s + 1)) - buf;
  218.         }
  219.         strncpy (tmp, buf, width);
  220.         tmp[width] = '\0';
  221.         buf += width;
  222.         if (!noassign)
  223.             atob (va_arg (ap, uint32_t *), tmp, base);
  224.         }
  225.         if (!noassign)
  226.         count++;
  227.         width = noassign = lflag = 0;
  228.         s++;
  229.     } else {
  230.         while (isspace (*buf))
  231.         buf++;
  232.         if (*s != *buf)
  233.         break;
  234.         else
  235.         s++, buf++;
  236.     }
  237.     }
  238.     return (count);
  239. }
  240.  
  241.  
  242. int sscanf (const char *buf, const char *fmt, ...){
  243.     int count;
  244.     va_list ap;
  245.    
  246.     va_start (ap, fmt);
  247.     count = vsscanf (buf, fmt, ap);
  248.     va_end (ap);
  249.     return (count);
  250. }
Add Comment
Please, Sign In to add comment