Switch.cpp 6.32 KB
Newer Older
/jdi/'s avatar
/jdi/ committed
1
2
3
4
5
6
7
/*
 * Switch.cpp
 *
 *  Created on: 29.09.2015
 *      Author: jdi
 */

/jdi/'s avatar
/jdi/ committed
8
#include <string>
/jdi/'s avatar
/jdi/ committed
9
#include "Types.h"
/jdi/'s avatar
/jdi/ committed
10
#include "Switch.h"
/jdi/'s avatar
/jdi/ committed
11
12
#include "Lookup.h"
#include "Options.h"
/jdi/'s avatar
/jdi/ committed
13

/jdi/'s avatar
/jdi/ committed
14
15
16
17
18
int Switch::parse(datasets arr) {
	for (auto a : arr) {
		parse(a.second);
	}
	return 0;
/jdi/'s avatar
/jdi/ committed
19
20
}

/jdi/'s avatar
/jdi/ committed
21
int Switch::parse(dataset d) {
/jdi/'s avatar
/jdi/ committed
22
	auto lookup = (options.flags & FLAG_REVERSE) ? snd_lookup : rcv_lookup;
/jdi/'s avatar
/jdi/ committed
23
	if (d.type == lookup["type"]) {
/jdi/'s avatar
/jdi/ committed
24
25
		device.type = d.value;
	}
/jdi/'s avatar
/jdi/ committed
26
	if (d.type == lookup["mac"]) {
/jdi/'s avatar
/jdi/ committed
27
28
		device.mac = d.value;
	}
/jdi/'s avatar
/jdi/ committed
29
30
31
32
33
34
35
36
37
38
39
	if (d.type == lookup["firmware_version"]) {
		device.firmware_version = d.value;
	}
	if (d.type == lookup["hardware_version"]) {
		device.hardware_version = d.value;
	}
	if (d.type == lookup["hostname"]) {
		settings.hostname = d.value;
	}
	if (d.type == lookup["ip_addr"]) {
		settings.ip_addr = d.value;
/jdi/'s avatar
/jdi/ committed
40
	}
/jdi/'s avatar
/jdi/ committed
41
42
43
44
45
46
47
48
49
50
	if (d.type == lookup["ip_mask"]) {
		settings.ip_mask = d.value;
	}
	if (d.type == lookup["gateway"]) {
		settings.gateway = d.value;
	}
	if (d.type == lookup["dhcp"]) {
		settings.dhcp = d.value[0];
	}
	return 0;
/jdi/'s avatar
/jdi/ committed
51
52
}

