[Freeciv-Dev] Re: Split patch (was Re: [RFC PATCH] init_techs)
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
On Sun, Sep 23, 2001 at 05:59:48PM -0400, Justin Moore wrote:
>
> > >
> > > /***************************************************************************
> > > + PERL-like split. Takes a string of characters to split on, a buffer to
> > > + split, and an array of pointers to return. Can set a max size on the
> >
> > It returns an int. It may fill/write an array of pointers.
>
> Er, right. s/"to return"/"fill in"/g.
>
> > > + NOTE: This WILL change the original buffer, so if you want an original
> > > + copy of it, make it before you pass it to this function.
> >
> > Can you please remove this restriction.
>
> Not very easily. The options are to a) have the caller malloc a copy
> of the string and pass it in, or b) malloc X buffers of unknown size ahead
> of time and pass those in. The memory management gets tricky.
What about a strdup() in split?
> > The handling of comments should be in this general purpose method.
I'm sorry: s/should/shouldn't/
>
> Which general purpose method?
cut_comment
> > What about memory management? Who allocates the space for args? The
> > caller or split? It looks like args are currently pointers to buf. I
> > don't like this.
>
> It is up to the caller to allocate the array of pointers. And, yes,
> they are pointers to buf. Otherwise it becomes a nightmare trying to do
> several small mallocs and frees along the way. I'm open to suggestions on
> another way to do this, but I find it just easier to accept the fact that
> the original buffer you pass in will be modified. If you want an original
> copy of it, make it before you call split. How is this restriction bad?
Either the caller allocates the memory and pass the size down to split:
char buffer[10][50];
...
split(...,buffer,10,50);
or split allocates the memory and the caller has the free it.
char *strings[10];
...
n=split(...,strings,10);
...
for(i=0;i<n;i++)
free(strings[i]);
IMHO the first one is nicer.
Raimar
--
email: rf13@xxxxxxxxxxxxxxxxx
Microsoft does have a year 2000 problem. I'm part of it. I'm running Linux.
- [Freeciv-Dev] [RFC PATCH] init_techs, Arien Malec, 2001/09/19
- [Freeciv-Dev] Re: [RFC PATCH] init_techs, Gregory Berkolaiko, 2001/09/20
- [Freeciv-Dev] Re: [RFC PATCH] init_techs, Arien Malec, 2001/09/20
- [Freeciv-Dev] Re: [RFC PATCH] init_techs, Justin Moore, 2001/09/20
- [Freeciv-Dev] Re: [RFC PATCH] init_techs, Reinier Post, 2001/09/20
- [Freeciv-Dev] Re: [RFC PATCH] init_techs, Justin Moore, 2001/09/22
- [Freeciv-Dev] Re: [RFC PATCH] init_techs, Raimar Falke, 2001/09/22
- [Freeciv-Dev] Re: Split patch (was Re: [RFC PATCH] init_techs), Justin Moore, 2001/09/22
- [Freeciv-Dev] Re: Split patch (was Re: [RFC PATCH] init_techs), Raimar Falke, 2001/09/23
- [Freeciv-Dev] Re: Split patch (was Re: [RFC PATCH] init_techs), Justin Moore, 2001/09/23
- [Freeciv-Dev] Re: Split patch (was Re: [RFC PATCH] init_techs),
Raimar Falke <=
- [Freeciv-Dev] Re: Split patch (was Re: [RFC PATCH] init_techs), Justin Moore, 2001/09/24
- [Freeciv-Dev] Re: Split patch (was Re: [RFC PATCH] init_techs), Raimar Falke, 2001/09/25
- [Freeciv-Dev] Re: Split patch (was Re: [RFC PATCH] init_techs), Justin Moore, 2001/09/26
- [Freeciv-Dev] Re: Split patch (was Re: [RFC PATCH] init_techs), Raimar Falke, 2001/09/26
- [Freeciv-Dev] Re: Split patch (was Re: [RFC PATCH] init_techs), Justin Moore, 2001/09/26
- [Freeciv-Dev] Re: Split patch (was Re: [RFC PATCH] init_techs), Ross W. Wetmore, 2001/09/26
- [Freeciv-Dev] Re: Split patch (was Re: [RFC PATCH] init_techs), Raimar Falke, 2001/09/27
- [Freeciv-Dev] Re: Split patch (was Re: [RFC PATCH] init_techs), Ross W. Wetmore, 2001/09/27
- [Freeciv-Dev] Re: Split patch (was Re: [RFC PATCH] init_techs), Reinier Post, 2001/09/28
- [Freeciv-Dev] Re: Split patch (was Re: [RFC PATCH] init_techs), Ross W. Wetmore, 2001/09/28
|
|