what does this code do?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • qianz99@gmail.com

    what does this code do?

    Hi I am not sure what this code does.
    I have the following questions
    1. where is the case?
    2. #define TLV_INTEGER(nam e, octets) p->name = -1; Is it define a
    function
    TLV_INTEGER(nam e, 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_numbe r = 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(nam e, octets) p->name = -1;
    #define TLV_NULTERMINAT ED(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(n ame, max_octets) p->name = NULL;
    #define OCTETS(name, field_giving_oc tetst) 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_numbe r = seq_no; \
    } break;
    default:
    error(0, "Unknown SMPP_PDU type, internal error.");
    gw_free(pdu);


    return NULL;
    }
  • Eric Sosman

    #2
    Re: what does this code do?

    qianz99@gmail.c om wrote:
    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(nam e, octets) p->name = -1; Is it define a
    function
    TLV_INTEGER(nam e, 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_numbe r = 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 <http://www.c-faq.com/>.
    [code snipped; see up-thread]
    --
    Eric.Sosman@sun .com

    Comment

    • qianz99@gmail.com

      #3
      Re: what does this code do?

      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 Sosman wrote:
      qianz99@gmail.c om wrote:
      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(nam e, octets) p->name = -1; Is it define a
      function
      TLV_INTEGER(nam e, 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_numbe r = 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 <http://www.c-faq.com/>.
      >
      [code snipped; see up-thread]
      >
      --
      Eric.Sosman@sun .com

      Comment

      • CBFalconer

        #4
        Re: what does this code do?

        qianz99@gmail.c om wrote:
        >
        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.htm l>
        <http://cfaj.freeshell. org/google/ (taming google)
        <http://members.fortune city.com/nnqweb/ (newusers)


        --
        Posted via a free Usenet account from http://www.teranews.com

        Comment

        Working...