Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream>
- #include <curl/curl.h>
- #include <regex.h>
- #include <fstream>
- using namespace std;
- int getFileSize(const string &fileName){
- ifstream file (fileName.c_str(), ifstream::in | ifstream::binary);
- if(!file.is_open()){
- return(-1);
- }
- file.seekg(0,ios::end);
- int fileSize = file.tellg();
- file.close();
- return fileSize;
- }
- size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *plik){
- size_t written = fwrite(ptr,size,nmemb,plik);
- return written;
- }
- int main(int args, char* argv[2]){
- CURL *curl;
- curl = curl_easy_init();
- CURLcode res;
- FILE *plik;
- plik = fopen("text.txt","a+");
- int lngh = getFileSize("text.txt");
- if(lngh==0){
- if(curl){
- curl_easy_setopt(curl,CURLOPT_USERAGENT,"Mozilla/5.0 Gecko/20100101 Firefox/33.1" );
- curl_easy_setopt(curl,CURLOPT_URL,argv[1]);//pobiera url
- curl_easy_setopt(curl, CURLOPT_POST, 1);//
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS,"title=CURL&action=edit");
- curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write_data);
- curl_easy_setopt(curl,CURLOPT_WRITEDATA,plik);
- res = curl_easy_perform(curl);
- if(res != CURLE_OK){
- cout<<"curl_easy_perform() failed \n";
- return(1);
- }
- curl_easy_cleanup(curl);
- fclose(plik);
- }
- }
- FILE *file;
- file = fopen("text.txt","r");
- int status;
- regmatch_t pm;
- char msgbuf[100];
- regex_t myRegex;
- char *buffer;
- size_t result;
- long lsize;
- int sizeWord;
- char *next=NULL;
- int number = 0;
- if(!file)return 1;
- fseek(file,0,SEEK_END);
- lsize = ftell(file);
- rewind(file);
- buffer= (char*)malloc (sizeof(char)*lsize);
- if (buffer == NULL){
- cout<<"BUFFER MA WARTOSC NULL \n";
- return(1);
- }
- result =fread(buffer,1,lsize,file);
- if(result !=lsize){
- cout<<"BLAD ODCZYTU";
- return(1);
- }
- status= regcomp(&myRegex,argv[2],REG_EXTENDED | REG_ICASE);
- if(status){
- cout<<"nie mozna skompilowac regex\n";
- exit(1);
- }
- next = buffer;
- while(!status){
- status = regexec(&myRegex,next,1,&pm,REG_EXTENDED|REG_ICASE);
- cout<<endl<< "STATUS: ";
- if( !status ){
- number += 1;
- cout<<"Wystapienie nr: "<<number<<endl;
- cout<< "slowo stoi na pozycji: "<< pm.rm_so <<"-"<<pm.rm_eo<< endl;
- sizeWord = pm.rm_eo - pm.rm_so;
- cout<< "dlugosc slowa: " << sizeWord<< endl;
- cout<<"slowo brzmi: ";
- for(int i = pm.rm_so; i<pm.rm_eo; i++){
- cout<<next[i];
- }
- cout<<endl<<endl;
- }
- else if( status == REG_NOMATCH ){
- puts("No match");
- }
- else{
- regerror(status, &myRegex, msgbuf, sizeof(msgbuf));
- fprintf(stderr, "Regex match failed: %s\n", msgbuf);
- exit(1);
- }
- next+=pm.rm_eo;
- }
- fclose(file);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment