typedef struct infoarea_data {
    int msgs;
    int size;
};

class infoarea {
public:
    //S char echotag[80];
    string echotag;
    //infoarea_data today;
    infoarea_data days[31];
    time_t lastdate; // last msg received
    time_t lasttime; // last time processed
    int ok;

    infoarea() {
        ok=1;
        lastdate=lasttime=0;
        for(int i=0;i<31;i++)
            days[i].msgs=days[i].size=0;
    }
    infoarea(string echo) {
        infoarea();
        echotag=echo;
    }
    infoarea& operator =(infoarea&a);
    int operator >(infoarea&a);
    int operator <(infoarea&a);
    int operator ==(infoarea&a);
    int operator !=(infoarea&a);
    //S char* string(char*buf);
    string str(/*string*/);
    char* str(char*);

    friend ostream& operator<<(ostream& s, const infoarea& x);
    friend istream& operator>>(istream& s, infoarea& x);
};

infoarea& infoarea::operator =(infoarea&a) {
    //S strcpy(echotag,a.echotag);
    echotag=a.echotag;
    //today=a.today;
    for(int i=0; i<31; i++)
        days[i]=a.days[i];
    lastdate=a.lastdate;
    ok=a.ok;
    return *this;
}

inline int infoarea::operator >(infoarea&a) {
    return (echotag>a.echotag);
}

inline int infoarea::operator <(infoarea&a) {
    return (echotag<a.echotag);
}

inline int infoarea::operator ==(infoarea&a) {
    return (echotag==a.echotag);
}

inline int infoarea::operator !=(infoarea&a) {
    return (echotag!=a.echotag);
}

//S char* list_el<infoarea>::string(char*buf)
string infoarea::str(/*string buf*/)
{
    string buf;
    char buf2[8192];
    /*S strcpy(buf,data.echotag);
    strftime(buf2,8192,"%d/%m/%y %T",localtime(&data.lastdate));
    strcat(buf," ");
    strcat(buf,buf2);
    strcat(buf," ! ");
    strftime(buf2,8192,"%d/%m/%y %T",localtime(&::lasttime));
    strcat(buf,buf2);*/
    buf=echotag;
    strftime(buf2,8192,"%d/%m/%y %T",localtime(&lastdate));
    buf+=" ";
    buf+=buf2;
    buf+=" ! ";
    time_t ttime=time(NULL);
    strftime(buf2,8192,"%d/%m/%y %T",localtime(&ttime));
    buf+=buf2;
    for(int i=0; i<31; i++)
    {
        sprintf(buf2," %d:%d",days[i].size,days[i].msgs);
        //S strcat(buf,buf2);
        buf+=buf2;
    }

    return buf;
}

char* infoarea::str(char*bufx)
{
    char buf2[8192];
    string buf;
    buf=echotag;
    strftime(buf2,8192,"%d/%m/%y %T",localtime(&lastdate));
    buf+=" ";
    buf+=buf2;
    buf+=" ! ";
    time_t ttime=time(NULL);
    strftime(buf2,8192,"%d/%m/%y %T",localtime(&ttime));
    buf+=buf2;
    for(int i=0; i<31; i++)
    {
        sprintf(buf2," %d:%d",days[i].size,days[i].msgs);
        buf+=buf2;
    }

    strcpy(bufx,buf);
    return bufx;
}

ostream& operator<<(ostream& s, const infoarea& x)
{
    if(x.ok)
    {
        s<<x.echotag<<" "<<x.lastdate<<" "<<time(NULL);
        for(int i=0; i<31; i++)
        {
            s<<" "<<x.days[i].size<<" "<<x.days[i].msgs;
        }
    }
    return s;
}

istream& operator>>(istream& s, infoarea& x)
{
    //String str;
    time_t lasttime, lastdate;
    string echotag;

    s>>echotag>>lastdate>>lasttime;
    if(s)
    {
        time_t ttime;
        struct tm today, t;
        int temp;

        x.echotag=echotag;
        x.lastdate=lastdate;
        memcpy(&t,localtime(&lasttime),sizeof(t));
        ttime=time(NULL);
        memcpy(&today,localtime(&ttime),sizeof(today));
        int diffdays=today.tm_yday-t.tm_yday+((today.tm_yday-t.tm_yday)<0?365:0);
        for(int i=0 ; i<diffdays; i++)
            x.days[i].size=x.days[i].msgs=0;
        for(int i=0, j=diffdays; i<31; i++, j++)
        {
            if(j<31)
                s>>x.days[j].size>>x.days[j].msgs;
            else
                s>>temp>>temp;
        }
        x.ok=1;
    }
    else
        x.ok=0;

    //cout<<x<<endl;
    return s;
}
