aboutsummaryrefslogtreecommitdiff
path: root/src/lexer/parseline.cc
blob: 064f8768fa4de05cb1b0927f6038edadc3be610e (plain)
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

#include "lexer/parseline.h"

#include <vector>

#include "lexer/enum_type.h"
#include "util/string.h"

using namespace std;

namespace ov4
{

char **parseline(const std::string &s)
{
    guard_type guard = GUARD_OFF;
    vector<string> v;
    size_t len = s.size();
    string prev_str = "";
    char c;

    for (size_t i = 0; i < len; i++)
    {

        c = s[i];
        if (c == ' ' && guard == GUARD_OFF) continue;
        
        if (c == '\'' || c == '\"')
        {

            // turn on GUARD
            if (guard == GUARD_OFF)
            {
                guard = (c == '\'') ? GUARD_STRONG : GUARD_WEAK;
                if (i+1 == len)
                {
                    v.push_back(prev_str);
                    prev_str = "";
                }
                continue;
            }
            // turn off
            else if (guard == GUARD_STRONG && c == '\'')
            {
                guard = GUARD_OFF;
                if (i+1 == len)
                {
                    v.push_back(prev_str);
                    prev_str = "";
                }
                continue;
            }
            // turn off
            else if (guard == GUARD_WEAK && c == '\"')
            {
                guard = GUARD_OFF;
                if (i+1 == len)
                {
                    v.push_back(prev_str);
                    prev_str = "";
                }
                continue;
            } 
            // if this (single/double) quotation mark is guarded
            // it should be saved
        }

        prev_str += c;

        if ((i+1 >= len) || (s[i+1] == ' ' && guard == GUARD_OFF))
        {
            v.push_back(prev_str);
            prev_str = "";
            continue;
        }
    }

    return string2argv_alloc(v);
}

}