mituri

[C][praktyki] Program pobierajacy kod strony internetowej

Oct 5th, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. #include <curl/curl.h>
  5. #include <regex.h>
  6. #include <fstream>
  7.  
  8. using namespace std;
  9.  
  10. int getFileSize(const string &fileName){
  11. ifstream file(fileName.c_str(), ifstream::in | ifstream::binary);
  12. if(!file.is_open()){
  13. return -1;
  14. }
  15. file.seekg(0,ios::end);
  16. int fileSize = file.tellg();
  17. file.close();
  18. return fileSize;
  19. }
  20.  
  21. size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *plik){
  22. size_t written = fwrite(ptr,size,nmemb,plik);
  23. return written;
  24. }
  25.  
  26. int main(int argos, char* argv[2]){
  27.  
  28.  
  29. CURL *curl;
  30. curl = curl_easy_init();
  31. CURLcode res;
  32. FILE *plikk;
  33. plikk = fopen("text.txt","a+");
  34. int lngh = getFileSize("text.txt");
  35. if(lngh==0){
  36.  
  37. if(curl){
  38. curl_easy_setopt(curl,CURLOPT_URL,argv[1]);//pobiera url
  39. curl_easy_setopt(curl, CURLOPT_POST, 1);//
  40. curl_easy_setopt(curl, CURLOPT_POSTFIELDS,
  41. "title=CURL&action=edit");
  42. curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write_data);
  43. curl_easy_setopt(curl,CURLOPT_WRITEDATA,plikk);
  44.  
  45. res = curl_easy_perform(curl);
  46. if(res != CURLE_OK){
  47. cout<<"curl_easy_perform() failed \n";
  48. //fprintf(stderr,"curl_easy_perform() failed: %s",curl_easy_strerror(res));
  49. return 1;
  50. }
  51. curl_easy_cleanup(curl);
  52. fclose(plikk);
  53. }
  54.  
  55. FILE *file;
  56. file = fopen("text.txt","r");
  57. int status;
  58. regmatch_t pm[2];
  59. char msgbuf[100];
  60. regex_t myRegex;
  61.  
  62. char *buffer;
  63. size_t result;
  64. long lsize;
  65.  
  66.  
  67. if(file){
  68. fseek(file,0,SEEK_END);
  69. lsize = ftell(file);
  70. rewind(file);
  71.  
  72. buffer= (char*)malloc (sizeof(char)*lsize);
  73. if (buffer == NULL){
  74. cout<<"BUFFER MA WARTOSC NULL \n";
  75. return(1);
  76. }
  77. result =fread(buffer,1,lsize,file);
  78. if(result !=lsize){
  79. cout<<"BLAD ODCZYTU";
  80. return(1);
  81. }
  82. status= regcomp(&myRegex,argv[2],REG_EXTENDED | REG_ICASE);
  83. if(status){
  84. cout<<"nie mozna skompilowac regex\n";
  85. exit(1);
  86. }
  87. status = regexec(&myRegex,buffer,1,pm,REG_EXTENDED|REG_ICASE);
  88. cout<< "STATUS"<<endl;
  89. if( !status ){
  90. puts("Match");
  91. }
  92. else if( status == REG_NOMATCH ){
  93. puts("No match");
  94. }
  95. else{
  96. regerror(status, &myRegex, msgbuf, sizeof(msgbuf));
  97. fprintf(stderr, "Regex match failed: %s\n", msgbuf);
  98. exit(1);
  99. }
  100.  
  101. fclose(file);
  102. }
  103. }
  104. else cout <<"nie otworzylismy pliku" << endl;
  105.  
  106. return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment