1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
diff -Nur lgogdownloader-1.16/include/config.h lgogdownloader-1.16.patched/include/config.h
--- lgogdownloader-1.16/include/config.h	1969-12-31 19:00:00.000000000 -0500
+++ lgogdownloader-1.16.patched/include/config.h	2013-02-05 01:29:38.473206499 -0500
@@ -0,0 +1,37 @@
+#pragma once
+
+#ifndef CONFIG_H__
+#define CONFIG_H__
+
+#include <curl/curl.h>
+
+class Config
+{
+    public:
+        Config() {};
+        virtual ~Config() {};
+        bool bVerbose;
+        bool bNoRemoteXML;
+        bool bNoCover;
+        bool bUpdateCheck;
+        bool bHelp;
+        bool bDownload;
+        bool bList;
+        bool bListDetails;
+        bool bLogin;
+        bool bRepair;
+        bool bNoInstallers;
+        bool bNoExtras;
+        bool bNoUnicode; // don't use Unicode in console output
+        std::string sGameRegex;
+        std::string sDirectory;
+        std::string sConfigDirectory;
+        std::string sXMLFile;
+        std::string sXMLDirectory;
+        std::string sToken;
+        std::string sSecret;
+        size_t iChunkSize;
+        curl_off_t iDownloadRate;
+};
+
+#endif // CONFIG_H__
diff -Nur lgogdownloader-1.16/include/curlhandle.h lgogdownloader-1.16.patched/include/curlhandle.h
--- lgogdownloader-1.16/include/curlhandle.h	2013-02-01 17:18:58.000000000 -0500
+++ lgogdownloader-1.16.patched/include/curlhandle.h	2013-02-06 01:08:39.359644071 -0500
@@ -14,8 +14,10 @@
 #include <cmath>
 #include <iostream>
 #include <sstream>
+#include <memory>
 #include <curl/curl.h>
 
+#include "config.h"
 
 size_t write_memory_callback(char *ptr, size_t size, size_t nmemb, void *userp);
 size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream);
@@ -33,21 +35,22 @@
 
 class curlhandle
 {
-	public:
-		curlhandle();
-		void url(const std::string& url) { curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str()); };
-		void setopt(CURLoption option, ...);
-		void setResumePosition(size_t pos);
-		size_t getResumePosition();
-		CURLcode perform();
-		std::string getResponse(const std::string& url);
-		~curlhandle();
-    	Timer *timer;
-        // Amount of data downloaded since the beginning of this execution, in contrast with dltotal
-        // which is a full amount from all attempts and resumes.
-        double downloaded;
-        double previous_dlnow;
-        bool   first_time;
+    public:
+        curlhandle(std::shared_ptr<Config> config_source = nullptr);
+        void url(const std::string& url) { curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str()); };
+        void setopt(CURLoption option, ...);
+        void setResumePosition(size_t pos);
+        size_t getResumePosition();
+        CURLcode perform();
+        std::string getResponse(const std::string& url);
+
+        // accessor to curl_easy_getinfo
+        template <typename _T>
+        CURLcode get_info(CURLINFO info, _T dest);
+
+        ~curlhandle();
+        Timer *timer;
+        std::shared_ptr<Config> config;
     protected:
     private:
     	CURL *curl_handle;
diff -Nur lgogdownloader-1.16/lgogdownloader.cbp lgogdownloader-1.16.patched/lgogdownloader.cbp
--- lgogdownloader-1.16/lgogdownloader.cbp	2013-02-01 17:18:58.000000000 -0500
+++ lgogdownloader-1.16.patched/lgogdownloader.cbp	2013-02-06 03:26:17.523375303 -0500
@@ -49,6 +49,7 @@
 			<Add library="rhash" />
 		</Linker>
 		<Unit filename="include/api.h" />
+		<Unit filename="include/config.h" />
 		<Unit filename="include/curlhandle.h" />
 		<Unit filename="include/http.h" />
 		<Unit filename="include/util.h" />
