Date: Thu, 18 Sep 2008 16:46:00 +1000 From: "Murray Taylor" <MTaylor@bytecraft.com.au> To: "Giorgos Keramidas" <keramida@ceid.upatras.gr>, <unga888@yahoo.com> Cc: freebsd-questions@freebsd.org Subject: RE: How to split a C string by a string? Message-ID: <E194A4DE220BBE4FAF3AB7C4E7EDA086055913@svmailmel.bytecraft.internal>
index | next in thread | raw e-mail
[-- Attachment #1 --]
> -----Original Message-----
> From: owner-freebsd-questions@freebsd.org
[mailto:owner-freebsd-questions@freebsd.org] On Behalf Of Giorgos
Keramidas
> Sent: Thursday, 18 September 2008 1:49 AM
> To: unga888@yahoo.com
> Cc: freebsd-questions@freebsd.org
> Subject: Re: How to split a C string by a string?
>
> On Wed, 17 Sep 2008 06:32:56 -0700 (PDT), Unga <unga888@yahoo.com>
wrote:
> > --- On Wed, 9/17/08, Giorgos Keramidas <keramida@ceid.upatras.gr>
wrote:
> >> From: Giorgos Keramidas <keramida@ceid.upatras.gr>
> >> Subject: Re: How to split a C string by a string?
> >> To: unga888@yahoo.com
> >> Cc: freebsd-questions@freebsd.org
> >> Date: Wednesday, September 17, 2008, 6:17 PM On Wed, 17 Sep 2008
> >> 00:45:46 -0700 (PDT), Unga <unga888@yahoo.com> wrote:
> >> > Hi all
> >> >
> >> > I'm writing an C application on FreeBSD 7+. I need to split a
> >> > string by another string (ie. the delimiter is "xxx") similar to
> >> > strtok split a string by a single char. Is there a standard
> >> > function or is there a FreeBSD functions for this?
> >>
> >> You can use strstr() to look for the "xxx" delimited and split that
> >> that point: [snip sample code]>
>
> > Thank you very much for the reply. That is, there is no existing
split
> > function. So I got to write to my own :)
>
> Yes, you have to roll our own. The standard C library doesn't have
> string splitting functions with a string as delimiter. It includes
strtok(),
> strspn() and strcspn(), but these work with character sets as
delimiters, not strings...
Hi,
the attached code does string splitting and insertion
it looks for a specific string, copies from start up to that
point to a workspace, inserts the new text, then appends
the remainder, starting AFTER the search string fragment.
with a bit of work you could make it do your string splitting....
Murray Taylor
Bytecraft Systems
Special Projects Engineer
P: +61 3 8710 0600
D: +61 3 9238 4275
F: +61 3 9238 4140
--
|_|0|_| "Absence of evidence
|_|_|0| is not evidence of absence"
|0|0|0| Carl Sagan
---------------------------------------------------------------
The information transmitted in this e-mail is for the exclusive
use of the intended addressee and may contain confidential
and/or privileged material. Any review, re-transmission,
dissemination or other use of it, or the taking of any action
in reliance upon this information by persons and/or entities
other than the intended recipient is prohibited. If you
received this in error, please inform the sender and/or
addressee immediately and delete the material.
E-mails may not be secure, may contain computer viruses and
may be corrupted in transmission. Please carefully check this
e-mail (and any attachment) accordingly. No warranties are
given and no liability is accepted for any loss or damage
caused by such matters.
---------------------------------------------------------------
### This e-mail message has been scanned for Viruses by Bytecraft ###
[-- Attachment #2 --]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char template [] = "\
/*****************************************\n\
* STANDARD CLOSED\n\
*/\n\
\n\
To: $ADDR$\n\
Subject: Request\n\
\n\
\"start-request\"\n\
Job Id:$JOBID$\n\
Status:Closed\n\
\"end-request\"\n\
Message sent: $DATETIME$\n\
\n\
/*\n\
* STANDARD ETA\n\
*/\n\
\n\
From: bcall@bcall.net\n\
To: $ADDR$\n\
Subject: Request\n\
\n\
\"start-request\"\n\
Job Id:$JOBID$\n\
Status:$STATUS$\n\
Timestamp: $DATETIME$\n\
ETA: $ETA$\n\
\"end-request\"\n\
";
char *tags[] = {"$ADDR$", "$JOBID$", "$STATUS$", "$DATETIME$", "$PARTBUF$", "$ETA$" };
int ntags = sizeof(tags) / sizeof(char *);
main()
{
char *val [] = { "mjt@example.com","98765", "Onsite", "2008-09-18 16:04:41",
"Some long part buffer stuff", "2010-01-01 00:00:00"};
int i;
char Buf_A [2048];
printf("sizeof tags = %d\n", ntags);
strcpy(Buf_A, template);
for( i = 0; i < ntags ; i++ ) {
printf("calling subst\n");
subst(Buf_A, sizeof(Buf_A), tags[i], val[i]);
}
printf("%s", Buf_A);
}
/**
* substitute all occurences of tag in template with replacement
* put it all into outbuf
*
* inputs: outbuf s/b setup with current text body.
* on first tag - copy initial template to outbuf then call subst
* on subsequent tags, just use call subst with existing outbuf.
*
* returns number of substitutions
*/
int
subst(char *outbuf, int outbufsize, char *tag, char *replacement)
{
char *hit;
int count = 0;
int start_of_substitution;
char *tmp;
printf( "Tag = %s, Val = %s\n", tag, replacement);
tmp = (char *)malloc(outbufsize * 2);
while (1) {
/* copy outbuf -> tmp */
strcpy(tmp, outbuf);
/* find tag */
if ((hit = strstr(tmp, tag)) == NULL) {
/* not found -> return (outbuf is correct) */
break;
}
/* found - continue */
start_of_substitution = (int)((hit) - (tmp));
/*
* copy 1st part tmp -> outbuf -- not necessary ?
*/
strlcpy(outbuf, tmp, start_of_substitution + 1);
/* append data into outbuf */
strlcat(outbuf, replacement, outbufsize);
/* append remainder of tmp after taglen chars */
strlcat(outbuf, tmp + (start_of_substitution + strlen(tag)), outbufsize);
/* loop */
count++;
}
/* clean up ad return */
free(tmp);
return (count);
}
help
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?E194A4DE220BBE4FAF3AB7C4E7EDA086055913>
