The server address book now uses a linked list. I had some issues merging this with some of taniwha's changes, so it may not be perfect. Please test it.

This commit is contained in:
Brian Koropoff 2000-05-21 20:14:09 +00:00
parent a1c3030387
commit d9da49c2a9
4 changed files with 223 additions and 192 deletions

View file

@ -30,24 +30,28 @@
$Id$ $Id$
*/ */
#include "quakeio.h" #include "quakeio.h"
#define MAX_SERVER_LIST 256
typedef struct { typedef struct server_entry_s {
char *server; char *server;
char *description; char *desc;
int ping; struct server_entry_s *next;
} server_entry_t; struct server_entry_s *prev;
} server_entry_t;
extern server_entry_t *slist;
server_entry_t *Server_List_Add(server_entry_t *start, char *ip, char *desc);
server_entry_t *Server_List_Del(server_entry_t *start, server_entry_t *del);
server_entry_t *Server_List_InsB(server_entry_t *start, server_entry_t *place, char *ip, char *desc);
void Server_List_Swap(server_entry_t *swap1, server_entry_t *swap2);
server_entry_t *Server_List_Get_By_Num(server_entry_t *start, int n);
int Server_List_Len(server_entry_t *start);
extern server_entry_t slist[MAX_SERVER_LIST]; server_entry_t *Server_List_LoadF(QFile *f, server_entry_t *start);
void Server_List_SaveF(QFile *f, server_entry_t *start);
void Server_List_Init(void); void Server_List_Del_All(server_entry_t *start);
void Server_List_Shutdown(void); void Server_List_Shutdown(server_entry_t *start);
int Server_List_Set(int i,char *addr,char *desc);
int Server_List_Reset_NoFree(int i); char *gettokstart(char *str, int req, char delim);
int Server_List_Reset(int i);
void Server_List_Switch(int a,int b);
int Server_List_Len(void);
int Server_List_Load(QFile *f);
int Server_List_Save(QFile *f);
char *gettokstart (char *str, int req, char delim);
int gettoklen(char *str, int req, char delim); int gettoklen(char *str, int req, char delim);

View file

@ -65,7 +65,7 @@
#else #else
#include <netinet/in.h> #include <netinet/in.h>
#endif #endif
#include "cl_slist.h" #include <cl_slist.h>
extern cvar_t *fs_basepath; extern cvar_t *fs_basepath;
extern cvar_t *fs_sharepath; extern cvar_t *fs_sharepath;
@ -1206,10 +1206,9 @@ void CL_Init (void)
CL_InitPrediction (); CL_InitPrediction ();
CL_InitCam (); CL_InitCam ();
Pmove_Init (); Pmove_Init ();
Server_List_Init (); //Init server list
if ((servlist = Qopen(va("%s/servers.txt",fs_basepath->string),"r"))) {
if ((servlist = Qopen("./servers.txt","r"))) { slist = Server_List_LoadF(servlist,slist);
Server_List_Load(servlist);
Qclose(servlist); Qclose(servlist);
} }
@ -1808,7 +1807,7 @@ void Host_Shutdown(void)
} }
isdown = true; isdown = true;
Server_List_Shutdown (); Server_List_Shutdown (slist);
Host_WriteConfiguration (); Host_WriteConfiguration ();

View file

