plover/vector.c
author J. Ali Harlow <ali@juiblex.co.uk>
Tue Jun 29 10:09:34 2021 +0100 (2021-06-29)
changeset 108 b3d8e196dac8
permissions -rw-r--r--
Added tag 0.6.1 for changeset 6ae203c8b28d
     1 /*
     2  * Copyright (C) 2009, 2014  J. Ali Harlow <ali@juiblex.co.uk>
     3  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 
    19 #include <stdlib.h>
    20 #include <string.h>
    21 #include "config.h"
    22 #include "plover.h"
    23 
    24 struct plover_vector *plover_vector_new(void)
    25 {
    26     struct plover_vector *vector;
    27     vector=malloc(sizeof(*vector));
    28     vector->len=0;
    29     vector->alloc=16;
    30     vector->strings=calloc(vector->alloc,sizeof(char *));
    31     return vector;
    32 }
    33 
    34 struct plover_vector *plover_vector_dup(struct plover_vector *old)
    35 {
    36     int i;
    37     struct plover_vector *vector;
    38     vector=malloc(sizeof(*vector));
    39     vector->len=old->len;
    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;
    45     return vector;
    46 }
    47 
    48 void plover_vector_append(struct plover_vector *vector,const char *str)
    49 {
    50     if (++(vector->len)>=vector->alloc)
    51     {
    52 	vector->alloc*=2;
    53 	vector->strings=realloc(vector->strings,vector->alloc*sizeof(char *));
    54     }
    55     vector->strings[vector->len-1]=strdup(str);
    56     vector->strings[vector->len]=NULL;
    57 }
    58 
    59 int plover_vector_contains(struct plover_vector *vector,const char *str)
    60 {
    61     int i;
    62     for(i=0;i<vector->len;i++)
    63 	if (!strcmp(vector->strings[i],str))
    64 	    return 1;
    65     return 0;
    66 }
    67 
    68 int plover_vector_remove(struct plover_vector *vector,const char *str)
    69 {
    70     int i;
    71     for(i=0;i<vector->len;i++)
    72 	if (!strcmp(vector->strings[i],str))
    73 	{
    74 	    free(vector->strings[i]);
    75 	    vector->len--;
    76 	    vector->strings[i]=vector->strings[vector->len];
    77 	    vector->strings[vector->len]=NULL;
    78 	    return 1;
    79 	}
    80     return 0;
    81 }
    82 
    83 static int string_compar(const void *p1,const void *p2)
    84 {
    85     return strcmp(*(char * const *)p1,*(char * const *)p2);
    86 }
    87 
    88 void plover_vector_sort(struct plover_vector *vector)
    89 {
    90     qsort(vector->strings,vector->len,sizeof(char *),string_compar);
    91 }
    92 
    93 char *plover_vector_format_for_display(struct plover_vector *vector)
    94 {
    95     int i,len;
    96     char *s,*p;
    97     if (!vector->len)
    98 	return strdup("none");
    99     else if (vector->len==1)
   100 	return strdup(vector->strings[0]);
   101     else
   102     {
   103 	len=(vector->len-1)*2+5+1;
   104 	for(i=0;i<vector->len;i++)
   105 	    len+=strlen(vector->strings[i]);
   106 	s=malloc(len);
   107 	strcpy(s,vector->strings[0]);
   108 	p=s+strlen(s);
   109 	for(i=1;i<vector->len-1;i++)
   110 	{
   111 	    *p++=',';
   112 	    *p++=' ';
   113 	    strcpy(p,vector->strings[i]);
   114 	    p+=strlen(p);
   115 	}
   116 	strcpy(p," and ");
   117 	p+=5;
   118 	strcpy(p,vector->strings[i]);
   119 	return s;
   120     }
   121 }
   122 
   123 void plover_vector_free(struct plover_vector *vector)
   124 {
   125     int i;
   126     for(i=0;i<vector->len;i++)
   127 	free(vector->strings[i]);
   128     free(vector->strings);
   129     free(vector);
   130 }