Subversion Repositories RepoView

Rev

Rev 3 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 nishi 1
/* $Id: query.c 41 2024-08-22 04:26:35Z nishi $ */
2
 
3
#include "rv_query.h"
4
 
5
#include "rv_util.h"
6
 
7
#include <string.h>
8
#include <stdlib.h>
9
#include <stdio.h>
10
 
11
struct query_entry {
12
	char* key;
13
	char* value;
14
};
15
 
16
struct query_entry** qentries;
17
 
41 nishi 18
struct query_entry** query = NULL;
19
struct query_entry** postquery = NULL;
1 nishi 20
 
3 nishi 21
void rv_save_query(char c) {
22
	if(c == 'Q') {
1 nishi 23
		query = qentries;
3 nishi 24
	} else if(c == 'P') {
1 nishi 25
		postquery = qentries;
26
	}
27
}
28
 
3 nishi 29
void rv_load_query(char c) {
30
	if(c == 'Q') {
1 nishi 31
		qentries = query;
3 nishi 32
	} else if(c == 'P') {
1 nishi 33
		qentries = postquery;
34
	}
35
}
36
 
3 nishi 37
void rv_parse_query(const char* oldquery) {
1 nishi 38
	char* query = rv_strdup(oldquery);
39
	int i;
40
	int incr = 0;
41
	qentries = malloc(sizeof(*qentries));
42
	qentries[0] = NULL;
3 nishi 43
	for(i = 0;; i++) {
44
		if(query[i] == '&' || query[i] == 0) {
1 nishi 45
			char oldc = query[i];
46
			query[i] = 0;
47
 
48
			char* key = query + incr;
49
			char* value = "";
50
 
51
			int j;
3 nishi 52
			for(j = 0; key[j] != 0; j++) {
53
				if(key[j] == '=') {
1 nishi 54
					key[j] = 0;
55
					value = key + j + 1;
56
					break;
57
				}
58
			}
59
 
60
			struct query_entry* entry = malloc(sizeof(*entry));
61
			entry->key = rv_url_decode(key);
62
			entry->value = rv_url_decode(value);
63
 
64
			struct query_entry** old_entries = qentries;
3 nishi 65
			for(j = 0; old_entries[j] != NULL; j++)
66
				;
1 nishi 67
			qentries = malloc(sizeof(*qentries) * (j + 2));
3 nishi 68
			for(j = 0; old_entries[j] != NULL; j++) {
1 nishi 69
				qentries[j] = old_entries[j];
70
			}
71
			qentries[j] = entry;
72
			qentries[j + 1] = NULL;
73
			free(old_entries);
74
 
75
			incr = i + 1;
76
			if(oldc == 0) break;
77
		}
78
	}
79
	free(query);
80
}
81
 
3 nishi 82
void rv_free_query(void) {
1 nishi 83
	int i;
3 nishi 84
	for(i = 0; qentries[i] != NULL; i++) {
1 nishi 85
		free(qentries[i]->key);
86
		free(qentries[i]->value);
87
		free(qentries[i]);
88
	}
89
	free(qentries);
90
}
91
 
3 nishi 92
char* rv_get_query(const char* key) {
1 nishi 93
	int i;
41 nishi 94
	if(qentries == NULL) return NULL;
3 nishi 95
	for(i = 0; qentries[i] != NULL; i++) {
96
		if(strcmp(qentries[i]->key, key) == 0) {
1 nishi 97
			return qentries[i]->value;
98
		}
99
	}
100
	return NULL;
101
}