Bonjour à tous,

Voilà je vous explique mon petit soucis...

Je développe sous VMWARE dans une machine virtuelle WIN2K avec DEVC++. Mon application fonctionne sans problème sous WIN2K et dés que je l'exécute sous WINXP paf... "Segmentation fault" Donc je vérifie tous mes tableaux istoire de voir si je n'appel pas le 15 éme élément d'un tableau qui en compte 10... Mais a première vue rien... Donc je lance GDB et je recherche la fonction ou la ligne qui foire... Toujours rien... Donc je lance un bon débug pas à pas et... à la position où le programme bloque il n'y a rien... Je suis complètement perdu... Apparement et ce n'est pas la première fois WIN2K est très tolérant pour les accès mémoires... Et WINXP tout le contraire... Mais bon savoir cela ne m'aide pas ! Donc voilà à tout hasard, je laisse mon code au bas de ce message...

Regarder pas trop au code car je débute en C++... C'est que mon troisième "robot"...

NB: L'erreur semble se produire au environs de la fonction parse_argv()

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
 
// Set up all happy API's that we will use !!!
#include <cstdlib> // Char Standard Library
#include <string> // C++ String Library
#include <windows.h> // Windows OS function Library
#include <time.h> // Time Library
#include <iostream> // Standard input / Output Stream Library
#include <conio2.h>
#include <fstream>
#include <sstream> // Standard stream var Library
#include <stdio.h> // Standard input / ouput Library
#include <stdlib.h> // Standard Library
#include <curl/curl.h> // The cURL Library ;-)
#include <greta/regexpr2.h> // REGEX compatible PCRE
#include <Console.h>
 
 
// We Will use Standard namespace
using namespace std;
// We Will use Regular Expression namespace
using namespace regex;
 
// Console Color API
namespace con = JadedHoboConsole;
 
// cURL error Buffer
static char errorBuffer[CURL_ERROR_SIZE];
 
// Write all expected data in here
static string HTML_buffer;
static string IMG_buffer;
 
string cfg_data[7]; // USEPROXY / IPPROXY / PORT / AUTH / LOGIN / PASS
string full_argv;
 
int perc = 0;
 
// Function to get all the data's gotten from cURL
static int HTML_writer(char *data, size_t size, size_t nmemb, std::string *HTML_buffer)
{
  // What we will return
  int result = 0;
  // Is there anything in the buffer?
  if (HTML_buffer != NULL)
  {
    // Append the data to the buffer
    HTML_buffer->append(data, size * nmemb);
    // How much did we write?
    result = size * nmemb;
  }
  return result;
}
 
// Function to get all the data's gotten from cURL
static int IMG_writer(char *data, size_t size, size_t nmemb, std::string *IMG_buffer)
{
  // What we will return
  int result = 0;
  // Is there anything in the buffer?
  if (IMG_buffer != NULL)
  {
    // Append the data to the buffer
    IMG_buffer->append(data, size * nmemb);
    // How much did we write?
    result = size * nmemb;
  }
  return result;
}
 
// Generate file name from URL
string create_file_name(string &StringReplace)
{
     static const rpattern Pattern_1("^http://", NOCASE | GLOBAL | EXTENDED | SINGLELINE);
     static const rpattern Pattern_2("/","-", GLOBAL | EXTENDED | SINGLELINE);
     subst_results Results;
     Pattern_1.substitute(StringReplace, Results);
     Pattern_2.substitute(StringReplace, Results);
     return StringReplace; 
}
 
// Strip the label of the option in the cURL.ini file
string strip_ini_header(string &StringReplace)
{
    static const rpattern Pattern("^(.+?)=", NOCASE | GLOBAL | EXTENDED | SINGLELINE);
    subst_results Results;
    Pattern.substitute(StringReplace, Results);
    return StringReplace;
}
 
string parse_argv(string partial_argv, int x)
{
       if (x == 1)
       {
          full_argv = full_argv + partial_argv;
       }
       else
       {
          full_argv = full_argv + " " + partial_argv;
       }
}
 
