Skip to content
This repository
tag: RELEASE_1_1_0
Fetching contributors…

Cannot retrieve contributors at this time

file 293 lines (197 sloc) 6.548 kb
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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
/*
Copyright (C) 2001-2009, Parrot Foundation.
$Id$

=head1 NAME

src/pmc/packfiledirectory.pmc - PackfileDirectory PMC

=head1 DESCRIPTION

This class implements a PackfileDirectory object, a segment of the .pbc data
file used for listing the rest of the file's contents. Essentially this
segment is a container for all other segments within a Packfile.

See packfile.pmc for the toplevel Packfile interface, see packfilesegment.pmc
for the list of common methods every packfile segment pmc must implement; see
PDD13 for the design spec.


=head2 Methods

=over 4

=cut

*/

#include "parrot/parrot.h"

pmclass PackfileDirectory extends PackfileSegment {
    /* Directory is a hash of Segments */
    ATTR PMC *hash;

/*

=item C<void init()>

Initialize PackfileDirectory.

=cut

*/
    VTABLE void init() {
        Parrot_PackfileDirectory_attributes * attrs =
                mem_allocate_zeroed_typed(Parrot_PackfileDirectory_attributes);

        attrs->hash = pmc_new(interp, enum_class_Hash);

        PObj_custom_mark_destroy_SETALL(SELF);
        PMC_data(SELF) = attrs;
    }

/*

=item C<void mark()>

Marks the object as live.

=cut

*/

    VTABLE void mark() {
        Parrot_PackfileDirectory_attributes * attrs =
                PARROT_PACKFILEDIRECTORY(SELF);

        if (attrs->hash)
            pobject_lives(interp, (PObj *)attrs->hash);
    }

/*

=item C<void destroy()>

Destroys the PMC and frees all allocated memory.

=cut

*/

    VTABLE void destroy() {
        Parrot_PackfileDirectory_attributes * attrs =
                PARROT_PACKFILEDIRECTORY(SELF);

        if (attrs) {
            mem_sys_free(attrs);
            PMC_data(SELF) = NULL;
        }
    }


/*

=item C<void set_pointer(void *ptr)>

Initialize PackfileDirectory from PackFile_Directory.

=cut

*/

    VTABLE void set_pointer(void *ptr) {
        const PackFile_Directory * const pfd = (const PackFile_Directory *)ptr;
        PackFile_Segment *pfseg;
        PMC *segment;
        int pmc_type;
        unsigned int i;
        STRING *name;
        PMC *hash = PARROT_PACKFILEDIRECTORY(SELF)->hash;

        SUPER(ptr);

        /* Iterate over elements and create corresponded PMCs */
        for (i=0; i < pfd->num_segments; ++i) {
            pfseg = pfd->segments[i];

            switch (pfseg->type) {
                case PF_DIR_SEG:
                    pmc_type = enum_class_PackfileDirectory;
                    break;
                case PF_FIXUP_SEG:
                    pmc_type = enum_class_PackfileFixupTable;
                    break;
                case PF_CONST_SEG:
                    pmc_type = enum_class_PackfileConstantTable;
                    break;
                case PF_ANNOTATIONS_SEG:
                    pmc_type = enum_class_PackfileAnnotations;
                    break;
                case PF_BYTEC_SEG:
                case PF_UNKNOWN_SEG:
                case PF_DEBUG_SEG:
                default:
                    pmc_type = enum_class_PackfileRawSegment;
                    break;
            }

            segment = pmc_new(interp, pmc_type);
            name = Parrot_str_new(interp, pfseg->name, strlen(pfseg->name));
            VTABLE_set_pmc_keyed_str(interp, SELF, name, segment);

            /* Initialize internal PMC structure */
            VTABLE_set_pointer(interp, segment, pfseg);

        }
    }

/*

=item C<void *get_pointer()>

Creates PackFile for given directory.

=cut

*/

    VTABLE void *get_pointer() {
        Parrot_PackfileDirectory_attributes * attrs =
                PARROT_PACKFILEDIRECTORY(SELF);
        PackFile * pf = PackFile_new(interp, 0); /* dummy PackFile... */
        PackFile_Directory * pfdir = &pf->directory;
        PackFile_Segment * pfseg;
        PMC * seg;
        PMC * iter;
        STRING * name;
        STRING * res;

        /* Create Segments. Add to Directory with transfering ownership */
        iter = VTABLE_get_iter(interp, attrs->hash);
        while (VTABLE_get_bool(interp, iter)) {
            name = VTABLE_shift_string(interp, iter);
            seg = VTABLE_get_pmc_keyed_str(interp, attrs->hash, name);
            pfseg = (PackFile_Segment*)VTABLE_get_pointer(interp, seg);
            pfseg->pf = pf;
            pfseg->name = strdup(Parrot_string_cstring(interp, name));
            PackFile_add_segment(interp, pfdir, pfseg);
        }

        return pf;
    }

/*

=item C<INTVAL elements()>

Get the number of elements in the array.

=cut

*/
    VTABLE INTVAL elements() {
        return VTABLE_elements(interp, PARROT_PACKFILEDIRECTORY(SELF)->hash);
    }


/*

=item C<PMC *get_iter()>

Return a new iterator for the directory.

=cut

*/

    VTABLE PMC *get_iter() {
        return VTABLE_get_iter(interp, PARROT_PACKFILEDIRECTORY(SELF)->hash);
    }

/*

=item C<PMC *get_pmc_keyed_str(STRING *name)>

Search the array for a segment with the given name, and return it if it exists.

=cut

*/
    VTABLE PMC *get_pmc_keyed_str(STRING *name) {
        return VTABLE_get_pmc_keyed_str(interp,
                PARROT_PACKFILEDIRECTORY(SELF)->hash, name);
    }


/*

=item C<PMC *get_pmc_keyed(PMC *key)>

Fetch a keyed string value from the packfiledirectory object.

=cut

*/
    VTABLE PMC *get_pmc_keyed(PMC *key) {
        STRING *s_key = VTABLE_get_string(interp, key);
        return VTABLE_get_pmc_keyed_str(interp,
                PARROT_PACKFILEDIRECTORY(SELF)->hash, s_key);
    }


/*

=item C<void set_pmc_keyed_str(STRING *name, PMC *segment)>

Add a segment with the given name to the directory. If a segment with this
name already existed, it will be replaced with the new segment.

=cut

TODO: check type

*/
    VTABLE void set_pmc_keyed_str(STRING *name, PMC *segment) {
        Parrot_PCCINVOKE(interp, segment,
            Parrot_str_new_constant(interp, "set_directory"),
            "P->", SELF);
        VTABLE_set_pmc_keyed_str(interp,
            PARROT_PACKFILEDIRECTORY(SELF)->hash, name, segment);
    }

/*

=item C<PMC *set_pmc_keyed(PMC *key)>

Add a segment with the given name to the directory. Dispatches to
set_pmc_keyed_str.

=cut

*/

    VTABLE void set_pmc_keyed(PMC *key, PMC *segment) {
        STRING *s_key = VTABLE_get_string(interp, key);
        VTABLE_set_pmc_keyed_str(interp, SELF, s_key, segment);
    }


}
/*

=back

=cut

*/

/*
 * Local variables:
 * c-file-style: "parrot"
 * End:
 * vim: expandtab shiftwidth=4:
 */
Something went wrong with that request. Please try again.