⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringc.c

📁 linux下的E_MAIL客户端源码
💻 C
📖 第 1 页 / 共 3 页
字号:
} // End AppendFile/*--------------------------------------------------------------------------- *  Method to read the specified number of bytes from an open file and *     replace the contents of this string */intStringC::ReadFile(FILE *fp, unsigned count){//// If length not specified, see how far to end//   if ( count == (unsigned)-1 ) {      long	cur  = ftell(fp);      fseek(fp, 0, SEEK_END);      long	last = ftell(fp);      count = (unsigned)(last - cur);      fseek(fp, cur, SEEK_SET);   }//// Allocate space and read data//   reallocate(count, autoShrink);   unsigned	readCount = fread(_p, 1, count, fp);   int	error = (readCount != count);   _len = readCount;   _p[_len] = 0;   return !error;} // End ReadFile/*--------------------------------------------------------------------------- *  Method to read a file into the string, replacing the current contents */intStringC::ReadFile(char *fname){//// Open file//   FILE	*fp = fopen(fname, "r");   if ( !fp ) return 0;   int	success = ReadFile(fp);   fclose(fp);   return success;} // End ReadFile/*--------------------------------------------------------------------------- *  Method to append the string to an open file */intStringC::WriteFile(FILE *fp){   int	error = (fwrite(_p, 1, _len, fp) != _len);   if ( !error ) error = (fflush(fp) != 0);   return !error;}/*--------------------------------------------------------------------------- *  Method to write the string to a file */intStringC::WriteFile(char *fname){//// Open file//   FILE	*fp = fopen(fname, "w+");   if ( !fp ) return 0;   int	success = WriteFile(fp);   fclose(fp);   return success;} // End WriteFile/*--------------------------------------------------------------------------- *  Method to change auto shrinking */voidStringC::AutoShrink(char val){   if ( (autoShrink && val) || (!autoShrink && !val) ) return;   autoShrink = val;   if ( autoShrink ) reallocate(_len, True);}/*--------------------------------------------------------------------------- *  Remove characters from the beginning */voidStringC::CutBeg(u_int l){   (*this)(0,l) = "";}/*--------------------------------------------------------------------------- *  Remove characters from the end */voidStringC::CutEnd(u_int l){   if ( !_p ) return;   if ( l > _len ) l = _len;   Clear(_len - l);}/*--------------------------------------------------------------------------- *  Do a string comparison at the beginning */intStringC::StartsWith(const char *cs, u_int clen, u_int off, Boolean checkCase)const{//// Do quick checks//   if ( _p == NULL || cs == NULL ) return 0;   if ( clen > (_len-off) ) return 0;//// Compare at beginning//   if ( checkCase )      return (strncmp(_p+off, cs, clen) == 0);   else      return (strncasecmp((char*)(_p+off), (char*)cs, clen) == 0);}intStringC::StartsWith(const CharC& c, u_int offset, Boolean checkCase) const{   return StartsWith(c.Addr(), c.Length(), offset, checkCase);}intStringC::StartsWith(char c, u_int offset, Boolean checkCase) const{//// Do quick checks//   if ( _p == NULL || offset >= _len ) return 0;//// Compare at beginning//   if ( checkCase )      return (*(_p+offset) == c);   else {      char	pc = *(_p+offset);      return (to_lower(pc) == to_lower(c));   }} // End StartsWith/*--------------------------------------------------------------------------- *  Do a string comparison at the end */intStringC::EndsWith(const char *cs, u_int clen, u_int off, Boolean checkCase)const{//// Do quick checks//   if ( _p == NULL || cs == NULL || clen > off+1 ) return 0;//// Compare backwards, starting at offset//   int	pos = off+1 - clen;   if ( checkCase )      return (strncmp(_p+pos, cs, clen) == 0);   else      return (strncasecmp((char*)(_p+pos), (char*)cs, clen) == 0);} // End EndsWithintStringC::EndsWith(const CharC& c, u_int offset, Boolean checkCase) const{   return EndsWith(c.Addr(), c.Length(), offset, checkCase);}/*--------------------------------------------------------------------------- *  See if string matches exactly */intStringC::Equals(const char *cs, u_int clen, u_int offset, Boolean checkCase)									   const{//// Do quick checks//   if ( _p == NULL || cs == NULL || clen != (_len-offset) ) return 0;//// Compare//   if ( checkCase )      return (strncmp(_p+offset, cs, clen) == 0);   else      return (strncasecmp((char*)(_p+offset), (char*)cs, clen) == 0);} // End EqualsintStringC::Equals(const CharC& c, u_int offset, Boolean checkCase) const{   return Equals(c.Addr(), c.Length(), offset, checkCase);}intStringC::Equals(char c, u_int offset, Boolean checkCase) const{//// Do quick checks//   if ( _p == NULL || offset != (_len-1) ) return 0;//// Compare at beginning//   if ( checkCase )      return (*(_p+offset) == c);   else {      char	pc = *(_p+offset);      return (to_lower(pc) == to_lower(c));   }} // End Equals/*--------------------------------------------------------------- *  Search for a string */const char *StringC::Search(const char *pat, u_int plen, u_int offset, Boolean checkCase)									   const{   if ( _p == NULL || pat == NULL || _len == 0 )      return NULL;   const char	*end = ((_p + _len) - plen) + 1;   const char	*src = _p + offset;   if (end < src) return NULL;   if ( checkCase ) {      while ((src = (const char *)memchr(src, pat[0], end - src))) {	 if ( (strncmp(src, pat, plen) == 0) ) return src;	 src++;      }   }   else {      while ( src < end ) {	 if ( strncasecmp((char*)src, (char*)pat, plen) == 0 ) return src;	 src++;      }   }   return NULL;}const char *StringC::Search(const CharC& c, u_int offset, Boolean checkCase) const{   return Search(c.Addr(), c.Length(), offset, checkCase);}/*--------------------------------------------------------------- *  Search for a particular character */const char *StringC::Search(char c, u_int offset, Boolean checkCase) const{   if ( _p == NULL || _len == 0 )      return NULL;   u_int	rem = _len - offset;   const char	*cs = _p   + offset;   if ( checkCase ) {      while ( rem > 0 && *cs != c ) cs++, rem--;   }   else {      c = to_lower(c);      while ( rem > 0 && to_lower(*cs) != c ) cs++, rem--;   }   if ( rem == 0 ) return NULL;   return cs;}/*--------------------------------------------------------------- *  Search in reverse for a string.  Start at offset and look backwards. */const char *StringC::RevSearch(const char *pat, u_int plen, u_int off, Boolean checkCase)									   const{   if ( _p == NULL || pat == NULL || _len == 0 )      return NULL;   const char	*src = _p + off - plen + 1;   if ( checkCase ) {      while ( src >= _p ) {	 if ( strncmp(src, pat, plen) == 0 ) return src;	 src--;      }   }   else {      while ( src >= _p ) {	 if ( strncasecmp((char*)src, (char*)pat, plen) == 0 ) return src;	 src--;      }   }   return NULL;}const char *StringC::RevSearch(const CharC& c, u_int offset, Boolean checkCase) const{   return RevSearch(c.Addr(), c.Length(), offset, checkCase);}/*--------------------------------------------------------------- *  Search in reverse for a particular character.  Start at offset and *     look backwards. */const char *StringC::RevSearch(char c, u_int offset, Boolean checkCase) const{   if ( _p == NULL || _len == 0 )      return NULL;   const char	*cs = _p + offset;   if ( checkCase ) {      while ( cs >= _p && *cs != c ) cs--;   }   else {      c = to_lower(c);      while ( cs >= _p && to_lower(*cs) != c ) cs--;   }   if ( cs < _p ) return NULL;   return cs;}/*--------------------------------------------------------------- *  Count the number of occurrences of a string */u_intStringC::NumberOf(const char *pat, u_int plen, u_int off, Boolean checkCase)									   const{   if ( _p == NULL || pat == NULL || _len == 0 )      return 0;   u_int	count = 0;   const char	*end = _p + _len - plen;   const char	*src = _p + off;   if ( checkCase ) {      while ( src <= end ) {	 if ( strncmp(src, pat, plen) == 0 ) {	    count++;	    src += plen;	 }	 else	    src++;      }   }   else {      while ( src <= end ) {	 if ( strncasecmp((char*)src, (char*)pat, plen) == 0 ) {	    count++;	    src += plen;	 }	 else	    src++;      }   }   return count;} // End NumberOfu_intStringC::NumberOf(const CharC& c, u_int offset, Boolean checkCase) const{   return NumberOf(c.Addr(), c.Length(), offset, checkCase);}/*--------------------------------------------------------------- *  Count the number of occurrences of a particular character */u_intStringC::NumberOf(char c, u_int offset, Boolean checkCase) const{   if ( _p == NULL || _len == 0 )      return 0;   u_int	count = 0;   u_int	rem = _len - offset;   const char	*cs = _p   + offset;   if ( checkCase ) {      while ( rem > 0 ) {	 if ( *cs == c ) count++;	 cs++;	 rem--;      }   }   else {      c = to_lower(c);      while ( rem > 0 ) {	 if ( to_lower(*cs) == c ) count++;	 cs++;	 rem--;      }   }   return count;} // End NumberOf/*--------------------------------------------------------------------------- *  Return the range of the next word delimited by the given characters */CharCStringC::NextWord(u_int offset, const char *delim){//// Skip any non-escaped characters in delim//   char	prev = 0;   char	ch;   while ( offset < _len && strchr(delim, ch=_p[offset]) != NULL &&	   prev != '\\' ) {      prev = ch;      offset++;   }   u_int	newPos = offset;//// Skip any characters not in delim//   while ( offset < _len &&           (strchr(delim, ch=_p[offset]) == NULL || prev == '\\') ) {      prev = ch;      offset++;   }   return Range(newPos, offset-newPos);}/*--------------------------------------------------------------------------- *  Return the range of the next word delimited by the given character */CharCStringC::NextWord(u_int offset, char delim){//// Skip non-escaped delim characters//   char	prev = 0;   char	ch;   while ( offset < _len && ((ch=_p[offset]) == delim) && prev != '\\' ) {      prev = ch;      offset++;   }   u_int	newPos = offset;//// Skip any non-delim characters//   while ( offset < _len && ((ch=_p[offset]) != delim || prev == '\\') ) {      prev = ch;      offset++;   }   return Range(newPos, offset-newPos);}/*--------------------------------------------------------------------------- *  Replace occurences of the first string with the second */voidStringC::Replace(CharC pat, CharC val){   int	pos = PosOf(pat);   while ( pos >= 0 ) {      (*this)(pos,pat.Length()) = val;      pos = PosOf(pat, (u_int)pos + val.Length()); // add val.Length() so we                                                   // don't search into the                                                   // string just replaced   }}/*--------------------------------------------------------------------------- *  Replace occurences of the first character with the second */voidStringC::Replace(char c1, char c2){   char	*cp = _p;   char	*ep = _p + _len;   while ( cp < ep ) {      if ( *cp == c1 ) *cp = c2;      cp++;   }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -