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
|
/*
* fields.c
*
* Copyright (c) Chris Putnam 2003-7
*
* Source code released under the GPL
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "strsearch.h"
#include "fields.h"
int
fields_add( fields *info, char *tag, char *data, int level )
{
newstr *newtags, *newdata;
int *newused, *newlevel;
int min_alloc = 20, i, found;
if ( !tag || !data ) return 1;
if ( info->maxfields==0 ){
info->tag = (newstr*)malloc( sizeof(newstr) * min_alloc );
info->data= (newstr*)malloc( sizeof(newstr) * min_alloc );
info->used= (int*) malloc( sizeof(int) * min_alloc );
info->level=(int*) malloc( sizeof(int) * min_alloc );
if ( !info->tag || !info->data || !info->used || !info->level ){
if ( info->tag ) free( info->tag );
if ( info->data ) free( info->data );
if ( info->used ) free( info->used );
if ( info->level ) free( info->level );
return 0;
}
info->maxfields = min_alloc;
info->nfields = 0;
for ( i=0; i<min_alloc; ++i ) {
newstr_init(&(info->tag[i]));
newstr_init(&(info->data[i]));
}
} else if ( info->nfields >= info->maxfields ){
min_alloc = info->maxfields * 2;
newtags = (newstr*) realloc( info->tag,
sizeof(newstr) * min_alloc );
newdata = (newstr*) realloc( info->data,
sizeof(newstr) * min_alloc );
newused = (int*) realloc( info->used,
sizeof(int) * min_alloc );
newlevel= (int*) realloc( info->level,
sizeof(int) * min_alloc );
if ( !newtags || !newdata || !newused || !newlevel ) {
if ( newtags ) info->tag=newtags;
if ( newdata ) info->data=newdata;
if ( newused ) info->used=newused;
if ( newlevel ) info->level=newlevel;
return 0;
}
info->tag = newtags;
info->data = newdata;
info->used = newused;
info->level = newlevel;
info->maxfields = min_alloc;
for ( i=info->nfields; i<min_alloc; ++i ) {
newstr_init(&(info->tag[i]));
newstr_init(&(info->data[i]));
}
}
found = 0;
for ( i=0; i<info->nfields && !found; ++i ) {
if ( info->level[i]==level &&
!strcasecmp( info->tag[i].data, tag ) &&
!strcasecmp( info->data[i].data, data ) ) found=1;
}
if ( !found ) {
newstr_strcpy( &(info->tag[info->nfields]), tag );
newstr_strcpy( &(info->data[info->nfields]), data );
info->used[ info->nfields ] = 0;
info->level[ info->nfields ] = level;
info->nfields++;
}
return 1;
}
int
fields_add_tagsuffix( fields *info, char *tag, char *suffix, char *data,
int level )
{
char *buf;
int len, ret;
len = strlen( tag ) + strlen( suffix ) + 1;
buf = ( char * ) malloc( sizeof(char)*len );
if ( !buf ) return 0;
strcpy( buf, tag );
strcat( buf, suffix );
ret = fields_add( info, buf, data, level );
free( buf );
return ret;
}
fields*
fields_new( void )
{
fields *info = ( fields * ) malloc( sizeof( fields ) );
if ( info ) fields_init( info );
return info;
}
void
fields_init( fields *info )
{
info->used = NULL;
info->level = NULL;
info->tag = NULL;
info->data = NULL;
info->maxfields = info->nfields = 0;
}
void
fields_free( fields *info )
{
int i;
for (i=0; i<info->maxfields; ++i) {
newstr_free( &(info->tag[i]) );
newstr_free( &(info->data[i]) );
}
if ( info->tag ) free( info->tag );
if ( info->data ) free( info->data );
if ( info->used ) free( info->used );
if ( info->level ) free( info->level );
fields_init( info );
}
int
fields_find( fields *info, char *searchtag, int level )
{
int i, found = -1;
for ( i=0; i<info->nfields && found==-1; ++i ) {
if ( (level==-1 || level==info->level[i]) &&
// strsearch( info->tag[i].data, searchtag )==info->tag[i].data ) {
!strcasecmp( info->tag[i].data, searchtag ) ) {
found = i;
/* if there is no data for the tag, mark as unfound */
/* but set "used" so noise is suppressed */
if ( info->data[i].len==0 ) {
found=-1;
info->used[i] = 1;
}
}
}
return found;
}
int
fields_maxlevel( fields *info )
{
int i, max = 0;
for ( i=0; i<info->nfields; ++i ) {
if ( info->level[i]>max ) max = info->level[i];
}
return max;
}
void
fields_clearused( fields *info )
{
int i;
for ( i=0; i<info->nfields; ++i )
info->used[i] = 0;
}
void
fields_setused( fields *info, int n )
{
if ( n < info->nfields )
info->used[n] = 1;
}
|