perf_replies: moved one function to separate header

This commit is contained in:
Evgeny Grin (Karlson2k)
2023-07-10 13:40:10 +03:00
parent f586f2e7c1
commit d3519acbbe
3 changed files with 75 additions and 42 deletions
+1 -1
View File
@@ -36,4 +36,4 @@ endif
perf_replies_SOURCES = \ perf_replies_SOURCES = \
perf_replies.c perf_replies.c mhd_tool_str_to_uint.h
+69
View File
@@ -0,0 +1,69 @@
/*
This file is part of GNU libmicrohttpd
Copyright (C) 2023 Evgeny Grin (Karlson2k)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file tools/mhd_tool_str_to_uint.c
* @brief Implementation of HTTP server optimised for fast replies
* based on MHD.
* @author Karlson2k (Evgeny Grin)
*/
#ifndef MHD_TOOL_STR_TO_UINT_H_
#define MHD_TOOL_STR_TO_UINT_H_
#include <stddef.h>
/**
* Convert decimal string to unsigned int.
* Function stops at the end of the string or on first non-digit character.
* @param str the string to convert
* @param[out] value the pointer to put the result
* @return return the number of digits converted or
* zero if no digits found or result would overflow the output
* variable (the output set to UINT_MAX in this case).
*/
static size_t
mhd_tool_str_to_uint (const char *str, unsigned int *value)
{
size_t i;
unsigned int v = 0;
*value = 0;
for (i = 0; 0 != str[i]; ++i)
{
const char chr = str[i];
unsigned int digit;
if (('0' > chr) || ('9' < chr))
break;
digit = (unsigned char) (chr - '0');
if ((((0U - 1) / 10) < v) || ((v * 10 + digit) < v))
{
/* Overflow */
*value = 0U - 1;
return 0;
}
v *= 10;
v += digit;
}
*value = v;
return i;
}
#endif /* MHD_TOOL_STR_TO_UINT_H_ */
+5 -41
View File
@@ -26,7 +26,7 @@
*/ */
/** /**
* @file examples/perf_replies.c * @file tools/perf_replies.c
* @brief Implementation of HTTP server optimised for fast replies * @brief Implementation of HTTP server optimised for fast replies
* based on MHD. * based on MHD.
* @author Karlson2k (Evgeny Grin) * @author Karlson2k (Evgeny Grin)
@@ -38,6 +38,7 @@
#include <stdint.h> #include <stdint.h>
#include "mhd_options.h" #include "mhd_options.h"
#include "microhttpd.h" #include "microhttpd.h"
#include "mhd_tool_str_to_uint.h"
#if defined(MHD_REAL_CPU_COUNT) #if defined(MHD_REAL_CPU_COUNT)
#if MHD_REAL_CPU_COUNT == 0 #if MHD_REAL_CPU_COUNT == 0
@@ -106,43 +107,6 @@ set_self_name (int argc, char *const *argv)
} }
/**
* Convert decimal string to unsigned int.
* Function stops at the end of the string or on first non-digit character.
* @param str the string to convert
* @param[out] value the pointer to put the result
* @return return the number of digits converted or
* zero if no digits found or result would overflow the output
* variable (the output set to UINT_MAX in this case).
*/
static size_t
str_to_uint (const char *str, unsigned int *value)
{
size_t i;
unsigned int v = 0;
*value = 0;
for (i = 0; 0 != str[i]; ++i)
{
const char chr = str[i];
unsigned int digit;
if (('0' > chr) || ('9' < chr))
break;
digit = (unsigned char) (chr - '0');
if ((((0U - 1) / 10) < v) || ((v * 10 + digit) < v))
{
/* Overflow */
*value = 0U - 1;
return 0;
}
v *= 10;
v += digit;
}
*value = v;
return i;
}
#if defined (HAVE_POPEN) && defined(HAVE_PCLOSE) #if defined (HAVE_POPEN) && defined(HAVE_PCLOSE)
/** /**
* Read the command output as a number and return the number. * Read the command output as a number and return the number.
@@ -175,7 +139,7 @@ get_cmd_out_as_number (const char *cmd)
{ {
size_t digits_found; size_t digits_found;
unsigned int out_value; unsigned int out_value;
digits_found = str_to_uint (buf, &out_value); digits_found = mhd_tool_str_to_uint (buf, &out_value);
if (0 != digits_found) if (0 != digits_found)
{ {
if ((0 == buf[digits_found]) if ((0 == buf[digits_found])
@@ -302,7 +266,7 @@ get_param_value (const char *param_name, const char *param_tail,
value_str = next_param; value_str = next_param;
if (NULL != value_str) if (NULL != value_str)
digits = str_to_uint (value_str, param_value); digits = mhd_tool_str_to_uint (value_str, param_value);
else else
digits = 0; digits = 0;
@@ -1033,7 +997,7 @@ process_params (int argc, char *const *argv)
/* Process the port number */ /* Process the port number */
unsigned int read_port; unsigned int read_port;
size_t num_digits; size_t num_digits;
num_digits = str_to_uint (p, &read_port); num_digits = mhd_tool_str_to_uint (p, &read_port);
if (0 != p[num_digits]) if (0 != p[num_digits])
{ {
fprintf (stderr, "Error in specified port number: %s\n", p); fprintf (stderr, "Error in specified port number: %s\n", p);