Umasoft
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
TripDub.h
1 //
2 // TripDub.h
3 //
4 // Module for Double-You-Double-You-Double-You support (WWW)
5 //
6 // Copyright (c) 2003-2008 Virtual Terrain Project
7 // Free for all uses, see license.txt for details.
8 //
9 
10 #ifndef VTDATA_TRIPDUB_H
11 #define VTDATA_TRIPDUB_H
12 
13 #include "vtString.h"
14 
15 #if __GNUC__ == 4 && __GNUC_MINOR__ >= 3
16  #include <cstdlib>
17 #endif
18 
19 // The dependency on LibCURL is optional. If not desired, skip this file.
20 #if SUPPORT_CURL
21 
22 class vtBytes
23 {
24 public:
25  vtBytes() { m_data = NULL; m_len = 0; }
26  ~vtBytes() { if (m_data) free(m_data); }
27 
28  void Set(uchar *data, size_t len)
29  {
30  m_data = (uchar *) malloc(len);
31  memcpy(m_data, data, len);
32  m_len = len;
33  }
34  void Append(uchar *data, size_t len)
35  {
36  if (m_data)
37  m_data = (uchar *) realloc(m_data, m_len + len);
38  else
39  m_data = (uchar *) malloc(len);
40  memcpy(m_data + m_len, data, len);
41  m_len += len;
42  }
43  uchar *Get() { return m_data; }
44  size_t Len() { return m_len; }
45 
46 protected:
47  uchar *m_data;
48  size_t m_len;
49 };
50 
51 
52 typedef void CURL;
53 
54 class ReqContext
55 {
56 public:
57  ReqContext();
58  ~ReqContext();
59 
60  bool GetURL(const char *url, vtString &str);
61  bool GetURL(const char *url, vtBytes &data);
62  bool DoQuery(vtBytes &data, int redirects = 0);
63  void SetProgressCallback(bool progress_callback(int));
64  vtString &GetErrorMsg() { return m_strErrorMsg; }
65 
67  void SetVerbosity(int i) { m_iVerbosity = i; }
68 
69  vtString *m_pDataString;
70  vtBytes *m_pDataBytes;
71  bool (*m_progress_callback)(int);
72 
73 protected:
74  bool Fetch(const char *url);
75  void InitializeLibrary();
76 
77  CURL *m_curl;
78 
79  int m_iVerbosity;
80  vtString m_strErrorMsg;
81  static bool s_bFirst;
82 };
83 
84 void AddCookie(const char *name, const char *value);
85 
86 #else
87 // provide dummies
89 {
90 public:
91  bool GetURL(const char *url, vtString &str) { return false; }
92 };
93 
94 #endif // SUPPORT_CURL
95 
96 #endif // VTDATA_TRIPDUB_H
97