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
|
/*===========================================================================
*
* 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 "coldefs.h"
#include <klib/text.h>
#include <klib/out.h>
#include <vdb/table.h>
#include <sysalloc.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
/*
* helper-function for: col_defs_destroy
* - free's everything a node owns
* - free's the node
*/
static void CC col_defs_destroy_pair( void * node, void * data )
{
col_pair * pair = node;
if ( pair != NULL )
{
if ( pair -> name != NULL )
{
free( pair -> name );
pair -> name = NULL;
}
free( pair );
}
}
/*
* initializes a column-definitions-list
*/
rc_t col_defs_init( col_defs ** defs )
{
rc_t rc = 0;
if ( defs == NULL )
rc = RC( rcVDB, rcNoTarg, rcConstructing, rcSelf, rcNull );
else
{
col_defs * tmp = calloc( 1, sizeof( * tmp ) );
if ( tmp == NULL )
rc = RC( rcVDB, rcNoTarg, rcConstructing, rcMemory, rcExhausted );
else
{
VectorInit( &( tmp ->cols ), 0, 5 );
*defs = tmp;
}
}
return rc;
}
/*
* destroys the column-definitions-list
*/
rc_t col_defs_destroy( col_defs * defs )
{
rc_t rc = 0;
if ( defs == NULL )
rc = RC( rcVDB, rcNoTarg, rcDestroying, rcSelf, rcNull );
else
{
VectorWhack( &( defs -> cols ), col_defs_destroy_pair, NULL );
free( defs );
}
return rc;
}
/*
* helper-function for: col_defs_parse_string / col_defs_extract_from_table
* - creates a column-definition by the column-name
* - adds the definition to the column-definition-vector
*/
static rc_t col_defs_append_col_pair( col_defs * defs, const char * name )
{
rc_t rc = 0;
if ( name == NULL )
rc = RC( rcExe, rcNoTarg, rcResolving, rcParam, rcNull );
else if ( name[ 0 ] == 0 )
rc = RC( rcExe, rcNoTarg, rcResolving, rcParam, rcEmpty );
else
{
col_pair * new_pair = calloc( 1, sizeof( * new_pair ) );
if ( new_pair == NULL )
rc = RC( rcVDB, rcNoTarg, rcParsing, rcMemory, rcExhausted );
else
{
new_pair -> name = string_dup_measure ( name, NULL );
rc = VectorAppend( &( defs -> cols ), NULL, new_pair );
if ( rc != 0 )
col_defs_destroy_pair( new_pair, NULL );
}
}
return rc;
}
/*
*/
rc_t col_defs_fill( col_defs * defs, const KNamelist * list )
{
rc_t rc = 0;
if ( defs == NULL )
rc = RC( rcExe, rcNoTarg, rcResolving, rcSelf, rcNull );
else if ( list == NULL )
rc = RC( rcExe, rcNoTarg, rcResolving, rcParam, rcNull );
else
{
uint32_t count;
rc = KNamelistCount( list, &count );
if ( rc != 0 )
KOutMsg( "col_defs_extract_from_table:KNamelistCount() failed %R\n", rc );
else
{
uint32_t idx;
for ( idx = 0; idx < count && rc == 0; ++idx )
{
const char * name;
rc = KNamelistGet( list, idx, &name );
if ( rc != 0 )
KOutMsg( "col_defs_extract_from_table:KNamelistGet() failed %R\n", rc );
else
rc = col_defs_append_col_pair( defs, name );
}
}
}
return rc;
}
/*
* how many columns do we have in here?
*/
rc_t col_defs_count( const col_defs * defs, uint32_t * count )
{
rc_t rc = 0;
if ( defs == NULL )
rc = RC( rcExe, rcNoTarg, rcResolving, rcSelf, rcNull );
else if ( count == NULL )
rc = RC( rcExe, rcNoTarg, rcResolving, rcParam, rcNull );
else
{
*count = VectorLength( &( defs -> cols ) );
}
return rc;
}
/*
* add the pairs in defs to the given cursor
*/
rc_t col_defs_add_to_cursor( col_defs * defs, const VCursor * cur, int idx )
{
rc_t rc = 0;
if ( defs == NULL )
rc = RC( rcExe, rcNoTarg, rcResolving, rcSelf, rcNull );
else if ( cur == NULL )
rc = RC( rcExe, rcNoTarg, rcResolving, rcParam, rcNull );
else if ( idx < 0 || idx > 1 )
rc = RC( rcExe, rcNoTarg, rcResolving, rcParam, rcInvalid );
else
{
uint32_t i;
uint32_t count = VectorLength( &( defs -> cols ) );
for ( i = 0; i < count && rc == 0; ++i )
{
col_pair * pair = VectorGet( &( defs -> cols ), i );
if ( pair != NULL )
rc = VCursorAddColumn( cur, &( pair -> pair[ idx ].idx ), "%s", pair -> name );
}
}
return rc;
}
|