// test
int SET_START_END(string url_str,string rxstring,int ref)
{
     match_results results;
     rpattern pat(rxstring, NOCASE);
     match_results::backref_type br = pat.match( url_str, results );
 
     string bla;
     bla = "";
 
     // If it worked or not...
     if( br.matched ) 
     {
         stringstream blubb;
         blubb.clear();
         blubb << results.backref(ref);
         bla = blubb.str();
         blubb.str("");
         return atoi(bla.c_str());
     } 
     else 
     {
         return 0;
     }
}
 
// Draw a simple window at x,y coordinate, with a title and a size
void Draw_window(int sx, int sy, int px, int py,string title)
{
     int height;
     int weight;
 
     height = 0;
     while (height < py)
     {
           weight = 0;
           while (weight < px)
           {
                 gotoxy((sx+weight),(sy+height));
                 if (height == 0 || height == (py-1))
                 {
                     cout << con::fg_red << con::bg_white;       
                 }
                 else
                 {
                     if (weight == 0 || weight == (px-1))
                     {
                        cout << con::fg_red << con::bg_white;        
                     }
                     else
                     {
                        cout << con::fg_white << con::bg_red; 
                     }
                 }
                 cout << " ";
                 weight++;  
           }
           height++;
     }
     gotoxy((sx+3),(sy+2));
     cout << con::fg_white << con::bg_red;
     cout << title;
     gotoxy(1,((sy+height)+1));
     cout << con::fg_white << con::bg_black;
}
 
// function to check is the url of the image is valid
int Check_it(string url_str,string rxstring)
{
     match_results results;
     rpattern pat(rxstring, NOCASE | EXTENDED | GLOBAL | ALLBACKREFS | SINGLELINE);
     match_results::backref_type br = pat.match( url_str, results );
 
     // If it worked or not...
     if( br.matched ) 
     {
         cout << con::fg_green << "[REGEX] -> MATCHED : " << url_str << " WITH " << rxstring << endl;
         return 1;
     } 
     else 
     {
         cout << con::fg_red << "[REGEX] -> MATCHED : " << url_str << " WITH " << rxstring << endl; 
         return 0;
     }  
}
 
// Function to check if the HREF is a page or an Full URL
string Eject_garbage(string url_str,string rxstring,int current)
{
     match_results results;
     rpattern pat(rxstring, NOCASE | EXTENDED | GLOBAL | ALLBACKREFS | SINGLELINE);
     match_results::backref_type br = pat.match( url_str, results );
 
     string bla;
     bla = "";
     // If it worked or not...
     if( br.matched ) 
     {
         stringstream blubb;
         int i = 6;
         blubb.clear();
         blubb << results.backref(i);
         bla = blubb.str();
 
         gotoxy(1,12);
         cout << con::fg_gray << "[IMG] : " << bla;
         cout << "                                                                                " << endl;
         blubb.str("");
         gotoxy(1,15);
         cout << con::fg_gray << "[WRITE] : " << bla;
         cout << "                                                                                " << endl;
 
         fstream file_op("files\\img_url_table.txt",ios::out|ios::app);
         file_op << bla << endl;
         file_op.close();
 
         fstream file_last("files\\last_link.txt",ios::out);
         file_last << current << endl;
         file_last.close();
         return bla;
     } 
     else 
     {
         fstream file_last("files\\last_link.txt",ios::out);
         file_last<< current <<endl;
         file_last.close();
         return bla;
     } 
     gotoxy(1,12);
}
 
// convert integeer to string
string convert_to_string(int number)
{
    char  n_string [8];
    sprintf(n_string,"%i",number);
    return n_string;
}
 
