forked from locklin/spatial-trees
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queryparser.c
133 lines (104 loc) · 3.13 KB
/
queryparser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <stdio.h>
#include <stdlib.h>
/*****************************************************************************
* This module contains code to parse the query expressions given by the
* user
******************************************************************************/
/*****************************************************************************/
char *StripBlanks(as)
/*****************************************************************************/
char *as;
{
int i=0,j=0;
char *bs;
bs = (char *) malloc((strlen(as)+1)*sizeof(char));
while (as[i] != '\0') {
if (as[i] != ' ') {
bs[j++]=as[i];
}
i++;
}
free(as);
bs[j]+='\0';
return(bs);
}
/*****************************************************************************/
float **ParseRectangular(rectstring, dimension)
/*****************************************************************************/
char *rectstring;
int dimension;
{
int stat,k;
char brac[1],lparen[1],rparen[1],comma[1],rest[500];
float **rectquery;
rectquery =(float **)malloc(dimension *sizeof(float *));
for (k=0; k < dimension; k++) {
rectquery[k] = (float *)malloc(2*sizeof(float));
} /* allocate array that holds rectangular query */
rectstring = StripBlanks(rectstring);
sscanf(rectstring,"%c%s",brac,rest); /* get first bracket */
rectstring=rest;
if (strncmp(brac,"[",1) != 0) {
DisplayError("Rectangular Query is invalid");
return(NULL);
}
for (k=0; k <dimension;k++) {
stat=sscanf(rectstring,"%c%f%c%f%c%s",lparen,&rectquery[k][0],comma,
&rectquery[k][1],rparen,rest);
rectstring=rest;
if ((strncmp(lparen,"(",1) !=0) || (strncmp(comma,",",1)) !=0 ||
(strncmp(rparen,")",1) != 0)) {
DisplayError("Rectangular Query is invalid");
return(NULL);
}
if (k < dimension -1 ) {
sscanf(rectstring,"%c%s",comma,rest);
rectstring=rest;
if (strncmp(comma,",",1) != 0) {
DisplayError("Rectangular Query is invalid");
return(NULL);
}
}
}
sscanf(rectstring,"%c",brac); /* get last bracket */
if (strncmp(brac,"]",1) !=0) {
DisplayError("Rectangular Query is invalid");
return(NULL);
}
return(rectquery);
}
/*****************************************************************************/
float *ParseNearestNeighbor(NNstring, dimension)
/*****************************************************************************/
char *NNstring;
int dimension;
{
int j;
char lparen[1],rparen[1],comma[1],rest[500];
float *point;
point = (float *) malloc((dimension)*sizeof(float));
NNstring = StripBlanks(NNstring);
if (sscanf(NNstring,"(%s",rest) != 1) {
return(NULL);
}
NNstring=rest;
for (j=0; j < dimension-1; j++) {
if (sscanf(NNstring,"%f%s",&point[j],rest) != 2) {
return(NULL);
}
NNstring = rest;
sscanf(NNstring,"%c%s",comma,rest);
if (strncmp(comma,",",1) != 0) {
return(NULL);
}
NNstring = rest;
}
if (sscanf(NNstring,"%f,",&point[dimension-1]) !=1) {
DisplayError("Nearest Neighbor Query is invalid 3\n");
return(NULL);
}
for (j=0;j < dimension; j++) {
printf("Dimension %d, %f\n",j,point[j]);
}
return(point);
}