//----------- main.h ----------- #pragma comment(lib, "libcurl.lib") #include #include char* ProcessPage(char* pageData); char* SubmitAnswer(char* past, char* info, char* info2, char* info3, unsigned int answer); char* ProcessNextPage(char* curPage); char* GetInitialPage(); unsigned int GoogleDefinition(char* keyword, char* option[4]); unsigned int Thesaurus(char* keyword, char* option[4]); unsigned int Dictionary(char* keyword, char* option[4]); unsigned int Wiktionary(char* keyword, char* option[4]); void *myrealloc(void *ptr, size_t size); size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data); //----------- main.cpp ----------- #include "main.h" CURL *curl; unsigned int curAttempts; struct MemoryStruct { char *memory; size_t size; }; void *myrealloc(void *ptr, size_t size){ if(ptr) return realloc(ptr, size); else return malloc(size); } size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data){ size_t realsize = size * nmemb; struct MemoryStruct *mem = (struct MemoryStruct *)data; mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1); if(mem->memory) { memcpy(&(mem->memory[mem->size]), ptr, realsize); mem->size += realsize; mem->memory[mem->size] = 0; } return realsize; } unsigned int Thesaurus(char* keyword, char* option[4]){ char urlBuffer[255]; sprintf_s(urlBuffer, 255, "http://thesaurus.reference.com/browse/%s", keyword); struct MemoryStruct chunk; chunk.memory = NULL; chunk.size = 0; curl_easy_reset(curl); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); curl_easy_setopt(curl, CURLOPT_URL, urlBuffer); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&chunk); curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0"); curl_easy_perform(curl); unsigned int retVal = rand() % 4; if(chunk.memory == NULL){ return retVal; } char* definitionList = chunk.memory; char* temp; unsigned int foundCount = 0; bool found[4]; for(unsigned int i = 0; i < 4; i++){ found[i] = false; temp = strstr(definitionList, option[i]); if(temp != NULL){found[i] = true; foundCount++; break;} } if(foundCount == 1){ for(unsigned int i = 0; i < 4; i++){ if(found[i]){ retVal = i; goto done; } } }else{ retVal = Dictionary(keyword, option); goto done; } done: if(chunk.memory) free(chunk.memory); return retVal; } unsigned int Dictionary(char* keyword, char* option[4]){ char urlBuffer[255]; sprintf_s(urlBuffer, 255, "http://dictionary.reference.com/browse/%s", keyword); struct MemoryStruct chunk; chunk.memory = NULL; chunk.size = 0; curl_easy_reset(curl); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); curl_easy_setopt(curl, CURLOPT_URL, urlBuffer); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&chunk); curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0"); curl_easy_perform(curl); unsigned int retVal = rand() % 4; if(chunk.memory == NULL){ return retVal; } char* definitionList = chunk.memory; char* temp; unsigned int foundCount = 0; bool found[4]; for(unsigned int i = 0; i < 4; i++){ found[i] = false; temp = strstr(definitionList, option[i]); if(temp != NULL){found[i] = true; foundCount++; break;} } if(foundCount == 1){ for(unsigned int i = 0; i < 4; i++){ if(found[i]){ retVal = i; goto done; } } }else{ retVal = Wiktionary(keyword, option); goto done; } done: if(chunk.memory) free(chunk.memory); return retVal; } unsigned int Wiktionary(char* keyword, char* option[4]){ char urlBuffer[255]; sprintf_s(urlBuffer, 255, "http://en.wiktionary.org/wiki/%s", keyword); struct MemoryStruct chunk; chunk.memory = NULL; chunk.size = 0; curl_easy_reset(curl); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); curl_easy_setopt(curl, CURLOPT_URL, urlBuffer); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&chunk); curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0"); curl_easy_perform(curl); unsigned int retVal = rand() % 4; if(chunk.memory == NULL){ return retVal; } char* definitionList = chunk.memory; char* temp; unsigned int foundCount = 0; bool found[4]; for(unsigned int i = 0; i < 4; i++){ found[i] = false; temp = strstr(definitionList, option[i]); if(temp != NULL){found[i] = true; foundCount++; break;} } if(foundCount == 1){ for(unsigned int i = 0; i < 4; i++){ if(found[i]){ retVal = i; goto done; } } }else{ retVal = GoogleDefinition(keyword, option); goto done; } done: if(chunk.memory) free(chunk.memory); return retVal; } unsigned int GoogleDefinition(char* keyword, char* option[4]){ char urlBuffer[255]; sprintf_s(urlBuffer, 255, "http://www.google.co.uk/search?q=define:%s", keyword); struct MemoryStruct chunk; chunk.memory = NULL; chunk.size = 0; curl_easy_reset(curl); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); curl_easy_setopt(curl, CURLOPT_URL, urlBuffer); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&chunk); curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0"); curl_easy_perform(curl); unsigned int retVal = rand() % 4; if(chunk.memory == NULL){ return retVal; } char* definitionList = strstr(chunk.memory, "