@ -36,109 +36,118 @@
#include "cl_slist.h" #include "cl_slist.h"
#include "bothdefs.h" #include "bothdefs.h"
#include "console.h" #include "console.h"
#include "commdef.h"
#include <stdio.h> #include "zone.h"
#include <stdlib.h>
#include <string.h> #include <string.h>
//Better watch out for buffer overflows extern cvar_t *fs_basepath;
server_entry_t slist[MAX_SERVER_LIST]; server_entry_t *slist;
server_entry_t *Server_List_Add (server_entry_t *start, char *ip, char *desc) {
server_entry_t *p;
p = start;
if (!start) { //Nothing at beginning of list, create it
start = Z_Malloc(sizeof(server_entry_t));
start->prev = 0;
start->next = 0;
start->server = Z_Malloc(strlen(ip) + 1);
start->desc = Z_Malloc(strlen(desc) + 1);
strcpy(start->server,ip);
strcpy(start->desc,desc);
return (start);
}
void Server_List_Init(void) { // Do this or everything else will sig11 for(p=start;p->next;p=p->next); //Get to end of list
p->next = Z_Malloc(sizeof(server_entry_t));
p->next->prev = p;
p->next->server = Z_Malloc(strlen(ip) + 1);
p->next->desc = Z_Malloc(strlen(desc) + 1);
strcpy(p->next->server,ip);
strcpy(p->next->desc,desc);
p->next->next = 0;
return (start);
}
server_entry_t *Server_List_Del(server_entry_t *start, server_entry_t *del) {
server_entry_t *n;
if (del == start) {
Z_Free(start->server);
Z_Free(start->desc);
n = start->next;
if (n)
n->prev = 0;
Z_Free(start);
return (n);
}
Z_Free(del->server);
Z_Free(del->desc);
if (del->prev)
del->prev->next = del->next;
if (del->next)
del->next->prev = del->prev;
Z_Free(del);
return (start);
}
server_entry_t *Server_List_InsB (server_entry_t *start, server_entry_t *place, char *ip, char *desc) {
server_entry_t *new;
server_entry_t *other;
new = Z_Malloc(sizeof(server_entry_t));
new->server = Z_Malloc(strlen(ip) + 1);
new->desc = Z_Malloc(strlen(desc) + 1);
strcpy(new->server,ip);
strcpy(new->desc,desc);
other = place->prev;
if (other)
other->next = new;
place->prev = new;
new->next = place;
new->prev = other;
if (!other)
return new;
return start;
}
void Server_List_Swap (server_entry_t *swap1, server_entry_t *swap2) {
char *p;
p = swap1->server;
swap1->server = swap2->server;
swap2->server = p;
p = swap1->desc;
swap1->desc = swap2->desc;
swap2->desc = p;
}
server_entry_t *Server_List_Get_By_Num (server_entry_t *start, int n) {
int i; int i;
for(i=0;i < MAX_SERVER_LIST;i++) { for (i=0;i < n;i++)
slist[i].server = '\0'; start = start->next;
slist[i].description = '\0'; if (!start)
slist[i].ping = 0; return (0);
} return (start);
} }
int Server_List_Len (server_entry_t *start) {
void Server_List_Shutdown(void) { // I am the liberator of memory.
int i; int i;
QFile *f; for (i=0;start;i++)
if (!(f = Qopen("./servers.txt","w"))) { start=start->next;
Con_Printf("Couldn't open servers.txt.\n");
return;
}
Server_List_Save(f);
Qclose(f);
for(i=0;i < MAX_SERVER_LIST;i++) {
if (slist[i].server)
free(slist[i].server);
if (slist[i].description)
free(slist[i].description);
}
}
int Server_List_Set(int i,char *addr,char *desc) {
int len;
if (i < MAX_SERVER_LIST && i >= 0) {
if (slist[i].server) // (Re)allocate memory first
free(slist[i].server);
if (slist[i].description)
free(slist[i].description);
len = strlen(desc);
slist[i].server = malloc(strlen(addr) + 1);
slist[i].description = malloc(len + 1);
strcpy(slist[i].server,addr);
strcpy(slist[i].description,desc);
return 0; // Yay, we haven't segfaulted yet.
}
return 1; // Out of range
}
int Server_List_Reset_NoFree (int i) { //NEVER USE THIS UNLESS REALLY NEEDED
if (i < MAX_SERVER_LIST && i >= 0) {
slist[i].server = '\0';
slist[i].description = '\0';
slist[i].ping = 0;
return 0;
}
return 1;
}
int Server_List_Reset (int i) {
if (i < MAX_SERVER_LIST && i >= 0) {
if (slist[i].server)
free(slist[i].server);
if (slist[i].description)
free(slist[i].description);
slist[i].server = '\0';
slist[i].description = '\0';
slist[i].ping = 0;
return 0;
}
return 1;
}
void Server_List_Switch(int a,int b) {
server_entry_t temp;
memcpy(&temp,&slist[a],sizeof(temp));
memcpy(&slist[a],&slist[b],sizeof(temp));
memcpy(&slist[b],&temp,sizeof(temp));
}
int Server_List_Len (void) {
int i;
for (i = 0; i < MAX_SERVER_LIST && slist[i].server;i++)
;
return i; return i;
} }
int Server_List_Load (QFile *f) { // This could get messy server_entry_t *Server_List_LoadF (QFile *f,server_entry_t *start) { // This could get messy
int serv = 0; char line[256]; /* Long lines get truncated. */
char line[256]; /* Long lines get truncated. */ int c = ' '; /* int so it can be compared to EOF properly*/
int c = ' '; /* int so it can be compared to EOF properly*/ int len;
char *start; int i;
int len; char *st;
int i; char *addr;
char *addr;
while (1) {
// Init again to clear the list
// Server_List_Shutdown();
// Server_List_Init();
while (serv < MAX_SERVER_LIST) {
//First, get a line //First, get a line
i = 0; i = 0;
c = ' '; c = ' ';
@ -150,35 +159,55 @@ int Server_List_Load (QFile *f) { // This could get messy
} }
} }
line[i - 1] = '\0'; // Now we can parse it line[i - 1] = '\0'; // Now we can parse it
if ((start = gettokstart(line,1,' ')) != NULL) { if ((st = gettokstart(line,1,' ')) != NULL) {
len = gettoklen(line,1,' '); len = gettoklen(line,1,' ');
addr = malloc(len + 1); addr = Z_Malloc(len + 1);
strncpy(addr,&line[0],len); strncpy(addr,&line[0],len);
addr[len] = '\0'; addr[len] = '\0';
if ((start = gettokstart(line,2,' '))) { if ((st = gettokstart(line,2,' '))) {
Server_List_Set(serv,addr,start); start = Server_List_Add(start,addr,st);
} }
else { else {
Server_List_Set(serv,addr,"Unknown"); start = Server_List_Add(start,addr,"Unknown");
} }
serv++; }
} if (c == EOF) // We're done
if (c == EOF) // We're done return start;
return 0; }
}
return 0;
} }
int Server_List_Save(QFile *f) { void Server_List_SaveF (QFile *f,server_entry_t *start) {
int i; do {
for(i=0;i < MAX_SERVER_LIST;i++) { Qprintf(f,"%s %s\n",start->server,start->desc);
if (slist[i].server) start = start->next;
Qprintf(f,"%s %s\n",
slist[i].server, } while (start);
slist[i].description); }
}
return 0; void Server_List_Shutdown (server_entry_t *start) {
} QFile *f;
if (start) {
if ((f = Qopen(va("%s/servers.txt",fs_basepath->string),"w"))) {
Server_List_SaveF(f,start);
Qclose(f);
}
Server_List_Del_All (start);
}
}
void Server_List_Del_All (server_entry_t *start) {
server_entry_t *n;
while (start) {
n = start->next;
Z_Free(start->server);
Z_Free(start->desc);
Z_Free(start);
start = n;
}
}
char *gettokstart (char *str, int req, char delim) { char *gettokstart (char *str, int req, char delim) {
char *start = str; char *start = str;

View file

@ -1164,6 +1164,7 @@ void M_Menu_MultiPlayer_f (void) {
void M_MultiPlayer_Draw (void) { void M_MultiPlayer_Draw (void) {
int serv; int serv;
int line = 1; int line = 1;
server_entry_t *cp;
qpic_t *p; qpic_t *p;
//int f; //int f;
@ -1171,26 +1172,25 @@ void M_MultiPlayer_Draw (void) {
p = Draw_CachePic("gfx/p_multi.lmp"); p = Draw_CachePic("gfx/p_multi.lmp");
M_DrawPic((320-p->width)/2,4,p); M_DrawPic((320-p->width)/2,4,p);
if (!(slist[0].server)) { if (!slist) {
M_DrawTextBox(60,80,23,4); M_DrawTextBox(60,80,23,4);
M_PrintWhite(110,12*8,"No server list"); M_PrintWhite(110,12*8,"No server list");
M_PrintWhite(140,13*8,"found."); M_PrintWhite(140,13*8,"found.");
return; return;
} }
M_DrawTextBox(STAT_X,STAT_Y,23,4); M_DrawTextBox(STAT_X,STAT_Y,23,4);
//M_DrawTextBox(STAT_X+96,STAT_Y+38,12,3);
M_DrawTextBox(STAT_X,STAT_Y+38,23,3); M_DrawTextBox(STAT_X,STAT_Y+38,23,3);
M_DrawTextBox(MENU_X,MENU_Y,23,(m_multip_maxs - m_multip_mins)+1); M_DrawTextBox(MENU_X,MENU_Y,23,(m_multip_maxs - m_multip_mins)+1);
for (serv = m_multip_mins; serv <= m_multip_maxs; serv++) { for (serv = m_multip_mins; serv <= m_multip_maxs && serv < Server_List_Len(slist); serv++) {
if (slist[serv].server) { cp = Server_List_Get_By_Num(slist,serv);
M_Print(MENU_X+18,line*8+MENU_Y, M_Print(MENU_X+18,line*8+MENU_Y,
va("%1.21s", va("%1.21s",
strlen(slist[serv].description) <= m_multip_horiz ? "" : slist[serv].description+m_multip_horiz)); strlen(cp->desc) <= m_multip_horiz ? "" : cp->desc+m_multip_horiz));
line++; line++;
}
} }
cp = Server_List_Get_By_Num(slist,m_multip_cursor);
M_PrintWhite(STAT_X+18,STAT_Y+16,"IP/Hostname:"); M_PrintWhite(STAT_X+18,STAT_Y+16,"IP/Hostname:");
M_Print(STAT_X+18,STAT_Y+24,slist[m_multip_cursor].server); M_Print(STAT_X+18,STAT_Y+24,cp->server);
M_DrawCharacter(MENU_X+8,(m_multip_cursor - m_multip_mins + 1) * 8+MENU_Y, M_DrawCharacter(MENU_X+8,(m_multip_cursor - m_multip_mins + 1) * 8+MENU_Y,
12+((int)(realtime*4)&1)); 12+((int)(realtime*4)&1));
//f = (int)(realtime * 10) % 6; //f = (int)(realtime * 10) % 6;
@ -1199,8 +1199,8 @@ void M_MultiPlayer_Draw (void) {
} }
void M_MultiPlayer_Key (key) { void M_MultiPlayer_Key (key) {
// server_entry_t *pt; server_entry_t *temp;
if (!(slist[0].server) && key != K_ESCAPE && key != K_INS) if (!slist && key != K_ESCAPE && key != K_INS)
return; return;
switch(key) { switch(key) {
case K_ESCAPE: case K_ESCAPE:
@ -1209,7 +1209,7 @@ void M_MultiPlayer_Key (key) {
case KP_DOWNARROW: case KP_DOWNARROW:
case K_DOWNARROW: case K_DOWNARROW:
S_LocalSound("misc/menu1.wav"); S_LocalSound("misc/menu1.wav");
if (m_multip_cursor < (MAX_SERVER_LIST-1) && slist[m_multip_cursor+1].server) { if (Server_List_Get_By_Num(slist,m_multip_cursor+1)) {
m_multip_cursor++; m_multip_cursor++;
} }
break; break;
@ -1229,10 +1229,8 @@ void M_MultiPlayer_Key (key) {
case K_PGDN: case K_PGDN:
S_LocalSound("misc/menu1.wav"); S_LocalSound("misc/menu1.wav");
m_multip_cursor += (m_multip_maxs - m_multip_mins); m_multip_cursor += (m_multip_maxs - m_multip_mins);
if (m_multip_cursor >= MAX_SERVER_LIST) if (Server_List_Len(slist) - 1 < m_multip_cursor )
m_multip_cursor = MAX_SERVER_LIST - 1; m_multip_cursor = Server_List_Len(slist) - 1;
while (!(slist[m_multip_cursor].server))
m_multip_cursor--;
break; break;
case K_RIGHTARROW: case K_RIGHTARROW:
S_LocalSound("misc/menu1.wav"); S_LocalSound("misc/menu1.wav");
@ -1248,7 +1246,7 @@ void M_MultiPlayer_Key (key) {
m_state = m_main; m_state = m_main;
M_ToggleMenu_f(); M_ToggleMenu_f();
CL_Disconnect(); CL_Disconnect();
strncpy(cls.servername,slist[m_multip_cursor].server,sizeof(cls.servername)-1); strncpy(cls.servername,Server_List_Get_By_Num(slist,m_multip_cursor)->server,sizeof(cls.servername)-1);
CL_BeginServerConnect(); CL_BeginServerConnect();
break; break;
case 'e': case 'e':
@ -1257,37 +1255,28 @@ void M_MultiPlayer_Key (key) {
break; break;
case K_INS: case K_INS:
S_LocalSound("misc/menu2.wav"); S_LocalSound("misc/menu2.wav");
if (Server_List_Len() < (MAX_SERVER_LIST-1)) { if (!slist) {
memmove(&slist[m_multip_cursor+1], m_multip_cursor = 0;
&slist[m_multip_cursor], slist = Server_List_Add(slist, "127.0.0.1","<BLANK>");
(Server_List_Len() - m_multip_cursor)*sizeof(slist[0])); }
Server_List_Reset_NoFree(m_multip_cursor); else {
Server_List_Set(m_multip_cursor,"127.0.0.1","<BLANK>"); temp = Server_List_Get_By_Num(slist,m_multip_cursor);
slist = Server_List_InsB(slist, temp, "127.0.0.1","<BLANK>");
} }
break; break;
case K_DEL: case K_DEL:
S_LocalSound("misc/menu2.wav"); S_LocalSound("misc/menu2.wav");
if (Server_List_Len() > 0) { if (Server_List_Len(slist) > 0) {
free(slist[m_multip_cursor].server); slist = Server_List_Del(slist, Server_List_Get_By_Num(slist,m_multip_cursor));
free(slist[m_multip_cursor].description); if (Server_List_Len(slist) == m_multip_cursor && slist)
if (Server_List_Len()-1 == m_multip_cursor) { m_multip_cursor--;
Server_List_Reset_NoFree(m_multip_cursor);
m_multip_cursor = !m_multip_cursor ? 0 : m_multip_cursor-1;
}
else {
memmove(&slist[m_multip_cursor],
&slist[m_multip_cursor+1],
(Server_List_Len()-m_multip_cursor-1) * sizeof(slist[0]));
Server_List_Reset_NoFree(Server_List_Len()-1);
}
} }
break; break;
case ']': case ']':
case '}': case '}':
S_LocalSound("misc/menu1.wav"); S_LocalSound("misc/menu1.wav");
if (m_multip_cursor != Server_List_Len() - 1) { if (m_multip_cursor != Server_List_Len(slist) - 1) {
Server_List_Switch(m_multip_cursor,m_multip_cursor+1); Server_List_Swap(Server_List_Get_By_Num(slist,m_multip_cursor),Server_List_Get_By_Num(slist,m_multip_cursor+1));
m_multip_cursor++; m_multip_cursor++;
} }
break; break;
@ -1295,7 +1284,7 @@ void M_MultiPlayer_Key (key) {
case '{': case '{':
S_LocalSound("misc/menu1.wav"); S_LocalSound("misc/menu1.wav");
if (m_multip_cursor) { if (m_multip_cursor) {
Server_List_Switch(m_multip_cursor,m_multip_cursor-1); Server_List_Swap(Server_List_Get_By_Num(slist,m_multip_cursor),Server_List_Get_By_Num(slist,m_multip_cursor-1));
m_multip_cursor--; m_multip_cursor--;
} }
break; break;
@ -1327,14 +1316,16 @@ int desc_min;
int sedit_state; int sedit_state;
void M_Menu_SEdit_f (void) { void M_Menu_SEdit_f (void) {
server_entry_t *c;
key_dest = key_menu; key_dest = key_menu;
m_entersound = true; m_entersound = true;
m_state = m_sedit; m_state = m_sedit;
sedit_state = 0; sedit_state = 0;
strncpy(serv,slist[m_multip_cursor].server,255); c = Server_List_Get_By_Num(slist,m_multip_cursor);
serv[strlen(slist[m_multip_cursor].server) + 1] = 0; strncpy(serv,c->server,255);
strncpy(desc,slist[m_multip_cursor].description,255); serv[strlen(c->server) + 1] = 0;
desc[strlen(slist[m_multip_cursor].description) + 1] = 0; strncpy(desc,c->desc,255);
desc[strlen(c->desc) + 1] = 0;
serv_max = strlen(serv) > SERV_L ? strlen(serv) : SERV_L; serv_max = strlen(serv) > SERV_L ? strlen(serv) : SERV_L;
serv_min = serv_max - (SERV_L); serv_min = serv_max - (SERV_L);
desc_max = strlen(desc) > DESC_L ? strlen(desc) : DESC_L; desc_max = strlen(desc) > DESC_L ? strlen(desc) : DESC_L;
@ -1362,14 +1353,22 @@ void M_SEdit_Draw (void) {
DESC_Y+8,10+((int)(realtime*4)&1)); DESC_Y+8,10+((int)(realtime*4)&1));
} }
void M_SEdit_Key (int key) { void M_SEdit_Key (int key) {
int l; int l;
server_entry_t *c;
switch (key) { switch (key) {
case K_ESCAPE: case K_ESCAPE:
M_Menu_MultiPlayer_f (); M_Menu_MultiPlayer_f ();
break; break;
case K_ENTER: case K_ENTER:
Server_List_Set(m_multip_cursor,serv,desc); c = Server_List_Get_By_Num(slist,m_multip_cursor);
free(c->server);
free(c->desc);
c->server = malloc(strlen(serv) + 1);
c->desc = malloc(strlen(desc) + 1);
strcpy(c->server,serv);
strcpy(c->desc,desc);
M_Menu_MultiPlayer_f (); M_Menu_MultiPlayer_f ();
break; break;
case K_UPARROW: case K_UPARROW: