Skip to content

Instantly share code, notes, and snippets.

@Taymindis
Created September 18, 2017 03:30
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 Taymindis/ebc0ba7b1b376b4feab01c935e9dc17e to your computer and use it in GitHub Desktop.
Save Taymindis/ebc0ba7b1b376b4feab01c935e9dc17e to your computer and use it in GitHub Desktop.
Get ParamKey From URL Query String, this is derived from https://github.com/Taymindis/fcgi-function
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int strpos(const char *haystack, const char *needle)
{
char *p = strstr(haystack, needle);
if (p)
return p - haystack;
return -1; // Not found = -1.
}
void* csif_getParam(const char *key, const char* query_str) {
int len, pos;
char *qs = query_str;
if (key && *key && qs && *qs) {
len = strlen(key);
do {
if ((pos = strpos(qs, key)) < 0) return NULL;
if (pos == 0 || qs[pos - 1] == '&') {
qs = (char*)qs + pos + len;
if (*qs++ == '=') {
char *src = qs,
*ret;
size_t sz = 0;
while (*qs && *qs++ != '&')sz++;
ret = malloc(sz + 1);
memset(ret, 0, sz + 1);
return memcpy(ret, src, sz);
} else while (*qs && *qs++ != '&');
} else while (*qs && *qs++ != '&');
} while (*qs);
}
return NULL;
}
int main ()
{
const char str1[] = "keyword=GHI&userId2=20&userId=100&userId3=S@102130";
const char str2[] = "my_id";
char* found = csif_getParam("keyword", str1);
char* found2 = csif_getParam("userId2", str1);
char* found3 = csif_getParam("userId", str1);
if (found) {
printf("%s\n", found);
free(found);
found = csif_getParam("userId3", str1);
if (found) {
printf("%s\n", found);
free(found);
}
}
if (found2) {
printf("%s\n", found2);
free(found2);
}
if (found3) {
printf("%s\n", found3);
free(found3);
}
return (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment