move strtrim to util-common
Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com> Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
parent
75fe6ef104
commit
edbe6c2bdc
6 changed files with 42 additions and 77 deletions
|
@ -17,6 +17,7 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <ctype.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -127,6 +128,44 @@ char *safe_fgets(char *s, int size, FILE *stream)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Trim whitespace and newlines from a string
|
||||||
|
*/
|
||||||
|
size_t strtrim(char *str)
|
||||||
|
{
|
||||||
|
char *end, *pch = str;
|
||||||
|
|
||||||
|
if(str == NULL || *str == '\0') {
|
||||||
|
/* string is empty, so we're done. */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
while(isspace((unsigned char)*pch)) {
|
||||||
|
pch++;
|
||||||
|
}
|
||||||
|
if(pch != str) {
|
||||||
|
size_t len = strlen(pch);
|
||||||
|
if(len) {
|
||||||
|
memmove(str, pch, len + 1);
|
||||||
|
pch = str;
|
||||||
|
} else {
|
||||||
|
*str = '\0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* check if there wasn't anything but whitespace in the string. */
|
||||||
|
if(*str == '\0') {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
end = (str + strlen(str) - 1);
|
||||||
|
while(isspace((unsigned char)*end)) {
|
||||||
|
end--;
|
||||||
|
}
|
||||||
|
*++end = '\0';
|
||||||
|
|
||||||
|
return end - pch;
|
||||||
|
}
|
||||||
|
|
||||||
#ifndef HAVE_STRNLEN
|
#ifndef HAVE_STRNLEN
|
||||||
/* A quick and dirty implementation derived from glibc */
|
/* A quick and dirty implementation derived from glibc */
|
||||||
/** Determines the length of a fixed-size string.
|
/** Determines the length of a fixed-size string.
|
||||||
|
|
|
@ -30,6 +30,8 @@ int llstat(char *path, struct stat *buf);
|
||||||
|
|
||||||
char *safe_fgets(char *s, int size, FILE *stream);
|
char *safe_fgets(char *s, int size, FILE *stream);
|
||||||
|
|
||||||
|
size_t strtrim(char *str);
|
||||||
|
|
||||||
#ifndef HAVE_STRNDUP
|
#ifndef HAVE_STRNDUP
|
||||||
char *strndup(const char *s, size_t n);
|
char *strndup(const char *s, size_t n);
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -24,7 +24,7 @@
|
||||||
#include <alpm.h>
|
#include <alpm.h>
|
||||||
|
|
||||||
#include "ini.h"
|
#include "ini.h"
|
||||||
#include "util.h"
|
#include "util-common.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Parse a pacman-style INI config file.
|
* @brief Parse a pacman-style INI config file.
|
||||||
|
|
|
@ -29,7 +29,6 @@
|
||||||
#include <stdint.h> /* intmax_t */
|
#include <stdint.h> /* intmax_t */
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <ctype.h>
|
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
@ -353,44 +352,6 @@ void indentprint(const char *str, unsigned short indent, unsigned short cols)
|
||||||
free(wcstr);
|
free(wcstr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Trim whitespace and newlines from a string
|
|
||||||
*/
|
|
||||||
size_t strtrim(char *str)
|
|
||||||
{
|
|
||||||
char *end, *pch = str;
|
|
||||||
|
|
||||||
if(str == NULL || *str == '\0') {
|
|
||||||
/* string is empty, so we're done. */
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
while(isspace((unsigned char)*pch)) {
|
|
||||||
pch++;
|
|
||||||
}
|
|
||||||
if(pch != str) {
|
|
||||||
size_t len = strlen(pch);
|
|
||||||
if(len) {
|
|
||||||
memmove(str, pch, len + 1);
|
|
||||||
pch = str;
|
|
||||||
} else {
|
|
||||||
*str = '\0';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* check if there wasn't anything but whitespace in the string. */
|
|
||||||
if(*str == '\0') {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
end = (str + strlen(str) - 1);
|
|
||||||
while(isspace((unsigned char)*end)) {
|
|
||||||
end--;
|
|
||||||
}
|
|
||||||
*++end = '\0';
|
|
||||||
|
|
||||||
return end - pch;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Replace all occurrences of 'needle' with 'replace' in 'str', returning
|
/* Replace all occurrences of 'needle' with 'replace' in 'str', returning
|
||||||
* a new string (must be free'd) */
|
* a new string (must be free'd) */
|
||||||
char *strreplace(const char *str, const char *needle, const char *replace)
|
char *strreplace(const char *str, const char *needle, const char *replace)
|
||||||
|
|
|
@ -54,7 +54,6 @@ unsigned short getcols(void);
|
||||||
void columns_cache_reset(void);
|
void columns_cache_reset(void);
|
||||||
int rmrf(const char *path);
|
int rmrf(const char *path);
|
||||||
void indentprint(const char *str, unsigned short indent, unsigned short cols);
|
void indentprint(const char *str, unsigned short indent, unsigned short cols);
|
||||||
size_t strtrim(char *str);
|
|
||||||
char *strreplace(const char *str, const char *needle, const char *replace);
|
char *strreplace(const char *str, const char *needle, const char *replace);
|
||||||
void string_display(const char *title, const char *string, unsigned short cols);
|
void string_display(const char *title, const char *string, unsigned short cols);
|
||||||
double humanize_size(off_t bytes, const char target_unit, int precision,
|
double humanize_size(off_t bytes, const char target_unit, int precision,
|
||||||
|
|
|
@ -123,42 +123,6 @@ int searchsyncs = 0;
|
||||||
const char *dbpath = DBPATH;
|
const char *dbpath = DBPATH;
|
||||||
const char *configfile = CONFFILE;
|
const char *configfile = CONFFILE;
|
||||||
|
|
||||||
static size_t strtrim(char *str)
|
|
||||||
{
|
|
||||||
char *end, *pch = str;
|
|
||||||
|
|
||||||
if(str == NULL || *str == '\0') {
|
|
||||||
/* string is empty, so we're done. */
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
while(isspace((unsigned char)*pch)) {
|
|
||||||
pch++;
|
|
||||||
}
|
|
||||||
if(pch != str) {
|
|
||||||
size_t len = strlen(pch);
|
|
||||||
if(len) {
|
|
||||||
memmove(str, pch, len + 1);
|
|
||||||
pch = str;
|
|
||||||
} else {
|
|
||||||
*str = '\0';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* check if there wasn't anything but whitespace in the string. */
|
|
||||||
if(*str == '\0') {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
end = (str + strlen(str) - 1);
|
|
||||||
while(isspace((unsigned char)*end)) {
|
|
||||||
end--;
|
|
||||||
}
|
|
||||||
*++end = '\0';
|
|
||||||
|
|
||||||
return end - pch;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int register_syncs(void)
|
static int register_syncs(void)
|
||||||
{
|
{
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
|
|
Loading…
Add table
Reference in a new issue