// Curl function to download photos (files)
void curldownloader(string url,int num)
{
    gotoxy(1,10);
    cout << con::fg_green << "[cURL] -> DOWNLOADING : " << url;
    cout << "                                                                                " << endl;
    string filename = url;
 
    // Our curl objects
    CURL *curl;
    CURLcode result;
    IMG_buffer = "";
 
    curl = curl_easy_init();
 
    if (curl)
    {
      // Now set up all of the curl options
      curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer); // Curl Error Buffer   
      curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // Url to seek  
      curl_easy_setopt(curl, CURLOPT_USERAGENT ,"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 1.1.4322)");
      curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 15); // Number of seconds before canceling connection attempt
      curl_easy_setopt(curl, CURLOPT_HEADER, 0); // Return or not the http header
      curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0); // Set if curl should follow redirection
      curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1); // Fail on 400 errors
      curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 500); // Set The Abort speed limit for transfert below 500 bytes/second
      curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 10); // Set The Abort time limit for transfert during more than 10 seconds with a speed below 500 bytes/second
      curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, IMG_writer); // Function to send data to
      curl_easy_setopt(curl, CURLOPT_WRITEDATA, &IMG_buffer); // Buffer to write data to
 
      if (cfg_data[0] == "1")
      {
      string proxy_address = cfg_data[1] + ":" + cfg_data[2];
      curl_easy_setopt(curl, CURLOPT_PROXY, proxy_address.c_str()); // Proxy address and port 
	  curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); // Proxy type
      }
 
      if (cfg_data[3] == "1")
      {
      string login_pass = cfg_data[4] + ":" + cfg_data[5];                
      curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); // Proxy Authentication type
	  curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, login_pass.c_str()); // Proxy Name and Pass
      }
 
      // Attempt to retrieve the remote page
      result = curl_easy_perform(curl);
 
      // Always cleanup
      curl_easy_cleanup(curl);
 
      // Did we succeed?
      if (result == CURLE_OK)
      {
            gotoxy(1,12); // http://www.tasoeur.biz/images/
            create_file_name(filename);
            filename = "images\\" + filename;
            cout << con::fg_green << "[cURL] -> OK " << filename;
            cout << "                                                                                " << endl;
            fstream file_op(filename.c_str(),ios::out|ios::binary);
            file_op << IMG_buffer;
            file_op.close();
      }
      else
      {
            gotoxy(1,17);
            // cout << con::fg_red << "[cURL] -> ERROR : " << errorBuffer << endl;
            cout << con::fg_red << "[cURL] -> ERROR " << endl;
      }
 
    }
}
 
// Try to get the URL leading to the real picture address ;-)
void curlfunc(string url,int qty)
{
    gotoxy(1,10);
    cout << con::fg_green << "[cURL] -> CHECKING : " << url;
    cout << "                                                                                " << endl;
 
    static string rxstring = "<img(.+?)id=(\"|')tasoeur_img[0-9]{1,6}(\"|')(.+?)src=(\"|')(.+?)(\"|')(.+?)/>";  // src 6:9 
 
    // Our curl objects
    CURL *curl;
    CURLcode result;
    HTML_buffer = "";
 
    curl = curl_easy_init();
 
    if (curl)
    {
      // Now set up all of the curl options
      curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer); // Curl Error Buffer   
      curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // Url to seek  
      curl_easy_setopt(curl, CURLOPT_USERAGENT ,"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 1.1.4322)");
      curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 15); // Number of seconds before canceling connection attempt
      curl_easy_setopt(curl, CURLOPT_HEADER, 0); // Return or not the http header
      curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0); // Set if curl should follow redirection
      curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1); // Fail on 400 errors
      curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 500); // Set The Abort speed limit for transfert below 500 bytes/second
      curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 10); // Set The Abort time limit for transfert during more than 10 seconds with a speed below 500 bytes/second
      curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, HTML_writer); // Function to send data to
      curl_easy_setopt(curl, CURLOPT_WRITEDATA, &HTML_buffer); // Buffer to write data to
 
      if (cfg_data[0] == "1")
      {
      string proxy_address = cfg_data[1] + ":" + cfg_data[2];
      curl_easy_setopt(curl, CURLOPT_PROXY, proxy_address.c_str()); // Proxy address and port 
	  curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); // Proxy type
      }
 
      if (cfg_data[3] == "1")
      {
      string login_pass = cfg_data[4] + ":" + cfg_data[5];                
      curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); // Proxy Authentication type
	  curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, login_pass.c_str()); // Proxy Name and Pass
      }
 
      // Attempt to retrieve the remote page
      result = curl_easy_perform(curl);
 
      // Always cleanup
      curl_easy_cleanup(curl);
 
      // Did we succeed?
      if (result == CURLE_OK)
      {
            gotoxy(1,11);
            cout << con::fg_green << "[cURL] -> OK " << qty;
            Eject_garbage(HTML_buffer,rxstring,qty);
      }
      else
      {
            gotoxy(1,17);
            // cout << con::fg_red << "[cURL] -> ERROR : " << errorBuffer << endl;
            cout << con::fg_red << "[cURL] -> ERROR " << qty << endl;
      }
 
    }
}
 
