X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Fattributes.c;h=c4ae97c285928ed6e701cc6594fe0fefed5824c7;hb=d33c44dbdaa272f29bb849ad0b0be80356b9485a;hp=7fc546ee23771e7382232b88c92555289aa8df80;hpb=81579d9e9f994fb2908f50af41c3eb033d216e58;p=pspp diff --git a/src/data/attributes.c b/src/data/attributes.c index 7fc546ee23..c4ae97c285 100644 --- a/src/data/attributes.c +++ b/src/data/attributes.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2008, 2009, 2011 Free Software Foundation, Inc. + Copyright (C) 2008, 2009, 2011, 2012 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -23,6 +23,7 @@ #include "libpspp/array.h" #include "libpspp/hash-functions.h" +#include "libpspp/i18n.h" #include "gl/xalloc.h" @@ -223,8 +224,8 @@ attrset_lookup (struct attrset *set, const char *name) { struct attribute *attr; HMAP_FOR_EACH_WITH_HASH (attr, struct attribute, node, - hash_case_string (name, 0), &set->map) - if (!strcasecmp (attribute_get_name (attr), name)) + utf8_hash_case_string (name, 0), &set->map) + if (!utf8_strcasecmp (attribute_get_name (attr), name)) break; return attr; } @@ -237,7 +238,7 @@ attrset_add (struct attrset *set, struct attribute *attr) { const char *name = attribute_get_name (attr); assert (attrset_lookup (set, name) == NULL); - hmap_insert (&set->map, &attr->node, hash_case_string (name, 0)); + hmap_insert (&set->map, &attr->node, utf8_hash_case_string (name, 0)); } /* Deletes any attribute from SET that matches NAME @@ -299,3 +300,38 @@ attrset_next (const struct attrset *set, struct attrset_iterator *iterator) iterator->node = hmap_next (&set->map, iterator->node); return iterator_data (iterator); } + +static int +compare_attribute_by_name (const void *a_, const void *b_) +{ + const struct attribute *const *a = a_; + const struct attribute *const *b = b_; + + return strcmp ((*a)->name, (*b)->name); +} + +/* Allocates and returns an array of pointers to attributes + that is sorted by attribute name. The array has + 'attrset_count (SET)' elements. The caller is responsible for + freeing the array. */ +struct attribute ** +attrset_sorted (const struct attrset *set) +{ + if (set != NULL && attrset_count (set) > 0) + { + struct attribute **attrs; + struct attribute *attr; + size_t i; + + attrs = xmalloc (attrset_count (set) * sizeof *attrs); + i = 0; + HMAP_FOR_EACH (attr, struct attribute, node, &set->map) + attrs[i++] = attr; + assert (i == attrset_count (set)); + qsort (attrs, attrset_count (set), sizeof *attrs, + compare_attribute_by_name); + return attrs; + } + else + return NULL; +}