This is dstring.c in view mode; [Download] [Up]
/* dstring v1.0.0 Dynamic string library
* Copyright (c) 1994 Bill Bereza
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*
* To reach the author
*
* email:
* berezaw@river.it.gvsu.edu ac368@leo.nmc.edu
*
* $Log: dstring.c,v $
* Revision 1.15 94/12/12 01:29:55 berezaw
* caught some more (char)s to change to (DCHAR)
*
* Revision 1.14 94/12/08 10:41:31 berezaw
* daddchar() now returns DCHAR with 0 on error
*
* Revision 1.13 94/12/03 00:09:31 berezaw
* changed all uses of (char) to (DCHAR)
*
* Revision 1.12 94/12/02 11:50:20 berezaw
* using DCHAR definition
*
* Revision 1.11 94/12/01 10:52:11 berezaw
* added getdnewchars() function to buffer mallocs
* of struct _dchar, then #ifdef'd it out,
* because it doesn't seem to be any faster
*
* Revision 1.10 94/11/30 15:43:25 berezaw
* added buffering to allocation of new _dchars
*
* Revision 1.9 94/11/28 12:04:03 berezaw
* *** empty log message ***
*
*
* @(#)dstring.c 1.8 (Bill Bereza) 11/15/94
* $Header: /Users/berezaw/src/dynstr/RCS/dstring.c,v 1.15 94/12/12 01:29:55 berezaw Exp $
*/
#include "dstring.h"
#define DOLDMALL
DSTRING *initdstr(void)
{
DSTRING *newstr;
newstr=(DSTRING *)malloc(sizeof(DSTRING));
if(newstr != NULL) {
newstr->_first=NULL;
newstr->_last=NULL;
newstr->_length=0;
}
return newstr;
}
void freedchars(DSTRING *oldstr)
{
struct _dchar *tchar, *pchar;
if(oldstr==NULL)
return;
pchar=oldstr->_first;
while(pchar!=NULL) {
tchar=pchar;
pchar=pchar->_next;
free(tchar);
}
oldstr->_first=oldstr->_last=NULL;
oldstr->_length=0;
}
void freedstr(DSTRING *oldstr)
{
if(oldstr==NULL)
return;
freedchars(oldstr);
free(oldstr);
}
#ifndef DOLDMALL
#define DBUFLEN ((size_t)10)
struct _dchar *getdnewchar(void)
{
static struct _dchar *dnewchar[DBUFLEN];
static size_t currbuff=0;
register size_t loop;
if(!currbuff) {
for(loop=0;loop<DBUFLEN;loop++)
dnewchar[loop]=(struct _dchar *)malloc(sizeof(struct _dchar));
currbuff=DBUFLEN;
}
return dnewchar[--currbuff];
}
#undef DBUFLEN
#endif
DCHAR daddchar(DSTRING *dstr, DCHAR addchar)
{
struct _dchar *newchar;
if(dstr==NULL)
return 0;
#ifdef DOLDMALL
newchar=(struct _dchar *)malloc(sizeof(struct _dchar));
#else
newchar=getdnewchar();
#endif
if(newchar == NULL) {
return 0;
}
else {
newchar->_character=addchar;
newchar->_next=NULL;
if(dstr->_last==NULL) {
dstr->_last=newchar;
dstr->_first=newchar;
}
else {
dstr->_last->_next=newchar;
dstr->_last=newchar;
}
(dstr->_length)++;
return addchar;
}
}
DCHAR *arrdstr(DSTRING *ostr)
{
struct _dchar *index;
register size_t i=0;
DCHAR *charray;
if(ostr==NULL)
return NULL;
if((charray=(DCHAR *)(calloc(((dstrlen(ostr))+1), sizeof(DCHAR)))) != NULL) {
index=ostr->_first;
while(index != NULL) {
charray[i++]=index->_character;
index=index->_next;
}
charray[i]='\0';
}
return charray;
}
DSTRING *dstrarr(DCHAR *charray)
{
DSTRING *nstr;
if(charray==NULL)
return NULL;
if((nstr=initdstr())!=NULL)
for(;*charray;daddchar(nstr, *charray++));
return nstr;
}
DSTRING *dstrarrcat(DSTRING *ostr,DCHAR *charray)
{
if(charray==NULL)
return NULL;
if(ostr!=NULL)
for(;*charray;daddchar(ostr, *charray++));
return ostr;
}
DSTRING *dstrcat(DSTRING *ostr, DSTRING *addstr)
{
struct _dchar *pchar;
if((ostr!=NULL) && (addstr!=NULL)) {
pchar=addstr->_first;
while(pchar != NULL) {
daddchar(ostr, pchar->_character);
pchar=pchar->_next;
}
}
return ostr;
}
DSTRING *dstrcpy(DSTRING *addstr)
{
struct _dchar *pchar;
DSTRING *ostr=NULL;
if(addstr==NULL)
return NULL;
ostr=initdstr();
if(ostr!=NULL) {
pchar=addstr->_first;
while(pchar != NULL) {
daddchar(ostr, pchar->_character);
pchar=pchar->_next;
}
}
return ostr;
}
DCHAR *arrdstrfromhead(DSTRING *ostr, size_t n)
{
DCHAR *charray;
struct _dchar *index;
register size_t loop;
if(ostr==NULL)
return NULL;
if(n>=dstrlen(ostr))
return arrdstr(ostr);
else {
index=ostr->_first;
if((charray=(DCHAR *)(calloc((n+1), sizeof(DCHAR)))) != NULL) {
for(loop=0;loop<n;loop++) {
charray[loop]=index->_character;
index=index->_next;
}
charray[loop]='\0';
}
return charray;
}
}
DSTRING *dstrfromhead(DSTRING *ostr, size_t n)
{
DSTRING *nstr;
struct _dchar *index;
register size_t loop;
if(ostr==NULL)
return NULL;
if(n>=dstrlen(ostr))
return dstrcpy(ostr);
else {
if((nstr=initdstr()) != NULL) {
index=ostr->_first;
for(loop=n;loop;loop--) {
daddchar(nstr, index->_character);
index=index->_next;
}
}
return nstr;
}
}
DCHAR *arrdstrfromtail(DSTRING *ostr, size_t n)
{
DCHAR *charray;
struct _dchar *index;
register size_t loop;
if(ostr==NULL)
return NULL;
if(n>=dstrlen(ostr))
return arrdstr(ostr);
else {
index=ostr->_first;
for(loop=1;loop<=(dstrlen(ostr)-n);loop++)
index=index->_next;
loop=0;
if((charray=(DCHAR *)(calloc((n+1), sizeof(DCHAR)))) != NULL) {
while(index != NULL) {
charray[loop++]=index->_character;
index=index->_next;
}
charray[loop]='\0';
}
return charray;
}
}
DSTRING *dstrfromtail(DSTRING *ostr, size_t n)
{
DSTRING *nstr;
struct _dchar *index;
register size_t loop;
if(ostr==NULL)
return NULL;
if(n>=dstrlen(ostr))
return dstrcpy(ostr);
else {
index=ostr->_first;
for(loop=1;loop<=(dstrlen(ostr)-n);loop++)
index=index->_next;
if((nstr=initdstr()) != NULL)
for(loop=n;loop;loop--) {
daddchar(nstr, index->_character);
index=index->_next;
}
return nstr;
}
}
DCHAR *arrdstrrmfromhead(DSTRING *ostr, size_t n)
{
DCHAR *charray;
struct _dchar *index;
register size_t loop;
if(ostr==NULL)
return NULL;
if(n>=dstrlen(ostr)) {
charray=arrdstr(ostr);
freedchars(ostr);
return charray;
}
else {
index=ostr->_first;
if((charray=(DCHAR *)(calloc((n+1), sizeof(DCHAR)))) != NULL) {
for(loop=0;loop<n;loop++) {
charray[loop]=ostr->_first->_character;
index=ostr->_first;
ostr->_first=ostr->_first->_next;
free(index);
ostr->_length--;
}
charray[loop]='\0';
}
return charray;
}
}
DCHAR *arrdstrrmfromtail(DSTRING *ostr, size_t n)
{
DCHAR *charray;
struct _dchar *index, *pchar, *tchar;
register size_t loop;
if(ostr==NULL)
return NULL;
if(n>=dstrlen(ostr)) {
charray=arrdstr(ostr);
freedchars(ostr);
return charray;
}
else {
index=ostr->_first;
pchar=index;
for(loop=1;loop<=(dstrlen(ostr)-n);loop++) {
pchar=index;
index=index->_next;
}
ostr->_last=pchar;
loop=0;
if((charray=(DCHAR *)(calloc((n+1), sizeof(DCHAR)))) != NULL) {
while(index != NULL) {
charray[loop++]=index->_character;
tchar=index;
index=index->_next;
pchar->_next=index;
free(tchar); ostr->_length--;
}
charray[loop]='\0';
}
return charray;
}
}
DSTRING *dstrrmfromhead(DSTRING *ostr, size_t n)
{
DSTRING *nstr;
struct _dchar *index;
register size_t loop;
if(ostr==NULL)
return NULL;
if(n>=dstrlen(ostr)) {
if((nstr=initdstr()) != NULL) {
nstr->_first=ostr->_first;
nstr->_last=ostr->_last;
nstr->_length=ostr->_length;
ostr->_first=NULL;
ostr->_last=NULL;
ostr->_length=0;
}
return nstr;
}
else {
if((nstr=initdstr()) != NULL) {
index=ostr->_first;
nstr->_length=n;
nstr->_first=index;
for(loop=0;loop<(n-1);loop++)
index=index->_next;
nstr->_last=index;
index=index->_next;
nstr->_last->_next=NULL;
ostr->_first=index;
ostr->_length-=n;
}
return nstr;
}
}
DSTRING *dstrrmfromtail(DSTRING *ostr, size_t n)
{
DSTRING *nstr;
struct _dchar *index, *pchar;
register size_t loop;
if(ostr==NULL)
return NULL;
if(n>=dstrlen(ostr)) {
if((nstr=initdstr()) != NULL) {
nstr->_first=ostr->_first;
nstr->_last=ostr->_last;
nstr->_length=ostr->_length;
ostr->_first=NULL;
ostr->_last=NULL;
ostr->_length=0;
}
return nstr;
}
else {
index=ostr->_first;
pchar=index;
for(loop=1;loop<=(dstrlen(ostr)-n);loop++) {
pchar=index;
index=index->_next;
}
if((nstr=initdstr()) != NULL) {
nstr->_first=index;
nstr->_last=ostr->_last;
nstr->_length=n;
ostr->_last=pchar;
ostr->_last->_next=NULL;
ostr->_length-=n;
}
return nstr;
}
}
size_t dstrlen(DSTRING *oldstring)
{
if(oldstring==NULL)
return 0;
else
return oldstring->_length;
}
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.