/jdi/'s avatar
/jdi/ committed
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
int Switch::parse(std::string str) {

    if (json.Parse(str.c_str()).HasParseError())
        return 1;
	if (options.flags & FLAG_DEBUG)
    std::cout<<"\nParsing to document succeeded.\n";
























    ////////////////////////////////////////////////////////////////////////////
        // 2. Access values in document.

        printf("\nAccess values in document:\n");
        assert(json.IsObject());    // Document is a JSON value represents the root of DOM. Root can be either an object or array.

        assert(json.HasMember("hello"));
        assert(json["hello"].IsString());
        printf("hello = %s\n", json["hello"].GetString());

        // Since version 0.2, you can use single lookup to check the existing of member and its value:
        rapidjson::Value::MemberIterator hello = json.FindMember("hello");
        assert(hello != json.MemberEnd());
        assert(hello->value.IsString());
        assert(strcmp("world", hello->value.GetString()) == 0);
        (void)hello;

        assert(json["t"].IsBool());     // JSON true/false are bool. Can also uses more specific function IsTrue().
        printf("t = %s\n", json["t"].GetBool() ? "true" : "false");

        assert(json["f"].IsBool());
        printf("f = %s\n", json["f"].GetBool() ? "true" : "false");

        printf("n = %s\n", json["n"].IsNull() ? "null" : "?");

        assert(json["i"].IsNumber());   // Number is a JSON type, but C++ needs more specific type.
        assert(json["i"].IsInt());      // In this case, IsUint()/IsInt64()/IsUInt64() also return true.
        printf("i = %d\n", json["i"].GetInt()); // Alternative (int)document["i"]

        assert(json["pi"].IsNumber());
        assert(json["pi"].IsDouble());
        printf("pi = %g\n", json["pi"].GetDouble());

        {
            const rapidjson::Value& a = json["a"]; // Using a reference for consecutive access is handy and faster.
            assert(a.IsArray());
            for (rapidjson::SizeType i = 0; i < a.Size(); i++) // rapidjson uses SizeType instead of size_t.
                printf("a[%d] = %d\n", i, a[i].GetInt());

            int y = a[0].GetInt();
            (void)y;

            // Iterating array with iterators
            printf("a = ");
            for (rapidjson::Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr)
                printf("%d ", itr->GetInt());
            printf("\n");
        }

        // Iterating object members
        static const char* kTypeNames[] = { "Null", "False", "True", "Object", "Array", "String", "Number" };
        for (rapidjson::Value::ConstMemberIterator itr = json.MemberBegin(); itr != json.MemberEnd(); ++itr)
            printf("Type of member %s is %s\n", itr->name.GetString(), kTypeNames[itr->value.GetType()]);



	return 0;
}

std::string Switch::toString() {
	////////////////////////////////////////////////////////////////////////////
	    // 3. Modify values in document.

	    // Change i to a bigger number
	    {
	        uint64_t f20 = 1;   // compute factorial of 20
	        for (uint64_t j = 1; j <= 20; j++)
	            f20 *= j;
	        json["i"] = f20;    // Alternate form: document["i"].SetUint64(f20)
	        assert(!json["i"].IsInt()); // No longer can be cast as int or uint.
	    }

	    // Adding values to array.
	    {
	    	rapidjson::Value& a = json["a"];   // This time we uses non-const reference.
	        rapidjson::Document::AllocatorType& allocator = json.GetAllocator();
	        for (int i = 5; i <= 10; i++)
	            a.PushBack(i, allocator);   // May look a bit strange, allocator is needed for potentially realloc. We normally uses the document's.

	        // Fluent API
	        a.PushBack("Lua", allocator).PushBack("Mio", allocator);
	    }

	    // Making string values.

	    // This version of SetString() just store the pointer to the string.
	    // So it is for literal and string that exists within value's life-cycle.
	    {
	        json["hello"] = "hostname"; //settings.hostname;    // This will invoke strlen()
	        // Faster version:
	        // document["hello"].SetString("rapidjson", 9);
	    }

	    // This version of SetString() needs an allocator, which means it will allocate a new buffer and copy the the string into the buffer.
	    rapidjson::Value author;
	    {
	        char buffer[10];
	        int len = sprintf(buffer, "%s %s", "Milo", "Yip");  // synthetic example of dynamically created string.

	        author.SetString(buffer, static_cast<size_t>(len), json.GetAllocator());
	        // Shorter but slower version:
	        // document["hello"].SetString(buffer, document.GetAllocator());

	        // Constructor version:
	        // Value author(buffer, len, document.GetAllocator());
	        // Value author(buffer, document.GetAllocator());
	        memset(buffer, 0, sizeof(buffer)); // For demonstration purpose.
	    }
	    // Variable 'buffer' is unusable now but 'author' has already made a copy.
	    json.AddMember("author", author, json.GetAllocator());

	    assert(author.IsNull());        // Move semantic for assignment. After this variable is assigned as a member, the variable becomes null.















    std::cout<<"\nModified JSON with reformatting:\n";
    rapidjson::StringBuffer sb;
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
    json.Accept(writer);    // Accept() traverses the DOM and generates Handler events.
    return sb.GetString();
}