mituri

[C][praktyki] wyszukuje w kodzie pobranej strony dane slowo

Oct 5th, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 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.  
  12. ifstream file (fileName.c_str(), ifstream::in | ifstream::binary);
  13. if(!file.is_open()){
  14. return(-1);
  15. }
  16. file.seekg(0,ios::end);
  17. int fileSize = file.tellg();
  18. file.close();
  19. return fileSize;
  20. }
  21.  
  22. size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *plik){
  23. size_t written = fwrite(ptr,size,nmemb,plik);
  24. return written;
  25. }
  26.  
  27. int main(int args, char* argv[2]){
  28.  
  29. CURL *curl;
  30. curl = curl_easy_init();
  31. CURLcode res;
  32. FILE *plik;
  33. plik = fopen("text.txt","a+");
  34. int lngh = getFileSize("text.txt");
  35. if(lngh==0){
  36. if(curl){
  37.  
  38. curl_easy_setopt(curl,CURLOPT_USERAGENT,"Mozilla/5.0 Gecko/20100101 Firefox/33.1" );
  39.  
  40. curl_easy_setopt(curl,CURLOPT_URL,argv[1]);//pobiera url
  41. curl_easy_setopt(curl, CURLOPT_POST, 1);//
  42. curl_easy_setopt(curl, CURLOPT_POSTFIELDS,"title=CURL&action=edit");
  43. curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write_data);
  44. curl_easy_setopt(curl,CURLOPT_WRITEDATA,plik);
  45.  
  46. res = curl_easy_perform(curl);
  47. if(res != CURLE_OK){
  48. cout<<"curl_easy_perform() failed \n";
  49. return(1);
  50. }
  51. curl_easy_cleanup(curl);
  52. fclose(plik);
  53. }
  54. }
  55. FILE *file;
  56. file = fopen("text.txt","r");
  57. int status;
  58. regmatch_t pm;
  59. char msgbuf[100];
  60. regex_t myRegex;
  61.  
  62. char *buffer;
  63. size_t result;
  64. long lsize;
  65. int sizeWord;
  66. char *next=NULL;
  67. int number = 0;
  68.  
  69. if(!file)return 1;
  70.  
  71. fseek(file,0,SEEK_END);
  72. lsize = ftell(file);
  73. rewind(file);
  74.  
  75. buffer= (char*)malloc (sizeof(char)*lsize);
  76. if (buffer == NULL){
  77. cout<<"BUFFER MA WARTOSC NULL \n";
  78. return(1);
  79. }
  80. result =fread(buffer,1,lsize,file);
  81. if(result !=lsize){
  82. cout<<"BLAD ODCZYTU";
  83. return(1);
  84. }
  85. status= regcomp(&myRegex,argv[2],REG_EXTENDED | REG_ICASE);
  86. if(status){
  87. cout<<"nie mozna skompilowac regex\n";
  88. exit(1);
  89. }
  90.  
  91. next = buffer;
  92. while(!status){
  93.  
  94. status = regexec(&myRegex,next,1,&pm,REG_EXTENDED|REG_ICASE);
  95.  
  96. cout<<endl<< "STATUS: ";
  97. if( !status ){
  98. number += 1;
  99.  
  100. cout<<"Wystapienie nr: "<<number<<endl;
  101. cout<< "slowo stoi na pozycji: "<< pm.rm_so <<"-"<<pm.rm_eo<< endl;
  102. sizeWord = pm.rm_eo - pm.rm_so;
  103. cout<< "dlugosc slowa: " << sizeWord<< endl;
  104. cout<<"slowo brzmi: ";
  105. for(int i = pm.rm_so; i<pm.rm_eo; i++){
  106. cout<<next[i];
  107. }
  108. cout<<endl<<endl;
  109.  
  110. }
  111. else if( status == REG_NOMATCH ){
  112. puts("No match");
  113. }
  114. else{
  115. regerror(status, &myRegex, msgbuf, sizeof(msgbuf));
  116. fprintf(stderr, "Regex match failed: %s\n", msgbuf);
  117. exit(1);
  118. }
  119. next+=pm.rm_eo;
  120. }
  121. fclose(file);
  122. return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment