-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringParser.cpp
More file actions
50 lines (42 loc) · 2.5 KB
/
StringParser.cpp
File metadata and controls
50 lines (42 loc) · 2.5 KB
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
#include "StringParser.h"
#include <cstring>
#include <iostream>
// This class will take a string that is passed to it in this format:
// input to constructor:
// <variable1>=<value1>;<variable2>=<value2>;
// You will then call the method getNextKeyValue until getNextKeyValue returns NULL.
// getNextKeyValue will return a KeyValue object. Inside of that KeyValue object will contain the variable and the value
// You will then call getKey or getValue to get the contents of those fields.
// The example in main() will show how to call this function.
// By extracting the contents you then can determine the rpc you need to switch to, along with variables you will need
// You can also use this class in your client program, since you will need to determine the contents that you receive from server
StringParser::StringParser() = default;
StringParser::~StringParser() {
if (m_pKeyValue)
delete (m_pKeyValue);
}
void StringParser::newRPC(char *szUnformattedString) {
// assert(strlen(szUnformattedString)); // debugging code, makes sure that there's a string maybe?
strcpy(rawString, szUnformattedString); // copies the code, to prevent it from editing the original
m_pch = rawString;
}
void StringParser::getNextKeyValue(KeyValue &keyVal) {
// It will attempt to parse out part of the string all the way up to the ";", it will then create a new KeyValue object with that partial string
// If it can;t it will return null;
char *pch1; // pointer character 1
char szTemp[32768];
pch1 = strchr(m_pch, ';'); // returns a pointer to the first instance of ;
// assert(pch1 != NULL); // debugging: assert that pch1 doesn't point to null
int subStringSize = (int)(pch1 - m_pch); // creates a new string starting from pch1
strncpy(szTemp, m_pch, subStringSize); // copies the string from m_pch to szTemp, subStringSize characters
szTemp[subStringSize] = 0; // the final character is now 0; ???
m_pch = pch1 + 1;
if (m_pKeyValue)
delete (m_pKeyValue);
keyVal.setKeyValue(szTemp); // sends szTemp to keyValue
}
StringParser::StringParser(char *szUnformattedString) {
// assert(strlen(szUnformattedString)); // debugging code, makes sure that there's a string maybe?
strcpy(rawString, szUnformattedString); // copies the code, to prevent it from editing the original
m_pch = rawString; // copies again, into a character array
}