1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
/*===========================================================================
*
* PUBLIC DOMAIN NOTICE
* National Center for Biotechnology Information
*
* This software/database is a "United States Government Work" under the
* terms of the United States Copyright Act. It was written as part of
* the author's official duties as a United States Government employee and
* thus cannot be copyrighted. This software/database is freely available
* to the public for use. The National Library of Medicine and the U.S.
* Government have not placed any restriction on its use or reproduction.
*
* Although all reasonable efforts have been taken to ensure the accuracy
* and reliability of the software and data, the NLM and the U.S.
* Government do not and cannot warrant the performance or results that
* may be obtained by using this software or data. The NLM and the U.S.
* Government disclaim all warranties, express or implied, including
* warranties of performance, merchantability or fitness for any particular
* purpose.
*
* Please cite the author in any work or product based on this material.
*
* ===========================================================================
*
*/
#include "namelist_tools.h"
#include <sysalloc.h>
#include <stdlib.h>
#include <string.h>
rc_t nlt_make_namelist_from_string( const KNamelist **list, const char * src )
{
VNamelist *v_names;
rc_t rc = VNamelistMake ( &v_names, 5 );
if ( rc == 0 )
{
char * s = string_dup_measure ( src, NULL );
if ( s )
{
uint32_t str_begin = 0;
uint32_t str_end = 0;
char c;
do
{
c = s[ str_end ];
if ( c == ',' || c == 0 )
{
if ( str_begin < str_end )
{
char c_temp = c;
s[ str_end ] = 0;
rc = VNamelistAppend ( v_names, &(s[str_begin]) );
s[ str_end ] = c_temp;
}
str_begin = str_end + 1;
}
str_end++;
} while ( c != 0 && rc == 0 );
free( s );
}
if ( rc == 0 )
rc = VNamelistToConstNamelist ( v_names, list );
VNamelistRelease( v_names );
}
return rc;
}
int nlt_strcmp( const char* s1, const char* s2 )
{
size_t n1 = string_size ( s1 );
size_t n2 = string_size ( s2 );
return string_cmp
( s1, n1, s2, n2, ( n1 < n2 ) ? ( uint32_t ) n2 : ( uint32_t ) n1 );
}
bool nlt_is_name_in_namelist( const KNamelist *list, const char *name_to_find )
{
uint32_t count, idx;
bool res = false;
if ( list == NULL || name_to_find == NULL )
return res;
if ( KNamelistCount( list, &count ) == 0 )
{
for ( idx = 0; idx < count && res == false; ++idx )
{
const char *item_name;
if ( KNamelistGet( list, idx, &item_name ) == 0 )
{
if ( nlt_strcmp( item_name, name_to_find ) == 0 )
res = true;
}
}
}
return res;
}
/*
- list1 and list2 containts strings
- if one of the strings in list2 is contained ( partial match, strstr() )
in one of the strings in list1 the function returns true...
*/
bool nlt_namelist_intersect( const KNamelist *list1, const KNamelist *list2 )
{
uint32_t count1;
bool res = false;
if ( list1 == NULL || list2 == NULL )
return res;
if ( KNamelistCount( list1, &count1 ) == 0 )
{
uint32_t idx1;
for ( idx1 = 0; idx1 < count1 && res == false; ++idx1 )
{
const char *string1;
if ( KNamelistGet( list1, idx1, &string1 ) == 0 )
{
uint32_t count2;
if ( KNamelistCount( list2, &count2 ) == 0 )
{
uint32_t idx2;
for ( idx2 = 0; idx2 < count2 && res == false; ++idx2 )
{
const char *string2;
if ( KNamelistGet( list2, idx2, &string2 ) == 0 )
{
if ( strstr( string1, string2 ) != NULL )
res = true;
}
}
}
}
}
}
return res;
}
rc_t nlt_remove_names_from_namelist( const KNamelist *source,
const KNamelist **dest, const KNamelist *to_remove )
{
rc_t rc = 0;
uint32_t count;
if ( source == NULL || dest == NULL || to_remove == NULL )
return RC( rcVDB, rcNoTarg, rcConstructing, rcParam, rcNull );
*dest = NULL;
rc = KNamelistCount( source, &count );
if ( rc == 0 && count > 0 )
{
VNamelist *cleaned;
rc = VNamelistMake ( &cleaned, count );
if ( rc == 0 )
{
uint32_t idx;
for ( idx = 0; idx < count && rc == 0; ++idx )
{
const char *s;
rc = KNamelistGet( source, idx, &s );
if ( rc == 0 )
{
if ( !nlt_is_name_in_namelist( to_remove, s ) )
rc = VNamelistAppend ( cleaned, s );
}
rc = VNamelistToConstNamelist ( cleaned, dest );
}
}
}
return rc;
}
rc_t nlt_remove_strings_from_namelist( const KNamelist *source,
const KNamelist **dest, const char *items_to_remove )
{
rc_t rc = 0;
const KNamelist *to_remove;
if ( source == NULL || dest == NULL || items_to_remove == NULL )
return RC( rcVDB, rcNoTarg, rcConstructing, rcParam, rcNull );
rc = nlt_make_namelist_from_string( &to_remove, items_to_remove );
if ( rc == 0 )
{
rc = nlt_remove_names_from_namelist( source, dest, to_remove );
KNamelistRelease( to_remove );
}
return rc;
}
bool nlt_compare_namelists( const KNamelist *nl1, const KNamelist *nl2, uint32_t * found )
{
bool res = false;
if ( nl1 != NULL && nl2 != NULL )
{
uint32_t count_1;
rc_t rc = KNamelistCount( nl1, &count_1 );
if ( rc == 0 )
{
uint32_t count_2;
rc = KNamelistCount( nl2, &count_2 );
if ( rc == 0 && count_1 == count_2 )
{
uint32_t idx;
uint32_t in_nl2 = 0;
for ( idx = 0; idx < count_1 && rc == 0; ++idx )
{
const char *s;
rc = KNamelistGet( nl1, idx, &s );
if ( rc == 0 && s != NULL && nlt_is_name_in_namelist( nl2, s ) )
in_nl2++;
}
res = ( rc == 0 && in_nl2 == count_1 );
if ( found != NULL ) *found = in_nl2;
}
}
}
return res;
}
rc_t nlt_copy_namelist( const KNamelist *src, const KNamelist ** dst )
{
VNamelist *v_names;
rc_t rc = VNamelistMake ( &v_names, 5 );
if ( rc == 0 )
{
uint32_t count;
rc = KNamelistCount( src, &count );
if ( rc == 0 )
{
uint32_t idx;
for ( idx = 0; idx < count && rc == 0; ++idx )
{
const char *s;
rc = KNamelistGet( src, idx, &s );
if ( rc == 0 && s != NULL )
rc = VNamelistAppend ( v_names, s );
}
}
if ( rc == 0 )
rc = VNamelistToConstNamelist ( v_names, dst );
VNamelistRelease( v_names );
}
return rc;
}
rc_t nlt_build_intersect( const KNamelist *nl1, const KNamelist *nl2, const KNamelist ** dst )
{
VNamelist *v_names;
rc_t rc = VNamelistMake ( &v_names, 5 );
if ( rc == 0 )
{
/* loop through nl1: if a entry is found in nl2 -> add it to dst */
uint32_t count;
rc = KNamelistCount( nl1, &count );
if ( rc == 0 )
{
uint32_t idx;
for ( idx = 0; idx < count && rc == 0; ++idx )
{
const char *s;
rc = KNamelistGet( nl1, idx, &s );
if ( rc == 0 && s != NULL )
{
if ( nlt_is_name_in_namelist( nl2, s ) )
rc = VNamelistAppend ( v_names, s );
}
}
}
if ( rc == 0 )
rc = VNamelistToConstNamelist ( v_names, dst );
VNamelistRelease( v_names );
}
return rc;
}
bool nlt_namelist_is_sub_set_in_full_set( const KNamelist * sub_set, const KNamelist * full_set )
{
bool res = false;
uint32_t count;
rc_t rc = KNamelistCount( sub_set, &count );
if ( rc == 0 )
{
uint32_t idx;
uint32_t found = 0;
for ( idx = 0; idx < count && rc == 0; ++idx )
{
const char *s;
rc = KNamelistGet( sub_set, idx, &s );
if ( rc == 0 && s != NULL && nlt_is_name_in_namelist( full_set, s ) )
found++;
}
res = ( rc == 0 && count == found );
}
return res;
}
|