<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Custom sscanf()]]></title><description><![CDATA[<p>Hi!<br />
Ich möchte euch meine eigene sscanf funktion präsentieren.</p>
<pre><code>[cpp]
#include &lt;cstdarg&gt;

using namespace std;

class StringParser
{
  private:
    // Extended ASCII
    bool m_abCharTable[255];

    const char* Source;
    const char* Parsing;

    // Auxiliary values
    int m_nParsingIndex;
    int m_nWidth;
    bool m_bIgnore;

    enum
    {
        // Types, including modified types  //-&gt; Values, must be different from each other
        T_NONE            = 0,              //
        T_BOOL            = 'b',            // -&gt; 98
        T_CHAR            = 'c',            // -&gt; 99
        T_STRING          = 'z',            // -&gt; 122
        T_SHORT           = 's',            // -&gt; 115
        T_UNSIGNED_SHORT  = 'u' + 's',      // -&gt; 232
        T_INT             = 'i',            // -&gt; 105
        T_UNSIGNED_INT    = 'u' + 'i',      // -&gt; 222
        T_HEX             = 'x',            // -&gt; 120
        T_OCT             = 'o',            // -&gt; 111
        T_LONG            = 'l',            // -&gt; 108
        T_UNSIGNED_LONG   = 'u' + 'l',      // -&gt; 225
        T_FLOAT           = 'f',            // -&gt; 102
        T_DOUBLE          = 'd',            // -&gt; 100

        // Modifier
        M_UNSIGNED        = 'u',            // -&gt; 117
    };

    void init()
    {
        // dont parse control codes like TAB, BELL, \n usw. (0-32)
        // dont parse ' ' (33)
        for(int i=0; i&lt;255; i++)
        {
        	if(i&lt;33)
        	{
                m_abCharTable[i] = 0;
        	}
        	else
        	{
        	    m_abCharTable[i] = 1;
        	}
        }
    }

    void set(int c, bool b)
    {
        m_abCharTable[c] = b;
    }

    void set(int c1, int c2, bool b)
    {
        for(int i=c1; i&lt;=c2; i++)
        {
            m_abCharTable[i] = b;
        }
    }

    int ParseOption()         // returning the Type - function can be use repetive
    {
        int n = Parsing[m_nParsingIndex];
        m_nParsingIndex++;

        switch(n)
        {
            case '*':
            {
                m_bIgnore = 1;
                return ParseOption();           // no Type yet - run again and get Type
            }

            case '[':
            {
                int last;
                bool bset = 1;
                bool quit = 0;

                while(quit == 0)
                {
                    n = Parsing[m_nParsingIndex];
                    m_nParsingIndex++;

                    switch(n)
                    {
                        case ']':
                        {
                            quit = 1;
                            break;
                        }

                        case '^':
                        {
                            bset = 0;
                            break;
                        }

                        case '-':
                        {
                            if(Parsing[m_nParsingIndex] != '-')
                            {
                                n = Parsing[m_nParsingIndex];
                                m_nParsingIndex++;

                                set(last, n, bset);
                            }

                            break;
                        }

                        default:
                        {
                            if(Parsing[m_nParsingIndex] != '-')
                            {
                                set(n, bset);
                                bset = 1;
                            }

                            last = n;
                        }
                    }
                }

                return ParseOption();            // this was no Type - 'just an option - run again
            }

            // Put your modifiers here
            // Take this as an example:
            case M_UNSIGNED:
            {
                m_nParsingIndex--;
                return n + ParseOption();       // this is a modifier - run again and get Type
            }

            case ' ': return T_NONE;            // there is no Type

            case '%': return ParseOption();

            default:
            {
                if(cisdigit(n) == 1)            // there is a width
                {
                    m_nParsingIndex--;
                    m_nWidth = catoi(Parsing, &amp;m_nParsingIndex);

                    return ParseOption();       // no Type yet - run again and get Type
                }
                else
                {
                    return n;                   // no Width, no Modifier - so this must be a Type
                }
            }
        }
    }

  public:
    StringParser()
    {
        init();             // intialize m_abCharTable;
    }

    StringParser&amp; parse(const char* Source, const char* Parsing, ...)
    {
        this-&gt;Source = Source;
        this-&gt;Parsing = Parsing;

        unsigned int sourcelen = cszlen(Source);
        unsigned int parsinglen = cszlen(Parsing);

        int nType = T_NONE;         // there is no need to make this a member
        m_bIgnore = 0;
        m_nWidth = 1;

        va_list pargs;              // Pointer To List of Arguments
        va_start(pargs, Parsing);   // Requires the last fixed parameter (to get the address)

        m_nParsingIndex = 0;

        for(int i=0; i&lt;sourcelen; i++)
        {
            int s = Source[i];
            int p;

            if(m_nParsingIndex &lt; parsinglen &amp;&amp; p != '%')
            {
                p = Parsing[m_nParsingIndex];
                m_nParsingIndex++;
            }

            if(p == '%' &amp;&amp; Parsing[m_nParsingIndex+1] != '%')
            {
                nType = ParseOption();           // sets ignores, terminates, width, type

                p = Parsing[m_nParsingIndex];
                m_nParsingIndex++;
            }

            if(Source[i] == p){continue;}

            if(m_abCharTable[Source[i&rsqb;&rsqb; == 0){break;}

            for(int j=0; j&lt;m_nWidth; j++)
            {
                if(m_abCharTable[Source[i&rsqb;&rsqb; == 0){return *this;}

                switch(nType)
                {
                    case T_BOOL:
                    {
                        if(m_bIgnore == 0){*va_arg(pargs, bool*) = (bool)Source[i];}
                        i++;
                        break;
                    }

                    case T_CHAR:
                    {
                        if(m_bIgnore == 0)
                        {
                            if(m_nWidth &gt; 1)
                            {
                                char* c = va_arg(pargs, char*);

                                for(int k=0; k&lt;m_nWidth; k++)
                                {
                                    if(m_abCharTable[Source[i&rsqb;&rsqb; == 0){*c = '\0'; return *this;}

                                    *c = Source[i];
                                    c++;
                                    i++;
                                }

                                *c = '\0';
                            }
                            else
                            {
                                *va_arg(pargs, char*) = Source[i];
                                i++;
                            }
                        }
                        else
                        {
                            for(int k=0; k&lt;m_nWidth; k++)
                            {
                                if(m_abCharTable[Source[i&rsqb;&rsqb; == 0){return *this;}
                                i++;
                            }
                        }

                        break;
                    }

                    case T_STRING:
                    {
                        if(m_bIgnore == 0)
                        {
                            char* z = va_arg(pargs, char*);

                            while(m_abCharTable[Source[i&rsqb;&rsqb; == 1)
                            {
                                *z = Source[i];
                                z++;
                                i++;
                            }
                        }
                        else
                        {
                            while(m_abCharTable[Source[i&rsqb;&rsqb; == 1)
                            {
                                i++;
                            }
                        }

                        break;
                    }

                    case T_SHORT:
                    {
                        if(m_bIgnore == 0)
                        {
                            short* ptr;
                            ptr = va_arg(pargs, short*);
                            *ptr = catoi(Source, &amp;i);
                        }
                        else
                        {
                            catoi(Source, &amp;i);
                        }

                        break;
                    }

                    case T_UNSIGNED_SHORT:
                    {
                        if(m_bIgnore == 0)
                        {
                            unsigned short* ptr;
                            ptr = va_arg(pargs, unsigned short*);
                            *ptr = catoi(Source, &amp;i);
                        }
                        else
                        {
                            catoi(Source, &amp;i);
                        }

                        break;
                    }

                    case T_INT:
                    {
                        if(m_bIgnore == 0)
                        {
                            int* ptr;
                            ptr = va_arg(pargs, int*);
                            *ptr = catoi(Source, &amp;i);
                        }
                        else
                        {
                            catoi(Source, &amp;i);
                        }

                        break;
                    }

                    case T_UNSIGNED_INT:
                    {
                        if(m_bIgnore == 0)
                        {
                            unsigned int* ptr;
                            ptr = va_arg(pargs, unsigned int*);
                            *ptr = catoi(Source, &amp;i);
                        }
                        else
                        {
                            catoi(Source, &amp;i);
                        }

                        break;
                    }

                    case T_OCT:
                    {
                        if(m_bIgnore == 0)
                        {
                            int* ptr;
                            ptr = va_arg(pargs, int*);
                            *ptr = catoOct(Source, &amp;i);
                        }
                        else
                        {
                            catoOct(Source, &amp;i);
                        }

                        break;
                    }

                    case T_HEX:
                    {
                        if(m_bIgnore == 0)
                        {
                            int* ptr;
                            ptr = va_arg(pargs, int*);
                            *ptr = catoHex(Source, &amp;i);
                        }
                        else
                        {
                            catoHex(Source, &amp;i);
                        }

                        break;
                    }

                    case T_LONG:
                    {
                        if(m_bIgnore == 0)
                        {
                            long* ptr;
                            ptr = va_arg(pargs, long*);
                            *ptr = catol(Source, &amp;i);
                        }
                        else
                        {
                            catol(Source, &amp;i);
                        }

                        break;
                    }

                    case T_UNSIGNED_LONG:
                    {
                        if(m_bIgnore == 0)
                        {
                            unsigned long* ptr;
                            ptr = va_arg(pargs, unsigned long*);
                            *ptr = catol(Source, &amp;i);
                        }
                        else
                        {
                            catol(Source, &amp;i);
                        }

                        break;
                    }

                    case T_FLOAT:
                    {
                        if(m_bIgnore == 0)
                        {
                            float* ptr;
                            ptr = va_arg(pargs, float*);
                            *ptr = catof(Source, &amp;i);
                        }
                        else
                        {
                            catof(Source, &amp;i);
                        }

                        break;
                    }

                    case T_DOUBLE:
                    {
                        if(m_bIgnore == 0)
                        {
                            double* ptr;
                            ptr = va_arg(pargs, double*);
                            *ptr = catod(Source, &amp;i);
                        }
                        else
                        {
                            catod(Source, &amp;i);
                        }

                        break;
                    }

                    default: break;
                }
            }

            nType = T_NONE;
            m_bIgnore = 0;
            m_nWidth = 1;
        }

        va_end(pargs);

        return *this;
    }

    // checks if char terminates the function when parsed
    bool is_parsed(char c)
    {
        return m_abCharTable[c];
    }

    // checks if char is a digit
    // IT DOESNT TAKE +/-/./, AS A DIGIT!
    bool cisdigit(const char c)
    {
        return c&lt;'0' || c&gt;'9' ? 0 : 1;
    }

    // get octadecimal integer from string
    // e.g. &quot;12&quot;, &quot;o12&quot;, &quot;0o12&quot;
    int catoOct(const char* sz, int* pos)
    {
        char digits[] = {&quot;01234567&quot;};

        int integer = 0;

        bool leading = 1;

        while(sz[*pos] != '\0')
        {
            if(sz[*pos] == 'o'){*pos += 1; leading = 0;}

            for(int i=0; i&lt;8; i++)
            {
                if(sz[*pos] == digits[i])
                {
                    if(sz[*pos] == '0' &amp;&amp; leading){*pos += 1; break;}

                    integer *= 8;
                    integer += i;
                    *pos += 1;

                    if(leading){leading = 0;}

                    break;
                }

                if(i==9){return integer;}
            }
        }

        return integer;
    }

    // get hexadecimal integer from string
    // e.g. &quot;45AD&quot;, &quot;0xFFFF&quot;
    int catoHex(const char* sz, int* pos)
    {
        char digits[] = {&quot;0123456789ABCDEF&quot;};

        int integer = 0;

        bool leading = 1;

        while(sz[*pos] != '\0')
        {
            if(sz[*pos] == 'x'){*pos += 1; leading = 0;}

            for(int i=0; i&lt;16; i++)
            {
                if(sz[*pos] == digits[i])
                {
                    if(sz[*pos] == '0' &amp;&amp; leading){*pos += 1; break;}

                    integer *= 16;
                    integer += i;
                    *pos += 1;

                    if(leading){leading = 0;}

                    break;
                }

                if(i==9){return integer;}
            }
        }

        return integer;
    }

    // get short/int/long from string
    // e.g. &quot;123&quot;, &quot;-123&quot;, &quot;+1&quot;, &quot;00123&quot;
    int catoi(const char* sz, int* pos)
    {
        char digits[] = {&quot;0123456789&quot;};

        int integer = 0;

        int sign = 1;
        bool leading = 1;

        while(sz[*pos] != '\0')
        {
            if(sz[*pos] == '-'){sign = -1; *pos += 1;}
            if(sz[*pos] == '+'){*pos += 1;}

            for(int i=0; i&lt;10; i++)
            {
                if(sz[*pos] == digits[i])
                {
                    if(sz[*pos] == '0' &amp;&amp; leading){*pos += 1; break;}

                    integer *= 10;
                    integer += i;
                    *pos += 1;

                    if(leading){leading = 0;}

                    break;
                }

                if(i==9){return integer * sign;}
            }
        }

        return integer * sign;;
    }

    long catol(const char* sz, int* pos)
    {
        char digits[] = {&quot;0123456789&quot;};

        long integer = 0;

        long sign = 1;
        bool leading = 1;

        while(sz[*pos] != '\0')
        {
            if(sz[*pos] == '-'){sign = -1; *pos += 1;}
            if(sz[*pos] == '+'){*pos += 1;}

            for(int i=0; i&lt;10; i++)
            {
                if(sz[*pos] == digits[i])
                {
                    if(sz[*pos] == '0' &amp;&amp; leading){*pos += 1; break;}

                    integer *= 10;
                    integer += i;
                    *pos += 1;

                    if(leading){leading = 0;}

                    break;
                }

                if(i==9){return integer * sign;}
            }
        }

        return integer * sign;;
    }

    // get float/double from string
    // e.g. &quot;12.34&quot;, &quot;123,4&quot;, &quot;12.34e2&quot;, &quot;+1.2&quot;, &quot;-12.0&quot;, &quot;0012.3&quot;
    float catof(const char* sz, int* pos)
    {
        char digits[] = {&quot;0123456789&quot;};

        float front = 0;
        float back = 0;
        float exp = 1;

        float back_exp = 1;

        float sign = 1;
        int point = 0;      // 0...front, 1...back, 2...exp
        bool leading = 1;

        while(sz[*pos] != '\0')
        {
            if(sz[*pos] == '-'){sign = -1; *pos += 1;}
            if(sz[*pos] == '+'){*pos += 1;}
            if(sz[*pos] == '.' ||sz[*pos] == ','){point = 1; *pos += 1; leading = 0;}
            if(sz[*pos] == 'e' || sz[*pos] == 'E'){point = 2; *pos += 1;}

            for(int i=0; i&lt;10; i++)
            {
                if(sz[*pos] == digits[i])
                {
                    if(sz[*pos] == '0' &amp;&amp; leading){*pos += 1; break;}

                    if(point == 0){front*=10; front+=i;}

                    if(point == 1){back_exp*=0.1f; back*=10; back+=i;}

                    if(point == 2){exp = pow(10, i); }

                    *pos += 1;

                    if(leading){leading = 0;}

                    break;
                }

                if(i==9)
                {
                    return (front+back*back_exp)*sign*exp;
                }
            }
        }

        return (front+back*back_exp)*sign*exp;
    }

    double catod(const char* sz, int* pos)
    {
        char digits[] = {&quot;0123456789&quot;};

        double front = 0;
        double back = 0;
        double exp = 1;

        double back_exp = 1;

        float sign = 1;
        int point = 0;      // 0...front, 1...back, 2...exp
        bool leading = 1;

        while(sz[*pos] != '\0')
        {
            if(sz[*pos] == '-'){sign = -1; *pos += 1;}
            if(sz[*pos] == '+'){*pos += 1;}
            if(sz[*pos] == '.' ||sz[*pos] == ','){point = 1; *pos += 1; leading = 0;}
            if(sz[*pos] == 'e' || sz[*pos] == 'E'){point = 2; *pos += 1;}

            for(int i=0; i&lt;10; i++)
            {
                if(sz[*pos] == digits[i])
                {
                    if(sz[*pos] == '0' &amp;&amp; leading){*pos += 1; break;}

                    if(point == 0){front*=10; front+=i;}

                    if(point == 1){back_exp*=0.1f; back*=10; back+=i;}

                    if(point == 2){exp = pow(10, i); }

                    *pos += 1;

                    if(leading){leading = 0;}

                    break;
                }

                if(i==9)
                {
                    return (front+back*back_exp)*sign*exp;
                }
            }
        }

        return (front+back*back_exp)*sign*exp;
    }

    // get length of a zero-terminated string
    unsigned int cszlen(const char* sz)
    {
        unsigned int n = 0;

        while(*sz)
        {
            if(*sz++ == '\0'){break;}
            n++;
        }

        return n;
    }

    // isnt used in parse() but maybe I need it in future
    template&lt;class T&gt;
    int numbofdigits(T n)
    {
        int digits = 0;
        int step = 1;

        if(n&lt;0){n = -n;}

        while(step &lt;= n)
        {
            digits++;
            step *= 10;
        }
        return digits ? digits : 1;
    }
};

int main()
{
    StringParser P;

    char sz[200];

    //&quot;%d&quot; -&gt; double
    //&quot;%o&quot; -&gt; octadecimal integer
    //&quot;%x&quot; -&gt; hexadecimal integer
    //&quot;%i&quot; -&gt; integer
    //&quot;%l&quot; -&gt; long
    //&quot;%s&quot; -&gt; short
    //&quot;%ui, %ul, %us&quot; -&gt; unsigned int, -long, -short
    //&quot;%z&quot; -&gt; string - not zero-terminated
    //&quot;%4c&quot; -&gt; string with 4 char + '\0'

    P.parse(&quot;Das NAME ein Test bool = 1 !\n&quot;,
            &quot;Das %z ein Test bool = %*b !\n&quot;, sz);

    cout&lt;&lt;sz&lt;&lt;endl;

    system(&quot;PAUSE&quot;);
    return 0;
}
[/cpp]
</code></pre>
<p>Die Funktion kann zurzeit das selbe wie sscanf, später möchte ich sie noch ausbauen (error-handling, mehr datatypes, etc).</p>
<p>Um Kommentare wird gebeten! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/297582/custom-sscanf</link><generator>RSS for Node</generator><lastBuildDate>Fri, 14 Aug 2026 10:48:25 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/297582.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 02 Jan 2012 15:34:26 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Custom sscanf() on Mon, 02 Jan 2012 15:34:26 GMT]]></title><description><![CDATA[<p>Hi!<br />
Ich möchte euch meine eigene sscanf funktion präsentieren.</p>
<pre><code>[cpp]
#include &lt;cstdarg&gt;

using namespace std;

class StringParser
{
  private:
    // Extended ASCII
    bool m_abCharTable[255];

    const char* Source;
    const char* Parsing;

    // Auxiliary values
    int m_nParsingIndex;
    int m_nWidth;
    bool m_bIgnore;

    enum
    {
        // Types, including modified types  //-&gt; Values, must be different from each other
        T_NONE            = 0,              //
        T_BOOL            = 'b',            // -&gt; 98
        T_CHAR            = 'c',            // -&gt; 99
        T_STRING          = 'z',            // -&gt; 122
        T_SHORT           = 's',            // -&gt; 115
        T_UNSIGNED_SHORT  = 'u' + 's',      // -&gt; 232
        T_INT             = 'i',            // -&gt; 105
        T_UNSIGNED_INT    = 'u' + 'i',      // -&gt; 222
        T_HEX             = 'x',            // -&gt; 120
        T_OCT             = 'o',            // -&gt; 111
        T_LONG            = 'l',            // -&gt; 108
        T_UNSIGNED_LONG   = 'u' + 'l',      // -&gt; 225
        T_FLOAT           = 'f',            // -&gt; 102
        T_DOUBLE          = 'd',            // -&gt; 100

        // Modifier
        M_UNSIGNED        = 'u',            // -&gt; 117
    };

    void init()
    {
        // dont parse control codes like TAB, BELL, \n usw. (0-32)
        // dont parse ' ' (33)
        for(int i=0; i&lt;255; i++)
        {
        	if(i&lt;33)
        	{
                m_abCharTable[i] = 0;
        	}
        	else
        	{
        	    m_abCharTable[i] = 1;
        	}
        }
    }

    void set(int c, bool b)
    {
        m_abCharTable[c] = b;
    }

    void set(int c1, int c2, bool b)
    {
        for(int i=c1; i&lt;=c2; i++)
        {
            m_abCharTable[i] = b;
        }
    }

    int ParseOption()         // returning the Type - function can be use repetive
    {
        int n = Parsing[m_nParsingIndex];
        m_nParsingIndex++;

        switch(n)
        {
            case '*':
            {
                m_bIgnore = 1;
                return ParseOption();           // no Type yet - run again and get Type
            }

            case '[':
            {
                int last;
                bool bset = 1;
                bool quit = 0;

                while(quit == 0)
                {
                    n = Parsing[m_nParsingIndex];
                    m_nParsingIndex++;

                    switch(n)
                    {
                        case ']':
                        {
                            quit = 1;
                            break;
                        }

                        case '^':
                        {
                            bset = 0;
                            break;
                        }

                        case '-':
                        {
                            if(Parsing[m_nParsingIndex] != '-')
                            {
                                n = Parsing[m_nParsingIndex];
                                m_nParsingIndex++;

                                set(last, n, bset);
                            }

                            break;
                        }

                        default:
                        {
                            if(Parsing[m_nParsingIndex] != '-')
                            {
                                set(n, bset);
                                bset = 1;
                            }

                            last = n;
                        }
                    }
                }

                return ParseOption();            // this was no Type - 'just an option - run again
            }

            // Put your modifiers here
            // Take this as an example:
            case M_UNSIGNED:
            {
                m_nParsingIndex--;
                return n + ParseOption();       // this is a modifier - run again and get Type
            }

            case ' ': return T_NONE;            // there is no Type

            case '%': return ParseOption();

            default:
            {
                if(cisdigit(n) == 1)            // there is a width
                {
                    m_nParsingIndex--;
                    m_nWidth = catoi(Parsing, &amp;m_nParsingIndex);

                    return ParseOption();       // no Type yet - run again and get Type
                }
                else
                {
                    return n;                   // no Width, no Modifier - so this must be a Type
                }
            }
        }
    }

  public:
    StringParser()
    {
        init();             // intialize m_abCharTable;
    }

    StringParser&amp; parse(const char* Source, const char* Parsing, ...)
    {
        this-&gt;Source = Source;
        this-&gt;Parsing = Parsing;

        unsigned int sourcelen = cszlen(Source);
        unsigned int parsinglen = cszlen(Parsing);

        int nType = T_NONE;         // there is no need to make this a member
        m_bIgnore = 0;
        m_nWidth = 1;

        va_list pargs;              // Pointer To List of Arguments
        va_start(pargs, Parsing);   // Requires the last fixed parameter (to get the address)

        m_nParsingIndex = 0;

        for(int i=0; i&lt;sourcelen; i++)
        {
            int s = Source[i];
            int p;

            if(m_nParsingIndex &lt; parsinglen &amp;&amp; p != '%')
            {
                p = Parsing[m_nParsingIndex];
                m_nParsingIndex++;
            }

            if(p == '%' &amp;&amp; Parsing[m_nParsingIndex+1] != '%')
            {
                nType = ParseOption();           // sets ignores, terminates, width, type

                p = Parsing[m_nParsingIndex];
                m_nParsingIndex++;
            }

            if(Source[i] == p){continue;}

            if(m_abCharTable[Source[i&rsqb;&rsqb; == 0){break;}

            for(int j=0; j&lt;m_nWidth; j++)
            {
                if(m_abCharTable[Source[i&rsqb;&rsqb; == 0){return *this;}

                switch(nType)
                {
                    case T_BOOL:
                    {
                        if(m_bIgnore == 0){*va_arg(pargs, bool*) = (bool)Source[i];}
                        i++;
                        break;
                    }

                    case T_CHAR:
                    {
                        if(m_bIgnore == 0)
                        {
                            if(m_nWidth &gt; 1)
                            {
                                char* c = va_arg(pargs, char*);

                                for(int k=0; k&lt;m_nWidth; k++)
                                {
                                    if(m_abCharTable[Source[i&rsqb;&rsqb; == 0){*c = '\0'; return *this;}

                                    *c = Source[i];
                                    c++;
                                    i++;
                                }

                                *c = '\0';
                            }
                            else
                            {
                                *va_arg(pargs, char*) = Source[i];
                                i++;
                            }
                        }
                        else
                        {
                            for(int k=0; k&lt;m_nWidth; k++)
                            {
                                if(m_abCharTable[Source[i&rsqb;&rsqb; == 0){return *this;}
                                i++;
                            }
                        }

                        break;
                    }

                    case T_STRING:
                    {
                        if(m_bIgnore == 0)
                        {
                            char* z = va_arg(pargs, char*);

                            while(m_abCharTable[Source[i&rsqb;&rsqb; == 1)
                            {
                                *z = Source[i];
                                z++;
                                i++;
                            }
                        }
                        else
                        {
                            while(m_abCharTable[Source[i&rsqb;&rsqb; == 1)
                            {
                                i++;
                            }
                        }

                        break;
                    }

                    case T_SHORT:
                    {
                        if(m_bIgnore == 0)
                        {
                            short* ptr;
                            ptr = va_arg(pargs, short*);
                            *ptr = catoi(Source, &amp;i);
                        }
                        else
                        {
                            catoi(Source, &amp;i);
                        }

                        break;
                    }

                    case T_UNSIGNED_SHORT:
                    {
                        if(m_bIgnore == 0)
                        {
                            unsigned short* ptr;
                            ptr = va_arg(pargs, unsigned short*);
                            *ptr = catoi(Source, &amp;i);
                        }
                        else
                        {
                            catoi(Source, &amp;i);
                        }

                        break;
                    }

                    case T_INT:
                    {
                        if(m_bIgnore == 0)
                        {
                            int* ptr;
                            ptr = va_arg(pargs, int*);
                            *ptr = catoi(Source, &amp;i);
                        }
                        else
                        {
                            catoi(Source, &amp;i);
                        }

                        break;
                    }

                    case T_UNSIGNED_INT:
                    {
                        if(m_bIgnore == 0)
                        {
                            unsigned int* ptr;
                            ptr = va_arg(pargs, unsigned int*);
                            *ptr = catoi(Source, &amp;i);
                        }
                        else
                        {
                            catoi(Source, &amp;i);
                        }

                        break;
                    }

                    case T_OCT:
                    {
                        if(m_bIgnore == 0)
                        {
                            int* ptr;
                            ptr = va_arg(pargs, int*);
                            *ptr = catoOct(Source, &amp;i);
                        }
                        else
                        {
                            catoOct(Source, &amp;i);
                        }

                        break;
                    }

                    case T_HEX:
                    {
                        if(m_bIgnore == 0)
                        {
                            int* ptr;
                            ptr = va_arg(pargs, int*);
                            *ptr = catoHex(Source, &amp;i);
                        }
                        else
                        {
                            catoHex(Source, &amp;i);
                        }

                        break;
                    }

                    case T_LONG:
                    {
                        if(m_bIgnore == 0)
                        {
                            long* ptr;
                            ptr = va_arg(pargs, long*);
                            *ptr = catol(Source, &amp;i);
                        }
                        else
                        {
                            catol(Source, &amp;i);
                        }

                        break;
                    }

                    case T_UNSIGNED_LONG:
                    {
                        if(m_bIgnore == 0)
                        {
                            unsigned long* ptr;
                            ptr = va_arg(pargs, unsigned long*);
                            *ptr = catol(Source, &amp;i);
                        }
                        else
                        {
                            catol(Source, &amp;i);
                        }

                        break;
                    }

                    case T_FLOAT:
                    {
                        if(m_bIgnore == 0)
                        {
                            float* ptr;
                            ptr = va_arg(pargs, float*);
                            *ptr = catof(Source, &amp;i);
                        }
                        else
                        {
                            catof(Source, &amp;i);
                        }

                        break;
                    }

                    case T_DOUBLE:
                    {
                        if(m_bIgnore == 0)
                        {
                            double* ptr;
                            ptr = va_arg(pargs, double*);
                            *ptr = catod(Source, &amp;i);
                        }
                        else
                        {
                            catod(Source, &amp;i);
                        }

                        break;
                    }

                    default: break;
                }
            }

            nType = T_NONE;
            m_bIgnore = 0;
            m_nWidth = 1;
        }

        va_end(pargs);

        return *this;
    }

    // checks if char terminates the function when parsed
    bool is_parsed(char c)
    {
        return m_abCharTable[c];
    }

    // checks if char is a digit
    // IT DOESNT TAKE +/-/./, AS A DIGIT!
    bool cisdigit(const char c)
    {
        return c&lt;'0' || c&gt;'9' ? 0 : 1;
    }

    // get octadecimal integer from string
    // e.g. &quot;12&quot;, &quot;o12&quot;, &quot;0o12&quot;
    int catoOct(const char* sz, int* pos)
    {
        char digits[] = {&quot;01234567&quot;};

        int integer = 0;

        bool leading = 1;

        while(sz[*pos] != '\0')
        {
            if(sz[*pos] == 'o'){*pos += 1; leading = 0;}

            for(int i=0; i&lt;8; i++)
            {
                if(sz[*pos] == digits[i])
                {
                    if(sz[*pos] == '0' &amp;&amp; leading){*pos += 1; break;}

                    integer *= 8;
                    integer += i;
                    *pos += 1;

                    if(leading){leading = 0;}

                    break;
                }

                if(i==9){return integer;}
            }
        }

        return integer;
    }

    // get hexadecimal integer from string
    // e.g. &quot;45AD&quot;, &quot;0xFFFF&quot;
    int catoHex(const char* sz, int* pos)
    {
        char digits[] = {&quot;0123456789ABCDEF&quot;};

        int integer = 0;

        bool leading = 1;

        while(sz[*pos] != '\0')
        {
            if(sz[*pos] == 'x'){*pos += 1; leading = 0;}

            for(int i=0; i&lt;16; i++)
            {
                if(sz[*pos] == digits[i])
                {
                    if(sz[*pos] == '0' &amp;&amp; leading){*pos += 1; break;}

                    integer *= 16;
                    integer += i;
                    *pos += 1;

                    if(leading){leading = 0;}

                    break;
                }

                if(i==9){return integer;}
            }
        }

        return integer;
    }

    // get short/int/long from string
    // e.g. &quot;123&quot;, &quot;-123&quot;, &quot;+1&quot;, &quot;00123&quot;
    int catoi(const char* sz, int* pos)
    {
        char digits[] = {&quot;0123456789&quot;};

        int integer = 0;

        int sign = 1;
        bool leading = 1;

        while(sz[*pos] != '\0')
        {
            if(sz[*pos] == '-'){sign = -1; *pos += 1;}
            if(sz[*pos] == '+'){*pos += 1;}

            for(int i=0; i&lt;10; i++)
            {
                if(sz[*pos] == digits[i])
                {
                    if(sz[*pos] == '0' &amp;&amp; leading){*pos += 1; break;}

                    integer *= 10;
                    integer += i;
                    *pos += 1;

                    if(leading){leading = 0;}

                    break;
                }

                if(i==9){return integer * sign;}
            }
        }

        return integer * sign;;
    }

    long catol(const char* sz, int* pos)
    {
        char digits[] = {&quot;0123456789&quot;};

        long integer = 0;

        long sign = 1;
        bool leading = 1;

        while(sz[*pos] != '\0')
        {
            if(sz[*pos] == '-'){sign = -1; *pos += 1;}
            if(sz[*pos] == '+'){*pos += 1;}

            for(int i=0; i&lt;10; i++)
            {
                if(sz[*pos] == digits[i])
                {
                    if(sz[*pos] == '0' &amp;&amp; leading){*pos += 1; break;}

                    integer *= 10;
                    integer += i;
                    *pos += 1;

                    if(leading){leading = 0;}

                    break;
                }

                if(i==9){return integer * sign;}
            }
        }

        return integer * sign;;
    }

    // get float/double from string
    // e.g. &quot;12.34&quot;, &quot;123,4&quot;, &quot;12.34e2&quot;, &quot;+1.2&quot;, &quot;-12.0&quot;, &quot;0012.3&quot;
    float catof(const char* sz, int* pos)
    {
        char digits[] = {&quot;0123456789&quot;};

        float front = 0;
        float back = 0;
        float exp = 1;

        float back_exp = 1;

        float sign = 1;
        int point = 0;      // 0...front, 1...back, 2...exp
        bool leading = 1;

        while(sz[*pos] != '\0')
        {
            if(sz[*pos] == '-'){sign = -1; *pos += 1;}
            if(sz[*pos] == '+'){*pos += 1;}
            if(sz[*pos] == '.' ||sz[*pos] == ','){point = 1; *pos += 1; leading = 0;}
            if(sz[*pos] == 'e' || sz[*pos] == 'E'){point = 2; *pos += 1;}

            for(int i=0; i&lt;10; i++)
            {
                if(sz[*pos] == digits[i])
                {
                    if(sz[*pos] == '0' &amp;&amp; leading){*pos += 1; break;}

                    if(point == 0){front*=10; front+=i;}

                    if(point == 1){back_exp*=0.1f; back*=10; back+=i;}

                    if(point == 2){exp = pow(10, i); }

                    *pos += 1;

                    if(leading){leading = 0;}

                    break;
                }

                if(i==9)
                {
                    return (front+back*back_exp)*sign*exp;
                }
            }
        }

        return (front+back*back_exp)*sign*exp;
    }

    double catod(const char* sz, int* pos)
    {
        char digits[] = {&quot;0123456789&quot;};

        double front = 0;
        double back = 0;
        double exp = 1;

        double back_exp = 1;

        float sign = 1;
        int point = 0;      // 0...front, 1...back, 2...exp
        bool leading = 1;

        while(sz[*pos] != '\0')
        {
            if(sz[*pos] == '-'){sign = -1; *pos += 1;}
            if(sz[*pos] == '+'){*pos += 1;}
            if(sz[*pos] == '.' ||sz[*pos] == ','){point = 1; *pos += 1; leading = 0;}
            if(sz[*pos] == 'e' || sz[*pos] == 'E'){point = 2; *pos += 1;}

            for(int i=0; i&lt;10; i++)
            {
                if(sz[*pos] == digits[i])
                {
                    if(sz[*pos] == '0' &amp;&amp; leading){*pos += 1; break;}

                    if(point == 0){front*=10; front+=i;}

                    if(point == 1){back_exp*=0.1f; back*=10; back+=i;}

                    if(point == 2){exp = pow(10, i); }

                    *pos += 1;

                    if(leading){leading = 0;}

                    break;
                }

                if(i==9)
                {
                    return (front+back*back_exp)*sign*exp;
                }
            }
        }

        return (front+back*back_exp)*sign*exp;
    }

    // get length of a zero-terminated string
    unsigned int cszlen(const char* sz)
    {
        unsigned int n = 0;

        while(*sz)
        {
            if(*sz++ == '\0'){break;}
            n++;
        }

        return n;
    }

    // isnt used in parse() but maybe I need it in future
    template&lt;class T&gt;
    int numbofdigits(T n)
    {
        int digits = 0;
        int step = 1;

        if(n&lt;0){n = -n;}

        while(step &lt;= n)
        {
            digits++;
            step *= 10;
        }
        return digits ? digits : 1;
    }
};

int main()
{
    StringParser P;

    char sz[200];

    //&quot;%d&quot; -&gt; double
    //&quot;%o&quot; -&gt; octadecimal integer
    //&quot;%x&quot; -&gt; hexadecimal integer
    //&quot;%i&quot; -&gt; integer
    //&quot;%l&quot; -&gt; long
    //&quot;%s&quot; -&gt; short
    //&quot;%ui, %ul, %us&quot; -&gt; unsigned int, -long, -short
    //&quot;%z&quot; -&gt; string - not zero-terminated
    //&quot;%4c&quot; -&gt; string with 4 char + '\0'

    P.parse(&quot;Das NAME ein Test bool = 1 !\n&quot;,
            &quot;Das %z ein Test bool = %*b !\n&quot;, sz);

    cout&lt;&lt;sz&lt;&lt;endl;

    system(&quot;PAUSE&quot;);
    return 0;
}
[/cpp]
</code></pre>
<p>Die Funktion kann zurzeit das selbe wie sscanf, später möchte ich sie noch ausbauen (error-handling, mehr datatypes, etc).</p>
<p>Um Kommentare wird gebeten! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2162924</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2162924</guid><dc:creator><![CDATA[Polygon]]></dc:creator><pubDate>Mon, 02 Jan 2012 15:34:26 GMT</pubDate></item><item><title><![CDATA[Reply to Custom sscanf() on Mon, 02 Jan 2012 16:28:51 GMT]]></title><description><![CDATA[<p>Polygon schrieb:</p>
<blockquote>
<p>Um Kommentare wird gebeten! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
</blockquote>
<p>tl;dr</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2162946</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2162946</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 02 Jan 2012 16:28:51 GMT</pubDate></item><item><title><![CDATA[Reply to Custom sscanf() on Tue, 03 Jan 2012 10:07:47 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Polygon schrieb:</p>
<blockquote>
<p>Um Kommentare wird gebeten! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
</blockquote>
<p>tl;dr</p>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
<p>aber:<br />
<code>//&quot;%z&quot; -&gt; string - not zero-terminated</code><br />
hm? wie soll das gehen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2163161</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2163161</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Tue, 03 Jan 2012 10:07:47 GMT</pubDate></item></channel></rss>