Created
August 7, 2012 17:30
-
-
Save rodacato/3287591 to your computer and use it in GitHub Desktop.
Wurfl library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <libxml/parser.h> | |
#include <libxml/tree.h> | |
#include <libxml/xpath.h> | |
#include <libxml/xpathInternals.h> | |
#include "wurfl.h" | |
xmlDoc *doc = NULL; | |
int wurfl_init(char *filename) { | |
doc = xmlReadFile(filename, NULL, 0); | |
return (doc != NULL); | |
} | |
char wurfl_device(char *useragent) { | |
xmlXPathContextPtr xpathCtx; | |
xmlXPathObjectPtr xpathObj; | |
// Lazy loading. Varnish does not support an init() function | |
if (doc == NULL) wurfl_init("/etc/wurfl.xml"); | |
// Create new context | |
xpathCtx = xmlXPathNewContext(doc); | |
if (xpathCtx == NULL) return -1; | |
// Create xpath expression | |
char *expr = malloc(strlen(useragent) + 50); | |
sprintf(expr, "//devices/device[@user_agent='%s']", (unsigned char *)useragent); | |
xpathObj = xmlXPathEvalExpression(expr, xpathCtx); | |
free(expr); | |
// Nothing found? | |
if (xpathObj == NULL) { | |
xmlXPathFreeContext(xpathCtx); | |
return ' '; | |
} | |
// Fetch the number of nodes found | |
xmlNodeSetPtr nodes = xpathObj->nodesetval; | |
char *device = xmlGetProp(nodes->nodeTab[0], "fall_back"); | |
// Something has been found... | |
xmlXPathFreeObject(xpathObj); | |
xmlXPathFreeContext(xpathCtx); | |
xmlFree(device); | |
return *device; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __WURFL_H__ | |
#define __WURFL_H__ | |
int wurfl_init(char *filename); | |
char wurfl_device(char *useragent); | |
#endif // __WURFL_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
echo -e '\n\n=============== Installing gem dependencies' | |
apt-get intall libxml2-dev | |
echo -e '\n\n=============== Compiling wurfl library' | |
wget https://raw.github.com/gist/3287591/afa1b6699e8524d0d3aa5c0d64ff4a76042d7891/wurfl.c | |
wget https://raw.github.com/gist/3287591/7a8464332553be89a66722de15f19ab6e8bc0056/wurfl.h | |
gcc -c -o wurfl.o wurfl.c -I/usr/include/libxml2 -fPIC | |
gcc -shared -Wl,-soname,libwurfl.so.1 -o libwurfl.so.1.0.1 wurfl.o -lxml2 | |
echo -e '\n\n=============== Getting wurfl xml database' | |
wget http://sourceforge.net/projects/wurfl/files/WURFL/2.3.2/wurfl-2.3.2.xml.gz/download | |
gzip -d wurfl-2.3.2.xml.gz | |
echo -e '\n\n=============== Copy files' | |
mv wurfl-2.3.2.xml /etc/wurfl.xml | |
cp libwurfl.so.1.0.1 /usr/lib/libwurfl.so | |
ln -s /usr/lib/libwurfl.so /usr/lib64/libwurfl.so |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment