File: EStdStringTokenizer.cc

package info (click to toggle)
miwm 1.1-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,296 kB
  • ctags: 910
  • sloc: cpp: 8,179; sh: 231; makefile: 148
file content (94 lines) | stat: -rw-r--r-- 3,377 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

#include "EStdStringTokenizer.h"
//#include <iostream> // only for debuggering

        enum quoteEnum { NO_QUOTE=0, SINGLE_QUOTE, DOUBLE_QUOTE };
        /**
           todo: implement a map<char,int> to count opening/closing quotes so i can add support
           for open/close { } without having to add to the enum, add if(), etc.
           map['\''] = 0. increment on an opener, decrement on a closer.
        */


        EStdStringTokenizer::EStdStringTokenizer()
        {
        }

        void EStdStringTokenizer::tokenize( const string &str, const string & sep )
        {
                const string::size_type ssize = str.size();
                if ( ssize == 0 ) return;
                if ( ssize == 1 )
                {
                        this->m_list.push( str ); //  += str;
                        return;
                }
                if( string::npos == str.find_first_of( sep ) )
                {
                        this->m_list.push( str ); // += str;
                        return;
                }

                int quoted = NO_QUOTE;
                char chat;
                string mystr;
                bool addit;
                for( string::size_type pos = 0; (pos < ssize); pos++ )
                {
                        chat = str[pos];
                        addit = true;
                        if ( (chat == '\"' ) && !quoted)
                        {
                                quoted = DOUBLE_QUOTE;
                                addit = false;
                        }
                        else if ( ( chat == '\'') && !quoted) 
                        {
                                quoted = SINGLE_QUOTE;
                                addit = false;
                        }
                        else if ( ( (chat == '\"') && (quoted == DOUBLE_QUOTE) )
                                  ||
                                  ( (chat == '\'') && (quoted == SINGLE_QUOTE) ) )
                        {
                                quoted = NO_QUOTE;
                                addit = false;
                        } 
                        
                       if ( !quoted ) 
                       {
                                for( string::size_type i = 0; i < sep.size(); i++ )
                                {
                                        if( chat == sep[i] )
                                        {
                                                m_list.push( mystr ); // += mystr;
                                                mystr = string();
                                                addit = false;
                                                break;
                                        }
                                }
                        }

                        if( addit ) mystr += chat;
                }
                if( ! mystr.empty() ) m_list.push( mystr ); // += mystr;
                return;
        }

        bool
        EStdStringTokenizer::hasMoreTokens() const
        {
                return ! this->m_list.empty();
        }
        std::string
        EStdStringTokenizer::nextToken()
        {
                string foo = this->m_list.front();
                this->m_list.pop();
                return foo;
        }

        EStdStringTokenizer::~EStdStringTokenizer()
        {
        }