~ubuntu-branches/ubuntu/quantal/miwm/quantal

« back to all changes in this revision

Viewing changes to EStdStringTokenizer.cc

  • Committer: Bazaar Package Importer
  • Author(s): Jari Aalto
  • Date: 2010-01-04 15:25:34 UTC
  • Revision ID: james.westby@ubuntu.com-20100104152534-l3fdvt162le460cv
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include "EStdStringTokenizer.h"
 
3
//#include <iostream> // only for debuggering
 
4
 
 
5
        enum quoteEnum { NO_QUOTE=0, SINGLE_QUOTE, DOUBLE_QUOTE };
 
6
        /**
 
7
           todo: implement a map<char,int> to count opening/closing quotes so i can add support
 
8
           for open/close { } without having to add to the enum, add if(), etc.
 
9
           map['\''] = 0. increment on an opener, decrement on a closer.
 
10
        */
 
11
 
 
12
 
 
13
        EStdStringTokenizer::EStdStringTokenizer()
 
14
        {
 
15
        }
 
16
 
 
17
        void EStdStringTokenizer::tokenize( const string &str, const string & sep )
 
18
        {
 
19
                const string::size_type ssize = str.size();
 
20
                if ( ssize == 0 ) return;
 
21
                if ( ssize == 1 )
 
22
                {
 
23
                        this->m_list.push( str ); //  += str;
 
24
                        return;
 
25
                }
 
26
                if( string::npos == str.find_first_of( sep ) )
 
27
                {
 
28
                        this->m_list.push( str ); // += str;
 
29
                        return;
 
30
                }
 
31
 
 
32
                int quoted = NO_QUOTE;
 
33
                char chat;
 
34
                string mystr;
 
35
                bool addit;
 
36
                for( string::size_type pos = 0; (pos < ssize); pos++ )
 
37
                {
 
38
                        chat = str[pos];
 
39
                        addit = true;
 
40
                        if ( (chat == '\"' ) && !quoted)
 
41
                        {
 
42
                                quoted = DOUBLE_QUOTE;
 
43
                                addit = false;
 
44
                        }
 
45
                        else if ( ( chat == '\'') && !quoted) 
 
46
                        {
 
47
                                quoted = SINGLE_QUOTE;
 
48
                                addit = false;
 
49
                        }
 
50
                        else if ( ( (chat == '\"') && (quoted == DOUBLE_QUOTE) )
 
51
                                  ||
 
52
                                  ( (chat == '\'') && (quoted == SINGLE_QUOTE) ) )
 
53
                        {
 
54
                                quoted = NO_QUOTE;
 
55
                                addit = false;
 
56
                        } 
 
57
                        
 
58
                       if ( !quoted ) 
 
59
                       {
 
60
                                for( string::size_type i = 0; i < sep.size(); i++ )
 
61
                                {
 
62
                                        if( chat == sep[i] )
 
63
                                        {
 
64
                                                m_list.push( mystr ); // += mystr;
 
65
                                                mystr = string();
 
66
                                                addit = false;
 
67
                                                break;
 
68
                                        }
 
69
                                }
 
70
                        }
 
71
 
 
72
                        if( addit ) mystr += chat;
 
73
                }
 
74
                if( ! mystr.empty() ) m_list.push( mystr ); // += mystr;
 
75
                return;
 
76
        }
 
77
 
 
78
        bool
 
79
        EStdStringTokenizer::hasMoreTokens() const
 
80
        {
 
81
                return ! this->m_list.empty();
 
82
        }
 
83
        std::string
 
84
        EStdStringTokenizer::nextToken()
 
85
        {
 
86
                string foo = this->m_list.front();
 
87
                this->m_list.pop();
 
88
                return foo;
 
89
        }
 
90
 
 
91
        EStdStringTokenizer::~EStdStringTokenizer()
 
92
        {
 
93
        }
 
94