diff -Nur lgogdownloader-1.16/main.cpp lgogdownloader-1.16.patched/main.cpp
--- lgogdownloader-1.16/main.cpp	2013-02-01 17:18:58.000000000 -0500
+++ lgogdownloader-1.16.patched/main.cpp	2013-02-05 02:45:36.455849465 -0500
@@ -5,6 +5,7 @@
  * http://sam.zoy.org/wtfpl/COPYING for more details. */
 
 #include "api.h"
+#include "config.h"
 #include "curlhandle.h"
 #include "http.h"
 #include "util.h"
@@ -26,34 +27,6 @@
 #   endif
 #endif
 
-class Config
-{
-    public:
-        Config() {};
-        virtual ~Config() {};
-        bool bVerbose;
-        bool bNoRemoteXML;
-        bool bNoCover;
-        bool bUpdateCheck;
-        bool bHelp;
-        bool bDownload;
-        bool bList;
-        bool bListDetails;
-        bool bLogin;
-        bool bRepair;
-        bool bNoInstallers;
-        bool bNoExtras;
-        std::string sGameRegex;
-        std::string sDirectory;
-        std::string sConfigDirectory;
-        std::string sXMLFile;
-        std::string sXMLDirectory;
-        std::string sToken;
-        std::string sSecret;
-        size_t iChunkSize;
-        curl_off_t iDownloadRate;
-};
-
 // Download a file, resume if possible
 int downloadFile(curlhandle *curl_handle, std::string url, std::string filepath)
 {
@@ -471,13 +444,13 @@
 	return res;
 }
 
-int updateGames(API *gogAPI, Config *config, std::vector<std::string> links)
+int updateGames(API *gogAPI, std::shared_ptr<Config> config, std::vector<std::string> links)
 {
     int res = 0;
 
     if (links.size() > 0)
     {
-        curlhandle *curl_handle = new curlhandle();
+        curlhandle *curl_handle = new curlhandle(config);
         curl_handle->setopt(CURLOPT_FOLLOWLOCATION, 1);
         curl_handle->setopt(CURLOPT_WRITEFUNCTION, write_data);
         curl_handle->setopt(CURLOPT_READFUNCTION, read_data);
@@ -648,7 +621,8 @@
 		}
 	}
 
-    Config *config = new Config();
+    std::shared_ptr<Config> config(new Config());
+
     boost::program_options::variables_map vm;
     boost::program_options::options_description desc("Options");
     boost::program_options::options_description config_file_options("Configuration");
@@ -674,6 +648,7 @@
             ("no-extras", boost::program_options::value<bool>(&config->bNoExtras)->zero_tokens()->default_value(false), "Don't download/list/repair extras")
             ("no-cover", boost::program_options::value<bool>(&config->bNoCover)->zero_tokens()->default_value(false), "Don't download cover images")
             ("no-remote-xml", boost::program_options::value<bool>(&config->bNoRemoteXML)->zero_tokens()->default_value(false), "Don't use remote XML for repair")
+            ("no-unicode", boost::program_options::value<bool>(&config->bNoUnicode)->zero_tokens()->default_value(false), "Don't use Unicode in the progress bar")
             ("verbose", boost::program_options::value<bool>(&config->bVerbose)->zero_tokens()->default_value(false), "Print lots of information")
         ;
 
