Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#include "keywordset.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "xmalloc.h"
#define KEYWORDSET_INITIAL_SLOTS (4-2)
struct keywordset {
int nr_slots;
int used;
char **names;
};
static int find_name(struct keywordset *kws, char *name, size_t len) {
int i;
size_t j;
for ( i = 0; i < kws->used ; i++ ) {
j = 0;
while(1) {
if (kws->names[i][j] == 0)
break;
if (kws->names[i][j] != name[j])
break;
j++;
if (j==len) {
if (kws->names[i][j] == 0)
return i;
else
break;
}
}
}
return -1;
}
static void expand(struct keywordset *kws) {
int new_slots=(kws->nr_slots+2)*2-2;
kws->names=xrealloc(kws->names,new_slots*sizeof(*kws->names));
kws->nr_slots=new_slots;
}
static void add_name(struct keywordset *kws, char *name, size_t len) {
int i=find_name(kws, name, len);
if (i>=0) {
free(kws->names[i]);
kws->names[i] = xstrndup(name, len);
} else {
if (kws->used == kws->nr_slots)
expand(kws);
kws->names[kws->used++] = xstrndup(name, len);
}
}
static void remove_name(struct keywordset *kws, char *name, size_t len) {
int i=find_name(kws, name, len);
if (i>=0) {
free(kws->names[i]);
memmove(&(kws->names[i]), &(kws->names[i+1]), (kws->used-i-1)*sizeof(*kws->names));
kws->used--;
}
}
void keywordset_purge(struct keywordset *kws) {
int i;
for ( i = 0 ; i < kws->used ; i++)
free(kws->names[i]);
kws->used = 0;
}
enum PHASE {
PHASE_SET,
PHASE_UPDATE,
};
static void keywordset_update_phase(struct keywordset *kws, char *input, enum PHASE phase) {
char *c=input;
char *name_start;
char action;
int purged=0;
while (*c) {
while (*c && isspace(*c))
c++;
if (*c == '+' || *c == '-') {
action = *c;
c++;
} else {
action = ' ';
}
if (*c) {
name_start=c++;
while (*c && !isspace(*c))
c++;
if (phase == PHASE_SET && action==' ') {
if (!purged) {
keywordset_purge(kws);
purged = 1;
}
add_name(kws, name_start, c-name_start);
} else if (phase == PHASE_UPDATE) {
if (action == '+')
add_name(kws, name_start, c-name_start);
else if (action == '-')
remove_name(kws, name_start, c-name_start);
}
}
}
}
void keywordset_update(struct keywordset *kws, char *input) {
keywordset_update_phase(kws, input, PHASE_SET);
keywordset_update_phase(kws, input, PHASE_UPDATE);
}
void keywordset_add(struct keywordset *kws, char *input) {
char *c=input;
char *name_start;
while (*c) {
while (*c && isspace(*c))
c++;
if (*c) {
name_start=c++;
while (*c && !isspace(*c))
c++;
add_name(kws, name_start, c-name_start);
}
}
}
struct keywordset *keywordset_new(char *input) {
struct keywordset *kws = xmalloc(sizeof(*kws));
kws->nr_slots = KEYWORDSET_INITIAL_SLOTS;
kws->used = 0;
kws->names = xmalloc(KEYWORDSET_INITIAL_SLOTS*sizeof(*kws->names));
if (input)
keywordset_update(kws, input);
return kws;
}
static int cmp(const void *a, const void *b) {
return strcmp(*(char **)a, *(char **)b);
}
char *keywordset_get(struct keywordset *kws) {
char **names=xmalloc(kws->used * sizeof(*names));
memcpy(names, kws->names, kws->used * sizeof(*names));
qsort(names, kws->used, sizeof(*names), cmp);
size_t len = 0;
int i;
for (i=0; i<kws->used; i++) {
len += strlen(names[i]);
}
size_t outlen = len + (kws->used >= 2 ? kws->used-1 : 0);
char *out=xmalloc(outlen + 1 );
char *p=out;
for ( i = 0 ; i < kws->used ; i++) {
p=stpcpy(p, names[i]);
*p++ = ' ';
}
out[outlen] = 0;
free(names);
return(out);
}
int keywordset_ismember(struct keywordset *kws, char *name) {
if (find_name(kws, name, strlen(name)) >= 0)
return 1;
else
return 0;
}
void keywordset_free(struct keywordset *kws) {
int i;
for ( i = 0 ; i < kws->used ; i++)
free(kws->names[i]);
free(kws->names);
free(kws);
}