Skip to content

Instantly share code, notes, and snippets.

@omerfarukz
Created December 23, 2015 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omerfarukz/9ad9b7008b5a44dff108 to your computer and use it in GitHub Desktop.
Save omerfarukz/9ad9b7008b5a44dff108 to your computer and use it in GitHub Desktop.
dummy query parser
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