This tutorial illustrates how to append a CSV file containing User-Agent strings with IsMobile, PlatformName and PlatformVersion properties. The following aspects of the API are covered:
How to perform a User-Agent match.
How to reuse resources to perform subsequent matching.
How to retrieve match results for a specific property.
How to append a property value to a CSV file.
Code and Explanation
Offline processing example of using 51Degrees device detection.
The example shows how to:
Specify name of the data file and properties the dataset should be
initialised with.
For the first 20 User-Agents in the input file, perform a match then
write the User-Agent along with the values for chosen properties to
the CSV.
for (i=0;i<20;i++) {
fgets(userAgent, sizeof(userAgent), fin);
userAgent[strlen(userAgent)-1] = '\0';
fprintf(fout, "%s", userAgent);
fiftyoneDegreesMatch(ws, userAgent);
for (j=0;j<propertiesCount;j++) {
value = getValue(ws, propertiesArray[j]);
fprintf(fout, "|%s", value);
}
fprintf(fout, "\n");
}
Release the workset back into the pool of worksets to be reused in one
of the next matches.
fiftyoneDegreesWorksetRelease(ws);
Finally release the memory taken by the provider.
fiftyoneDegreesProviderFree(&provider);
This example assumes you have compiled with 51Degrees.c and city.c.
This will happen automatically if you are compiling as part of the
Visual Studio solution. Additionally, when running the program, the
location of a 51Degrees data file must be passed as a command line
argument if you wish to use Premium or Enterprise data files.
This example demonstrates one possible use of the API and device data for
the offline data processing. It also demostrates that you can reuse the
retrieved workset for multiple uses and only then return it to the pool.
Summary
Offline device detection is frequently required for a variety of reasons such as generating reports. The example is based on an actual support request where several properties had to be added to the CSV file before it could be passed on for another department to use.