// Read the file where the image URL are
void read_link_table()
{
    ifstream file;
    int i = 0;
    string line;
    file.open("files\\img_url_table.txt");	//open a file
 
    if (file.is_open())
    {
       while (! file.eof() )
       {
             getline (file,line);
             Sleep(1000);
             curldownloader(line,i);
             i++;
       }
       file.close();
    }
    else
    {
       cout << "Unable to open file";
    } 		 
}
 
// Read and get infos within the INI file
void read_ini_file()
{
    ifstream file;
    int i = 0;
    string line;
    file.open("tsh.ini");	//open a file
    if (file.is_open())
    {
       while (! file.eof() )
       {
             getline (file,line);
             cfg_data[i] = strip_ini_header(line);
             i++;
       }
       file.close();
    }
    else
    {
       cout << "Unable to open file";
    } 		 
}
 
// At startup choose what to do.... 
void Draw_choice(int min, int max)
{
     int touche = 0;
     gotoxy(1,20);
 
     cout << "Do you want to download images(I) or URL(U)?" << endl;
 
     touche = getch();
     if (touche == 85 || touche == 117)
     {        
        int i = min;
 
        while (i < max)
        {
              Sleep(1000);
              curlfunc("http://www.tasoeur.biz/bonnasses/voir/" + convert_to_string(i),i);
              i++;
        }
      }
      else
      {
          if (touche == 73 || touche == 105)
          {
             read_link_table();       
          }
          else
          {
             cout << "Please choose between I or U ;-)" << endl;
             Draw_choice(min,max);         
          }
      }
}
 
// main function
int main(int argc, char *argv[])
{            
             int x = 1;
             clrscr();
             Draw_window(2,2,40,5,"HARVESTER v0.4b");
             string start_pos = "-S([0-9]{1,5})";
             string end_pos = "-E([0-9]{1,5})";
 
             if (argc == 3)
             {
                read_ini_file();
 
                while (x < argc)
                { 
                      parse_argv(argv[x],x);
                      x++;
                }
 
                if (SET_START_END(full_argv,start_pos,1) != 0 && SET_START_END(full_argv,end_pos,1) != 0)
                {
                   if (SET_START_END(full_argv,start_pos,1) < SET_START_END(full_argv,end_pos,1))
                   {
                         Draw_choice(SET_START_END(full_argv,start_pos,1),SET_START_END(full_argv,end_pos,1));                                   
                   }
                   else
                   {
                       gotoxy(1,21);
                       cout << con::fg_red << "[MSG]::BE CLEVER... -S SHOULD BE SMALLER THAN -E ;-)";
                   }
                }
                else
                {
                    gotoxy(1,21);
                    cout << con::fg_red << "[MSG]::PLEASE CHECK YOUR PARAMETERS ONE OF THEM IS NULL OR NOT A NUMBER !";
                }                
 
             }
             else
             {
                 gotoxy(1,21);
                 cout << con::fg_red << "[MSG]::USAGE \"TSH -S<integer> -E<integer>\"";
 
             }
             gotoxy(1,22);
             system("pause");
             return 0;
}