home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / leda / src / basic / _string.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-15  |  5.0 KB  |  255 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  _string.c
  7. +
  8. +
  9. +  Copyright (c) 1991  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16. #include <LEDA/basic.h>
  17.  
  18. #include <stdarg.h>
  19. #include <string.h>
  20.  
  21.  
  22. //------------------------------------------------------------------------------
  23. // String
  24. //------------------------------------------------------------------------------
  25.  
  26.  
  27. char* str_dup(const char* p)
  28. { if (p==nil) error_handler(1,"str_dup: nil argument");
  29.   char* q = new char[strlen(p)+1];
  30.   strcpy(q,p);
  31.   return q;
  32. }
  33.  
  34. char* str_cat(const char* p1, const char* p2)
  35. { char* q = new char[strlen(p1)+strlen(p2)+1];
  36.   strcpy(q,p1);
  37.   strcat(q,p2);
  38.   return q;
  39.  }
  40.  
  41. void string::read(istream& s, char delim)
  42. { char buf[512];
  43.   char* p = buf;
  44.   char* q = p+511;
  45.  
  46.   while (s.get(*p) && p < q)
  47.   { if (*p == delim) break;
  48.     p++;
  49.    } 
  50.   *p = '\0';
  51.  
  52.   *this = buf;
  53. }
  54.  
  55.  
  56. string::string(const char* format, ...)      // printf-like constructor
  57. {
  58.   va_list arg_list;
  59.   Va_Start(arg_list,format);
  60.  
  61.   int arg[10];
  62.  
  63.   for(int i=0;i<10;i++) arg[i] = va_arg(arg_list,int);
  64.  
  65.   va_end(arg_list);
  66.  
  67.   char buf[512];
  68.  
  69.   sprintf(buf,format,arg[0],arg[1],arg[2],arg[3],arg[4],
  70.                      arg[5],arg[6],arg[7], arg[8],arg[9]);
  71.  
  72.   p = new srep(str_dup(buf));
  73. }
  74.  
  75. int  compare(string& x, string& y) 
  76. { return strcmp(x.p->s,y.p->s); }
  77.  
  78. int   string::length()     const   
  79. { return strlen(p->s); }
  80.  
  81. char string::operator[](int i) const
  82. { if (i<0 || strlen(p->s)<=i) error_handler(1,"string[]: index out of range");
  83.   return p->s[i];
  84. }
  85.  
  86. char& string::operator[](int i)
  87. { if (i<0 || strlen(p->s)<=i) error_handler(1,"string[]: index out of range");
  88.   *this = p->s;    //disconnect
  89.   return p->s[i];
  90. }
  91.  
  92.  
  93. string& string::operator=(const char* s)
  94. { char* t = str_dup(s);
  95.   if (p->n > 1) { p->n--; p = new srep(str_dup(""));}
  96.   delete p->s;
  97.   p->s = t;
  98.   return *this;
  99. }
  100.  
  101. string& string::operator=(const string& x)
  102. { x.p->n++;
  103.   if (--p->n == 0) { delete p->s; delete p; }
  104.   p = x.p;
  105.   return *this;
  106. }
  107.  
  108. string  string::operator+(const string& x)  const 
  109. { string res; 
  110.   res.p->s = str_cat(p->s,x.p->s); 
  111.   return res;
  112.  }
  113.  
  114. string  string::operator+(const char* s1)   const 
  115. { string res;
  116.   res.p->s = str_cat(p->s,s1); 
  117.   return res;
  118.  }
  119.  
  120. string& string::operator+=(const char* s1)  
  121. { *this = *this + s1; 
  122.   return *this; 
  123.  }
  124.  
  125. string& string::operator+=(const string& x) 
  126. { *this = *this + x; 
  127.   return *this; 
  128.  }
  129.  
  130. int operator==(const string& x, const char* s)   
  131. { return strcmp(x.p->s,s)==0; }
  132.  
  133. int operator==(const string& x, const string& y) 
  134. { return strcmp(x.p->s,y.p->s)==0; }
  135.  
  136. int operator!=(const string& x, const char* s)   
  137. { return strcmp(x.p->s,s)!=0; }
  138.  
  139. int operator!=(const string& x, const string& y) 
  140. { return strcmp(x.p->s,y.p->s)!=0; }
  141.  
  142. int operator<(const string& x, const string& y)  
  143. { return strcmp(x.p->s,y.p->s)<0; }
  144.  
  145. int operator>(const string& x, const string& y)  
  146. { return strcmp(x.p->s,y.p->s)>0; }
  147.  
  148. int operator<=(const string& x, const string& y) 
  149. { return strcmp(x.p->s,y.p->s)<=0; }
  150.  
  151. int operator>=(const string& x, const string& y) 
  152. { return strcmp(x.p->s,y.p->s)>=0; }
  153.  
  154.  
  155. istream& operator>>(istream& in, string& x)
  156. { char buf[256];
  157.   if (in >> buf) x = buf; 
  158.   return in;
  159. }
  160.  
  161. ostream& operator<<(ostream& out, const string& x) 
  162. { return out << x.p->s; }
  163.  
  164.  
  165. string string::sub(int i, int j) const
  166. {
  167.   if (j>= strlen(p->s)) j = strlen(p->s) - 1;
  168.  
  169.   if (i < 0) i = 0;
  170.  
  171.   int l = j-i+1;
  172.  
  173.   if (l <= 0)  return string("");
  174.  
  175.   char* q = new char[l+1];
  176.   strncpy(q,p->s+i,l);
  177.   q[l] = '\0';
  178.   return string(q);
  179. }
  180.  
  181. int string::pos(string s1, int i) const
  182. {
  183.   int l1 = s1.length();
  184.   if (l1==0 || i >= length()) return -1;
  185.  
  186.   char c = s1[0];
  187.  
  188.   char* q = p->s + i;
  189.   while (*q)
  190.     { while ( (*q) && (*q != c) ) q++;
  191.       if ( strncmp(q,s1.p->s,l1)==0 ) break ;
  192.       if (*q) q++;
  193.      }
  194.   return (*q) ? (q - p->s)/ sizeof(char) :  -1;
  195. }
  196.  
  197. string string::insert(string s, int i) const
  198. { return sub(0,i-1) + s + sub(i,length()-1); }
  199.  
  200. string string::insert(int i, string s) const
  201. { return sub(0,i-1) + s + sub(i,length()-1); }
  202.  
  203.  
  204. string string::del(int i, int j) const
  205. { return sub(0,i-1) + sub(j+1,length()-1); }
  206.  
  207. string string::del(string s, int n) const
  208. { return replace(s,"",n); }
  209.  
  210.  
  211.  
  212. string string::replace(int i, int j, string s) const
  213. { return sub(0,i-1) + s + sub(j+1,length()-1); }
  214.  
  215. string string::replace(string s1, string s2, int n) const 
  216.   // replace n-th (all if n=0) occurrence of s1 by s2 
  217.  
  218.   int i = 0;
  219.   int match = 0;  
  220.  
  221.   int l1 = s1.length();
  222.  
  223.   string tmp;
  224.  
  225.   for(;;)
  226.   { int j = pos(s1,i);
  227.     if (j < 0 ) break;
  228.     tmp += sub(i,j-1);
  229.  
  230.     if (n==0 || ++match == n)
  231.        tmp += s2;
  232.     else
  233.        tmp += s1;
  234.  
  235.     i = j+l1;
  236.    }
  237.  
  238.   tmp += sub(i,length()-1);
  239.  
  240.   return tmp;
  241.  
  242.  }
  243.  
  244.  
  245. string string::format(string f) const
  246.   char buf[512];
  247.   sprintf(buf,~f,p->s);
  248.   return buf;
  249.  
  250.  }
  251.  
  252.