static int test_strcpy( newstr *s ) { int failed = 0; int numstrings = 1000, i; /* Copying null string should reset string */ newstr_empty( s ); for ( i=0; i<numstrings; ++i ) { newstr_strcpy( s, "1" ); newstr_strcpy( s, "" ); if ( string_mismatch( s, 0, "" ) ) failed++; } /* Many rounds of copying just "1" should give "1" */ newstr_empty( s ); for ( i=0; i<numstrings; ++i ) { newstr_strcpy( s, "1" ); if ( string_mismatch( s, 1, "1" ) ) failed++; } /* Many rounds of copying just "XXOO" should give "XXOO" */ newstr_empty( s ); for ( i=0; i<numstrings; ++i ) { newstr_strcpy( s, "XXOO" ); if ( string_mismatch( s, 4, "XXOO" ) ) failed++; } return failed; }
static int test_reverse( newstr *s ) { int failed = 0; /* empty string */ newstr_strcpy( s, "" ); newstr_reverse( s ); if ( string_mismatch( s, 0, "" ) ) failed++; /* string with even number of elements */ newstr_strcpy( s, "0123456789" ); newstr_reverse( s ); if ( string_mismatch( s, 10, "9876543210" ) ) failed++; newstr_reverse( s ); if ( string_mismatch( s, 10, "0123456789" ) ) failed++; /* string with odd number of elements */ newstr_strcpy( s, "123456789" ); newstr_reverse( s ); if ( string_mismatch( s, 9, "987654321" ) ) failed++; newstr_reverse( s ); if ( string_mismatch( s, 9, "123456789" ) ) failed++; return failed; }
static void resolve_citekeys( bibl *b, lists *citekeys, int *dup ) { char abc[]="abcdefghijklmnopqrstuvwxyz"; newstr tmp; int nsame, ntmp, n, i, j; newstr_init( &tmp ); for ( i=0; i<citekeys->n; ++i ) { if ( dup[i]==-1 ) continue; nsame = 0; for ( j=i; j<citekeys->n; ++j ) { if ( dup[j]!=i ) continue; newstr_strcpy( &tmp, citekeys->str[j].data ); ntmp = nsame; while ( ntmp >= 26 ) { newstr_addchar( &tmp, 'a' ); ntmp -= 26; } if ( ntmp<26 && ntmp>=0 ) newstr_addchar( &tmp, abc[ntmp] ); nsame++; dup[j] = -1; n = fields_find( b->ref[j], "REFNUM", -1 ); if ( n!=-1 ) newstr_strcpy( &((b->ref[j])->data[n]), tmp.data); } } newstr_free( &tmp ); }
int test_strcpy( newstr *s ) { int failed = 0; int numstrings = 1000, i; /* Copying null string should reset string */ newstr_empty( s ); for ( i=0; i<numstrings; ++i ) { newstr_strcpy( s, "1" ); newstr_strcpy( s, "" ); if ( test_consistency( s, 0, __FUNCTION__ ) || test_identity( s, "" ) ) failed++; } newstr_empty( s ); for ( i=0; i<numstrings; ++i ) { newstr_strcpy( s, "1" ); if ( test_consistency( s, 1, __FUNCTION__ ) || test_identity( s, "1" ) ) failed++; } newstr_empty( s ); for ( i=0; i<numstrings; ++i ) { newstr_strcpy( s, "XXOO" ); if ( test_consistency( s, 4, __FUNCTION__ ) || test_identity( s, "XXOO" ) ) failed++; } return failed; }
static int test_newstrcat( newstr *s ) { int numshort = 5, numstrings = 1000, i; int failed = 0; newstr t; newstr_init( &t ); /* ...adding empty strings to an empty string shouldn't change length */ newstr_empty( s ); for ( i=0; i<numstrings; ++i ) newstr_newstrcat( s, &t ); if ( string_mismatch( s, 0, "" ) ) failed++; /* ...adding empty strings to a defined string shouldn't change string */ newstr_strcpy( s, "1" ); for ( i=0; i<numstrings; ++i ) newstr_newstrcat( s, &t ); if ( string_mismatch( s, 1, "1" ) ) failed++; /* ...build "1111" with newstr_strcat */ newstr_empty( s ); newstr_strcpy( &t, "1" ); for ( i=0; i<numshort; ++i ) newstr_newstrcat( s, &t ); if ( string_mismatch( s, numshort, "11111" ) ) failed++; /* ...build "xoxoxoxoxo" with newstr_strcat */ newstr_empty( s ); newstr_strcpy( &t, "xo" ); for ( i=0; i<numshort; ++i ) newstr_newstrcat( s, &t ); if ( string_mismatch( s, numshort*2, "xoxoxoxoxo" ) ) failed++; newstr_empty( s ); newstr_strcpy( &t, "1" ); for ( i=0; i<numstrings; ++i ) newstr_newstrcat( s, &t ); if ( inconsistent_len( s, numstrings ) ) failed++; newstr_empty( s ); newstr_strcpy( &t, "XXOO" ); for ( i=0; i<numstrings; ++i ) newstr_newstrcat( s, &t ); if ( inconsistent_len( s, numstrings*4 ) ) failed++; newstr_free( &t ); return failed; }
static int test_case( newstr *s ) { int failed = 0; newstr_strcpy( s, "asdfjalskjfljasdfjlsfjd" ); if ( !newstr_is_lowercase( s ) ) { fprintf( stdout, "%s line %d: newstr_is_lowercase('%s') returned false\n", __FUNCTION__, __LINE__, s->data ); failed++; } if ( newstr_is_uppercase( s ) ) { fprintf( stdout, "%s line %d: newstr_is_uppercase('%s') returned true\n", __FUNCTION__, __LINE__, s->data ); failed++; } if ( newstr_is_mixedcase( s ) ) { fprintf( stdout, "%s line %d: newstr_is_mixedcase('%s') returned true\n", __FUNCTION__, __LINE__, s->data ); failed++; } newstr_strcpy( s, "ASDFJALSKJFLJASDFJLSFJD" ); if ( newstr_is_lowercase( s ) ) { fprintf( stdout, "%s line %d: newstr_is_lowercase('%s') returned true\n", __FUNCTION__, __LINE__, s->data ); failed++; } if ( !newstr_is_uppercase( s ) ) { fprintf( stdout, "%s line %d: newstr_is_uppercase('%s') returned false\n", __FUNCTION__, __LINE__, s->data ); failed++; } if ( newstr_is_mixedcase( s ) ) { fprintf( stdout, "%s line %d: newstr_is_mixedcase('%s') returned true\n", __FUNCTION__, __LINE__, s->data ); failed++; } newstr_strcpy( s, "ASdfjalsKJFLJASdfjlsfjd" ); if ( newstr_is_lowercase( s ) ) { fprintf( stdout, "%s line %d: newstr_is_lowercase('%s') returned true\n", __FUNCTION__, __LINE__, s->data ); failed++; } if ( newstr_is_uppercase( s ) ) { fprintf( stdout, "%s line %d: newstr_is_uppercase('%s') returned true\n", __FUNCTION__, __LINE__, s->data ); failed++; } if ( !newstr_is_mixedcase( s ) ) { fprintf( stdout, "%s line %d: newstr_is_mixedcase('%s') returned false\n", __FUNCTION__, __LINE__, s->data ); failed++; } return failed; }
newstr * list_setc( list *a, int n, const char *s ) { if ( !list_valid_num( a, n ) ) return NULL; newstr_strcpy( &(a->str[n]), s ); return list_set_cleanup( a, n ); }
static int generate_citekey( fields *info, int nref ) { newstr citekey; int n1, n2; char *p, buf[100]; newstr_init( &citekey ); n1 = fields_find( info, "AUTHOR", 0 ); if ( n1==-1 ) n1 = fields_find( info, "AUTHOR", -1 ); n2 = fields_find( info, "YEAR", 0 ); if ( n2==-1 ) n2 = fields_find( info, "YEAR", -1 ); if ( n2==-1 ) n2 = fields_find( info, "PARTYEAR", 0 ); if ( n2==-1 ) n2 = fields_find( info, "PARTYEAR", -1 ); if ( n1!=-1 && n2!=-1 ) { p = info->data[n1].data; while ( p && *p && *p!='|' ) { if ( !is_ws( *p ) ) newstr_addchar( &citekey, *p ); p++; } p = info->data[n2].data; while ( p && *p ) { if ( !is_ws( *p ) ) newstr_addchar( &citekey, *p ); p++; } fields_add( info, "REFNUM", citekey.data, 0 ); } else { sprintf( buf, "ref%d\n", nref ); newstr_strcpy( &citekey, buf ); } newstr_free( &citekey ); return fields_find( info, "REFNUM", -1 ); }
static int build_refnum( fields *info, long nrefs ) { newstr refnum; char *p, num[512]; int y, a; newstr_init( &refnum ); y = fields_find( info, "YEAR", -1 ); if ( y==-1 ) y = fields_find( info, "PARTYEAR", -1 ); a = fields_find( info, "AUTHOR", -1 ); if ( a==-1 ) a = fields_find( info, "AUTHOR:CORP", -1 ); if ( a!=-1 && y!=-1 ) { p = info->data[a].data; while ( p && *p && *p!='|' ) newstr_addchar( &refnum, *p++ ); p = info->data[y].data; while ( p && *p && *p!=' ' && *p!='\t' ) newstr_addchar( &refnum, *p++ ); } else { sprintf( num, "%ld", nrefs ); newstr_strcpy( &refnum, "ref" ); newstr_strcat( &refnum, num ); } fields_add( info, "REFNUM", refnum.data, 0 ); newstr_free( &refnum ); return fields_find( info, "REFNUM", 0 ); }
char * xml_findend( char *buffer, char *tag ) { newstr endtag; char *p; newstr_init( &endtag ); newstr_strcpy( &endtag, "</" ); if ( xml_pns ) { newstr_strcat( &endtag, xml_pns ); newstr_addchar( &endtag, ':' ); } newstr_strcat( &endtag, tag ); newstr_addchar( &endtag, '>' ); p = strsearch( buffer, endtag.data ); if ( p && *p ) { if ( *p ) p++; /* skip <random_tag></end> combo */ while ( *p && *(p-1)!='>' ) p++; } newstr_free( &endtag ); return p; }
static char* process_bibtexid( char *p, newstr *id ) { char *start_p = p; newstr tmp; newstr_init( &tmp ); p = newstr_cpytodelim( &tmp, p, ",", 1 ); if ( tmp.len ) { if ( strchr( tmp.data, '=' ) ) { /* Endnote writes bibtex files w/o fields, try to * distinguish via presence of an equal sign.... if * it's there, assume that it's a tag/data pair instead * and roll back. */ p = start_p; newstr_empty( id ); } else { newstr_strcpy( id, tmp.data ); } } else { newstr_empty( id ); } newstr_free( &tmp ); return skip_ws( p ); }
static int medin_corpauthor( xml *node, newstr *name ) { if ( xml_tagexact( node, "CollectiveName" ) ) { newstr_strcpy( name, xml_data( node ) ); } else if ( node->next ) medin_corpauthor( node->next, name ); return BIBL_OK; }
newstr * newstr_strdup( char *s1 ) { newstr *s2 = newstr_new(); if ( s2 ) newstr_strcpy( s2, s1 ); return s2; }
int test_findreplace( newstr *s ) { int failed = 0; int numstrings = 1000, i; char segment[]="0123456789"; for ( i=0; i<numstrings; ++i ) { newstr_strcpy( s, segment ); newstr_findreplace( s, "234", "" ); } failed += test_consistency( s, 7, __FUNCTION__ ); for ( i=0; i<numstrings; ++i ) { newstr_strcpy( s, segment ); newstr_findreplace( s, "234", "223344" ); } failed += test_consistency( s, 13, __FUNCTION__ ); return failed; }
void newstr_newstrcpy( newstr *s, newstr *old ) { assert( s && old ); if ( !old->data || !old->dim ) { s->dim = 0; if ( s->data ) s->data[0]='\0'; /* modify for NEWSTR_PARANOIA */ } else newstr_strcpy( s, old->data ); }
/* wordin_person_last() * * From an xml list, extract the value from the first entry * of <b:Last>xxxx</b:Last> and copy into name * * Additional <b:Last>yyyyy</b:Last> will be ignored. * * Returns BIBL_ERR_MEMERR on memory error, BIBL_OK otherwise. */ static int wordin_person_last( xml *node, newstr *name ) { while ( node && !xml_tagexact( node, "b:Last" ) ) node = node->next; if ( node && node->value->len ) { newstr_strcpy( name, node->value->data ); if ( newstr_memerr( name ) ) return BIBL_ERR_MEMERR; } return BIBL_OK; }
static void construct_url( char *prefix, newstr *id, newstr *id_url ) { if ( !strncasecmp( id->data, "http:", 5 ) ) newstr_newstrcpy( id_url, id ); else { newstr_strcpy( id_url, prefix ); if ( id->data[0]!='/' ) newstr_addchar( id_url, '/' ); newstr_newstrcat( id_url, id ); } }
static int test_tolower( newstr *s ) { char str1[] = "abcde_ABCDE_12345"; char str2[] = "0123456789"; int failed = 0; newstr_empty( s ); newstr_tolower( s ); if ( string_mismatch( s, 0, "" ) ) failed++; newstr_strcpy( s, str1 ); newstr_tolower( s ); if ( string_mismatch( s, strlen(str1), "abcde_abcde_12345" ) ) failed++; newstr_strcpy( s, str2 ); newstr_tolower( s ); if ( string_mismatch( s, strlen(str2), str2 ) ) failed++; return failed; }
static int test_findreplace( newstr *s ) { char segment[]="0123456789"; int numstrings = 1000, i; int failed = 0; for ( i=0; i<numstrings; ++i ) { newstr_strcpy( s, segment ); newstr_findreplace( s, "234", "" ); } if ( string_mismatch( s, 7, "0156789" ) ) failed++; for ( i=0; i<numstrings; ++i ) { newstr_strcpy( s, segment ); newstr_findreplace( s, "234", "223344" ); } if ( string_mismatch( s, 13, "0122334456789" ) ) failed++; return failed; }
static int test_pad( newstr *s ) { int failed = 0; newstr_empty( s ); newstr_pad( s, 10, '-' ); if ( string_mismatch( s, 10, "----------" ) ) failed++; newstr_strcpy( s, "012" ); newstr_pad( s, 10, '-' ); if ( string_mismatch( s, 10, "012-------" ) ) failed++; newstr_strcpy( s, "0123456789" ); newstr_pad( s, 10, '-' ); if ( string_mismatch( s, 10, "0123456789" ) ) failed++; newstr_strcpy( s, "01234567890" ); newstr_pad( s, 10, '-' ); if ( string_mismatch( s, 11, "01234567890" ) ) failed++; return failed; }
static int test_swapstrings( newstr *s ) { int failed = 0; newstr t; newstr_init( &t ); newstr_strcpy( &t, "0123456789" ); newstr_strcpy( s, "abcde" ); newstr_swapstrings( s, &t ); if ( string_mismatch( &t, 5, "abcde" ) ) failed++; if ( string_mismatch( s, 10, "0123456789" ) ) failed++; newstr_swapstrings( s, &t ); if ( string_mismatch( s, 5, "abcde" ) ) failed++; if ( string_mismatch( &t, 10, "0123456789" ) ) failed++; newstr_free( &t ); return failed; }
/* copac names appear to always start with last name first, but don't * always seem to have a comma after the name * * editors seem to be stuck in as authors with the tag "[Editor]" in it */ static int copacin_person( fields *bibin, newstr *intag, newstr *invalue, int level, param *pm, char *outtag, fields *bibout ) { char *usetag = outtag, editor[]="EDITOR"; newstr usename, *s; list tokens; int comma = 0, i, ok; if ( list_find( &(pm->asis), invalue->data ) !=-1 || list_find( &(pm->corps), invalue->data ) !=-1 ) { ok = name_add( bibout, outtag, invalue->data, level, &(pm->asis), &(pm->corps) ); if ( ok ) return BIBL_OK; else return BIBL_ERR_MEMERR; } list_init( &tokens ); newstr_init( &usename ); list_tokenize( &tokens, invalue, " ", 1 ); for ( i=0; i<tokens.n; ++i ) { s = list_get( &tokens, i ); if ( !strcmp( s->data, "[Editor]" ) ) { usetag = editor; newstr_strcpy( s, "" ); } else if ( s->len && s->data[s->len-1]==',' ) { comma++; } } if ( comma==0 && tokens.n ) { s = list_get( &tokens, 0 ); newstr_addchar( s, ',' ); } for ( i=0; i<tokens.n; ++i ) { s = list_get( &tokens, i ); if ( s->len==0 ) continue; if ( i ) newstr_addchar( &usename, ' ' ); newstr_newstrcat( &usename, s ); } list_free( &tokens ); ok = name_add( bibout, usetag, usename.data, level, &(pm->asis), &(pm->corps) ); newstr_free( &usename ); if ( ok ) return BIBL_OK; else return BIBL_ERR_MEMERR; }
static int test_newstrcpy( newstr *s ) { int failed = 0; int numstrings = 1000, i; newstr t; newstr_init( &t ); /* Copying null string should reset string */ newstr_empty( s ); for ( i=0; i<numstrings; ++i ) { newstr_strcpy( s, "1" ); newstr_newstrcpy( s, &t ); if ( string_mismatch( s, 0, "" ) ) failed++; } /* Many rounds of copying just "1" should give "1" */ newstr_empty( s ); newstr_strcpy( &t, "1" ); for ( i=0; i<numstrings; ++i ) { newstr_newstrcpy( s, &t ); if ( string_mismatch( s, t.len, t.data ) ) failed++; } /* Many rounds of copying just "XXOO" should give "XXOO" */ newstr_empty( s ); newstr_strcpy( &t, "XXOO" ); for ( i=0; i<numstrings; ++i ) { newstr_newstrcpy( s, &t ); if ( string_mismatch( s, t.len, t.data ) ) failed++; } newstr_free( &t ); return failed; }
int lists_add( lists *a, char *value ) { int ok = 1; /* ensure sufficient space */ if ( a->max==0 ) ok = lists_alloc( a ); else if ( a->n >= a->max ) ok = lists_realloc( a ); if ( ok ) { newstr_strcpy( &(a->str[a->n]), value ); a->n++; } return ok; }
int test_strcat( newstr *s ) { int failed = 0; int numshort = 5, numstrings = 1000, i; /* ...adding empty strings to an empty string shouldn't change length */ newstr_empty( s ); for ( i=0; i<numstrings; ++i ) newstr_strcat( s, "" ); if ( test_consistency( s, 0, __FUNCTION__ ) || test_identity( s, "" ) ) failed++; /* ...adding empty strings to a defined string shouldn't change string */ newstr_strcpy( s, "1" ); for ( i=0; i<numstrings; ++i ) newstr_strcat( s, "" ); if ( test_consistency( s, 1, __FUNCTION__ ) || test_identity( s, "1" ) ) failed++; /* ...build "1111" with newstr_strcat */ newstr_empty( s ); for ( i=0; i<numshort; ++i ) newstr_strcat( s, "1" ); if ( test_consistency( s, numshort, __FUNCTION__ ) || test_identity( s, "11111" ) ) failed++; /* ...build "xoxoxoxoxo" with newstr_strcat */ newstr_empty( s ); for ( i=0; i<numshort; ++i ) newstr_strcat( s, "xo" ); if ( test_consistency( s, numshort*2, __FUNCTION__ ) || test_identity( s, "xoxoxoxoxo" ) ) failed++; newstr_empty( s ); for ( i=0; i<numstrings; ++i ) newstr_strcat( s, "1" ); if ( test_consistency( s, numstrings, __FUNCTION__ ) ) failed++; newstr_empty( s ); for ( i=0; i<numstrings; ++i ) { newstr_strcat( s, "XXOO" ); } failed += test_consistency( s, numstrings*4, __FUNCTION__ ); return failed; }
/* copac names appear to always start with last name first, but don't * always seem to have a comma after the name * * editors seem to be stuck in as authors with the tag "[Editor]" in it */ static int copacin_addname( fields *info, char *tag, newstr *name, int level, list *asis, list *corps ) { char *usetag = tag, editor[]="EDITOR"; newstr usename, *s; list tokens; int comma = 0, i, ok; if ( list_find( asis, name->data ) !=-1 || list_find( corps, name->data ) !=-1 ) { ok = name_add( info, tag, name->data, level, asis, corps ); if ( ok ) return BIBL_OK; else return BIBL_ERR_MEMERR; } list_init( &tokens ); newstr_init( &usename ); list_tokenize( &tokens, name, " ", 1 ); for ( i=0; i<tokens.n; ++i ) { s = list_get( &tokens, i ); if ( !strcmp( s->data, "[Editor]" ) ) { usetag = editor; newstr_strcpy( s, "" ); } else if ( s->len && s->data[s->len-1]==',' ) { comma++; } } if ( comma==0 && tokens.n ) { s = list_get( &tokens, 0 ); newstr_addchar( s, ',' ); } for ( i=0; i<tokens.n; ++i ) { if ( i ) newstr_addchar( &usename, ' ' ); newstr_newstrcat( &usename, list_get( &tokens, i ) ); } list_free( &tokens ); ok = name_add( info, usetag, usename.data, level, asis, corps ); if ( ok ) return BIBL_OK; else return BIBL_ERR_MEMERR; }
static int test_prepend( newstr *s ) { int failed = 0; newstr_empty( s ); newstr_prepend( s, "" ); if ( string_mismatch( s, 0, "" ) ) failed++; newstr_prepend( s, "asdf" ); if ( string_mismatch( s, 4, "asdf" ) ) failed++; newstr_strcpy( s, "567890" ); newstr_prepend( s, "01234" ); if ( string_mismatch( s, 11, "01234567890" ) ) failed++; return failed; }
static char* process_bibtextype( char *p, newstr *type ) { newstr tmp; newstr_init( &tmp ); if ( *p=='@' ) p++; p = newstr_cpytodelim( &tmp, p, "{( \t\r\n", 0 ); p = skip_ws( p ); if ( *p=='{' || *p=='(' ) p++; p = skip_ws( p ); if ( tmp.len ) newstr_strcpy( type, tmp.data ); else newstr_empty( type ); newstr_free( &tmp ); return p; }
static int test_newstrcmp( newstr *s ) { int failed = 0; newstr t; newstr_init( &t ); newstr_empty( s ); if ( newstr_newstrcmp( s, s ) ) { fprintf( stdout, "%s line %d: newstr_newstrcmp(s,s) returned non-zero\n", __FUNCTION__, __LINE__ ); failed++; } if ( newstr_newstrcmp( s, &t ) ) { fprintf( stdout, "%s line %d: newstr_newstrcmp(s,t) returned non-zero\n", __FUNCTION__, __LINE__ ); failed++; } newstr_strcpy( s, "lakjsdlfjdskljfklsjf" ); if ( newstr_newstrcmp( s, s ) ) { fprintf( stdout, "%s line %d: newstr_newstrcmp(s,s) returned non-zero\n", __FUNCTION__, __LINE__ ); failed++; } if ( !newstr_newstrcmp( s, &t ) ) { fprintf( stdout, "%s line %d: newstr_newstrcmp(s,t) returned zero\n", __FUNCTION__, __LINE__ ); failed++; } newstr_newstrcpy( &t, s ); if ( newstr_newstrcmp( s, s ) ) { fprintf( stdout, "%s line %d: newstr_newstrcmp(s,s) returned non-zero\n", __FUNCTION__, __LINE__ ); failed++; } if ( newstr_newstrcmp( s, &t ) ) { fprintf( stdout, "%s line %d: newstr_newstrcmp(s,t) returned non-zero\n", __FUNCTION__, __LINE__ ); failed++; } newstr_free( &t ); return failed; }
/* void newstr_copyposlen ( newstr *s, newstr *in, unsigned long pos, unsigned long len ); */ static int test_copyposlen( newstr *s ) { newstr t; int failed = 0; newstr_init( &t ); newstr_copyposlen( s, &t, 1, 5 ); if ( string_mismatch( s, 0, "" ) ) failed++; newstr_strcpy( &t, "0123456789" ); newstr_copyposlen( s, &t, 1, 5 ); if ( string_mismatch( s, 5, "12345" ) ) failed++; newstr_free( &t ); return failed; }