Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | JohnF <john@please.see.sig.for.email.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: packed structs |
| Date | 2012-09-22 15:33 +0000 |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Message-ID | <k3klnr$lq7$1@reader1.panix.com> (permalink) |
| References | <k3j5or$q37$1@reader1.panix.com> <k3jaun$8al$1@dont-email.me> <e5a6fdcf-0343-4ea9-bfa8-5464c8d355a4@ib4g2000vbb.googlegroups.com> <k3jua2$l2e$1@reader1.panix.com> <slrnk5ri4f.1d3.grahn+nntp@frailea.sa.invalid> |
Jorgen Grahn <grahn+nntp@snipabacken.se> wrote:
> JohnF wrote:
>> Nick Keighley <nick_keighley_nospam@hotmail.com> wrote:
>>> Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
>>>
>>> serializing structs
>>>
>>>> One way is the function-per-struct-type approach, and
>>>> although it may be "b..o..r..i..n..g" it has advantages
>>>> that should not be dismissed lightly. ?Consider that there
>>>> are (most likely) only a handful of structs and hence only
>>>> a handful of functions; writing them won't take enough
>>>> time to b..o..r..e anyone except an ADHD sufferer.
>>>
>>> I've dealt with cases where there were considerably more than a
>>> "handful"- communication protocol.
>>>
>>> Since I have a low b..o..r..e..d..o..m threshold I resorted to code
>>> generation. The protocol was defined by tables in a PDF document (ug).
>>> Copy-paste turned them into text and perl turned them into something
>>> easily processed. Most of the code was generated by people who didn't
>>> appear to mind writing tons of tedious boring repetitive code. There
>>> was a bit-banging library that did most of the heavy lifting.
>>
>> What do you think of my my smemf()-type of solution in other post?
>> Not necessarily that particular solution, but the point being that
>> this has got to be a pretty frequently occurring problem, and the
>> only available solutions, like yours above, seem to be pretty
>> awfully ugly.
> Didn't read very carefully, but it's like the Python 'struct' module,
> isn't it?
> http://docs.python.org/library/struct.html
Thanks, Jorgen, that's >>excellent<<. Quick read suggests it's
exactly like my smemf(), or vice versa since theirs clearly
came first. I guess "great minds think alike". Of course,
I guess idiots probably think alike, too... Okay, I've made
my choice :)
In any event, I'll read that much more carefully, and
incorporate their inevitable improvements into my "functional
spec", such as it is, and then see if it seems worth re-coding
any changes and finishing it.
> Well, that works well in Python and the only drawbacks I can see in C
> are:
> - you give up some type safety
> - your output buffer must be big enough
Yeah, yeah, programmers ought to be careful.
Not much different than sprintf()'s potential pitfalls.
> - you give up some speed
Hardly a likely problem. This is typically for i/o,
not some compute intensive loop. You'd have to be
formatting god-knows-how-many GB's before anybody
would notice the overhead.
> I don't know why I've never seen this done in C or C++ before.
Well, I'm inclined to re-do smemf()'s specs as close as possible
to python's, except that their format strings are more different
from C's than necessary. In any case, I'm sure their stuff deals
with problems that hadn't occurred to me yet. So it'll be a great
improvement to see precisely what they're doing.
> I'm happy with the b..o..r..i..n..g approach because I've never had
> many such message formats (or at least they have had so much common
> structure that it was manageable).
> ...
>> And I suppose there are other kinds of ways to deal with this
>> whole class of problems, which wouldn't exist at all if some
>> kind of packed structs were C standard.
>
> Packed structs would not solve the class of problems. You still have
> endianness, you still have variable-sized message formats, and so on.
> /Jorgen
Yeah, I wrote that "packed structs solves all" sentence too quickly,
and it's wrong. smemf() and python pack(fmt,v1,v2,...) both already
handle endianness. And smemf() implements "%*x" to pick up variable
lengths from the arg list (my very quick glance at python doesn't
show that yet, but I'll bet it's there somewheres I'll find later).
You'd have to be more specific about "and so on", but that's also
on my to-do list. Thanks again for the pointer,
--
John Forkosh ( mailto: j@f.com where j=john and f=forkosh )
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar
packed structs JohnF <john@please.see.sig.for.email.com> - 2012-09-22 01:54 +0000
Re: packed structs Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-09-21 23:22 -0400
Re: packed structs JohnF <john@please.see.sig.for.email.com> - 2012-09-22 06:37 +0000
Re: packed structs "BartC" <bc@freeuk.com> - 2012-09-22 13:47 +0100
Re: packed structs Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-22 14:00 +0100
Re: packed structs JohnF <john@please.see.sig.for.email.com> - 2012-09-22 15:42 +0000
Re: packed structs Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-09-22 09:13 -0400
Re: packed structs Johann Klammer <klammerj@NOSPAM.a1.net> - 2012-09-23 03:10 +0200
Re: packed structs JohnF <john@please.see.sig.for.email.com> - 2012-09-23 02:10 +0000
Re: packed structs Stephen Sprunk <stephen@sprunk.org> - 2012-09-23 11:44 -0500
Re: packed structs JohnF <john@please.see.sig.for.email.com> - 2012-09-23 23:23 +0000
Re: packed structs Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-24 01:59 +0100
Re: packed structs JohnF <john@please.see.sig.for.email.com> - 2012-09-24 02:54 +0000
Re: packed structs Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-24 04:38 +0100
Re: packed structs JohnF <john@please.see.sig.for.email.com> - 2012-09-24 04:07 +0000
Re: packed structs Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-24 12:16 +0100
Re: packed structs JohnF <john@please.see.sig.for.email.com> - 2012-09-24 11:45 +0000
Re: packed structs "BartC" <bc@freeuk.com> - 2012-09-24 10:18 +0100
Re: packed structs JohnF <john@please.see.sig.for.email.com> - 2012-09-24 11:04 +0000
Re: packed structs Stephen Sprunk <stephen@sprunk.org> - 2012-09-30 14:21 -0500
Re: packed structs JohnF <john@please.see.sig.for.email.com> - 2012-10-01 07:34 +0000
Re: packed structs Stephen Sprunk <stephen@sprunk.org> - 2012-09-30 13:52 -0500
Re: packed structs Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-09-22 01:31 -0700
Re: packed structs JohnF <john@please.see.sig.for.email.com> - 2012-09-22 08:53 +0000
Re: packed structs Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-09-22 14:17 +0000
Re: packed structs JohnF <john@please.see.sig.for.email.com> - 2012-09-22 15:33 +0000
Re: packed structs Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-09-22 20:43 +0000
Re: packed structs "BartC" <bc@freeuk.com> - 2012-09-22 22:52 +0100
Re: packed structs Keith Thompson <kst-u@mib.org> - 2012-09-22 13:47 -0700
Re: packed structs JohnF <john@forkosh.com.com> - 2012-09-23 00:19 +0000
Re: packed structs Ian Collins <ian-news@hotmail.com> - 2012-09-23 13:32 +1200
Re: packed structs JohnF <john@please.see.sig.for.email.com> - 2012-09-23 02:16 +0000
Re: packed structs Ian Collins <ian-news@hotmail.com> - 2012-09-23 10:33 +1200
Re: packed structs Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-09-23 01:38 -0700
Re: packed structs The Great Firewall of China Blue <chine.bleu@yahoo.com> - 2012-09-21 21:29 -0700
csiph-web