/************************************************************************** * * * Atlantis talker MaxZaw header file. * * * * Programmed by Ondrej Jombik. * * (c) Copyright 2000 Condy software inc., All rights reserved. * * * * Updates: 26.9.2000 * * * **************************************************************************/ /* $Platon: maxzaw.h,v 1.2 2002/02/04 15:04:46 host8 Exp $ */ #ifndef __MAXZAW_H #define __MAXZAW_H #include #include #include #ifndef ATL_NICK_STRSIZE # define ATL_NICK_STRSIZE 15 #endif #define MAXZAW_BUFSIZE (100) #define MAXZAW_REALLOCSTEP (15) typedef struct maxzaw_user{ long time,tonline; char nick[ATL_NICK_STRSIZE+1]; }MAXZAW_USER; class maxzawUserTable{ private: int users; struct maxzaw_user*user; int Realloc(void); public: /* Konstruktory. */ maxzawUserTable(); maxzawUserTable(char*filename); /* Destructor. */ ~maxzawUserTable(); /* Vypis na stream. */ void PrintToStream(FILE*f); /* Pomocny vypis na stream. */ void DebugPrintToStream(FILE*f); /* Inicializuje objekt datami zo suboru. */ int ReadFromFile(char*filename); /* Vrati pocet userov. */ int GetUsers(void); /* Utriedi podla zvoleneho stlpca. Defaultne podla tonline. */ /* 1 - time, 2 - nick, 3 - tonline */ void Sort(void); void Sort(int col); /* Vrati pointer na usera s indexom index. */ maxzaw_user*GetUser(int index); /* Prida usera. */ int AddUser(maxzaw_user*user); /* Vymaze usera s indexom index. */ int DeleteUser(int index); /* Prepise usera s indexom index userom s pointrom auser. */ int ReplaceUser(int index,maxzaw_user*auser); }; // Triediace funkcie pre qsort() - interface /////////////////////////////// int maxzaw_time_sort_fnc(const void*a,const void*b); int maxzaw_nick_sort_fnc(const void*a,const void*b); int maxzaw_tonline_sort_fnc(const void*a,const void*b); // maxzawUserTable - methods //////////////////////////////////////////////// int maxzawUserTable::Realloc(void){ /* Realokacia na skutocnu velkost. */ user=(maxzaw_user*)realloc(user,users*sizeof(maxzaw_user)); if(user==NULL){ #if DEBUG fprintf(stderr,"maxzawUserTable::Realloc() ERROR: realloc() failure.\n"); #endif return(0); } return(1); } maxzawUserTable::maxzawUserTable(){ users=0; user=NULL; } maxzawUserTable::maxzawUserTable(char*filename){ maxzawUserTable(); ReadFromFile(filename); } maxzawUserTable::~maxzawUserTable(){ free(user); user=NULL; users=0; } void maxzawUserTable::PrintToStream(FILE*f){ register int k; for(k=0;k0?MAXZAW_REALLOCSTEP:10; user=(maxzaw_user*)realloc(user,ausers*sizeof(maxzaw_user)); if(user==NULL){ #if DEBUG fprintf(stderr,"maxzawUserTable::ReadFromFile() ERROR: realloc() failure.\n"); #endif return(0); } } sscanf(buf,"%ld %15s %ld", &(user[users].time),user[users].nick,&(user[users].tonline)); users++; } fclose(f); return(Realloc()); } inline int maxzawUserTable::GetUsers(void){ return(users); } inline void maxzawUserTable::Sort(void){ Sort(3); } void maxzawUserTable::Sort(int col){ switch(col){ case 1: qsort(user,users,sizeof(user[0]),maxzaw_time_sort_fnc); break; case 2: qsort(user,users,sizeof(user[0]),maxzaw_nick_sort_fnc); break; default: qsort(user,users,sizeof(user[0]),maxzaw_tonline_sort_fnc); break; } } inline maxzaw_user*maxzawUserTable::GetUser(int index){ return(index>=0&&indextime; // user[users-1].tonline=user->tonline; // strcpy(user[users-1].nick,user->nick); return(1); } int maxzawUserTable::DeleteUser(int index){ register int k; if(index<0||index>=users)return(0); users--; for(k=index;k=users||auser==NULL)return(0); user[index]=*auser; return(1); } // Triediace funkcie pre qsort() - implementation ////////////////////////// int maxzaw_time_sort_fnc(const void*a,const void*b){ return(((struct maxzaw_user*)a)->time>((struct maxzaw_user*)b)->time?-1: ((struct maxzaw_user*)a)->time<((struct maxzaw_user*)b)->time?1:maxzaw_nick_sort_fnc(a,b)); } int maxzaw_nick_sort_fnc(const void*a,const void*b){ return(strcmp(((struct maxzaw_user*)a)->nick,((struct maxzaw_user*)b)->nick)); } int maxzaw_tonline_sort_fnc(const void*a,const void*b){ return(((struct maxzaw_user*)a)->tonline>((struct maxzaw_user*)b)->tonline?-1: ((struct maxzaw_user*)a)->tonline<((struct maxzaw_user*)b)->tonline?1:maxzaw_time_sort_fnc(a,b)); } #endif // __MAXZAW_H