Advertisement
Armandur

Dilbert retriever

Jul 10th, 2012
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.95 KB | None | 0 0
  1. _Calendar.h
  2.  
  3. #ifndef CALENDAR_H
  4. #define CALENDAR_H
  5.  
  6. #include <string>
  7.  
  8. class _Calendar
  9. {
  10. public:
  11.     _Calendar(void);
  12.     _Calendar(int y, int m, int d);
  13.     _Calendar(int date);
  14.  
  15.     ~_Calendar(void);
  16.  
  17.     void setDate(int y, int m, int d);
  18.     void setDate(int date);
  19.  
  20.     void setYear(int y);
  21.     void setMonth(int m);
  22.     void setDay(int d);
  23.  
  24.     int getYear();
  25.     int getMonth();
  26.     int getDay();
  27.  
  28.     void advanceYear();
  29.     void advanceMonth();
  30.     void advanceDay();
  31.  
  32.     bool leapYear();
  33.  
  34.     int getNumDate();
  35.     std::string getStrDate();
  36.  
  37.     int getSystemDate();
  38.     std::string getSystemTime();
  39.  
  40. private:
  41.     int year;
  42.     int month;
  43.     int day;
  44. };
  45.  
  46. #endif
  47.  
  48. _Calendar.cpp
  49.  
  50. #include "_Calendar.h"
  51.  
  52. #include <sstream>
  53. #include <time.h>
  54.  
  55. _Calendar::_Calendar(void)
  56. {
  57. }
  58.  
  59. _Calendar::_Calendar(int y, int m, int d)
  60. {
  61.     this->year = y;
  62.  
  63.     if(m <= 12 && m > 0)
  64.     {
  65.         this->month = m;
  66.     }
  67.     else
  68.     {
  69.         this->month = 1;
  70.     }
  71.  
  72.     if(this->month == 2)
  73.     {
  74.         if(d > 0 && d <= 29)
  75.         {
  76.             if(!leapYear() && d == 29)
  77.             {
  78.                 this->day = 28;
  79.             }
  80.             else
  81.             {
  82.                 this->day = d;
  83.             }
  84.         }
  85.     }
  86.     else if(this->month == 1 || this->month == 3 || this->month == 5 || this->month == 7 || this->month == 8 || this->month == 10 || this->month == 12)
  87.     {
  88.         if(d > 0 && d <= 31)
  89.         {
  90.             this->day = d;
  91.         }
  92.     }
  93.     else
  94.     {
  95.         if(d > 0 && d <= 30)
  96.         {
  97.             this->day = d;
  98.         }
  99.     }
  100. }
  101.  
  102. _Calendar::_Calendar(int date)
  103. {
  104.     this->setDate(date);
  105. }
  106.  
  107.  
  108. _Calendar::~_Calendar(void)
  109. {
  110. }
  111.  
  112. void _Calendar::setDate(int y, int m, int d)
  113. {
  114.     this->year = y;
  115.  
  116.     if(m <= 12 && m > 0)
  117.     {
  118.         this->month = m;
  119.     }
  120.     else
  121.     {
  122.         this->month = 1;
  123.     }
  124.  
  125.     if(this->month == 2)
  126.     {
  127.         if(d > 0 && d <= 29)
  128.         {
  129.             if(!leapYear() && d == 29)
  130.             {
  131.                 this->day = 28;
  132.             }
  133.             else
  134.             {
  135.                 this->day = d;
  136.             }
  137.         }
  138.     }
  139.     else if(this->month == 1 ||this->month == 3 ||this->month == 5 || this->month == 7 || this->month == 8 || this->month == 10 || this->month == 12)
  140.     {
  141.         if(d > 0 && d <= 31)
  142.         {
  143.             this->day = d;
  144.         }
  145.     }
  146.     else
  147.     {
  148.         if(d > 0 && d <= 30)
  149.         {
  150.             this->day = d;
  151.         }
  152.     }
  153. }
  154.  
  155. void _Calendar::setDate(int date)
  156. {
  157.     int y = date / 10000;
  158.     int m = (date % 1000) / 100;
  159.     int d = date % 100;
  160.  
  161.     this->year = y;
  162.     this->month = m;
  163.     this->day = d;
  164. }
  165.  
  166. void _Calendar::setYear(int y)
  167. {
  168.     this->year = y;
  169. }
  170.  
  171. void _Calendar::setMonth(int m)
  172. {
  173.     if(m <= 12 && m > 0)
  174.     {
  175.         this->month = m;
  176.     }
  177.     else
  178.     {
  179.         this->month = 1;
  180.     }
  181. }
  182.  
  183. void _Calendar::setDay(int d)
  184. {
  185.     if(this->month == 2)
  186.     {
  187.         if(d > 0 && d <= 29)
  188.         {
  189.             if(!leapYear() && d == 29)
  190.             {
  191.                 this->day = 28;
  192.             }
  193.             else
  194.             {
  195.                 this->day = d;
  196.             }
  197.         }
  198.     }
  199.     else if(this->month == 1 ||this->month == 3 ||this->month == 5 || this->month == 7 || this->month == 8 || this->month == 10 || this->month == 12)
  200.     {
  201.         if(d > 0 && d <= 31)
  202.         {
  203.             this->day = d;
  204.         }
  205.     }
  206.     else
  207.     {
  208.         if(d > 0 && d <= 30)
  209.         {
  210.             this->day = d;
  211.         }
  212.     }
  213. }
  214.  
  215. int _Calendar::getYear()
  216. {
  217.     return this->year;
  218. }
  219.  
  220. int _Calendar::getMonth()
  221. {
  222.     return this->month;
  223. }
  224.  
  225. int _Calendar::getDay()
  226. {
  227.     return this->day;
  228. }
  229.  
  230. void _Calendar::advanceYear()
  231. {
  232.     this->year++;
  233. }
  234.  
  235. void _Calendar::advanceMonth()
  236. {
  237.     if(this->month == 12)
  238.     {
  239.         this->month = 1;
  240.         this->year++;
  241.     }
  242.     else
  243.     {
  244.         this->month++;
  245.     }
  246. }
  247.  
  248. void _Calendar::advanceDay()
  249. {
  250.     if(this->month == 2)
  251.     {
  252.         if(this->day == 28)
  253.         {
  254.             if(leapYear())
  255.             {
  256.                 this->day++;
  257.             }
  258.             else
  259.             {
  260.                 this->day = 1;
  261.                 this->month++;
  262.             }
  263.         }
  264.         else if(this->day == 29)
  265.         {
  266.             this->day = 1;
  267.             this->month++;
  268.         }
  269.         else
  270.         {
  271.             this->day++;
  272.         }
  273.     }
  274.     else if(this->month == 1 || this->month == 3 || this->month == 5 || this->month == 7 || this->month == 8 || this->month == 10 || this->month == 12)
  275.     {
  276.         if(this->day == 31)
  277.         {
  278.             this->day = 1;
  279.             if(this->month == 12)
  280.             {
  281.                 this->month = 1;
  282.                 year++;
  283.             }
  284.             else
  285.             {
  286.                 this->month++;
  287.             }
  288.         }
  289.         else
  290.         {
  291.             this->day++;
  292.         }
  293.     }
  294.     else
  295.     {
  296.         if(this->day == 30)
  297.         {
  298.             this->day = 1;
  299.             this->month++;
  300.         }
  301.         else
  302.         {
  303.             this->day++;
  304.         }
  305.     }
  306. }
  307.  
  308. bool _Calendar::leapYear()
  309. {
  310.     if(year % 400 == 0)
  311.     {
  312.         return true;
  313.     }
  314.     else if(year % 100 == 0)
  315.     {
  316.         return false;
  317.     }
  318.     else if(year % 4 == 0)
  319.     {
  320.         return true;
  321.     }
  322.     else
  323.     {
  324.         return false;
  325.     }
  326. }
  327.  
  328. int _Calendar::getNumDate()
  329. {
  330.     int date = this->year * 10000;
  331.     date += month * 100;
  332.     date += day;
  333.  
  334.     return date;
  335. }
  336.  
  337. std::string _Calendar::getStrDate()
  338. {
  339.     std::stringstream ss;
  340.     ss << getNumDate();
  341.  
  342.     return ss.str();
  343. }
  344.  
  345. int _Calendar::getSystemDate()
  346. {
  347.     time_t     now = time(0);
  348.     struct tm  tstruct;
  349.     char       buf[9];
  350.     tstruct = *localtime(&now);
  351.     strftime(buf, sizeof(buf), "%Y%m%d", &tstruct);
  352.  
  353.     int date;
  354.  
  355.     std::string str = buf;
  356.  
  357.     str.erase(str.end());
  358.  
  359.     std::istringstream ss(str);
  360.  
  361.     ss >> date;
  362.  
  363.     return date;
  364. }
  365.  
  366. std::string getSystemTime()
  367. {
  368.     time_t     now = time(0);
  369.     struct tm  tstruct;
  370.     char       buf[80];
  371.     tstruct = *localtime(&now);
  372.     strftime(buf, sizeof(buf), "%X", &tstruct);
  373.  
  374.     return buf;
  375. }
  376.  
  377. DilbertImage.h
  378.  
  379. #ifndef DILBERTIMAGE_H
  380. #define DILBERTIMAGE_H
  381.  
  382. #include <string>
  383.  
  384. #include "_Calendar.h"
  385.  
  386. class DilbertImage
  387. {
  388.     public:
  389.         DilbertImage(int d);
  390.         ~DilbertImage(void);
  391.  
  392.         std::string getFilePath();
  393.         std::string getUrl();
  394.  
  395.         _Calendar cal;
  396.  
  397.         static const int first;
  398.  
  399.     private:
  400.         std::string filePath;
  401.         std::string url;
  402.  
  403.         const static std::string startUrl, endUrl;
  404. };
  405.  
  406. #endif
  407.  
  408. DilbertImage.cpp
  409.  
  410. #include "DilbertImage.h"
  411.  
  412. #include <sstream>
  413. #include <iostream>
  414.  
  415. std::string const DilbertImage::startUrl = "http://tjanster.idg.se/dilbertimages/dil";
  416. std::string const DilbertImage::endUrl = ".gif";
  417.  
  418. int const DilbertImage::first = 20060720;
  419.  
  420. DilbertImage::DilbertImage(int d)
  421. {
  422.     cal.setDate(d);
  423.  
  424.     int year, month, date;
  425.  
  426.     year = cal.getYear();
  427.     month = cal.getMonth();
  428.     date = cal.getNumDate();
  429.  
  430.     std::stringstream ss;
  431.  
  432.     ss << "/" << year << "/";
  433.  
  434.     if(month < 10)
  435.     {
  436.         ss << 0;
  437.     }
  438.  
  439.     ss << month << "/" << "Dilbert - " << cal.getNumDate() << ".gif";
  440.  
  441.     filePath = ss.str();
  442.  
  443.     ss.str("");
  444.     ss.clear();
  445.  
  446.     ss << startUrl << date << endUrl;
  447.  
  448.     url = ss.str();
  449.  
  450.     std::cout << url << '\t' << filePath << std::endl;
  451. }
  452.  
  453.  
  454. DilbertImage::~DilbertImage(void)
  455. {
  456. }
  457.  
  458. std::string DilbertImage::getFilePath()
  459. {
  460.     return filePath;
  461. }
  462.  
  463. std::string DilbertImage::getUrl()
  464. {
  465.     return url;
  466. }
  467.  
  468. Application.cpp
  469.  
  470. #define CURL_STATICLIB
  471. #include <curl/curl.h>
  472. #include <curl/easy.h>
  473.  
  474. #include <iostream>
  475. #include <vector>
  476.  
  477. #include "_Calendar.h"
  478. #include "DilbertImage.h"
  479.  
  480. size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
  481. {
  482.     size_t written;
  483.     written = fwrite(ptr, size, nmemb, stream);
  484.     return written;
  485. }
  486.  
  487. int main()
  488. {
  489.     std::locale swedish("swedish");
  490.     std::locale::global(swedish);
  491.  
  492.     int choice, start, end;
  493.  
  494.     choice = start = end = 0;
  495.  
  496.     system("cls");
  497.     do
  498.     {
  499.         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;
  500.  
  501.         std::cin >> choice;
  502.     }
  503.     while(choice < 1 && choice > 4);
  504.  
  505.     std::vector<DilbertImage*> comics;
  506.  
  507.     _Calendar cal;
  508.  
  509.     if(choice == 1)
  510.     {
  511.         cal.setDate(DilbertImage::first);
  512.  
  513.         while(cal.getNumDate() <= cal.getSystemDate())
  514.         {
  515.             comics.push_back(new DilbertImage(cal.getNumDate()));
  516.             cal.advanceDay();
  517.         }
  518.     }
  519.     else if(choice == 2)
  520.     {
  521.         std::cout << "Skriv in startdatum och slutdatum YYYYMMDD" << std::endl;
  522.         std::cin >> start >> end;
  523.  
  524.         cal.setDate(start);
  525.  
  526.         while(cal.getNumDate() <= end)
  527.         {
  528.             comics.push_back(new DilbertImage(cal.getNumDate()));
  529.             cal.advanceDay();
  530.         }
  531.     }
  532.     else if(choice == 3)
  533.     {
  534.         std::cout << "Skriv in slutdatum YYYYMMDD" << std::endl;
  535.         std::cin >> end;
  536.  
  537.         cal.setDate(DilbertImage::first);
  538.  
  539.         while(cal.getNumDate() <= end)
  540.         {
  541.             comics.push_back(new DilbertImage(cal.getNumDate()));
  542.             cal.advanceDay();
  543.         }
  544.     }
  545.     else if(choice == 4)
  546.     {
  547.         return 0;
  548.     }
  549.  
  550.     if(!comics.empty())
  551.     {
  552.         std::cout << comics.size() << " URLer till Dilbert strippar genererade, starta hämntning? Y/N" << std::endl;
  553.  
  554.         char start;
  555.  
  556.         std::cin >> start;
  557.  
  558.         if(start == 'Y' || start == 'y')
  559.         {
  560.             CURL *curl;
  561.             FILE *fp;
  562.             CURLcode res;
  563.  
  564.             curl = curl_easy_init();
  565.  
  566.             if (curl)
  567.             {
  568.                 for(int n = 0; n < comics.size(); n++)
  569.                 {
  570.                     curl = curl_easy_init();
  571.  
  572.                     fp = fopen(comics[n]->getFilePath().c_str(),"wb");
  573.                     curl_easy_setopt(curl, CURLOPT_URL, comics[n]->getUrl().c_str());
  574.                     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
  575.                     curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
  576.                     res = curl_easy_perform(curl);
  577.  
  578.                     curl_easy_cleanup(curl);
  579.                     fclose(fp);
  580.                 }
  581.             }
  582.  
  583.             std::cout << "Hämntning klar" << std::endl;
  584.         }
  585.         else
  586.         {
  587.             std::cout << "Avbruten" << std::endl;
  588.         }
  589.         while(!comics.empty()) delete comics.back(), comics.pop_back();
  590.     }
  591.     else
  592.     {
  593.         std::cout << "comics.empty() == TRUE" << std::endl;
  594.     }
  595.  
  596.     std::cin.get();
  597.     return 0;
  598. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement