All pastes #793566 Raw Edit

Unnamed

public cpp v1 · immutable
·published 2007-11-22 11:50 UTC
rendered paste body
#ifndef __CALENDAR_H__#define __CALENDAR_H__#include <list>#include <map>#include <string>#include <stdexcept>//#include "vector.h"namespace lab2 {typedef std::list<std::string> strlist;    template<typename T>    class Calendar {    public:        enum format {list,cal,iCalendar};        Calendar<T>() {            m_Date = T();        };                template<typename U> Calendar<T>(const Calendar<U> &c) {            operator=(c);        }                bool set_date(int year,int month,int day){            try {                m_Date = T(year,month,day);            }catch(std::out_of_range e) {                return false;            }            return true;        };                bool add_event(const std::string &event) {            return add_event(event,m_Date.day(),m_Date.month(),m_Date.year());        };                bool add_event(const std::string &event, int day) {            return add_event(event,day,m_Date.month(),m_Date.year());        };                bool add_event(const std::string &event, int day,int month){            return add_event(event,day,month,m_Date.year());        };                bool add_event(const std::string &event, int day, int month,int year){            T newDate;                        try {                newDate = T(year,month,day);            }catch(std::out_of_range e) {                return false;            }            //adds a newDate or returns an iterator to it            typename std::map<T,strlist>::iterator i = events.insert(make_pair(newDate,strlist())).first;            strlist::iterator eI;            for(eI = i->second.begin();eI != i->second.end();eI++) {                if(*eI == event)                    return false;            }            i->second.push_back(event);            return true;        };                bool remove_event(const std::string &event,int day = -1, int month = -1, int year = -1) {            T evdate;            if(day == -1)                day = m_Date.day();            if(month == -1)                month = m_Date.month();            if(year == -1)                year = m_Date.year();                            try {                evdate = T(year,month,day);            }catch(std::out_of_range e) {                return false;            }            typename std::map<T,strlist>::iterator i = events.find(evdate);                        if( i == events.end())                 return false;            else {                strlist::iterator eI;                for(eI = i->second.begin(); eI != i->second.end();eI++) {                    if(*eI == event) {                        i->second.erase(eI);                        return true;                    }                }            }            return false;        };                /* Tries to move an event           It removes the event from d1, then creates a new event in d2.            If the event already is in d2, it will not be duplicated           */        bool move_event(const T &d1,const T &d2,const std::string &ev) {            if(!remove_event(ev,d1.day(),d1.month(),d1.year()) )                return false;                        if(!add_event(ev,d2.day(),d2.month(),d2.year()))                return false;                        return true;        };                        bool set_format(Calendar::format f) {            m_format = f;            return true;        };        template<typename U> Calendar<T>& operator=(const Calendar<U>& calendar) {            m_Date = calendar.m_Date;                        typename std::map<U,strlist>::const_iterator i;            for(i = calendar.events.begin(); i != calendar.events.end(); i++) {                events.insert(*i);            }            return *this;        };            template<typename U> friend std::ostream& operator<<(std::ostream &of,const Calendar<U> &c);        enum format m_format;        std::map<T,strlist> events;        T m_Date;    };        template<typename U> std::ostream &operator<<(std::ostream &of, const Calendar<U> &c){    typename std::map<U,strlist>::const_iterator i;        //print "heaaders"    if(c.m_format == Calendar<U>::iCalendar) {        of << std::string("BEGIN:VCALENDAR\n");        of << "VERSION:2.0\n";//        of << "PRODID:-\\iCal Exporter\\Johan\n";    } else if(c.m_format == Calendar<U>::cal)        for(i = c.events.begin(); i != c.events.end();i++) {        if(i->first > c.m_Date) {            strlist::const_iterator strit;            for(strit = i->second.begin();strit != i->second.end();strit++){                if(c.m_format == Calendar<U>::list || c.m_format == Calendar<U>::cal) {                    of << i->first << " : " << *strit << "\n";                } else if(c.m_format == Calendar<U>::iCalendar) {/*                    of << "BEGIN:VEVENT\n";                    //set padding!                    of << "DSTART: " << i->first.year() << i.first.month() << i.first.day() << "T080000\n";                    of << "DEND: " << i->first.year() << i.first.month() << i.first.day() << "T090000\n";                     of << "SUMMARY: " << *strit << "\n";                    of << "END:VEVENT\n"; */               }            }        }    }    //footer    if(c.m_format == Calendar<U>::iCalendar) {//        of << "END:VCALENDAR\n";    }    return of;    }    };#endif