Move safe_ato[il]() to misc.[ch]

This commit is contained in:
Julien BLACHE 2009-04-30 14:25:52 +02:00
parent 91414c10d7
commit 0a1c4545dc
6 changed files with 111 additions and 102 deletions

View File

@ -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 \

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

96
src/misc.c Normal file
View File

@ -0,0 +1,96 @@
/*
* Copyright (C) 2009 Julien BLACHE <jb@jblache.org>
*
* 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 <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#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;
}

11
src/misc.h Normal file
View File

@ -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__ */