Skip to content
Browse files

Moved name-list handling out to new files 'names.*'

  • Loading branch information...
1 parent 81f7bb7 commit a886cb22b66c51a8e276b85678ee61404640d443 Sound and Fury committed Aug 14, 2010
Showing with 104 additions and 134 deletions.
  1. +5 −2 Makefile
  2. +1 −0 buffer.c
  3. +1 −7 buffer.h
  4. +61 −0 names.c
  5. +21 −0 names.h
  6. +15 −125 quirc.c
View
7 Makefile
@@ -10,9 +10,9 @@ all: quirc
install: all
install -D quirc $(PREFIX)/bin/quirc
-quirc: quirc.c ttyraw.o ttyraw.h ttyesc.o ttyesc.h irc.o irc.h bits.o bits.h colour.o colour.h buffer.o buffer.h numeric.h
+quirc: quirc.c ttyraw.o ttyraw.h ttyesc.o ttyesc.h irc.o irc.h bits.o bits.h colour.o colour.h buffer.o buffer.h names.o names.h numeric.h
-./gitversion
- $(CC) $(CFLAGS) -o quirc quirc.c ttyraw.o ttyesc.o irc.o bits.o colour.o buffer.o
+ $(CC) $(CFLAGS) -o quirc quirc.c ttyraw.o ttyesc.o irc.o bits.o colour.o buffer.o names.o
# TODO use funky make cleverness for these rules as they're all basically the same
@@ -34,6 +34,9 @@ colour.o: colour.c colour.h
buffer.o: buffer.c buffer.h
$(CC) $(CFLAGS) -o buffer.o -c buffer.c
+names.o: names.c names.h
+ $(CC) $(CFLAGS) -o names.o -c names.c
+
dist: all
-mkdir quirc_$(VERSION)
for p in $$(ls); do cp $$p quirc_$(VERSION)/$$p; done;
View
1 buffer.c
@@ -169,6 +169,7 @@ int buf_print(int buf, colour lc, char *lt, bool nl)
void in_update(char *inp)
{
+ resetcol();
printf(LOCATE, height-1, 1);
// tab strip
int mbw = (width-1)/nbufs;
View
8 buffer.h
@@ -16,13 +16,7 @@
#include "ttyesc.h"
#include "colour.h"
#include "bits.h"
-
-typedef struct _name
-{
- char *data;
- struct _name *next, *prev;
-}
-name;
+#include "names.h"
typedef enum
{
View
61 names.c
@@ -0,0 +1,61 @@
+/*
+ quIRC - simple terminal-based IRC client
+ Copyright (C) 2010 Edward Cree
+
+ See quirc.c for license information
+ names: handling for name lists
+*/
+
+#include "names.h"
+
+name * n_add(name ** list, char *data)
+{
+ n_cull(list, data);
+ name *new=(name *)malloc(sizeof(name));
+ new->data=strdup(data);
+ new->prev=NULL;
+ new->next=*list;
+ if(*list)
+ *list->prev=new;
+ *list=new;
+ return(new);
+}
+
+int n_cull(name ** list, char *data)
+{
+ int rv=0;
+ name *curr=*list;
+ while(curr)
+ {
+ name *next=curr->next;
+ if(strcmp(curr->data, data)==0)
+ {
+ if(curr->prev)
+ {
+ curr->prev->next=curr->next;
+ }
+ else
+ {
+ *list=curr->next;
+ }
+ if(curr->next)
+ curr->next->prev=curr->prev;
+ free(curr->data);
+ free(curr);
+ rv++;
+ }
+ curr=next;
+ }
+ return(rv);
+}
+
+void n_free(name * list)
+{
+ if(list)
+ {
+ n_free(list->next);
+ if(list->data)
+ free(list->data);
+ free(list);
+ }
+}
View
21 names.h
@@ -0,0 +1,21 @@
+/*
+ quIRC - simple terminal-based IRC client
+ Copyright (C) 2010 Edward Cree
+
+ See quirc.c for license information
+ names: handling for name lists
+*/
+
+#include <stdlib.h>
+#include <string.h>
+
+typedef struct _name
+{
+ char *data; // is a unique pointer (eg. from strdup()), and must be free()d
+ struct _name *next, *prev;
+}
+name;
+
+name * n_add(name ** list, char *data); // returns pointer to the added name (which should also be the new value of *list). Calls n_cull() first
+int n_cull(name ** list, char *data); // returns number of instances culled
+void n_free(name * list);
View
140 quirc.c
@@ -535,7 +535,7 @@ int main(int argc, char *argv[])
{
if(fd==bufs[b].handle)
{
- char *packet; // TODO detect appropriate destination buffer for packet
+ char *packet;
int e;
if((e=irc_rx(fd, &packet))!=0)
{
@@ -578,34 +578,7 @@ int main(int argc, char *argv[])
char *nn;
while((nn=strtok(NULL, ":@ ")))
{
- name *curr=bufs[b2].nlist; // cull existing copies of this nick
- while(curr)
- {
- name *next=curr->next;
- if(strcmp(curr->data, nn)==0)
- {
- if(curr->prev)
- {
- curr->prev->next=curr->next;
- }
- else
- {
- bufs[b2].nlist=curr->next;
- }
- if(curr->next)
- curr->next->prev=curr->prev;
- free(curr->data);
- free(curr);
- }
- curr=next;
- }
- name *new=(name *)malloc(sizeof(name));
- new->data=strdup(nn);
- new->prev=NULL;
- new->next=bufs[b2].nlist;
- if(bufs[b2].nlist)
- bufs[b2].nlist->prev=new;
- bufs[b2].nlist=new;
+ n_add(&bufs[b2].nlist, nn);
}
}
}
@@ -866,34 +839,7 @@ int main(int argc, char *argv[])
char dstr[16+strlen(src)+strlen(dest+1)];
sprintf(dstr, "=%s= has joined %s", src, dest+1);
buf_print(b2, c_join[1], dstr, true);
- name *curr=bufs[b2].nlist; // cull existing copies of this nick
- while(curr)
- {
- name *next=curr->next;
- if(strcmp(curr->data, src)==0)
- {
- if(curr->prev)
- {
- curr->prev->next=curr->next;
- }
- else
- {
- bufs[b2].nlist=curr->next;
- }
- if(curr->next)
- curr->next->prev=curr->prev;
- free(curr->data);
- free(curr);
- }
- curr=next;
- }
- name *new=(name *)malloc(sizeof(name));
- new->data=strdup(src);
- new->prev=NULL;
- new->next=bufs[b2].nlist;
- if(bufs[b2].nlist)
- bufs[b2].nlist->prev=new;
- bufs[b2].nlist=new;
+ n_add(&bufs[b2].nlist, src);
}
}
if(!match)
@@ -903,7 +849,6 @@ int main(int argc, char *argv[])
buf_print(b, c_err, dstr, true);
}
}
- resetcol();
}
else if(strcmp(cmd, "PART")==0)
{
@@ -943,27 +888,7 @@ int main(int argc, char *argv[])
char dstr[16+strlen(src)+strlen(dest)];
sprintf(dstr, "=%s= has left %s", src, dest);
buf_print(b2, c_part[1], dstr, true);
- name *curr=bufs[b2].nlist;
- while(curr)
- {
- name *next=curr->next;
- if(strcmp(curr->data, src)==0)
- {
- if(curr->prev)
- {
- curr->prev->next=curr->next;
- }
- else
- {
- bufs[b2].nlist=curr->next;
- }
- if(curr->next)
- curr->next->prev=curr->prev;
- free(curr->data);
- free(curr);
- }
- curr=next;
- }
+ n_cull(&bufs[b2].nlist, src);
}
}
if(!match)
@@ -973,7 +898,6 @@ int main(int argc, char *argv[])
buf_print(b, c_err, dstr, true);
}
}
- resetcol();
}
else if(strcmp(cmd, "QUIT")==0)
{
@@ -1003,34 +927,15 @@ int main(int argc, char *argv[])
{
if((bufs[b2].server==b) && (bufs[b2].type==CHANNEL))
{
- name *curr=bufs[b2].nlist;
- while(curr)
+ if(n_cull(&bufs[b2].nlist, src))
{
- name *next=curr->next;
- if(strcmp(curr->data, src)==0)
- {
- char dstr[24+strlen(src)+strlen(bufs[b].bname)+strlen(dest+1)];
- sprintf(dstr, "=%s= has left %s (%s)", src, bufs[b].bname, dest+1);
- buf_print(b2, c_quit[1], dstr, true);
- if(curr->prev)
- {
- curr->prev->next=curr->next;
- }
- else
- {
- bufs[b2].nlist=curr->next;
- }
- if(curr->next)
- curr->next->prev=curr->prev;
- free(curr->data);
- free(curr);
- }
- curr=next;
+ char dstr[24+strlen(src)+strlen(bufs[b].bname)+strlen(dest+1)];
+ sprintf(dstr, "=%s= has left %s (%s)", src, bufs[b].bname, dest+1);
+ buf_print(b2, c_quit[1], dstr, true);
}
}
}
}
- resetcol();
}
else if(strcmp(cmd, "NICK")==0)
{
@@ -1050,16 +955,8 @@ int main(int argc, char *argv[])
if(bufs[b2].server==b)
{
buf_print(b2, c_nick[1], dstr, true);
- name *curr=bufs[b2].nlist;
- while(curr)
- {
- if(strcmp(curr->data, src)==0)
- {
- free(curr->data);
- curr->data=strdup(dest+1);
- }
- curr=curr->next;
- }
+ n_cull(&bufs[b2].nlist, src);
+ n_add(&bufs[b2].nlist, dest+1);
}
}
}
@@ -1072,18 +969,12 @@ int main(int argc, char *argv[])
if((bufs[b2].server==b) && (bufs[b2].type==CHANNEL))
{
match=true;
- name *curr=bufs[b2].nlist;
- while(curr)
+ if(n_cull(&bufs[b2].nlist, src))
{
- if(strcmp(curr->data, src)==0)
- {
- free(curr->data);
- curr->data=strdup(dest+1);
- char dstr[30+strlen(src)+strlen(dest+1)];
- sprintf(dstr, "=%s= is now known as %s", src, dest+1);
- buf_print(b2, c_nick[1], dstr, true);
- }
- curr=curr->next;
+ n_add(&bufs[b2].nlist, dest+1);
+ char dstr[30+strlen(src)+strlen(dest+1)];
+ sprintf(dstr, "=%s= is now known as %s", src, dest+1);
+ buf_print(b2, c_nick[1], dstr, true);
}
}
}
@@ -1094,7 +985,6 @@ int main(int argc, char *argv[])
buf_print(b, c_err, dstr, true);
}
}
- resetcol();
}
else
{

0 comments on commit a886cb2

Please sign in to comment.
Something went wrong with that request. Please try again.