Permalink
Browse files

Refactored string buffers to new strbuf.h, shared with the gen* progs.

Fixes: Issue 9
  • Loading branch information...
1 parent 3b69162 commit ebc74a9ddbbc00dd27f456e7fc15afffdbf67e10 ec429 committed Jul 9, 2012
Showing with 121 additions and 217 deletions.
  1. +7 −5 Makefile
  2. +0 −83 bits.c
  3. +1 −5 bits.h
  4. +2 −61 genconfig.c
  5. +1 −63 genkeymap.c
  6. +93 −0 strbuf.c
  7. +17 −0 strbuf.h
View
@@ -8,8 +8,8 @@ VERSION := `git describe --tags`
PREFIX := /usr/local
# -lanl is for ASYNCH_NL
OPTLIBS := -lanl
-LIBS := -lncurses ttyraw.o ttyesc.o irc.o bits.o colour.o buffer.o names.o config.o input.o logging.o types.o $(OPTLIBS)
-INCLUDE := ttyraw.h ttyesc.h irc.h bits.h colour.h buffer.h names.h config.h input.h logging.h types.h quirc.h version.h osconf.h
+LIBS := -lncurses ttyraw.o ttyesc.o irc.o bits.o strbuf.o colour.o buffer.o names.o config.o input.o logging.o types.o $(OPTLIBS)
+INCLUDE := ttyraw.h ttyesc.h irc.h bits.h strbuf.h colour.h buffer.h names.h config.h input.h logging.h types.h quirc.h version.h osconf.h
all: quirc doc
@@ -63,7 +63,7 @@ irc.h: config.h
bits.o: bits.c bits.h ttyesc.h colour.h
-bits.h: config.h
+bits.h: config.h strbuf.h
touch bits.h
colour.o: colour.c colour.h c_init.c ttyesc.h
@@ -84,7 +84,8 @@ config.h: config_globals.h version.h keymod.h
config_%: config.cdl genconfig
./genconfig $@ < config.cdl > $@ || (rm $@ && false)
-genconfig: genconfig.c
+genconfig: genconfig.c strbuf.h strbuf.o
+ $(CC) $(CFLAGS) $(CPPFLAGS) $< $(LDFLAGS) strbuf.o -o $@
input.h: keymod.h
touch input.h
@@ -103,7 +104,8 @@ script.o: script.c script.h bits.h buffer.h
c_init.c: colour.d c_init.awk
$(AWK) -f c_init.awk colour.d > c_init.c
-genkeymap: genkeymap.c
+genkeymap: genkeymap.c strbuf.h strbuf.o
+ $(CC) $(CFLAGS) $(CPPFLAGS) $< $(LDFLAGS) strbuf.o -o $@
keymod.h: keys genkeymap
./genkeymap h < keys > $@ || (rm $@ && false)
View
83 bits.c
@@ -8,44 +8,6 @@
#include "bits.h"
-char * fgetl(FILE *fp)
-{
- char * lout;
- int l,i;
- init_char(&lout, &l, &i);
- signed int c;
- while(!feof(fp))
- {
- c=fgetc(fp);
- if((c==EOF)||(c=='\n'))
- break;
- if(c!=0)
- {
- append_char(&lout, &l, &i, c);
- }
- }
- return(lout);
-}
-
-char *slurp(FILE *fp)
-{
- char *fout;
- int l,i;
- init_char(&fout, &l, &i);
- signed int c;
- while(!feof(fp))
- {
- c=fgetc(fp);
- if(c==EOF)
- break;
- if(c!=0)
- {
- append_char(&fout, &l, &i, c);
- }
- }
- return(fout);
-}
-
int wordline(const char *msg, unsigned int x, char **out, int *l, int *i, colour lc)
{
if(!msg) return(x);
@@ -175,51 +137,6 @@ int wordline(const char *msg, unsigned int x, char **out, int *l, int *i, colour
return(x);
}
-void append_char(char **buf, int *l, int *i, char c)
-{
- if(*buf)
- {
- (*buf)[(*i)++]=c;
- }
- else
- {
- init_char(buf, l, i);
- append_char(buf, l, i, c);
- }
- char *nbuf=*buf;
- if((*i)>=(*l))
- {
- *l=*i*2;
- nbuf=(char *)realloc(*buf, *l);
- }
- if(nbuf)
- {
- *buf=nbuf;
- (*buf)[*i]=0;
- }
- else
- {
- free(*buf);
- init_char(buf, l, i);
- }
-}
-
-void append_str(char **buf, int *l, int *i, const char *str)
-{
- while(str && *str) // not the most tremendously efficient implementation, but conceptually simple at least
- {
- append_char(buf, l, i, *str++);
- }
-}
-
-void init_char(char **buf, int *l, int *i)
-{
- *l=80;
- *buf=(char *)malloc(*l);
- (*buf)[0]=0;
- *i=0;
-}
-
void crush(char **buf, unsigned int len)
{
if(strlen(*buf)>len)
View
6 bits.h
@@ -11,6 +11,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include "strbuf.h"
#include "ttyesc.h"
#include "colour.h"
#include "config.h"
@@ -23,12 +24,7 @@
char *strndup(const char *s, size_t size);
#endif
-char *fgetl(FILE *); // gets a line of string data; returns a malloc-like pointer
-char *slurp(FILE *); // gets an entire file of string data; returns a malloc-like pointer
int wordline(const char *msg, unsigned int x, char **out, int *l, int *i, colour lc); // prepares a string for printing, breaking lines in between words; returns new x
-void init_char(char **buf, int *l, int *i); // initialises a string buffer in heap. *buf becomes a malloc-like pointer
-void append_char(char **buf, int *l, int *i, char c); // adds a character to a string buffer in heap (and realloc()s if needed)
-void append_str(char **buf, int *l, int *i, const char *str); // adds a string to a string buffer in heap (and realloc()s if needed)
void crush(char **buf, unsigned int len);
void scrush(char **buf, unsigned int len);
char *mktag(char *fmt, char *from);
View
@@ -13,6 +13,7 @@
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
+#include "strbuf.h"
typedef struct
{
@@ -28,10 +29,6 @@ typedef struct
}
ent;
-char * fgetl(FILE *); // gets a line of string data; returns a malloc-like pointer
-void init_char(char **buf, int *l, int *i); // initialises a string buffer in heap. *buf becomes a malloc-like pointer
-void append_char(char **buf, int *l, int *i, char c); // adds a character to a string buffer in heap (and realloc()s if needed)
-
int main(int argc, char **argv)
{
if(argc!=2)
@@ -610,61 +607,5 @@ Generated by <small>genconfig</small></h5>\n\
printf("</table>\n</div><!--#table-->\n</body>\n</html>\n");
break;
}
-
-}
-
-char * fgetl(FILE *fp)
-{
- char * lout;
- int l,i;
- init_char(&lout, &l, &i);
- signed int c;
- while(!feof(fp))
- {
- c=fgetc(fp);
- if((c==EOF)||(c=='\n'))
- break;
- if(c!=0)
- {
- append_char(&lout, &l, &i, c);
- }
- }
- return(lout);
-}
-
-void append_char(char **buf, int *l, int *i, char c)
-{
- if(*buf)
- {
- (*buf)[(*i)++]=c;
- }
- else
- {
- init_char(buf, l, i);
- append_char(buf, l, i, c);
- }
- char *nbuf=*buf;
- if((*i)>=(*l))
- {
- *l=*i*2;
- nbuf=(char *)realloc(*buf, *l);
- }
- if(nbuf)
- {
- *buf=nbuf;
- (*buf)[*i]=0;
- }
- else
- {
- free(*buf);
- init_char(buf, l, i);
- }
-}
-
-void init_char(char **buf, int *l, int *i)
-{
- *l=80;
- *buf=(char *)malloc(*l);
- (*buf)[0]=0;
- *i=0;
+ return(0);
}
View
@@ -2,6 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
+#include "strbuf.h"
typedef struct
{
@@ -10,10 +11,6 @@ typedef struct
}
keymod;
-char * fgetl(FILE *); // gets a line of string data; returns a malloc-like pointer
-void init_char(char **buf, int *l, int *i); // initialises a string buffer in heap. *buf becomes a malloc-like pointer
-void append_char(char **buf, int *l, int *i, char c); // adds a character to a string buffer in heap (and realloc()s if needed)
-
int main(int argc, char **argv)
{
if(argc!=2)
@@ -123,62 +120,3 @@ int main(int argc, char **argv)
}
return(0);
}
-
-char * fgetl(FILE *fp)
-{
- char * lout;
- int l,i;
- init_char(&lout, &l, &i);
- signed int c;
- while(!feof(fp))
- {
- c=fgetc(fp);
- if((c==EOF)||(c=='\n'))
- break;
- if(c!=0)
- {
- append_char(&lout, &l, &i, c);
- }
- }
- return(lout);
-}
-
-void append_char(char **buf, int *l, int *i, char c)
-{
- if(!((c==0)||(c==EOF)))
- {
- if(*buf)
- {
- (*buf)[(*i)++]=c;
- }
- else
- {
- init_char(buf, l, i);
- append_char(buf, l, i, c);
- }
- char *nbuf=*buf;
- if((*i)>=(*l))
- {
- *l=*i*2;
- nbuf=(char *)realloc(*buf, *l);
- }
- if(nbuf)
- {
- *buf=nbuf;
- (*buf)[*i]=0;
- }
- else
- {
- free(*buf);
- init_char(buf, l, i);
- }
- }
-}
-
-void init_char(char **buf, int *l, int *i)
-{
- *l=80;
- *buf=(char *)malloc(*l);
- (*buf)[0]=0;
- *i=0;
-}
Oops, something went wrong.

1 comment on commit ebc74a9

rofl0r commented on ebc74a9 Jul 9, 2012

code duplication - the root of all evil...

Please sign in to comment.