what does this code do?

Q

qianz99

Hi I am not sure what this code does.
I have the following questions
1. where is the case?
2. #define TLV_INTEGER(name, octets) p->name = -1; Is it define a
function
TLV_INTEGER(name, octets) and return a -1? and similar questions on
other #define

3. in
#define PDU(name, id, fields) \
case id: { \
struct name *p = &pdu->u.name; \
pdu->type_name = #name; \
fields \
p->command_id = type; \
p->sequence_number = seq_no; \
} break;

Why do I need a '"\" and what does it mean by "#name"

Thank you very much!


switch (type) {
#define OPTIONAL_BEGIN
#define TLV_INTEGER(name, octets) p->name = -1;
#define TLV_NULTERMINATED(name, max_len) p->name = NULL;
#define TLV_OCTETS(name, min_len, max_len) p->name = NULL;
#define OPTIONAL_END
#define INTEGER(name, octets) p->name = 0;
#define NULTERMINATED(name, max_octets) p->name = NULL;
#define OCTETS(name, field_giving_octetst) p->name = NULL;
#define PDU(name, id, fields) \
case id: { \
struct name *p = &pdu->u.name; \
pdu->type_name = #name; \
fields \
p->command_id = type; \
p->sequence_number = seq_no; \
} break;
default:
error(0, "Unknown SMPP_PDU type, internal error.");
gw_free(pdu);


return NULL;
}
 
E

Eric Sosman

Hi I am not sure what this code does.

It switches on `type', goes to the default: label,
calls error() and gw_free(), and returns NULL from the
function that contains it.
I have the following questions
1. where is the case?

The only case in this switch statement is the default.
2. #define TLV_INTEGER(name, octets) p->name = -1; Is it define a
function
TLV_INTEGER(name, octets) and return a -1? and similar questions on
other #define

#define defines a preprocessor macro.
3. in
#define PDU(name, id, fields) \
case id: { \
struct name *p = &pdu->u.name; \
pdu->type_name = #name; \
fields \
p->command_id = type; \
p->sequence_number = seq_no; \
} break;

Why do I need a '"\" and what does it mean by "#name"

Each backslash-newline pair ends a line of source code
but does not end the macro definition (without the backslash,
the macro definition would end at the newline). So the body
of the PDU macro contains not only what's on the #define line
itself, but all of the following seven lines as well. The
definition ends after the eighth line because the newline
at the end of that line is not preceded by a backslash.

As for #name, see Question 11.18 in the comp.lang.c
Frequently Asked Questions (FAQ) list said:
[code snipped; see up-thread]
 
Q

qianz99

Thank you very much! I will find a preprocessor macro tutorial.
However, I'd like to ask another question.
If switch only goes to the default value, why there are so many
#define in front of the default value. Does it mean it goes to the
macros one by one?

Thank you very much!

Eric said:
Hi I am not sure what this code does.

It switches on `type', goes to the default: label,
calls error() and gw_free(), and returns NULL from the
function that contains it.
I have the following questions
1. where is the case?

The only case in this switch statement is the default.
2. #define TLV_INTEGER(name, octets) p->name = -1; Is it define a
function
TLV_INTEGER(name, octets) and return a -1? and similar questions on
other #define

#define defines a preprocessor macro.
3. in
#define PDU(name, id, fields) \
case id: { \
struct name *p = &pdu->u.name; \
pdu->type_name = #name; \
fields \
p->command_id = type; \
p->sequence_number = seq_no; \
} break;

Why do I need a '"\" and what does it mean by "#name"

Each backslash-newline pair ends a line of source code
but does not end the macro definition (without the backslash,
the macro definition would end at the newline). So the body
of the PDU macro contains not only what's on the #define line
itself, but all of the following seven lines as well. The
definition ends after the eighth line because the newline
at the end of that line is not preceded by a backslash.

As for #name, see Question 11.18 in the comp.lang.c
Frequently Asked Questions (FAQ) list said:
[code snipped; see up-thread]
 
E

Eric Sosman

Putting the response before the stimulus.
What is "top-posting?"
Please don't top-post.

Thank you very much! I will find a preprocessor macro tutorial.
However, I'd like to ask another question.
If switch only goes to the default value, why there are so many
#define in front of the default value. Does it mean it goes to the
macros one by one?

You seem not to understand what preprocessor macros are.
I suggest you find a good C textbook or tutorial and start
reading it. Usenet is a fine medium for focused questions
and discussions (and flames), but is not effective as a means
for mass transfer of basic knowledge.
 
B

Ben Bacarisse

(e-mail address removed) writes:

Please don't "top post". I've fixed it by moving your comments below
the text they refer to. I've also snipped some text as you should
have done...
Thank you very much! I will find a preprocessor macro tutorial.
However, I'd like to ask another question.
If switch only goes to the default value, why there are so many
#define in front of the default value. Does it mean it goes to the
macros one by one?

No, the code just defines some macros that seem not to be used. The
switch has only one case -- the default one.

You will have to read up on what #define does, but one of the macros
(PDU) is was obviously deigned to allow switch cases to be added. It
looks odd that it is not used, so I searched for similar code and
found a version of what you posted with one crucial difference:

switch (type) {
#define OPTIONAL_BEGIN
#define TLV_INTEGER(name, octets) p->name = -1;
#define TLV_NULTERMINATED(name, max_len) p->name = NULL;
#define TLV_OCTETS(name, min_len, max_len) p->name = NULL;
#define OPTIONAL_END
#define INTEGER(name, octets) p->name = 0;
#define NULTERMINATED(name, max_octets) p->name = NULL;
#define OCTETS(name, field_giving_octetst) p->name = NULL;
#define PDU(name, id, fields) \
case id: { \
struct name *p = &pdu->u.name; \
pdu->type_name = #name; \
fields \
p->command_id = type; \
p->sequence_number = seq_no; \
} break;
#include "smpp_pdu.def"
default:
error(0, "Unknown SMPP_PDU type, internal error.");
gw_free(pdu);
return NULL;
}

Just before the default case, the file "smpp_pdu.def" is included.
This file uses all the macros defined here to add cases to the switch
statement. Obviously the code you posted is designed with this
possibility in mind.
 
C

CBFalconer

Thank you very much! I will find a preprocessor macro tutorial.
However, I'd like to ask another question. If switch only goes to
the default value, why there are so many #define in front of the
default value. Does it mean it goes to the macros one by one?

Please do not top-post. Your answer belongs after (or intermixed
with) the quoted material to which you reply, after snipping all
irrelevant material. See the following links:

--
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/> (taming google)
<http://members.fortunecity.com/nnqweb/> (newusers)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top