Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- _Calendar.h
- #ifndef CALENDAR_H
- #define CALENDAR_H
- #include <string>
- class _Calendar
- {
- public:
- _Calendar(void);
- _Calendar(int y, int m, int d);
- _Calendar(int date);
- ~_Calendar(void);
- void setDate(int y, int m, int d);
- void setDate(int date);
- void setYear(int y);
- void setMonth(int m);
- void setDay(int d);
- int getYear();
- int getMonth();
- int getDay();
- void advanceYear();
- void advanceMonth();
- void advanceDay();
- bool leapYear();
- int getNumDate();
- std::string getStrDate();
- int getSystemDate();
- std::string getSystemTime();
- private:
- int year;
- int month;
- int day;
- };
- #endif
- _Calendar.cpp
- #include "_Calendar.h"
- #include <sstream>
- #include <time.h>
- _Calendar::_Calendar(void)
- {
- }
- _Calendar::_Calendar(int y, int m, int d)
- {
- this->year = y;
- if(m <= 12 && m > 0)
- {
- this->month = m;
- }
- else
- {
- this->month = 1;
- }
- if(this->month == 2)
- {
- if(d > 0 && d <= 29)
- {
- if(!leapYear() && d == 29)
- {
- this->day = 28;
- }
- else
- {
- this->day = d;
- }
- }
- }
- else if(this->month == 1 || this->month == 3 || this->month == 5 || this->month == 7 || this->month == 8 || this->month == 10 || this->month == 12)
- {
- if(d > 0 && d <= 31)
- {
- this->day = d;
- }
- }
- else
- {
- if(d > 0 && d <= 30)
- {
- this->day = d;
- }
- }
- }
- _Calendar::_Calendar(int date)
- {
- this->setDate(date);
- }
- _Calendar::~_Calendar(void)
- {
- }
- void _Calendar::setDate(int y, int m, int d)
- {
- this->year = y;
- if(m <= 12 && m > 0)
- {
- this->month = m;
- }
- else
- {
- this->month = 1;
- }
- if(this->month == 2)
- {
- if(d > 0 && d <= 29)
- {
- if(!leapYear() && d == 29)
- {
- this->day = 28;
- }
- else
- {
- this->day = d;
- }
- }
- }
- else if(this->month == 1 ||this->month == 3 ||this->month == 5 || this->month == 7 || this->month == 8 || this->month == 10 || this->month == 12)
- {
- if(d > 0 && d <= 31)
- {
- this->day = d;
- }
- }
- else
- {
- if(d > 0 && d <= 30)
- {
- this->day = d;
- }
- }
- }
- void _Calendar::setDate(int date)
- {
- int y = date / 10000;
- int m = (date % 1000) / 100;
- int d = date % 100;
- this->year = y;
- this->month = m;
- this->day = d;
- }
- void _Calendar::setYear(int y)
- {
- this->year = y;
- }
- void _Calendar::setMonth(int m)
- {
- if(m <= 12 && m > 0)
- {
- this->month = m;
- }
- else
- {
- this->month = 1;
- }
- }
- void _Calendar::setDay(int d)
- {
- if(this->month == 2)
- {
- if(d > 0 && d <= 29)
- {
- if(!leapYear() && d == 29)
- {
- this->day = 28;
- }
- else
- {
- this->day = d;
- }
- }
- }
- else if(this->month == 1 ||this->month == 3 ||this->month == 5 || this->month == 7 || this->month == 8 || this->month == 10 || this->month == 12)
- {
- if(d > 0 && d <= 31)
- {
- this->day = d;
- }
- }
- else
- {
- if(d > 0 && d <= 30)
- {
- this->day = d;
- }
- }
- }
- int _Calendar::getYear()
- {
- return this->year;
- }
- int _Calendar::getMonth()
- {
- return this->month;
- }
- int _Calendar::getDay()
- {
- return this->day;
- }
- void _Calendar::advanceYear()
- {
- this->year++;
- }
- void _Calendar::advanceMonth()
- {
- if(this->month == 12)
- {
- this->month = 1;
- this->year++;
- }
- else
- {
- this->month++;
- }
- }
- void _Calendar::advanceDay()
- {
- if(this->month == 2)
- {
- if(this->day == 28)
- {
- if(leapYear())
- {
- this->day++;
- }
- else
- {
- this->day = 1;
- this->month++;
- }
- }
- else if(this->day == 29)
- {
- this->day = 1;
- this->month++;
- }
- else
- {
- this->day++;
- }
- }
- else if(this->month == 1 || this->month == 3 || this->month == 5 || this->month == 7 || this->month == 8 || this->month == 10 || this->month == 12)
- {
- if(this->day == 31)
- {
- this->day = 1;
- if(this->month == 12)
- {
- this->month = 1;
- year++;
- }
- else
- {
- this->month++;
- }
- }
- else
- {
- this->day++;
- }
- }
- else
- {
- if(this->day == 30)
- {
- this->day = 1;
- this->month++;
- }
- else
- {
- this->day++;
- }
- }
- }
- bool _Calendar::leapYear()
- {
- if(year % 400 == 0)
- {
- return true;
- }
- else if(year % 100 == 0)
- {
- return false;
- }
- else if(year % 4 == 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- int _Calendar::getNumDate()
- {
- int date = this->year * 10000;
- date += month * 100;
- date += day;
- return date;
- }
- std::string _Calendar::getStrDate()
- {
- std::stringstream ss;
- ss << getNumDate();
- return ss.str();
- }
- int _Calendar::getSystemDate()
- {
- time_t now = time(0);
- struct tm tstruct;
- char buf[9];
- tstruct = *localtime(&now);
- strftime(buf, sizeof(buf), "%Y%m%d", &tstruct);
- int date;
- std::string str = buf;
- str.erase(str.end());
- std::istringstream ss(str);
- ss >> date;
- return date;
- }
- std::string getSystemTime()
- {
- time_t now = time(0);
- struct tm tstruct;
- char buf[80];
- tstruct = *localtime(&now);
- strftime(buf, sizeof(buf), "%X", &tstruct);
- return buf;
- }
- DilbertImage.h
- #ifndef DILBERTIMAGE_H
- #define DILBERTIMAGE_H
- #include <string>
- #include "_Calendar.h"
- class DilbertImage
- {
- public:
- DilbertImage(int d);
- ~DilbertImage(void);
- std::string getFilePath();
- std::string getUrl();
- _Calendar cal;
- static const int first;
- private:
- std::string filePath;
- std::string url;
- const static std::string startUrl, endUrl;
- };
- #endif
- DilbertImage.cpp
- #include "DilbertImage.h"
- #include <sstream>
- #include <iostream>
- std::string const DilbertImage::startUrl = "http://tjanster.idg.se/dilbertimages/dil";
- std::string const DilbertImage::endUrl = ".gif";
- int const DilbertImage::first = 20060720;
- DilbertImage::DilbertImage(int d)
- {
- cal.setDate(d);
- int year, month, date;
- year = cal.getYear();
- month = cal.getMonth();
- date = cal.getNumDate();
- std::stringstream ss;
- ss << "/" << year << "/";
- if(month < 10)
- {
- ss << 0;
- }
- ss << month << "/" << "Dilbert - " << cal.getNumDate() << ".gif";
- filePath = ss.str();
- ss.str("");
- ss.clear();
- ss << startUrl << date << endUrl;
- url = ss.str();
- std::cout << url << '\t' << filePath << std::endl;
- }
- DilbertImage::~DilbertImage(void)
- {
- }
- std::string DilbertImage::getFilePath()
- {
- return filePath;
- }
- std::string DilbertImage::getUrl()
- {
- return url;
- }
- Application.cpp
- #define CURL_STATICLIB
- #include <curl/curl.h>
- #include <curl/easy.h>
- #include <iostream>
- #include <vector>
- #include "_Calendar.h"
- #include "DilbertImage.h"
- size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
- {
- size_t written;
- written = fwrite(ptr, size, nmemb, stream);
- return written;
- }
- int main()
- {
- std::locale swedish("swedish");
- std::locale::global(swedish);
- int choice, start, end;
- choice = start = end = 0;
- system("cls");
- do
- {
- std::cout << "1.\tHämta serier från 20060720 till dagens datum.\n2.\tHämta serier från ett datum till och med ett annat.\n3.\tHämta serier från 20060720 till och med ett datum.\n4.\tAvsluta." << std::endl;
- std::cin >> choice;
- }
- while(choice < 1 && choice > 4);
- std::vector<DilbertImage*> comics;
- _Calendar cal;
- if(choice == 1)
- {
- cal.setDate(DilbertImage::first);
- while(cal.getNumDate() <= cal.getSystemDate())
- {
- comics.push_back(new DilbertImage(cal.getNumDate()));
- cal.advanceDay();
- }
- }
- else if(choice == 2)
- {
- std::cout << "Skriv in startdatum och slutdatum YYYYMMDD" << std::endl;
- std::cin >> start >> end;
- cal.setDate(start);
- while(cal.getNumDate() <= end)
- {
- comics.push_back(new DilbertImage(cal.getNumDate()));
- cal.advanceDay();
- }
- }
- else if(choice == 3)
- {
- std::cout << "Skriv in slutdatum YYYYMMDD" << std::endl;
- std::cin >> end;
- cal.setDate(DilbertImage::first);
- while(cal.getNumDate() <= end)
- {
- comics.push_back(new DilbertImage(cal.getNumDate()));
- cal.advanceDay();
- }
- }
- else if(choice == 4)
- {
- return 0;
- }
- if(!comics.empty())
- {
- std::cout << comics.size() << " URLer till Dilbert strippar genererade, starta hämntning? Y/N" << std::endl;
- char start;
- std::cin >> start;
- if(start == 'Y' || start == 'y')
- {
- CURL *curl;
- FILE *fp;
- CURLcode res;
- curl = curl_easy_init();
- if (curl)
- {
- for(int n = 0; n < comics.size(); n++)
- {
- curl = curl_easy_init();
- fp = fopen(comics[n]->getFilePath().c_str(),"wb");
- curl_easy_setopt(curl, CURLOPT_URL, comics[n]->getUrl().c_str());
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
- res = curl_easy_perform(curl);
- curl_easy_cleanup(curl);
- fclose(fp);
- }
- }
- std::cout << "Hämntning klar" << std::endl;
- }
- else
- {
- std::cout << "Avbruten" << std::endl;
- }
- while(!comics.empty()) delete comics.back(), comics.pop_back();
- }
- else
- {
- std::cout << "comics.empty() == TRUE" << std::endl;
- }
- std::cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement