Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

102 lines (93 sloc) 1.501 kb
/*
quIRC - simple terminal-based IRC client
Copyright (C) 2010-13 Edward Cree
See quirc.c for license information
strbuf: auto-reallocating string buffers
*/
#include <stdlib.h>
#include <string.h> // for memmove()
#include "strbuf.h"
void append_char(char **buf, size_t *l, size_t *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=realloc(*buf, *l);
}
if(nbuf)
{
*buf=nbuf;
(*buf)[*i]=0;
}
else
{
free(*buf);
init_char(buf, l, i);
}
}
void prepend_char(char **buf, size_t *l, size_t *i, char c)
{
append_char(buf, l, i, 0);
memmove((*buf)+1, *buf, *i);
(*buf)[0]=c;
}
void append_str(char **buf, size_t *l, size_t *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, size_t *l, size_t *i)
{
*l=80;
*buf=malloc(*l);
(*buf)[0]=0;
*i=0;
}
char * fgetl(FILE *fp)
{
char * lout;
size_t 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;
size_t 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);
}
Jump to Line
Something went wrong with that request. Please try again.