From e7c4df57d3838ac1c0c15569daec2d118f4abb2c Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Tue, 16 Jul 2013 14:00:10 +0200 Subject: [PATCH] Add yaz_strcmp_null --- include/yaz/matchstr.h | 8 ++++++++ src/matchstr.c | 14 ++++++++++++++ src/zoom-record-cache.c | 22 ++++++---------------- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/include/yaz/matchstr.h b/include/yaz/matchstr.h index 600cdaf..bdc5386 100644 --- a/include/yaz/matchstr.h +++ b/include/yaz/matchstr.h @@ -87,6 +87,14 @@ YAZ_EXPORT int yaz_strncasecmp(const char *s1, const char *s2, size_t n); */ YAZ_EXPORT int yaz_strcasecmp(const char *s1, const char *s2); +/** \brief strcmp, null may be passed + \param s1 first buffer or NULL + \param s2 second buffer or NULL + \retval 0 buffers are equal + \retval >0 a > b + \retval <0 a < b +*/ +YAZ_EXPORT int yaz_strcmp_null(const char *s1, const char *s2); YAZ_END_CDECL diff --git a/src/matchstr.c b/src/matchstr.c index 82aa006..832ec45 100644 --- a/src/matchstr.c +++ b/src/matchstr.c @@ -92,6 +92,20 @@ int yaz_memcmp(const void *a, const void *b, size_t len_a, size_t len_b) return len_a - len_b; } +int yaz_strcmp_null(const char *v1, const char *v2) +{ + if (v1) + { + if (v2) + return strcmp(v1, v2); + else + return 1; + } + else if (v2) + return -1; + return 0; +} + /* * Local variables: * c-basic-offset: 4 diff --git a/src/zoom-record-cache.c b/src/zoom-record-cache.c index 75b3945..0b22b33 100644 --- a/src/zoom-record-cache.c +++ b/src/zoom-record-cache.c @@ -49,16 +49,6 @@ struct ZOOM_record_cache_p { ZOOM_record_cache next; }; - -static int strcmp_null(const char *v1, const char *v2) -{ - if (!v1 && !v2) - return 0; - if (!v1 || !v2) - return -1; - return strcmp(v1, v2); -} - static size_t record_hash(int pos) { if (pos < 0) @@ -80,9 +70,9 @@ void ZOOM_record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr, for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next) { if (pos == rc->pos - && strcmp_null(r->schema, rc->schema) == 0 - && strcmp_null(elementSetName,rc->elementSetName) == 0 - && strcmp_null(syntax, rc->syntax) == 0) + && yaz_strcmp_null(r->schema, rc->schema) == 0 + && yaz_strcmp_null(elementSetName,rc->elementSetName) == 0 + && yaz_strcmp_null(syntax, rc->syntax) == 0) break; } if (!rc) @@ -136,11 +126,11 @@ ZOOM_record ZOOM_record_cache_lookup(ZOOM_resultset r, int pos, { if (pos == rc->pos) { - if (strcmp_null(r->schema, rc->schema)) + if (yaz_strcmp_null(r->schema, rc->schema)) continue; - if (strcmp_null(elementSetName,rc->elementSetName)) + if (yaz_strcmp_null(elementSetName,rc->elementSetName)) continue; - if (strcmp_null(syntax, rc->syntax)) + if (yaz_strcmp_null(syntax, rc->syntax)) continue; return &rc->rec; } -- 1.7.10.4