[ovs-dev] [PATCH 2/2] idl: New helpers for accessing string maps.

Ben Pfaff blp at nicira.com
Wed Mar 21 16:27:15 UTC 2012


On Tue, Mar 20, 2012 at 07:24:46PM -0700, Reid Price wrote:
> >
> > +        # String Map Helpers.
> > +        for columnName, column in sorted(table.columns.iteritems()):
> > +            if not is_string_map(column):
> > +                continue
> > +
> > +            print
> > +            print "const char *"
> > +            print "%(s)s_get_%(c)s_value(const struct %(s)s *row," \
> > +                    " const char *key, const char *default_value)" \
> > +                    % {'s': structName, 'c': columnName}
> > +            print "{"
> > +            print "    char **keys = row->key_%s;" % columnName
> > +            print "    char **values = row->value_%s;" % columnName
> > +            print "    size_t n = row->n_%s;" % columnName
> > +            print "    size_t i;"
> > +            print
> > +            print "    assert(inited);"
> > +            print "    for (i = 0; i < n; i++) {"
> > +            print "        if (!strcmp(keys[i], key)) {"
> > +            print "            return values[i];"
> > +            print "        }"
> > +            print "    }"
> > +            print "    return default_value;"
> > +            print "}"
> > +
> >
> 
> You could do this as a single long string and apply a dict formatting at
> the end of it, don't know whether there is an existing standard you've been
> doing for printing the generated code.

I'm not sure we really have a policy per se.  I've been using long
strings where they seem convenient; maybe this is a good spot.



More information about the dev mailing list