#include <stdio.h>
#include <string.h>

// call char *s strings
typedef char * string;


typedef struct {
	string key;
	string value;
} pair;

void printQuery(string query, string splitBy) {
	/*string queryStart = query;
	
	// skip through bad characters
	while(*queryStart == splitBy || *queryStart == associateBy) {
		queryStart++;
	}
	
	// point query to the real starting place
	query = queryStart;
	
	int numPairs = 0;
	
	// not at the end
	for(query = queryStart; *query; query++) {
		if(*query == splitBy && *(query + 1) != splitBy && *(query + 1)) {
			numPairs++;
		}
	}
	
	pair* queries = (pair*)malloc(sizeof(pair) * numPairs);
	
	int index = 0;
	
	for(query = queryStart; *query; query++) {
		if(
	}*/
	
	string key = NULL;
	
	for(query = strtok(query, splitBy); query != NULL; query = strtok(NULL, splitBy)) {
		// we found a key
		if(key == NULL) {
			key = query;
		} else {
			// we could also add it to a list
			printf("\t<li>Pair: %s = %s</li>\n", key, query);
			key = NULL;
		}
	}
}

int main(int argc, string argv[]) {
	int i;
	
	string splitBy = "&/:-.";
	
	// we have to print this header
	printf("Content-Type: text/html\n\n");
	
	printf("Hello world. Arguments:\n<ol>\n");
	
	for(i = 0; i < argc; i++) {
		printf("\t<li>%s</li>\n", argv[i]);
	}
	
	printf("</ol>\n");
	
	if(argc > 1) {
		printf("Detected argument(s) through URL: %s\n", argv[1]);
		printf("Attempted parsing with separators\n", splitBy);
		
		printf("<ol>\n");
		printQuery(argv[1], splitBy);
		printf("</ol>\n");
	}

	return 0;
}

