From 0a1c4545dc9ed3147fb4728ba192c30b4408b110 Mon Sep 17 00:00:00 2001 From: Julien BLACHE Date: Thu, 30 Apr 2009 14:25:52 +0200 Subject: [PATCH] Move safe_ato[il]() to misc.[ch] --- src/Makefile.am | 1 + src/httpd.c | 31 +--------------- src/httpd_daap.c | 37 +------------------ src/httpd_rsp.c | 37 +------------------ src/misc.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++ src/misc.h | 11 ++++++ 6 files changed, 111 insertions(+), 102 deletions(-) create mode 100644 src/misc.c create mode 100644 src/misc.h diff --git a/src/Makefile.am b/src/Makefile.am index b61646ac..1c9461b5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -23,6 +23,7 @@ mt_daapd_SOURCES = main.c daapd.h webserver.c \ httpd.c httpd.h \ httpd_rsp.c httpd_rsp.h \ httpd_daap.c httpd_daap.h \ + misc.c misc.h \ db-generic.c db-generic.h ff-plugins.c ff-plugins.h \ scan-wma.c \ smart-parser.c smart-parser.h xml-rpc.c xml-rpc.h \ diff --git a/src/httpd.c b/src/httpd.c index 0548e9f5..823b81e0 100644 --- a/src/httpd.c +++ b/src/httpd.c @@ -39,6 +39,7 @@ #include "ff-dbstruct.h" #include "db-generic.h" #include "conffile.h" +#include "misc.h" #include "httpd.h" #include "httpd_rsp.h" #include "httpd_daap.h" @@ -99,36 +100,6 @@ static struct evhttp *evhttpd; static pthread_t tid_httpd; -static int -safe_atol(const char *str, long *val) -{ - char *end; - long intval; - - errno = 0; - intval = strtol(str, &end, 10); - - if (((errno == ERANGE) && ((intval == LONG_MAX) || (intval == LONG_MIN))) - || ((errno != 0) && (intval == 0))) - { - DPRINTF(E_WARN, L_RSP, "Invalid integer in string (%s): %s\n", str, strerror(errno)); - - return -1; - } - - if (end == str) - { - DPRINTF(E_WARN, L_RSP, "No integer found in string (%s)\n", str); - - return -1; - } - - *val = intval; - - return 0; -} - - static void stream_chunk_cb(int fd, short event, void *arg) { diff --git a/src/httpd_daap.c b/src/httpd_daap.c index ef7f502d..c572a7e8 100644 --- a/src/httpd_daap.c +++ b/src/httpd_daap.c @@ -43,6 +43,7 @@ #include "ff-dbstruct.h" #include "db-generic.h" #include "conffile.h" +#include "misc.h" #include "httpd.h" #include "httpd_daap.h" @@ -331,42 +332,6 @@ avl_tree_t *dmap_fields_hash; static int session_id; -static int -safe_atoi(const char *str, int *val) -{ - char *end; - long intval; - - errno = 0; - intval = strtol(str, &end, 10); - - if (((errno == ERANGE) && ((intval == LONG_MAX) || (intval == LONG_MIN))) - || ((errno != 0) && (intval == 0))) - { - DPRINTF(E_WARN, L_DAAP, "Invalid integer in string (%s): %s\n", str, strerror(errno)); - - return -1; - } - - if (end == str) - { - DPRINTF(E_WARN, L_DAAP, "No integer found in string (%s)\n", str); - - return -1; - } - - if (intval > INT_MAX) - { - DPRINTF(E_WARN, L_DAAP, "Integer value too large (%s)\n", str); - - return -1; - } - - *val = (int)intval; - - return 0; -} - static int dmap_field_map_compare(const void *aa, const void *bb) { diff --git a/src/httpd_rsp.c b/src/httpd_rsp.c index 1e040a8f..bde7cddd 100644 --- a/src/httpd_rsp.c +++ b/src/httpd_rsp.c @@ -42,6 +42,7 @@ #include "ff-dbstruct.h" #include "db-generic.h" #include "conffile.h" +#include "misc.h" #include "httpd.h" #include "httpd_rsp.h" @@ -130,42 +131,6 @@ static struct field_map rsp_fields[] = }; -static int -safe_atoi(const char *str, int *val) -{ - char *end; - long intval; - - errno = 0; - intval = strtol(str, &end, 10); - - if (((errno == ERANGE) && ((intval == LONG_MAX) || (intval == LONG_MIN))) - || ((errno != 0) && (intval == 0))) - { - DPRINTF(E_WARN, L_RSP, "Invalid integer in string (%s): %s\n", str, strerror(errno)); - - return -1; - } - - if (end == str) - { - DPRINTF(E_WARN, L_RSP, "No integer found in string (%s)\n", str); - - return -1; - } - - if (intval > INT_MAX) - { - DPRINTF(E_WARN, L_RSP, "Integer value too large (%s)\n", str); - - return -1; - } - - *val = (int)intval; - - return 0; -} - static struct evbuffer * mxml_to_evbuf(mxml_node_t *tree) { diff --git a/src/misc.c b/src/misc.c new file mode 100644 index 00000000..b5c01069 --- /dev/null +++ b/src/misc.c @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2009 Julien BLACHE + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include +#include +#include +#include + +#include "daapd.h" +#include "err.h" +#include "misc.h" + + +int +safe_atoi(const char *str, int *val) +{ + char *end; + long intval; + + errno = 0; + intval = strtol(str, &end, 10); + + if (((errno == ERANGE) && ((intval == LONG_MAX) || (intval == LONG_MIN))) + || ((errno != 0) && (intval == 0))) + { + DPRINTF(E_DBG, L_MISC, "Invalid integer in string (%s): %s\n", str, strerror(errno)); + + return -1; + } + + if (end == str) + { + DPRINTF(E_DBG, L_MISC, "No integer found in string (%s)\n", str); + + return -1; + } + + if (intval > INT_MAX) + { + DPRINTF(E_DBG, L_MISC, "Integer value too large (%s)\n", str); + + return -1; + } + + *val = (int)intval; + + return 0; +} + +int +safe_atol(const char *str, long *val) +{ + char *end; + long intval; + + errno = 0; + intval = strtol(str, &end, 10); + + if (((errno == ERANGE) && ((intval == LONG_MAX) || (intval == LONG_MIN))) + || ((errno != 0) && (intval == 0))) + { + DPRINTF(E_DBG, L_MISC, "Invalid integer in string (%s): %s\n", str, strerror(errno)); + + return -1; + } + + if (end == str) + { + DPRINTF(E_DBG, L_MISC, "No integer found in string (%s)\n", str); + + return -1; + } + + *val = intval; + + return 0; +} diff --git a/src/misc.h b/src/misc.h new file mode 100644 index 00000000..06ef6646 --- /dev/null +++ b/src/misc.h @@ -0,0 +1,11 @@ + +#ifndef __MISC_H__ +#define __MISC_H__ + +int +safe_atoi(const char *str, int *val); + +int +safe_atol(const char *str, long *val); + +#endif /* !__MISC_H__ */