summaryrefslogtreecommitdiff
path: root/utils/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'utils/utils.c')
-rw-r--r--utils/utils.c104
1 files changed, 23 insertions, 81 deletions
diff --git a/utils/utils.c b/utils/utils.c
index bb30f921e..c036ff2f7 100644
--- a/utils/utils.c
+++ b/utils/utils.c
@@ -37,32 +37,7 @@
#include "utils/utf8.h"
#include "utils/utils.h"
-char * strip(char * const s)
-{
- size_t i;
- for (i = strlen(s);
- i != 0 && (s[i - 1] == ' ' || s[i - 1] == '\n' ||
- s[i - 1] == '\r' || s[i - 1] == '\t');
- i--)
- ;
- s[i] = 0;
- return s + strspn(s, " \t\r\n");
-}
-
-int whitespace(const char * str)
-{
- unsigned int i;
- for (i = 0; i < strlen(str); i++)
- if (!isspace(str[i]))
- return 0;
- return 1;
-}
-
-/**
- * returns a string without its underscores
- * \param replacespace true to insert a space where there was an underscore
- */
-
+/* exported interface documented in utils/utils.h */
char *remove_underscores(const char *s, bool replacespace)
{
size_t i, ii, len;
@@ -81,15 +56,8 @@ char *remove_underscores(const char *s, bool replacespace)
return ret;
}
-/**
- * Replace consecutive whitespace with a single space.
- *
- * @todo determine if squash_whitespace utf-8 safe and that it needs to be
- *
- * \param s source string
- * \return heap allocated result, or NULL on memory exhaustion
- */
+/* exported interface documented in utils/utils.h */
char *squash_whitespace(const char *s)
{
char *c;
@@ -116,14 +84,7 @@ char *squash_whitespace(const char *s)
}
-/**
- * Converts NUL terminated UTF-8 encoded string s containing zero or more
- * spaces (char 32) or TABs (char 9) to non-breaking spaces
- * (0xC2 + 0xA0 in UTF-8 encoding).
- *
- * Caller needs to free() result. Returns NULL in case of error. No
- * checking is done on validness of the UTF-8 input string.
- */
+/* exported interface documented in utils/utils.h */
char *cnv_space2nbsp(const char *s)
{
const char *srcP;
@@ -146,10 +107,8 @@ char *cnv_space2nbsp(const char *s)
return d;
}
-/**
- * Check if a directory exists.
- */
+/* exported interface documented in utils/utils.h */
bool is_dir(const char *path)
{
struct stat s;
@@ -160,6 +119,7 @@ bool is_dir(const char *path)
return S_ISDIR(s.st_mode) ? true : false;
}
+
/* exported interface documented in utils/utils.h */
nserror vsnstrjoin(char **str, size_t *size, char sep, size_t nelm, va_list ap)
{
@@ -240,12 +200,8 @@ nserror snstrjoin(char **str, size_t *size, char sep, size_t nelm, ...)
return ret;
}
-/**
- * Compile a regular expression, handling errors.
- *
- * Parameters as for regcomp(), see man regex.
- */
+/* exported interface documented in utils/utils.h */
void regcomp_wrapper(regex_t *preg, const char *regex, int cflags)
{
int r;
@@ -258,24 +214,20 @@ void regcomp_wrapper(regex_t *preg, const char *regex, int cflags)
}
}
-/** We can have a fairly good estimate of how long the buffer needs to
- * be. The unsigned long can store a value representing a maximum size
- * of around 4 GB. Therefore the greatest space required is to
- * represent 1023MB. Currently that would be represented as "1023MB" so 12
- * including a null terminator.
- * Ideally we would be able to know this value for sure, in the mean
- * time the following should suffice.
- **/
-
-#define BYTESIZE_BUFFER_SIZE 20
-
/**
- * Does a simple conversion which assumes the user speaks English. The buffer
- * returned is one of three static ones so may change each time this call is
- * made. Don't store the buffer for later use. It's done this way for
- * convenience and to fight possible memory leaks, it is not necessarily pretty.
- **/
+ * The size of buffers within human_friendly_bytesize.
+ *
+ * We can have a fairly good estimate of how long the buffer needs to
+ * be. The unsigned long can store a value representing a maximum
+ * size of around 4 GB. Therefore the greatest space required is to
+ * represent 1023MB. Currently that would be represented as "1023MB"
+ * so 12 including a null terminator. Ideally we would be able to
+ * know this value for sure, in the mean time the following should
+ * suffice.
+ */
+#define BYTESIZE_BUFFER_SIZE 20
+/* exported interface documented in utils/utils.h */
char *human_friendly_bytesize(unsigned long bsize) {
static char buffer1[BYTESIZE_BUFFER_SIZE];
static char buffer2[BYTESIZE_BUFFER_SIZE];
@@ -308,17 +260,13 @@ char *human_friendly_bytesize(unsigned long bsize) {
unit = gigabytes;
}
- sprintf(curbuffer, "%3.2f%s", bytesize, messages_get(units[unit]));
+ snprintf(curbuffer, BYTESIZE_BUFFER_SIZE, "%3.2f%s", bytesize, messages_get(units[unit]));
return curbuffer;
}
-/**
- * Create an RFC 1123 compliant date string from a Unix timestamp
- *
- * \param t The timestamp to consider
- * \return Pointer to buffer containing string - invalidated by next call.
- */
+
+/* exported interface documented in utils/utils.h */
const char *rfc1123_date(time_t t)
{
static char ret[30];
@@ -336,14 +284,8 @@ const char *rfc1123_date(time_t t)
return ret;
}
-/**
- * Returns a number of centiseconds, that increases in real time, for the
- * purposes of measuring how long something takes in wall-clock terms. It uses
- * gettimeofday() for this. Should the call to gettimeofday() fail, it returns
- * zero.
- *
- * \return number of centiseconds that increases monotonically
- */
+
+/* exported interface documented in utils/utils.h */
unsigned int wallclock(void)
{
struct timeval tv;