summaryrefslogtreecommitdiff
path: root/perl/header.xs
blob: d132a7884c6193d13826b3634b03347f2ba63494 (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211

MODULE = rpm		PACKAGE = Header		PREFIX = Header

AV *
HeaderItemByValRef(header, item)
  Header	header
  int		item
  PREINIT:
	int_32 count, type;
	int rc;
	void * value;
	char ** src;
	AV * array;
  CODE:
	rc = headerGetEntry(header, item, &type, &value, &count);
	array = newAV();
	if (rc != 0) {
	  switch(type) {
	    case RPM_CHAR_TYPE:
		while (count-- > 0) {
		    av_push(array, newSViv((char) (int) value));
		    value++;
		}
	        break;
	    case RPM_INT8_TYPE:
		while (count-- > 0) {
		    av_push(array, newSViv((int_8) (int) value));
		    value++;
		}
	        break;
	    case RPM_INT16_TYPE:
		while (count-- > 0) {
		    av_push(array, newSViv((int_16) (int) value));
		    value++;
		}
	        break;
	    case RPM_INT32_TYPE:
		while (count-- > 0) {		    
		    av_push(array, newSViv((int_32)value));
		    value++;
		}
	        break;
	    case RPM_STRING_TYPE:
	        av_push(array, newSVpv((char *)value, 0));
   	        break;
	    case RPM_BIN_TYPE:
	        /* XXX: this looks mostly unused - how do we deal with it? */
	        break;
	    case RPM_STRING_ARRAY_TYPE:
	    case RPM_I18NSTRING_TYPE:
		src = (char **) value;
		while (count--) {
		  av_push(array, newSVpv(*src++, 0));
		}
	        free(value);
	        break;
	  }
	}
	RETVAL = array;
  OUTPUT:
	RETVAL

AV *
HeaderItemByNameRef(header, tag)
  Header	header
  const char *	tag
  PREINIT:
	int_32 count, type, item = -1;
	int rc, i;
	void * value;
	char ** src;
	AV * array;
  CODE:
	/* walk first through the list of items and get the proper value */
	for (i = 0; i < rpmTagTableSize; i++) {
	  if (rpmTagTable[i].name != NULL && strcasecmp(tag, rpmTagTable[i].name + 7) == 0) {
	    item = rpmTagTable[i].val;
	    break;
	  }
	}
	rc = headerGetEntry(header, item, &type, &value, &count);
	array = newAV();
	if (rc != 0) {
	  switch(type) {
	    case RPM_CHAR_TYPE:
		while (count-- > 0) {
		    av_push(array, newSViv((char) (int) value));
		    value++;
		}
	        break;
	    case RPM_INT8_TYPE:
		while (count-- > 0) {
		    av_push(array, newSViv((int_8) (int) value));
		    value++;
		}
	        break;
	    case RPM_INT16_TYPE:
		while (count-- > 0) {
		    av_push(array, newSViv((int_16) (int) value));
		    value++;
		}
	        break;
	    case RPM_INT32_TYPE:
		while (count-- > 0) {		    
		    av_push(array, newSViv((int_32)value));
		    value++;
		}
	        break;
	    case RPM_STRING_TYPE:
	        av_push(array, newSVpv((char *)value, 0));
   	        break;
	    case RPM_BIN_TYPE:
	        /* XXX: this looks mostly unused - how do we deal with it? */
	        break;
	    case RPM_STRING_ARRAY_TYPE:
	    case RPM_I18NSTRING_TYPE:
		src = (char **) value;
		while (count--) {
		  av_push(array, newSVpv(*src++, 0));
		}
	        free(value);
	        break;
	  }
	}
	RETVAL = array;
  OUTPUT:
	RETVAL


HV *
HeaderListRef(header)
  Header	header
  PREINIT:
	HeaderIterator iterator;
	int_32 tag, type, count;
	void *value;
  CODE:
	RETVAL = newHV();
	iterator = headerInitIterator(header);
	while (headerNextIterator(iterator, &tag, &type, &value, &count)) {
	  SV  ** sv;
	  AV   * array;
          char ** src;
	  char * tagStr = tagName(tag);
	  array = newAV();
	  switch(type) {
	    case RPM_CHAR_TYPE:
		while (count-- > 0) {
		    av_push(array, newSViv((char) (int) value));
		    value++;
		}
	        break;
	    case RPM_INT8_TYPE:
		while (count-- > 0) {
		    av_push(array, newSViv((int_8) (int) value));
		    value++;
		}
	        break;
	    case RPM_INT16_TYPE:
		while (count-- > 0) {
		    av_push(array, newSViv((int_16) (int) value));
		    value++;
		}
	        break;
	    case RPM_INT32_TYPE:
		while (count-- > 0) {		    
		    av_push(array, newSViv((int_32)value));
		    value++;
		}
	        break;
	    case RPM_STRING_TYPE:
	        av_push(array, newSVpv((char *)value, 0));
   	        break;
	    case RPM_BIN_TYPE:
	        /* XXX: this looks mostly unused - how do we deal with it? */
	        break;
	    case RPM_STRING_ARRAY_TYPE:
	    case RPM_I18NSTRING_TYPE:
		/* we have to build an array first */
		src = (char **) value;
		while (count--) {
		  av_push(array, newSVpv(*src++, 0));
		}
	        free(value);
	        break;
	  }
	  sv = hv_store(RETVAL, tagStr, strlen(tagStr), newRV_inc((SV*)array), 0);
	}
	headerFreeIterator(iterator);
  OUTPUT:
	RETVAL

AV * 
HeaderTagsRef(header) 
  Header      header 
  PREINIT: 
        HeaderIterator iterator; 
        int_32 tag, type;
        void *value; 
  CODE: 
        RETVAL = newAV(); 
	iterator = headerInitIterator(header); 
	while (headerNextIterator(iterator, &tag, &type, &value, NULL)) { 
	  av_push(RETVAL, newSVpv(tagName(tag), 0));
	  if (type == RPM_STRING_ARRAY_TYPE || type == RPM_I18NSTRING_TYPE)
	    free(value);
	}
	headerFreeIterator(iterator);      
  OUTPUT:
	RETVAL