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

📄 sflstr.c

📁 短小精悍的C语言标准函数库。提供450个以上的可移植的算法和工具代码。
💻 C
📖 第 1 页 / 共 5 页
字号:
    Synopsis: A case insensitive search and replace. Searches for all
    occurances of a string, and replaces it with another string.
    Returns a pointer
    to head of the buffer.  Submitted by Scott Beasley <jscottb@infoave.com>
    ---------------------------------------------------------------------[>]-*/

char *
searchreplace (
    char *strbuf,
    char *strtofnd,
    char *strtoins)
{
   char *offset, *strbase;

   ASSERT (strbuf);
   ASSERT (strtofnd);
   ASSERT (strtoins);

   offset = strbase = (char *)NULL;

   while (*strbuf)
     {
       offset = stricstr (!offset ? strbuf : strbase, strtofnd);
       if (offset)
         {
           strbase = (offset + strlen (strtoins));
           strcpy (offset, (offset + strlen (strtofnd)));
           memmove (offset + strlen (strtoins),
                     offset, strlen (offset) + 1);
           memcpy (offset, strtoins, strlen (strtoins));
         }

      else
           break;
     }

   return strbuf;
}

/*  ---------------------------------------------------------------------[<]-
    Function: deletestring

    Synopsis: Deletes all occurances of one string, in another string.
    Returns a pointer to head of the buffer.
    Submitted by Scott Beasley <jscottb@infoave.com>
    ---------------------------------------------------------------------[>]-*/

char *
deletestring (
    char *strbuf,
    char *strtodel,
    int ignorecase)
{
   char *offset;

   ASSERT (strbuf);
   ASSERT (strtodel);

   offset = (char *)NULL;

   while (*strbuf)
     {
        if (!ignorecase)
            offset = stricstr (strbuf, strtodel);
        else
            offset = strstr (strbuf, strtodel);
        if (offset)
          {
            strcpy (offset, (offset + strlen (strtodel)));    /* NO OVERRUN */
          }
        else
            break;
     }

   return strbuf;
}

/*  ---------------------------------------------------------------------[<]-
    Function: getstrfld

    Synopsis: Gets a sub-string from a formated string. nice strtok
    replacement.

    usage:
      char strarray[] = { "123,456,789,abc" };
      char strretbuff[4];
      getstrfld (strarray, 2, 0, ",", strretbuff);

    This would return the string "789" and place it also in strretbuff.
    Returns a NULL if fldno is out of range, else returns a pointer to
    head of the buffer.  Submitted by Scott Beasley <jscottb@infoave.com>
    ---------------------------------------------------------------------[>]-*/

char *
getstrfld (
    char *strbuf,
    int fldno,
    int ofset,
    char *sep,
    char *retstr)
{
   char *offset, *strptr;
   int curfld;

   ASSERT (strbuf);
   ASSERT (sep);
   ASSERT (retstr);

   offset = strptr = (char *)NULL;
   curfld = 0;

   strbuf += ofset;

   while (*strbuf)
     {
       strptr = !offset ? strbuf : offset;
       offset = strpbrk ((!offset ? strbuf : offset), sep);

       if (offset)
          offset++;
       else if (curfld != fldno)
         {
           *retstr = (char)NULL;
           break;
         }

       if (curfld == fldno)
         {
           strncpy (retstr, strptr,
              (int)(!offset ? strlen (strptr)+ 1 :
              (int)(offset - strptr)));
           if (offset)
              retstr[offset - strptr - 1] = 0;

           break;
         }
       curfld++;
     }
   return retstr;
}

/*  ---------------------------------------------------------------------[<]-
    Function: setstrfld

    Synopsis: Inserts a string into a fomated string.

    usage:
       char strsrray[26] = { "this is a test." };
       setstrfld (strsrray, 2, 0, " ", "big ");

       result: this is a big test.

    Does nothing if fldno is out of range, else returns pointer to head
    of the buffer.  Returns a pointer to head of the buffer.
    Submitted by Scott Beasley <jscottb@infoave.com>
    ---------------------------------------------------------------------[>]-*/

char *
setstrfld (
    char *strbuf,
    int fldno,
    int ofset,
    char *sep,
    char *strtoins)
{
   char *offset, *strptr, *strhead;
   int curfld;

   ASSERT (strbuf);
   ASSERT (sep);
   ASSERT (strtoins);

   offset = strptr = (char *)NULL;
   curfld = 0;

   strhead = strbuf;
   strbuf += ofset;

   while (*strbuf)
     {
       strptr = !offset ? strbuf : offset;
       offset = strpbrk ((!offset ? strbuf : offset), sep);

       if (offset)
          offset++;

       if (curfld == fldno)
          {
            insertstring (strptr, strtoins,
               (int)(!offset ? strlen (strptr):
               (int)(offset - strptr)));
            break;
          }
       curfld++;
     }

   return strhead;
}

/*  ---------------------------------------------------------------------[<]-
    Function: getstrfldlen

    Synopsis: Get the length of as a field in a string.  Used mainly
    for getting the len to malloc mem to call getstrfld with.  Returns
    the length of the field.
    Submitted by Scott Beasley <jscottb@infoave.com>
    ---------------------------------------------------------------------[>]-*/

