File: member.xs

package info (click to toggle)
libconvert-binary-c-perl 0.74-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 9,100 kB
  • ctags: 21,416
  • sloc: ansic: 63,666; perl: 18,582; yacc: 2,143; makefile: 44
file content (136 lines) | stat: -rw-r--r-- 3,201 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
################################################################################
#
# $Project: /Convert-Binary-C $
# $Author: mhx $
# $Date: 2009/03/15 04:10:43 +0100 $
# $Revision: 11 $
# $Source: /xsubs/member.xs $
#
################################################################################
#
# Copyright (c) 2002-2009 Marcus Holland-Moritz. All rights reserved.
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
################################################################################


################################################################################
#
#   METHOD: member
#
#   WRITTEN BY: Marcus Holland-Moritz             ON: Jan 2002
#   CHANGED BY:                                   ON:
#
################################################################################

void
CBC::member(type, offset = NULL)
  const char *type
  SV *offset

  PREINIT:
    CBC_METHOD(member);
    MemberInfo mi;
    int have_offset, off;

  PPCODE:
    off = (have_offset = DEFINED(offset)) ? SvIV(offset) : 0;

    CT_DEBUG_METHOD2("'%s', %d", type, off);

    CHECK_PARSE_DATA;
    CHECK_VOID_CONTEXT;

    NEED_PARSE_DATA;

    if (!get_member_info(aTHX_ THIS, type, &mi, 0))
      Perl_croak(aTHX_ "Cannot find '%s'", type);

    check_allowed_types(aTHX_ &mi, method, ALLOW_STRUCTS
                                         | ALLOW_UNIONS
                                         | ALLOW_ARRAYS);

    if (mi.flags)
    {
      u_32 flags = mi.flags;

      /* bitfields are not a problem without offset given */
      if (!have_offset)
        flags &= ~T_HASBITFIELD;

      WARN_FLAGS(type, flags);
    }

    if (have_offset)
    {
      if (off < 0 || off >= (int) mi.size)
        Perl_croak(aTHX_ "Offset %d out of range (0 <= offset < %d)", off, mi.size);

      if (GIMME_V == G_ARRAY)
      {
        ListIterator li;
        GMSInfo info;
        SV     *member;
        int     count;

        info.hit = LL_new();
        info.off = LL_new();
        info.pad = LL_new();

        (void) get_member_string(aTHX_ &mi, off, &info);

        count = LL_count(info.hit)
              + LL_count(info.off)
              + LL_count(info.pad);

        EXTEND(SP, count);

        LL_foreach(member, li, info.hit)
          PUSHs(member);

        LL_foreach(member, li, info.off)
          PUSHs(member);

        LL_foreach(member, li, info.pad)
          PUSHs(member);

        LL_destroy(info.hit, NULL);
        LL_destroy(info.off, NULL);
        LL_destroy(info.pad, NULL);

        XSRETURN(count);
      }
      else
      {
        SV *member = get_member_string(aTHX_ &mi, off, NULL);
        PUSHs(member);
        XSRETURN(1);
      }
    }
    else
    {
      LinkedList list;
      SV *member;
      int count;

      list = GIMME_V == G_ARRAY ? LL_new() : NULL;
      count = get_all_member_strings(aTHX_ &mi, list);

      if (GIMME_V == G_ARRAY)
      {
        ListIterator li;

        EXTEND(SP, count);

        LL_foreach(member, li, list)
          PUSHs(member);

        LL_destroy(list, NULL);

        XSRETURN(count);
      }
      else
        XSRETURN_IV(count);
    }