+void expand_settings_array2(struct settings_array *settings, int offset, NMEM nmem)
+{
+ assert(offset >= 0);
+ assert(settings);
+ if (offset >= settings->num_settings)
+ {
+ int i, n_num = offset + 10;
+ struct setting **n_ar = nmem_malloc(nmem, n_num * sizeof(*n_ar));
+ for (i = 0; i < settings->num_settings; i++)
+ n_ar[i] = settings->settings[i];
+ for (; i < n_num; i++)
+ n_ar[i] = 0;
+ settings->num_settings = n_num;
+ settings->settings = n_ar;
+ }
+}
+
+static void update_settings(struct setting *set, struct settings_array *settings, int offset, NMEM nmem)
+{
+ struct setting **sp;
+ yaz_log(YLOG_DEBUG, "update service settings offset %d with %s=%s", offset, set->name, set->value);
+ expand_settings_array2(settings, offset, nmem);
+
+ // First we determine if this setting is overriding any existing settings
+ // with the same name.
+ assert(offset < settings->num_settings);
+ for (sp = &settings->settings[offset]; *sp; )
+ if (!strcmp((*sp)->name, set->name))
+ {
+ if ((*sp)->precedence < set->precedence)
+ {
+ // We discard the value (nmem keeps track of the space)
+ *sp = (*sp)->next; // unlink value from existing setting
+ }
+ else if ((*sp)->precedence > set->precedence)
+ {
+ // Db contains a higher-priority setting. Abort search
+ break;
+ }
+ else if (zurl_wildcard((*sp)->target) > zurl_wildcard(set->target))
+ {
+ // target-specific value trumps wildcard. Delete.
+ *sp = (*sp)->next; // unlink.....
+ }
+ else if (zurl_wildcard((*sp)->target) < zurl_wildcard(set->target))
+ // Db already contains higher-priority setting. Abort search
+ break;
+ else
+ sp = &(*sp)->next;
+ }
+ else
+ sp = &(*sp)->next;
+ if (!*sp) // is null when there are no higher-priority settings, so we add one
+ {
+ struct setting *new = nmem_malloc(nmem, sizeof(*new));
+ memset(new, 0, sizeof(*new));
+ new->precedence = set->precedence;
+ new->target = nmem_strdup_null(nmem, set->target);
+ new->name = nmem_strdup_null(nmem, set->name);
+ new->value = nmem_strdup_null(nmem, set->value);
+ new->next = settings->settings[offset];
+ settings->settings[offset] = new;
+ }
+}
+
+