int
getstrfldlen (
    char *strbuf,
    int fldno,
    int ofset,
    char *sep)
{
   char *offset, *strptr;
   int curfld, retlen = 0;

   ASSERT (strbuf);
   ASSERT (sep);

   offset = strptr = (char *)NULL;
   curfld = 0;

   strbuf += ofset;

   while (*strbuf)
     {
       strptr = !offset ? strbuf : offset;
       offset = strpbrk ((!offset ? strbuf : offset), sep);

       if (offset)
          offset++;
       else if (curfld != fldno)
         {
           retlen = 0;
           break;
         }
       if (curfld == fldno)
         {
           retlen = (int)(!offset ? strlen (strptr) + 1 :
                    (int)(offset - strptr));
           break;
         }
       curfld++;
     }
   return retlen;
}

/*  ---------------------------------------------------------------------[<]-
    Function: findstrinfile

    Synopsis:
    Find's a string inside a text file and reads the line in and sets the
    file pointer to the beginning of that line.  Assumes the line length
    to be <= 1024 bytes.  Returns a pointer to head of the return buffer,
    and the file postion will be at the start of the found string. If
    the strretstr param is != NULL then strretstr will contain the line
    that the search string was found in.
    Submitted by Scott Beasley <jscottb@infoave.com>
    ---------------------------------------------------------------------[>]-*/

char *
findstrinfile (
    FILE *fp,
    char *strtofind,
    char *strretstr,
    int *iLnNo)
{
   char strline[1025];
   int nfnd = 0;
   long lfpos;

   ASSERT (fp);
   ASSERT (strtofind);
   ASSERT (strretstr);

   if (strretstr)
       *strretstr = 0;

   while (1)
     {
       lfpos = ftell (fp);
       fgets (strline, 1024, fp);
       trim (strline);

       if (!*strline)
          continue;

       if (iLnNo)
           (*iLnNo)++;

       if (stricstr (strline, strtofind))
         {
           if (strretstr)
             {
               strcpy (strretstr, strline);
             }

           fseek (fp, lfpos, SEEK_SET);
           nfnd = 1;
           break;
         }

       if (feof (fp))
           break;
     }

   if (strretstr)
       return strretstr;
   else
       return (char *)nfnd;
}

/*  ---------------------------------------------------------------------[<]-
    Function: getequval

    Synopsis:
    get the everything on a line past a '='.

    Examples:
       char strtest[] = { "progpath=c:\sfl");
       char strret[256];
       getequval (strtest, strret);

    This would return: "c:\sfl".  Returns a pointer to head of the
    return buffer.  Submitted by Scott Beasley <jscottb@infoave.com>
    ---------------------------------------------------------------------[>]-*/

char *
getequval (
    char *strline,
    char *strretstr)
{
   char *stroffset;

   ASSERT (strline);
   ASSERT (strretstr);

   stroffset = strstr (strline, "=");

   if (stroffset)
       ltrim ((stroffset + 1));
   else
       return (char *)NULL;

   return strcpy (strretstr,
                  (stroffset && *(stroffset + 1))? (stroffset + 1): "");
}

/*  ---------------------------------------------------------------------[<]-
    Function: matchtable

    Synopsis:
    Function to compare a string with a set of strings.

    Examples:
       iret = matchtable (strname, "bill|william|billy", "|", IGNORECASE);

    If strname == "william", then matchtable would return 1.
    Returns >= 0 if match is found.  a -1 if no match is found.
    Submitted by Scott Beasley <jscottb@infoave.com>
    ---------------------------------------------------------------------[>]-*/

int
matchtable (
    char *strbuf,
    char *strmatch,
    char *strsept,
    int ncase)
{
   int nstate = -1, cnt = 0, icmpres;
   int ilen;
   char *strtemp;

   ASSERT (strbuf);
   ASSERT (strmatch);
   ASSERT (strsept);

   while (1)
     {
       ilen = getstrfldlen (strmatch, cnt, 0, strsept);
       strtemp = (char *) malloc (sizeof (char) * ilen + 1);
       ASSERT (strtemp);
       getstrfld (strmatch, cnt, 0, strsept, strtemp);
       if (*strtemp)
         {
           trim (strtemp);
           if (!ncase)
             {
               icmpres = lexcmp (strbuf, strtemp);
             }
           else
             {
               icmpres = strcmp (strbuf, strtemp);
             }

           if (!icmpres)
             {
               nstate = cnt;
               break;
             }
           else
             {
               if (!strcmp (strbuf, strtemp))
                 {
                   nstate = cnt;
                   break;
                 }
             }
         }
       else
         {
           nstate = -1;
           break;
         }
       cnt++;
       free (strtemp);
     }

   return nstate;
}

/*  ---------------------------------------------------------------------[<]-
    Function: stringreplace

    Synopsis:
    This function searches for known strings, and replaces them with
    another string.

    Examples:
       stringreplace (strfilename, "sqv|sqr,ruv|run,h_v|h");

    This example would replace all occurences of sqv, with sqr, ruv with
    run and h_v with h. Returns pointer to head of the return buffer.
    Submitted by Scott Beasley <jscottb@infoave.com>
    ---------------------------------------------------------------------[>]-*/

char *
stringreplace (
    char *strbuf,
    char *strpattern)
{
   int ilen, ifld = 0;
   char *strsrch, *strrpl, *strpat;

   ASSERT (strbuf);
   ASSERT (strpattern);

   if (!strpattern)
       return strbuf;

   while (1)

⌨️ 快捷键说明

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