@@ -836,7 +811,7 @@
                 if (answer == "Y" || answer == "y")
                 {
                     std::vector<std::string> links = HTTP::getUpdated();
-                    if (!updateGames(gogAPI, config, links))
+                    if (!updateGames(gogAPI, config, links, config))
                     {
                         std::cout << "Something went wrong while updating" << std::endl;
                         return 1;
@@ -915,7 +890,7 @@
     if (config->bDownload) // Download games
     {
         std::string cover_xml;
-        curlhandle *curl_handle = new curlhandle();
+        curlhandle *curl_handle = new curlhandle(config);
 
         if (!config->bNoCover)
             cover_xml = curl_handle->getResponse("https://sites.google.com/site/gogdownloader/GOG_covers.xml");
@@ -1014,7 +989,7 @@
     }
     else if (config->bRepair) // Repair file
     {
-        curlhandle *curl_handle = new curlhandle();
+        curlhandle *curl_handle = new curlhandle(config);
         curl_handle->setopt(CURLOPT_FOLLOWLOCATION, 1);
         curl_handle->setopt(CURLOPT_WRITEFUNCTION, write_data);
         curl_handle->setopt(CURLOPT_READFUNCTION, read_data);
diff -Nur lgogdownloader-1.16/src/curlhandle.cpp lgogdownloader-1.16.patched/src/curlhandle.cpp
--- lgogdownloader-1.16/src/curlhandle.cpp	2013-02-01 17:18:58.000000000 -0500
+++ lgogdownloader-1.16.patched/src/curlhandle.cpp	2013-02-06 03:11:44.623404586 -0500
@@ -11,29 +11,111 @@
 
 namespace bptime = boost::posix_time;
 
-int progress_callback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
+// progress_bar
+class progress_bar
 {
-    int length = 26;
-	time_t time_now = time(NULL); // current time
-	curlhandle *curl_handle = static_cast<curlhandle*>(clientp);
-    time_t time_start = curl_handle->timer->time_start;
+ private:
+   // for now - all const, no custom stuff.
+   std::string static const m_left_border;
+   std::string static const m_right_border;
+   std::vector<std::string> static const m_bar_chars;
+
+   std::string static const m_simple_left_border;
+   std::string static const m_simple_right_border;
+   std::string static const m_simple_empty_fill;
+   std::string static const m_simple_bar_char;
+
+   bool m_use_unicode;
+
+ public:
+   progress_bar(bool use_unicode = true);
+   bool set_use_unicode(bool use_unicode);
+   bool get_use_unicode();
+   void draw(unsigned int length, double fraction);
+};
+
+std::string const progress_bar::m_left_border(u8"\u2595");
+std::string const progress_bar::m_right_border(u8"\u258F");
+std::vector<std::string> const progress_bar::m_bar_chars
+   { u8"\u2591", u8"\u258F", u8"\u258E", u8"\u258D", u8"\u258C",
+     u8"\u258B", u8"\u258A", u8"\u2589", u8"\u2588" };
+
+std::string const progress_bar::m_simple_left_border("[");
+std::string const progress_bar::m_simple_right_border("]");
+std::string const progress_bar::m_simple_empty_fill(" ");
+std::string const progress_bar::m_simple_bar_char("=");
+
+progress_bar::progress_bar(bool use_unicode) : m_use_unicode(use_unicode) { }
+
+inline bool progress_bar::set_use_unicode(bool use_unicode)
+{
+   return m_use_unicode = use_unicode;
+}
+
+inline bool progress_bar::get_use_unicode()
+{
+   return m_use_unicode;
+}
+
+void progress_bar::draw(unsigned int length, double fraction)
+{
+   // validation
+   if (fraction < 0.0) fraction = 0.0;
+   else if (fraction > 1.0) fraction = 1.0;
+
+   double bar_part                = fraction * length;
+   double whole_bar_chars         = std::floor(bar_part);
+   unsigned int whole_bar_chars_i = (unsigned int) whole_bar_chars;
+   // The bar uses symbols graded with 1/8
+   unsigned int partial_bar_char_index = (unsigned int) std::floor((bar_part - whole_bar_chars) * 8.0);
+
+   // left border
+   std::cout << (m_use_unicode ? m_left_border : m_simple_left_border);
+   // whole completed bars
+   unsigned int i = 0;
+   for (; i < whole_bar_chars_i; i++)
+   {
+      std::cout << (m_use_unicode ? m_bar_chars[8] : m_simple_bar_char);
+   }
+   // partial completed bar
+   if (i < length) std::cout << (m_use_unicode ? m_bar_chars[partial_bar_char_index] : m_simple_empty_fill);
+   // whole unfinished bars
+   for (i = whole_bar_chars_i + 1; i < length; i++)
+   {  // first entry in m_bar_chars is assumed to be the empty bar
+      std::cout << (m_use_unicode ? m_bar_chars[0] : m_simple_empty_fill);
+   }
+   // right border
+   std::cout << (m_use_unicode ? m_right_border : m_simple_right_border);
+}
 
-    double rate(0.0L); // average download speed in B/s
-    if (curl_handle->first_time)
-    {
-       curl_handle->first_time = false;
-    }
-    else
-    {
-       curl_handle->downloaded += dlnow - curl_handle->previous_dlnow;
-       rate = curl_handle->downloaded/(time_now - time_start);
-    }
-    curl_handle->previous_dlnow = dlnow;
+///////////////////////////////////////////////////////////////////////////////////////////////
+int progress_callback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
+{
+    // on entry: dltotal - how much remains to download till the end of the file (bytes)
+    //           dlnow   - how much was downloaded from the start of the program (bytes)
+    unsigned int bar_length  = 26;
+    curlhandle*  curl_handle = static_cast<curlhandle*>(clientp);
+
+    double rate; //  average download speed in B/s
+    // trying to get rate and setting to NaN if it fails
+    if (CURLE_OK != curl_handle->get_info(CURLINFO_SPEED_DOWNLOAD, &rate))
+       rate = std::numeric_limits<double>::quiet_NaN();
+
+    // (Shmerl): DEBUG: strange thing - when resuming a file which is already downloaded, dlnow is correctly 0.0 
+    // but dltotal is 389.0! This messes things up in the progress bar not showing the very last bar as full.
+    // enable this debug line to test the problem:
+    //
+    //   printf("\r\033[0K dlnow: %0.2f, dltotal: %0.2f\r", dlnow, dltotal); fflush(stdout); return 0;
+    //
+    // For now making a quirky workaround and setting dltotal to 0.0 in that case.
+    // It's probably better to find a real fix.
+    if ((0.0 == dlnow) && (389.0 == dltotal)) dltotal = 0.0;
 
+    // setting full dlwnow and dltotal
     double offset = static_cast<double>(curl_handle->getResumePosition());
     if (offset>0)
     {
-        dlnow += offset;
+        dlnow   += offset;
         dltotal += offset;
     }
 
@@ -62,19 +144,16 @@
        eta_ss << eta.seconds() << "s";
     }
 
-    double percent = dlnow / dltotal;
-    int lengthnow = round(percent * length);
-    int i=0;
     // Create progressbar
-    printf("\033[0K\r%3.0f%% [",percent*100);
-    for (; i<lengthnow; i++)
-        printf("=");
-    for (; i<length; i++)
-        printf(" ");
-    printf("] %0.2f/%0.2fMB @ %0.2fkB/s ETA: %s\r", dlnow/1024/1024, dltotal/1024/1024, rate/1024, eta_ss.str().c_str());
+    double fraction  = dlnow / dltotal;
+    // assuming that config is provided.
+    static progress_bar bar(!curl_handle->config->bNoUnicode);
+    printf("\033[0K\r%3.0f%% ", fraction * 100);
+    bar.draw(bar_length, fraction);
+    printf(" %0.2f/%0.2fMB @ %0.2fkB/s ETA: %s\r", dlnow/1024/1024, dltotal/1024/1024, rate/1024, eta_ss.str().c_str());
     fflush(stdout);
 
-	return 0;
+    return 0;
 }
 
 size_t write_memory_callback(char *ptr, size_t size, size_t nmemb, void *userp) {
@@ -94,11 +173,9 @@
     return fread(ptr, size, nmemb, stream);
 }
 
-curlhandle::curlhandle()
+curlhandle::curlhandle(std::shared_ptr<Config> config_source)
 :
-   downloaded(0.0L),
-   previous_dlnow(0.0L),
-   first_time(true)
+   config(config_source)
 {
     this->timer = new Timer();
     curl_global_init(CURL_GLOBAL_ALL);
@@ -155,6 +232,12 @@
     return this->resume_position;
 }
 
+template <typename _T>
+CURLcode curlhandle::get_info(CURLINFO info, _T dest)
+{
+   return curl_easy_getinfo(curl_handle, info, dest);
+}
+
 curlhandle::~curlhandle()
 {
     delete this->timer;