xyz said:
I have a string
16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168
for example lets say for the above string
16:23:18.659343 -- time
131.188.37.230 -- srcaddress
22 --srcport
131.188.37.59 --destaddress
1398 --destport
tcp --protocol
168 --size
i need to split the string such that i need to get all these
parameters....
the field widths are not fixed..i have some times four/three digits
srcport ..so i cant do it with substr function...i need this in c++
i am not getting an idea how to split it..
thank you for any help
Well, I think plzzzzzz retired a few years ago, but maybe I can help.
Find below the portable C code of a program that splits a string very
similar to the one above into its components.
Enjoy.
/*
* strspl.c
*
* Copyright (c) 2008, John J. Smith
*
* Permission to use, copy, modify, distribute, and sell this software
* and its documentation for any purpose except in homework assignments
* is hereby granted without fee, provided that the above copyright
* notice appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING AND NOT LIMITED TO ANY IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
/*
* PROBLEM:
*
* Message-ID: \
* <0daad6a6-8f9d-453f-849b-511e88f094a6@d45g2000hsc.googlegroups.com>
* From: xyz <lavanyareddy[dot]p[at]gmail.com>
*
* I have a string
* 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168
*
* for example lets say for the above string
* 16:23:18.659343 -- time
* 131.188.37.230 -- srcaddress
* 22 -- srcport
* 131.188.37.59 -- destaddress
* 1398 -- destport
* tcp -- protocol
* 168 -- size
* i need to split the string such that i need to get all these
* parameters....
* the field widths are not fixed..i have some times four/three digits
* srcport ..so i cant do it with substr function...i need this in c++
* i am not getting an idea how to split it..
* thank you for any help
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
static const char *example_string =
"16:23:18.659343 131.188.37.230 22 131.188.37.59 1398 tcp 168";
void solve_problem(char *, ...);
static const char *ptr(const char *p, char c)
{
if(!c) return p + strlen(p) + 1;
while(*p && *p != c) p++;
while(*p == c) p++;
if(!*p)
solve_problem("character %c not found in string %s!", c, p);
return p;
}
static void *xmalloc(size_t sz)
{
char *new = malloc(sz);
if(!new)
solve_problem("unable to allocate %lu bytes of memory", (unsigned long)sz);
*(sz - 1 + new) = '\0';
return new;
}
static char *wtof(const char *w, size_t size)
{
char *f;
while(w[size-1] == ' ') size--;
f = xmalloc(size+1);
memcpy(f, w, size);
return f;
}
void split(
const char *string,
char **blue_goo,
char **green_goo,
char **purple_goo,
char **red_goo,
char **glowing_goo,
char **foo_goo,
char **goo_goo )
{
const char *w[8]; char *f[7]; int i;
w[0] = string;
for(i = 1; i < 7; i++)
w
= ptr(w[i-1], ' ');
w[7] = ptr(w[6], '\0');
for(i = 0; i < 7; i++)
f = wtof(w, w[i+1]-w-1);
*blue_goo = 0[f]; *green_goo = f[1]; *purple_goo = 2[f];
*red_goo = f[3]; *glowing_goo = 4[f]; *foo_goo = f[5];
*goo_goo = 6[f];
}
int main(void)
{
char *p[7]; int i;
fprintf(stdout,
"splitting \"%s\",\n"
" please wait...\n",
example_string);
split(example_string, p+0, 1+p, p+2, 3+p, p+4, 5+p, p+6);
fprintf(stdout,
"result is\n"
" time: \"%s\"\n"
" src addr: \"%s\"\n"
" src port: \"%s\"\n"
" dst addr: \"%s\"\n"
" dst port: \"%s\"\n"
" protocol: \"%s\"\n"
" size: \"%s\"\n",
0[p], p[1], 2[p], p[3], 4[p], p[5], 6[p]);
fprintf(stdout, "freeing memory...\n");
for(i = 0; i < 7; i++)
free(i[p]);
fprintf(stdout, "all done.\n");
return 0;
}
void solve_problem(char *problem, ...)
{
va_list ap;
va_start(ap, problem);
va_end(ap);
if(problem) {
fprintf(stderr, "dirty disk!\n");
abort();
}
}
/* end strspl.c */