Skip to content

Commit

Permalink
Move string trim functions to util.c
Browse files Browse the repository at this point in the history
  • Loading branch information
3cky committed Dec 25, 2023
1 parent 8591a2f commit 2b32d09
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 41 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ set(mbusd_SOURCES
src/state.c
src/sig.c
src/sock.c
src/util.c
)
add_executable(mbusd ${mbusd_SOURCES})
install(TARGETS mbusd DESTINATION bin)
Expand Down
49 changes: 8 additions & 41 deletions src/cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
#include "cfg.h"

#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#include "util.h"

#define CFG_MAX_LINE_LENGTH 200

#define CFG_NAME_MATCH(n) strcmp(n, name) == 0
Expand Down Expand Up @@ -81,37 +82,6 @@ cfg_init(void)
cfg.replyonbroadcast=0;
}

static char *
cfg_rtrim(char *s)
{
char *p = s + strlen(s);
while (p > s && isspace((unsigned char )(*--p)))
*p = '\0';
return s;
}

static char *
cfg_ltrim(const char *s)
{
while (*s && isspace((unsigned char )(*s)))
s++;
return (char *) s;
}

static int
cfg_is_empty(char *s)
{
char *p = s + strlen(s);
if (strlen(s) == 0)
return 1;
while (p > s && isspace((unsigned char )(*--p)))
{ //no-op
}
if (p == s && isspace((unsigned char )(*p)))
return 1;
return 0;
}

int
cfg_handle_param(char *name, char *value)
{
Expand Down Expand Up @@ -255,15 +225,15 @@ cfg_handle_param(char *name, char *value)
{ /* report about invalid log level */
CFG_ERR("invalid loglevel value: %s (must be 0-2)", value);
# endif
return(0);
return 0;
}
}
else if (CFG_NAME_MATCH("logfile"))
{
if (cfg_is_empty(value))
if (!strlen(value))
{
CFG_ERR("missing logfile value", value);
return(0);
return 0;
}
else if (*value != '/')
{
Expand All @@ -279,9 +249,6 @@ cfg_handle_param(char *name, char *value)
}
}
else strncpy(cfg.logname, value, INTBUFSIZE);



#endif
}
else {
Expand Down Expand Up @@ -315,7 +282,7 @@ cfg_parse_file(void *file)
{
lineno++;

start = cfg_ltrim(cfg_rtrim(line));
start = util_trim(line);

if (*start == '#')
{
Expand All @@ -330,8 +297,8 @@ cfg_parse_file(void *file)
{
*end = '\0';

name = cfg_rtrim(start);
value = cfg_ltrim(cfg_rtrim(end + 1));
name = util_rtrim(start);
value = util_trim(end + 1);

/* handle name/value pair */
if (!cfg_handle_param(name, value))
Expand Down
80 changes: 80 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* OpenMODBUS/TCP to RS-232/485 MODBUS RTU gateway
*
* util.h - utility functions
*
* Copyright (c) 2002-2023, Victor Antonovich (v.antonovich@gmail.com)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "util.h"

#include <ctype.h>
#include <string.h>

/**
* Removes leading whitespace from the string.
* Parameters: s - the string to be trimmed.
* Return: a pointer to the first non-whitespace character in string.
*/
char *
util_ltrim(const char *s)
{
while (*s && isspace((unsigned char )(*s)))
s++;
return (char *) s;
}

/**
* Removes trailing whitespace from the string.
* The first trailing whitespace is replaced with a NUL-terminator
* in the given string.
* Parameters: s - the string to be trimmed.
* Return: a pointer to the string.
*/
char *
util_rtrim(char *s)
{
char *p = s + strlen(s);
while (p > s && isspace((unsigned char )(*--p)))
*p = '\0';
return s;
}

/**
* Removes leading and trailing whitespace from the string.
* The first trailing whitespace is replaced with a NUL-terminator
* in the given string.
* Parameters: s - the string to be trimmed.
* Return: a pointer to the first non-whitespace character in string.
*/
char *
util_trim(char *s)
{
if (!strlen(s))
return s;

return util_ltrim(util_rtrim(s));
}
39 changes: 39 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* OpenMODBUS/TCP to RS-232/485 MODBUS RTU gateway
*
* util.h - utility functions
*
* Copyright (c) 2002-2023, Victor Antonovich (v.antonovich@gmail.com)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef _UTIL_H
#define _UTIL_H

char *util_ltrim(const char *s);
char *util_rtrim(char *s);
char *util_trim(char *s);

#endif /* _UTIL_H */

0 comments on commit 2b32d09

Please sign in to comment.