Created
December 23, 2015 10:15
-
-
Save omerfarukz/9ad9b7008b5a44dff108 to your computer and use it in GitHub Desktop.
dummy query parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace demo2 | |
{ | |
class MainClass | |
{ | |
public static void Main(string[] args) | |
{ | |
Interpret("command=read;pin=3;callback=handle_temperature"); | |
Console.ReadKey(); | |
} | |
static void Interpret(string command) | |
{ | |
if (command == null) | |
return; | |
int keyValueCount = 0; | |
for (int i = 0; i < command.Length; i++) | |
{ | |
if (command[i] == '=') | |
++keyValueCount; | |
} | |
char[][] keyValues = new char[keyValueCount * 2][]; | |
int currentKeyValuePairIndex = 0; | |
int startIndex = 0; | |
for (int i = 0; i < command.Length; i++) | |
{ | |
// read key and value pairs | |
if (command[i] == '=') | |
{ | |
int keyLength = i - startIndex; | |
char[] key = new char[keyLength]; | |
for (int keyIndex = 0; keyIndex < keyLength; keyIndex++) | |
{ | |
key[keyIndex] = command[startIndex + keyIndex]; | |
} | |
startIndex = i + 1; | |
for (int j = startIndex; j < command.Length; j++) | |
{ | |
if (command[j] == ';' || j == command.Length - 1) | |
{ | |
int valueLength = j - startIndex; | |
char[] value = new char[valueLength]; | |
for (int valueIndex = 0; valueIndex < valueLength; valueIndex++) | |
{ | |
value[valueIndex] = command[startIndex + valueIndex]; | |
} | |
keyValues[currentKeyValuePairIndex++] = key; | |
keyValues[currentKeyValuePairIndex++] = value; | |
startIndex = j + 1; | |
break; | |
} | |
} | |
} | |
// read next key, value pair | |
} | |
// keyValues = command, read, pin, 3, callback, handle_temperature | |
// TODO: execute | |
if (keyValues[0] == "commmand") | |
{ | |
// TODO: ... | |
if (keyValues[2] = "pin") | |
{ | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment