MEDIUM: sample: add trie support to 51Degrees
Trie or pattern algorithm is used depending on what 51Degrees source
files are provided to MAKE.
diff --git a/src/sample.c b/src/sample.c
index 6418be8..1993255 100644
--- a/src/sample.c
+++ b/src/sample.c
@@ -2130,13 +2130,64 @@
}
#ifdef USE_51DEGREES
-static int fiftyone_degrees(const struct arg *args,
- struct sample *smp, void *private)
+#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
+static int
+sample_fiftyone_degrees(const struct arg *args, struct sample *smp, void *private)
+{
+ int i; // used in loops.
+ int device_offset;
+ int property_index;
+ char no_data[] = "NoData"; // response when no data could be found.
+ struct chunk *tmp;
+
+ // use a temporary trash buffer and copy data in it
+ smp->data.str.str[smp->data.str.len] = '\0';
+
+ // perform detection.
+ device_offset = fiftyoneDegreesGetDeviceOffset(smp->data.str.str);
+
+ i = 0;
+ tmp = get_trash_chunk();
+ chunk_reset(tmp);
+
+ // loop through property names passed to the filter and fetch them from the dataset.
+ while (args[i].data.str.str) {
+ // try to find request property in dataset.
+ property_index = fiftyoneDegreesGetPropertyIndex(args[i].data.str.str);
+ if (property_index > 0) {
+ chunk_appendf(tmp, "%s", fiftyoneDegreesGetValue(device_offset, property_index));
+ }
+ else {
+ chunk_appendf(tmp, "%s", no_data);
+ }
+ // add seperator
+ if (global._51d_property_seperator)
+ chunk_appendf(tmp, "%c", global._51d_property_seperator);
+ else
+ chunk_appendf(tmp, ",");
+ ++i;
+ }
+
+ if (tmp->len) {
+ --tmp->len;
+ tmp->str[tmp->len] = '\0';
+ }
+
+ smp->data.str.str = tmp->str;
+ smp->data.str.len = strlen(smp->data.str.str);
+
+ return 1;
+}
+#endif // FIFTYONEDEGREES_H_TRIE_INCLUDED
+
+#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
+static int
+sample_fiftyone_degrees(const struct arg *args, struct sample *smp, void *private)
{
int i, j, found; // used in loops.
fiftyoneDegreesWorkset* ws; // workset for detection.
char no_data[] = "NoData"; // response when no data could be found.
- struct _51d_property_names *property;
+ const char* property_name;
struct chunk *tmp;
// use a temporary trash buffer and copy data in it
@@ -2157,17 +2208,18 @@
while (args[i].data.str.str) {
found = j = 0;
// try to find request property in dataset.
- list_for_each_entry(property, &global._51d_property_names, list) {
- if (strcmp(property->name, args[i].data.str.str) == 0) {
+ for (j = 0; j < ws->dataSet->requiredPropertyCount; j++) {
+ property_name = fiftyoneDegreesGetPropertyName(ws->dataSet, ws->dataSet->requiredProperties[j]);
+ if (strcmp(property_name, args[i].data.str.str) == 0) {
found = 1;
fiftyoneDegreesSetValues(ws, j);
chunk_appendf(tmp, "%s", fiftyoneDegreesGetValueName(ws->dataSet, *ws->values));
break;
}
- ++j;
}
- if (!found)
+ if (!found) {
chunk_appendf(tmp, "%s", no_data);
+ }
// add seperator
if (global._51d_property_seperator)
@@ -2189,7 +2241,8 @@
return 1;
}
-#endif
+#endif // FIFTYONEDEGREES_H_PATTERN_INCLUDED
+#endif // USE_51DEGREES
/************************************************************************/
@@ -2346,7 +2399,7 @@
{ "mod", sample_conv_arith_mod, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
{ "neg", sample_conv_arith_neg, 0, NULL, SMP_T_UINT, SMP_T_UINT },
#ifdef USE_51DEGREES
- { "51d", fiftyone_degrees, ARG5(1,STR,STR,STR,STR,STR), NULL, SMP_T_STR, SMP_T_STR },
+ { "51d", sample_fiftyone_degrees,ARG5(1,STR,STR,STR,STR,STR), NULL, SMP_T_STR, SMP_T_STR },
#endif
{ NULL, NULL, 0, 0, 0 },