Skip to content

Instantly share code, notes, and snippets.

@Inviz
Created July 14, 2016 02:50
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 Inviz/6db707d46358e777e0bc6e41a0c3c7a6 to your computer and use it in GitHub Desktop.
Save Inviz/6db707d46358e777e0bc6e41a0c3c7a6 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
int array_push_sorted(char *array[128], int size, char *string, int length) {
int j, k;
char *old;
for (j = 0; j < size; j++ ) {
old = array[j];
int i = 0;
for (; i < length; i++)
if (string[i] != old[i])
break;
if (string[i] < old[i]) {
// shift
for (k = size; --k >= j;)
array[k + 1] = array[k];
break;
}
}
array[j] = string;
return size + 1;
}
int main () {
char *array[128];
char response[1024];
int length = 0;
char *qs = "person[abc]=def&person[cba][af]=eee&hello=world&person[1]=2&baba=bang&zerson[a]=2\0";
int size = strlen(qs);
char *last = qs;
for (char *p = qs; p < qs + size; p++) {
if (*p == '&' || p == qs + size - 1) {
length = array_push_sorted(array, length, last, p - last - 1);
last = p + 1;
}
}
*response = '{';
int lastch = 0;
char *finish, *start, *position, *previous, *v;
for (int i = 0; i < length; i++) {
start = *(array + i);
position = start;
int separated = 0;
int closed = 0;
for (char *p = *(array + i); p < qs + size; p++) {
char *next = p;
lastch = (p == qs + size - 1 || *next == '&' || *next == '=') ? 1 : 0;
// found boundaries of a keyword (EOL, [ or ])
if (*next == '[' || lastch) {
finish = p;
int prepended = 0;
if (previous != NULL) {
// Dont prepend context that matches with previous key
int len = last - previous;
if (strncmp(previous, start, len > position - start ? len : position - start) == 0) {
fprintf(stdout, "\n%d %d \n%s\n%s\n", len, position - start, position, last);
if (!lastch && len >= finish - start)
prepended = 1;
}
// close all mismatching objects
if (len > finish - start && prepended == 0 && closed == 0) {
closed = 1;
for (char *m = previous + 1; m < last; m++)
if (*m == '[')
strcat(response, "}\n");
}
// add comma
if (!separated) {
separated = 1;
strcat(response, ",");
}
}
// prepend key
if (!prepended) {
char *fin = *(finish - 1) == ']' ? finish - 1 : finish;
char *pos = *position == '[' ? position + 1 : position;
strcat(response, "\"");
strncpy(response + strlen(response), pos, fin - pos);
}
if (lastch) {
strcat(response, "\":\"");
// find value
char *v = finish + 1;
for (; v < qs + size; v++) {
if (*v == '&' || *v == '=')
break;
}
// append value
strncpy(response + strlen(response), finish + 1, v - finish - 1);
strcat(response, "\"\n");
// remember path
previous = start;
last = position;
} else {
// prepend key
if (!prepended) strcat(response, "\": {\n");
if (last != NULL) {
}
position = finish + 1;
}
}
if (lastch) break;
}
}
// close all objects
for (char *m = previous + 1; m < last; m++)
if (*m == '[')
strcat(response, "}\n");
sprintf(response + strlen(response), "}");
fprintf(stdout, "\n\n%s\n", response);
for (int i = 0; i < length; i++)
fprintf(stdout, "Result %s\n", *(array + i));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment