"Fossies" - the Fresh Open Source Software Archive

Member "bas-2.6/str.c" (2 Jul 2019, 6856 Bytes) of package /linux/privat/bas-2.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "str.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.5_vs_2.6.

    1 /* Dyanamic strings. */
    2 /* #includes */ /*{{{C}}}*//*{{{*/
    3 #include "config.h"
    4 
    5 #include <assert.h>
    6 #include <ctype.h>
    7 #include <stdarg.h>
    8 #include <stddef.h>
    9 #include <stdio.h>
   10 #include <stdlib.h>
   11 #include <string.h>
   12 
   13 #include "str.h"
   14 
   15 #ifdef USE_DMALLOC
   16 #include "dmalloc.h"
   17 #endif
   18 /*}}}*/
   19 
   20 int cistrcmp(const char *s, const char *r) /*{{{*/
   21 {
   22   assert(s!=(char*)0);
   23   assert(r!=(char*)0);
   24   while (*s && tolower(*s)==tolower(*r)) { ++s; ++r; };
   25   return ((tolower(*s)-tolower(*r)));
   26 }
   27 /*}}}*/
   28 
   29 struct String *String_new(struct String *this) /*{{{*/
   30 {
   31   assert(this!=(struct String*)0);
   32   this->length=0;
   33   this->character=(char*)0;
   34   this->field=(struct StringField*)0;
   35   return this;
   36 }
   37 /*}}}*/
   38 void String_destroy(struct String *this) /*{{{*/
   39 {
   40   assert(this!=(struct String*)0);
   41   if (this->field) String_leaveField(this);
   42   if (this->length) free(this->character);
   43 }
   44 /*}}}*/
   45 int String_joinField(struct String *this, struct StringField *field, char *character, size_t length) /*{{{*/
   46 {
   47   struct String **n;
   48 
   49   assert(this!=(struct String*)0);
   50   if (this->field) String_leaveField(this);
   51   this->field=field;
   52   if ((n=(struct String**)realloc(field->refStrings,sizeof(struct String*)*(field->refCount+1)))==(struct String**)0) return -1;
   53   field->refStrings=n;
   54   field->refStrings[field->refCount]=this;
   55   ++field->refCount;
   56   if (this->length) free(this->character);
   57   this->character=character;
   58   this->length=length;
   59   return 0;
   60 }
   61 /*}}}*/
   62 void String_leaveField(struct String *this) /*{{{*/
   63 {
   64   struct StringField *field;
   65   int i;
   66   struct String **ref;
   67 
   68   assert(this!=(struct String*)0);
   69   field=this->field;
   70   assert(field!=(struct StringField*)0);
   71   for (i=0,ref=field->refStrings; i<field->refCount; ++i,++ref)
   72   {
   73     if (*ref==this)
   74     {
   75       int further=--field->refCount-i;
   76 
   77       if (further) memmove(ref,ref+1,further*sizeof(struct String**));
   78       this->character=(char*)0;
   79       this->length=0;
   80       this->field=(struct StringField*)0;
   81       return;
   82     }
   83   }
   84   assert(0);
   85 }
   86 /*}}}*/
   87 struct String *String_clone(struct String *this, const struct String *original) /*{{{*/
   88 {
   89   assert(this!=(struct String*)0);
   90   String_new(this);
   91   String_appendString(this,original);
   92   return this;
   93 }
   94 /*}}}*/
   95 int String_size(struct String *this, size_t length) /*{{{*/
   96 {
   97   char *n;
   98 
   99   assert(this!=(struct String*)0);
  100   if (this->field) String_leaveField(this);
  101   if (length)
  102   {
  103     if (length>this->length)
  104     {
  105       if ((n=realloc(this->character,length+1))==(char*)0) return -1;
  106       this->character=n;
  107     }
  108     this->character[length]='\0';
  109   }
  110   else
  111   {
  112     if (this->length) free(this->character);
  113     this->character=(char*)0;
  114   }
  115   this->length=length;
  116   return 0;
  117 }
  118 /*}}}*/
  119 int String_appendString(struct String *this, const struct String *app) /*{{{*/
  120 {
  121   size_t oldlength=this->length;
  122 
  123   if (this->field) String_leaveField(this);
  124   if (app->length==0) return 0;
  125   if (String_size(this,this->length+app->length)==-1) return -1;
  126   memcpy(this->character+oldlength,app->character,app->length);
  127   return 0;
  128 }
  129 /*}}}*/
  130 int String_appendChar(struct String *this, char ch) /*{{{*/
  131 {
  132   size_t oldlength=this->length;
  133 
  134   if (this->field) String_leaveField(this);
  135   if (String_size(this,this->length+1)==-1) return -1;
  136   this->character[oldlength]=ch;
  137   return 0;
  138 }
  139 /*}}}*/
  140 int String_appendChars(struct String *this, const char *ch) /*{{{*/
  141 {
  142   size_t oldlength=this->length;
  143   size_t chlen=strlen(ch);
  144 
  145   if (this->field) String_leaveField(this);
  146   if (String_size(this,this->length+chlen)==-1) return -1;
  147   memcpy(this->character+oldlength,ch,chlen);
  148   return 0;
  149 }
  150 /*}}}*/
  151 int String_appendPrintf(struct String *this, const char *fmt, ...) /*{{{*/
  152 {
  153   char buf[1024];
  154   size_t l,j;
  155   va_list ap;
  156 
  157   if (this->field) String_leaveField(this);
  158   va_start(ap, fmt);
  159   l=vsprintf(buf,fmt,ap);
  160   va_end(ap);
  161   j=this->length;
  162   if (String_size(this,j+l)==-1) return -1;
  163   memcpy(this->character+j,buf,l);
  164   return 0;
  165 }
  166 /*}}}*/
  167 int String_insertChar(struct String *this, size_t where, char ch) /*{{{*/
  168 {
  169   size_t oldlength=this->length;
  170 
  171   if (this->field) String_leaveField(this);
  172   assert(where<=oldlength);
  173   if (String_size(this,this->length+1)==-1) return -1;
  174   memmove(this->character+where+1,this->character+where,oldlength-where);
  175   this->character[where]=ch;
  176   return 0;
  177 }
  178 /*}}}*/
  179 int String_delete(struct String *this, size_t where, size_t len) /*{{{*/
  180 {
  181   size_t oldlength=this->length;
  182 
  183   if (this->field) String_leaveField(this);
  184   assert(where<oldlength);
  185   assert(len>0);
  186   if ((where+len)<oldlength) memmove(this->character+where,this->character+where+len,oldlength-where-len);
  187   this->character[this->length-=len]='\0';
  188   return 0;
  189 }
  190 /*}}}*/
  191 void String_ucase(struct String *this) /*{{{*/
  192 {
  193   size_t i;
  194 
  195   for (i=0; i<this->length; ++i) this->character[i]=toupper(this->character[i]);
  196 }
  197 /*}}}*/
  198 void String_lcase(struct String *this) /*{{{*/
  199 {
  200   size_t i;
  201 
  202   for (i=0; i<this->length; ++i) this->character[i]=tolower(this->character[i]);
  203 }
  204 /*}}}*/
  205 int String_cmp(const struct String *this, const struct String *s) /*{{{*/
  206 {
  207   size_t pos;
  208   int res;
  209   const char *thisch,*sch;
  210 
  211   for (pos=0,thisch=this->character,sch=s->character; pos<this->length && pos<s->length; ++pos,++thisch,++sch)
  212   {
  213     if ((res=(*thisch-*sch))) return res;
  214   }
  215   return (this->length-s->length);
  216 }
  217 /*}}}*/
  218 int String_quote(struct String *this) /*{{{*/
  219 {
  220   size_t i;
  221 
  222   for (i=0; i<this->length; ++i)
  223   {
  224     int ch;
  225 
  226     ch=this->character[i];
  227     if (!isalpha(ch) && !isdigit(ch) && ch!='*' && ch!='?' && ch!='[' && ch!=']')
  228     {
  229       if (String_insertChar(this, i, '\\')==-1) return -1;
  230       ++i;
  231     }
  232   }
  233   return 0;
  234 }
  235 /*}}}*/
  236 void String_lset(struct String *this, const struct String *s) /*{{{*/
  237 {
  238   size_t copy;
  239 
  240   copy=(this->length<s->length ? this->length : s->length);
  241   if (copy) memcpy(this->character,s->character,copy);
  242   if (copy<this->length) memset(this->character+copy,' ',this->length-copy);
  243 }
  244 /*}}}*/
  245 void String_rset(struct String *this, const struct String *s) /*{{{*/
  246 {
  247   size_t copy;
  248 
  249   copy=(this->length<s->length ? this->length : s->length);
  250   if (copy) memcpy(this->character+this->length-copy,s->character,copy);
  251   if (copy<this->length) memset(this->character,' ',this->length-copy);
  252 }
  253 /*}}}*/
  254 void String_set(struct String *this, size_t pos, const struct String *s, size_t length) /*{{{*/
  255 {
  256   if (this->length>=pos)
  257   {
  258     if (this->length<(pos+length)) length=this->length-pos;
  259     if (length) memcpy(this->character+pos,s->character,length);
  260   }
  261 }
  262 /*}}}*/
  263 
  264 struct StringField *StringField_new(struct StringField *this) /*{{{*/
  265 {
  266   this->refStrings=(struct String**)0;
  267   this->refCount=0;
  268   return this;
  269 }
  270 /*}}}*/
  271 void StringField_destroy(struct StringField *this) /*{{{*/
  272 {
  273   int i;
  274 
  275   for (i=this->refCount; i>0; ) String_leaveField(this->refStrings[--i]);
  276   this->refCount=-1;
  277   free(this->refStrings);
  278 }
  279 /*}}}*/