DeviceAtlas C++ Api documentation
devatlas.h
1 #ifndef MOBI_MTLD_DA
2 #define MOBI_MTLD_DA
3 #include <mtld/common.h>
4 
5 #define DA_CONTEXT_HAS_REFINE 1
6 #define API_MAJOR_VERSION 2
7 #define API_MINOR_VERSION 3
8 #define API_REVISION_VERSION 0
9 
10 /*
11  * Copyright (c) DeviceAtlas Limited 2021. All Rights Reserved.
12  */
13 
14 namespace Mobi { namespace Mtld { namespace Da {
15 using namespace std;
16 /*
17  * Forward declarations of classes.
18  */
19 
20 class Node;
21 class NodeParse;
22 class REInfo;
23 class RuleGroup;
24 class RuleSet;
25 class UAR;
26 class CPR;
27 struct ChildrenParse;
28 struct ValueParse;
29 
30 
37 enum TreeID {
38  UA,
39  UAPROF,
40  TREEID_MAX = 16 // space for 16 trees without having to recompile.
41 };
42 
43 enum LangID {
44  Default = 0,
45  PHP = 1,
46  Java = 2,
47  DotNET = 3,
48  Cplusplus = 4,
49  Python = 5,
50  Ruby = 6
51 };
52 
53 /*
54  * Basic information from the Version Descriptor
55  */
56 struct VersionInfo {
57  std::string ver;
58  std::string gen;
59  std::string rev;
60  std::string usr;
61  std::string tm;
62  time_t utc;
63 };
64 
65 struct DaConfig {
66  bool include_ua_props;
67  bool include_lang_props;
68  bool include_matched_props;
69  DaConfig()
70  : include_ua_props(true)
71  , include_lang_props(true)
72  , include_matched_props(false) {}
73  DaConfig(bool include_ua_props_, bool include_lang_props_,
74  bool include_matched_props_)
75  : include_ua_props(include_ua_props_)
76  , include_lang_props(include_lang_props_)
77  , include_matched_props(include_matched_props_) {}
78 };
79 
80 DA_EXPORT extern const char *languageHeader;
81 DA_EXPORT extern const char *defaultHeaderTreePriorities[];
82 
89 struct DA_EXPORT HttpHeaders : public CommonHttpHeaders{
90  std::string evidence[TREEID_MAX];
91  std::string userlanguage;
92  size_t uaindex;
93  size_t uajsontreewalk;
94  HttpHeaders(const char *userAgent, const char *acceptLanguage = 0);
95  HttpHeaders(const HeaderMap & headers, const char ***priorities = defaultHeaderPriorities);
96 };
97 
103 typedef std::map<LangID, std::vector<REInfo *> > TaggedRegexMap;
104 typedef std::map<size_t, std::string> ResetWalkMap;
105 class DA_EXPORT DeviceAtlas : public CommonDeviceAtlas {
106  friend class DAParse;
107  friend class NodeParse;
108  friend class ReplacementStringParser;
109  friend class RuleGroup;
110  friend struct CPRParser;
111  friend struct RGParser;
112  friend struct UARParser;
113  friend struct AttrVecParser;
114  friend struct CPRulesParser;
115  friend struct UAStockParser;
116  friend struct ResetWalkParser;
117  friend struct DataParse;
118  friend struct ValueParse;
119 
120  VersionInfo vi;
121  const std::vector<REInfo *> *languageReplacementStrings;
122 
123  CPR *cpr;
124  const Property *propid;
125  TaggedRegexMap replacementStrings;
126  ResetWalkMap resetWalkStrings;
127 
128  vector<string> stockUaFromTree;
129 
130  void initAtlas(std::istream &, const char *[]);
131  void resetRoots();
132  void resetPriorities();
133 
134 public:
135  const Property *getpropId() const { return propid; }
136 
137  Node *roots[TREEID_MAX];
138  unsigned long flags;
139 private:
140  DaConfig dcfg;
141  UAR *uar;
142 public:
143  unsigned maxDepth;
144  void doReplacements(const std::vector<size_t> *, char *, size_t) const;
145 
150  struct {
151  size_t _user_agent_matched;
152  size_t _user_agent_unmatched;
153  // dynamic language properties
154  size_t _language_locale;
155  size_t _language;
156  } synthetic;
157 
158  void setConfig(DaConfig &_dcfg) { dcfg = _dcfg; }
159  bool hasUAProps() const { return dcfg.include_ua_props == true; }
160  bool hasLangProps() const { return dcfg.include_lang_props == true; }
161  bool hasMatchedProps() const { return dcfg.include_matched_props == true; }
162 
163  const UAR *getUAR() const { return uar; }
164  const CPR *getCPR() const { return cpr; }
165 
173  static const char *treeUA[];
174  static const char *treeAll[];
175  static const char **getTreeUA();
176  static const char **getTreeAll();
177 
178  enum {
179  NoREFold = 1 << 0
180  , Debug = 1 << 1
181  , OverwriteUnconflicted = 1 << 2
182  };
183  DeviceAtlas() : CommonDeviceAtlas(), cpr(0), uar(0) { resetRoots(); }
184  DeviceAtlas(DaConfig &_dcfg) : CommonDeviceAtlas(), cpr(0), uar(0), dcfg(_dcfg) { resetRoots(); }
192  DeviceAtlas(std::istream &, const char *treeFields[] = getTreeUA(), unsigned long flags = 0);
193  ~DeviceAtlas();
198  void loadDataFromFile(const char *, const char *treeFields[] = getTreeUA(), unsigned long flags = 0);
203  void loadDataFromStream(std::istream &, const char *treeFields[] = getTreeUA(), unsigned long flags = 0);
204  const Value *getValue(size_t) const;
205  const std::string &getResetWalkString(size_t) const;
210  int getTreeRevision() const;
211  time_t getTreeGeneration() const;
212  int getDataRevision() const { return getTreeRevision(); }
213  time_t getDataGeneration() const { return getTreeGeneration(); }
214 };
215 
216 class LocalProperties;
217 
218 class DA_EXPORT DeliveryContext : public CommonDeliveryContext {
219 private:
224  Value user_agent_matched;
225  Value user_agent_unmatched;
226  const DeviceAtlas &da;
227  size_t dapropcount;
228  size_t dalocalcount;
229 
230 
239  bool doConflicts;
240 
241 
242  // the node in the atlas where the search terminated.
243  const Node *node;
244  LocalProperties *localProperties;
245 
246  // The number of properties currently in conflict
247  unsigned conflictCount;
248 
249  // indexed by property: indicates a found-but-conflicted property.
250  bool *conflicts;
251 
252  void getRulesetWithProps(const char *&uastrng);
253  void getRuleset(const char *&uastrng);
254  void runRules(const char *txt, const RuleSet &rules);
255 
256 public:
257  // Create a context reflecting the properties of the device
258  // identified by the HttpHeaders object.
259  DeliveryContext(const DeviceAtlas &da_, const HttpHeaders &);
260  ~DeliveryContext();
261 
262  const Property &getPropertyDescr(const char *propname, PropertyType *addAs);
263  const Property &getPropertyDescr(size_t idx) const;
264  void setProperty(const Property &, const Value*);
265  void setProperty(size_t, const Value *);
266  const Value *findProperty(const Property &p) const;
267  const Value *findProperty(size_t) const;
268 
269  const DeviceAtlas &getAtlas() const { return da; }
270 
271  // Refine the tree with retrieved client-side properties.
272  void refine(const std::vector<const Value *> &clientprops);
273 
274  // this variant does ad-hoc decoding similar to the other .mobi APIs.
275  void refine(const char *cookieprops);
276  const Value *findGlobalProperty(size_t) const;
277  const Value *findLocalProperty(size_t) const;
278 
279  class const_iterator;
280  friend class const_iterator;
282  public:
283  typedef std::pair<const Property *, const Value *> PVPair;
284  private:
285  const DeliveryContext &props;
286  size_t idx;
287  size_t propcount;
288  friend class DeliveryContext;
289  PVPair builtin;
290  public:
291  const_iterator(const DeliveryContext &props_, size_t idx_);
292  const PVPair operator * () const;
293  const PVPair *operator -> ();
294  const_iterator &operator++();
295  bool operator==(const const_iterator &rhs) const;
296  bool operator!=(const const_iterator &rhs) const;
297  };
298 
299  // called for each node during the search, from the find() template on Node
300  bool operator()(const Node &cur, char *str, size_t offset);
301  // std-style const iterator for all properties.
302  const_iterator begin() const;
303  const_iterator end() const;
304  const_iterator find(const Property &) const;
305  const_iterator find(const char *) const;
306  size_t size() const;
307  size_t totalsize() const;
308  bool empty();
309  const Value *operator[](const Property &) const;
310  const Value *operator[](const char *propname) const;
311 };
312 
313 }}}
314 
315 // pretty-printer for stream output
316 #endif
Definition: dajson.cpp:595
Definition: devatlas.h:105
Definition: devatlas.h:56
Definition: common.h:230
Definition: devatlas_priv.h:531
Definition: dajson.cpp:244
Definition: dajson.cpp:612
Definition: devatlas_priv.h:85
Definition: common.h:190
Definition: dajson.cpp:628
Definition: dajson.cpp:460
Definition: dajson.cpp:339
Definition: devatlas_priv.h:16
Definition: dajson.cpp:564
Definition: dajson.cpp:174
Definition: devatlas_priv.h:254
Definition: dajson.cpp:198
Definition: devatlas_priv.h:273
Definition: common.h:240
Definition: binary.h:13
Definition: devatlas.h:218
Definition: devatlas_priv.h:262
Definition: devatlas.h:65
Definition: dajson.cpp:497
HttpHeaders represents the evidence used to look up the DeviceAtlas for properties of a specific clie...
Definition: devatlas.h:89
Definition: common.h:140
Definition: dajson.cpp:536