2 * Copyright (C) 2009, 2014 J. Ali Harlow <ali@juiblex.co.uk>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 struct plover_vector *plover_vector_new(void)
26 struct plover_vector *vector;
27 vector=malloc(sizeof(*vector));
30 vector->strings=calloc(vector->alloc,sizeof(char *));
34 struct plover_vector *plover_vector_dup(struct plover_vector *old)
37 struct plover_vector *vector;
38 vector=malloc(sizeof(*vector));
40 vector->alloc=old->alloc;
41 vector->strings=calloc(vector->alloc,sizeof(char *));
42 for(i=0;i<old->len;i++)
43 vector->strings[i]=strdup(old->strings[i]);
44 vector->strings[i]=NULL;
48 void plover_vector_append(struct plover_vector *vector,const char *str)
50 if (++(vector->len)>=vector->alloc)
53 vector->strings=realloc(vector->strings,vector->alloc*sizeof(char *));
55 vector->strings[vector->len-1]=strdup(str);
56 vector->strings[vector->len]=NULL;
59 int plover_vector_contains(struct plover_vector *vector,const char *str)
62 for(i=0;i<vector->len;i++)
63 if (!strcmp(vector->strings[i],str))
68 int plover_vector_remove(struct plover_vector *vector,const char *str)
71 for(i=0;i<vector->len;i++)
72 if (!strcmp(vector->strings[i],str))
74 free(vector->strings[i]);
76 vector->strings[i]=vector->strings[vector->len];
77 vector->strings[vector->len]=NULL;
83 static int string_compar(const void *p1,const void *p2)
85 return strcmp(*(char * const *)p1,*(char * const *)p2);
88 void plover_vector_sort(struct plover_vector *vector)
90 qsort(vector->strings,vector->len,sizeof(char *),string_compar);
93 char *plover_vector_format_for_display(struct plover_vector *vector)
98 return strdup("none");
99 else if (vector->len==1)
100 return strdup(vector->strings[0]);
103 len=(vector->len-1)*2+5+1;
104 for(i=0;i<vector->len;i++)
105 len+=strlen(vector->strings[i]);
107 strcpy(s,vector->strings[0]);
109 for(i=1;i<vector->len-1;i++)
113 strcpy(p,vector->strings[i]);
118 strcpy(p,vector->strings[i]);
123 void plover_vector_free(struct plover_vector *vector)
126 for(i=0;i<vector->len;i++)
127 free(vector->strings[i]);
128 free(vector->strings);