الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

تحليل ال Query String بإستخدام سي

بدأه mhewedy في 19 فبراير 2011 · 0 رد · 458 مشاهدة · في لغة C و ++C
مشاركة: واتساب X فيسبوك تيليجرام
#1

السلام عليكم..

قمت بكتابة دالة تقوم بعمل parse (او تحليل) لل Query String المرسل من المتصفح لإستخدامها في برامج ال CGI...

أرجو من الأخوه التعليق عليها و توجيهي للأخطاء بالكود و (ال memory leaks)..

أولا stringutil ... و هي تستخدم من قبل httputil التي بها الدالة التي نتحدث عنها:

//stringutil.h
#ifndef _STRING_UTIL_
#define _STRING_UTIL_

static size_t count(const char*, char);
size_t split(const char *const, const char*, char***);

#endif

//stringutil.c
#include <string.h>
#include <stdlib.h>
#include "stringutil.h"

static size_t count(const char *str, char ch)
{
	if (str == NULL) return 0;
	size_t count = 1;
	while (*str)
    	if (*str++ == ch) count++;
	return count;
}

size_t split(const char *const str, const char* delim, char ***out)
{
	size_t size = count(str, *delim);
	*out = calloc(size, sizeof(char));
	char* token = NULL;
	char* tmp = (char*) str;
	int i=0;
	while ((token = strtok(tmp, delim)) != NULL)
	{
    	tmp = NULL;
    	(*out) = (char*) malloc(sizeof strlen(token));
    	strcpy((*out)[i++], token);
	}
	return size;
}

ثانيا httputil :

//httputil.h
#ifndef _HTTP_UTIL_
#define _HTTP_UTIL_

static char ***out;

/**
 * get parameter value from the QUERY_STRING
 * return NULL if QUERY_STRING is null or the key has no associated value or if key found but without a value
 * returns pointer to char that you should free after use.
 */
char* get_parameter(char*);

#endif

//httputil.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "stringutil.h"

static char ***out;

/**
 * get parameter value from the QUERY_STRING
 * return NULL if QUERY_STRING is null or the key has no associated value or if key found but without a value
 */
char* get_parameter(char* key)
{
	char *querystr = getenv("QUERY_STRING");
	if (querystr == NULL)
    	return (void*)0;

	char s[strlen(querystr)] ;
	strcpy(s, querystr);
	const char delim = '&';
	const char delim2 = '=';
	static size_t size = 0;
	if (out == 0)
	{
    	out = malloc(sizeof(char));
    	size = split(s, &delim, out);
	}

	int i=0;
	for (; i<size; i++)
	{
    	char* seg = (*out);
    	if (seg != NULL)
    	{
        	char ***iout = malloc(sizeof(char));
        	int isize = split(seg, &delim2, iout);
        	if (isize > 1 && ((*iout)[1]) != NULL && strcmp(key, (*iout)[0]) == 0)
        	{
            	size_t _size = strlen((*iout)[1]);
            	char* value = (char*) malloc(_size*sizeof(char));
            	strcpy(value, (*iout)[1]);
            	free(iout);
            	return value;
        	}
    	}
	}
	return (void*) 0;
}

ثالثا مثال لإستخدام الداله get_parameter ..

//employee_test.c
#include <stdio.h>
#include <stdlib.h>
#include "httputil.h"

static void writeHeaders();

int main(void)
{
	writeHeaders();
	char* empId = get_parameter("empId");
	printf("you enterd employee Id: %s\n", empId);

	free(empId);

	return 0;
}

static void writeHeaders()
{
	printf("content-type: text/plain;charset=UTF-8\n\n");
}

لا تنسي أن تضع متغير النظام QUERY_STRING قمة على شكل key1=value1&key2=value2 .. قبل تنفيذ البرنامج ..

أنتظر تعليقاتكم يا أخوه على الكود ... و بخاصه إستخدامي للمتيغر

static char ***out;

فأنا إستخدمه لتوفير معالجة ال query string كل مره يتم فيها البحث عن key داخل ال query string ..

تم تعديل هذه المشاركة بواسطة هويدي في 19 فبراير 2011 في 23:18

1

مواضيع مشابهة