Browse Source

faux: log + faux_ncasecmp()

Serj Kalichev 4 years ago
parent
commit
0c3eb90da2
7 changed files with 152 additions and 105 deletions
  1. 6 5
      faux/Makefile.am
  2. 2 1
      faux/ctype/ctype.c
  3. 11 8
      faux/log.h
  4. 2 0
      faux/log/Makefile.am
  5. 43 24
      faux/log/log.c
  6. 0 2
      faux/log/module.am
  7. 88 65
      faux/str/str.c

+ 6 - 5
faux/Makefile.am

@@ -7,20 +7,21 @@ nobase_include_HEADERS += \
 	faux/ctype.h \
 	faux/str.h \
 	faux/sysdb.h \
-	faux/conv.h
+	faux/conv.h \
+	faux/log.h
 
 #	faux/argv.h \
 #	faux/list.h \
 #	faux/dump.h \
 #	faux/system.h \
-#	faux/ini.h \
-#	faux/log.h
+#	faux/ini.h
 
 EXTRA_DIST += \
 	faux/ctype/Makefile.am \
 	faux/str/Makefile.am \
 	faux/sysdb/Makefile.am \
-	faux/conv/Makefile.am
+	faux/conv/Makefile.am \
+	faux/log/module.am
 
 #	faux/argv/module.am \
 #	faux/list/module.am \
@@ -34,10 +35,10 @@ include $(top_srcdir)/faux/ctype/Makefile.am
 include $(top_srcdir)/faux/str/Makefile.am
 include $(top_srcdir)/faux/sysdb/Makefile.am
 include $(top_srcdir)/faux/conv/Makefile.am
+include $(top_srcdir)/faux/log/Makefile.am
 
 #include $(top_srcdir)/faux/argv/module.am
 #include $(top_srcdir)/faux/list/module.am
 #include $(top_srcdir)/faux/dump/module.am
 #include $(top_srcdir)/faux/system/module.am
 #include $(top_srcdir)/faux/ini/module.am
-#include $(top_srcdir)/faux/log/module.am

+ 2 - 1
faux/ctype/ctype.c

@@ -3,8 +3,9 @@
  *
  * Some ctype functions are not compatible among different OSes.
  * So faux library functions use their own versions of some
- * ctype functions to unify the behaviour. Really the most of the
+ * ctype functions to unify the behaviour. Really for now all
  * faux ctype functions are only a wrappers for standard functions.
+ * But it can be changed in future for portability purposes.
  */
 
 #include <ctype.h>

+ 11 - 8
faux/log.h

@@ -1,13 +1,16 @@
-#ifndef _lub_log_h
-#define _lub_log_h
+/** @file log.h
+ * @brief Public interface for faux log functions.
+ */
 
-#include <syslog.h>
+#ifndef _faux_log_h
+#define _faux_log_h
 
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif /* HAVE_CONFIG_H */
+#include "faux/types.h"
 
-int lub_log_facility(const char *str, int *facility);
+C_DECL_BEGIN
 
-#endif
+int faux_log_facility(const char *str, int *facility);
+
+C_DECL_END
 
+#endif

+ 2 - 0
faux/log/Makefile.am

@@ -0,0 +1,2 @@
+libfaux_la_SOURCES += \
+	faux/log/log.c

+ 43 - 24
faux/log/log.c

@@ -1,52 +1,71 @@
-#include "lub/log.h"
+/** @file log.c
+ * @brief Helpers for logging
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif /* HAVE_CONFIG_H */
+
 #include <syslog.h>
-#include "lub/string.h"
 
-int lub_log_facility(const char *str, int *facility)
-{
-	if (!lub_string_nocasecmp(str, "local0"))
+#include "faux/str.h"
+#include "faux/log.h"
+
+
+/** @brief Parses syslog facility string and returns the facility id.
+ *
+ * Gets syslog facility string, parse it and finds out the facility
+ * id in digital form. Usefull config or command line options parsing.
+ *
+ * @param [in] str Facility string.
+ * @param [out] facility Facility in digital form.
+ * @returns 0 - success, < 0 - parsing error
+ */
+int faux_log_facility(const char *str, int *facility) {
+
+	if (!faux_str_casecmp(str, "local0"))
 		*facility = LOG_LOCAL0;
-	else if (!lub_string_nocasecmp(str, "local1"))
+	else if (!faux_str_casecmp(str, "local1"))
 		*facility = LOG_LOCAL1;
-	else if (!lub_string_nocasecmp(str, "local2"))
+	else if (!faux_str_casecmp(str, "local2"))
 		*facility = LOG_LOCAL2;
-	else if (!lub_string_nocasecmp(str, "local3"))
+	else if (!faux_str_casecmp(str, "local3"))
 		*facility = LOG_LOCAL3;
-	else if (!lub_string_nocasecmp(str, "local4"))
+	else if (!faux_str_casecmp(str, "local4"))
 		*facility = LOG_LOCAL4;
-	else if (!lub_string_nocasecmp(str, "local5"))
+	else if (!faux_str_casecmp(str, "local5"))
 		*facility = LOG_LOCAL5;
-	else if (!lub_string_nocasecmp(str, "local6"))
+	else if (!faux_str_casecmp(str, "local6"))
 		*facility = LOG_LOCAL6;
-	else if (!lub_string_nocasecmp(str, "local7"))
+	else if (!faux_str_casecmp(str, "local7"))
 		*facility = LOG_LOCAL7;
-	else if (!lub_string_nocasecmp(str, "auth"))
+	else if (!faux_str_casecmp(str, "auth"))
 		*facility = LOG_AUTH;
 #ifdef LOG_AUTHPRIV
-	else if (!lub_string_nocasecmp(str, "authpriv"))
+	else if (!faux_str_casecmp(str, "authpriv"))
 		*facility = LOG_AUTHPRIV;
 #endif
-	else if (!lub_string_nocasecmp(str, "cron"))
+	else if (!faux_str_casecmp(str, "cron"))
 		*facility = LOG_CRON;
-	else if (!lub_string_nocasecmp(str, "daemon"))
+	else if (!faux_str_casecmp(str, "daemon"))
 		*facility = LOG_DAEMON;
 #ifdef LOG_FTP
-	else if (!lub_string_nocasecmp(str, "ftp"))
+	else if (!faux_str_casecmp(str, "ftp"))
 		*facility = LOG_FTP;
 #endif
-	else if (!lub_string_nocasecmp(str, "kern"))
+	else if (!faux_str_casecmp(str, "kern"))
 		*facility = LOG_KERN;
-	else if (!lub_string_nocasecmp(str, "lpr"))
+	else if (!faux_str_casecmp(str, "lpr"))
 		*facility = LOG_LPR;
-	else if (!lub_string_nocasecmp(str, "mail"))
+	else if (!faux_str_casecmp(str, "mail"))
 		*facility = LOG_MAIL;
-	else if (!lub_string_nocasecmp(str, "news"))
+	else if (!faux_str_casecmp(str, "news"))
 		*facility = LOG_NEWS;
-	else if (!lub_string_nocasecmp(str, "syslog"))
+	else if (!faux_str_casecmp(str, "syslog"))
 		*facility = LOG_SYSLOG;
-	else if (!lub_string_nocasecmp(str, "user"))
+	else if (!faux_str_casecmp(str, "user"))
 		*facility = LOG_USER;
-	else if (!lub_string_nocasecmp(str, "uucp"))
+	else if (!faux_str_casecmp(str, "uucp"))
 		*facility = LOG_UUCP;
 	else
 		return -1;

+ 0 - 2
faux/log/module.am

@@ -1,2 +0,0 @@
-liblub_la_SOURCES += lub/log/log.c
-

+ 88 - 65
faux/str/str.c

@@ -198,6 +198,94 @@ char *faux_str_cat(char **str, const char *text) {
 }
 
 
+/** @brief Compare n first characters of two strings ignoring case.
+ *
+ * The difference beetween this function an standard strncasecmp() is
+ * faux function uses faux ctype functions. It can be important for
+ * portability.
+ *
+ * @param [in] str1 First string to compare.
+ * @param [in] str2 Second string to compare.
+ * @param [in] n Number of characters to compare.
+ * @return < 0, 0, > 0, see the strcasecmp().
+ */
+int faux_str_ncasecmp(const char *str1, const char *str2, size_t n) {
+
+	const char *p1 = str1;
+	const char *p2 = str2;
+	size_t num = n;
+
+	while ((*p1 || *p2) && num) {
+		int res = 0;
+		char c1 = faux_ctype_tolower(*p1);
+		char c2 = faux_ctype_tolower(*p2);
+		res = c1 - c2;
+		if (res)
+			return res;
+		p1++;
+		p2++;
+		num--;
+	}
+
+	return 0;
+}
+
+
+/** @brief Compare two strings ignoring case.
+ *
+ * The difference beetween this function an standard strcasecmp() is
+ * faux function uses faux ctype functions. It can be important for
+ * portability.
+ *
+ * @param [in] str1 First string to compare.
+ * @param [in] str2 Second string to compare.
+ * @return < 0, 0, > 0, see the strcasecmp().
+ */
+int faux_str_casecmp(const char *str1, const char *str2) {
+
+	const char *p1 = str1;
+	const char *p2 = str2;
+
+	while (*p1 || *p2) {
+		int res = 0;
+		char c1 = faux_ctype_tolower(*p1);
+		char c2 = faux_ctype_tolower(*p2);
+		res = c1 - c2;
+		if (res)
+			return res;
+		p1++;
+		p2++;
+	}
+
+	return 0;
+}
+
+
+const char *lub_string_nocasestr(const char *cs, const char *ct)
+{
+	const char *p = NULL;
+	const char *result = NULL;
+
+	while (*cs) {
+		const char *q = cs;
+
+		p = ct;
+		while (*p && *q
+		       && (faux_ctype_tolower(*p) == faux_ctype_tolower(*q))) {
+			p++, q++;
+		}
+		if (0 == *p) {
+			break;
+		}
+		cs++;
+	}
+	if (p && !*p) {
+		result = cs;
+	}
+	return result;
+}
+
+
 // TODO: Is it needed?
 /*
 char *lub_string_ndecode(const char *string, unsigned int len)
@@ -285,71 +373,6 @@ char *lub_string_encode(const char *string, const char *escape_chars)
 */
 
 
-// TODO: Is it needed?
-/*--------------------------------------------------------- */
-/*
-int lub_string_nocasecmp(const char *cs, const char *ct)
-{
-	int result = 0;
-	while ((0 == result) && *cs && *ct) {
-		//lint -e155 Ignoring { }'ed sequence within an expression, 0 assumed 
-		// MACRO implementation uses braces to prevent multiple increments
-		// when called.
-		//
-		int s = faux_ctype_tolower(*cs++);
-		int t = faux_ctype_tolower(*ct++);
-
-		result = s - t;
-	}
-	//lint -e774 Boolean within 'if' always evealuates to True 
-	// not the case because of tolower() evaluating to 0 under lint
-	// (see above)
-	//
-	if (0 == result) {
-		// account for different string lengths
-		result = *cs - *ct;
-	}
-	return result;
-}
-*/
-
-// TODO: Is it needed?
-
-/*--------------------------------------------------------- */
-/*
-const char *lub_string_nocasestr(const char *cs, const char *ct)
-{
-	const char *p = NULL;
-	const char *result = NULL;
-
-	while (*cs) {
-		const char *q = cs;
-
-		p = ct;
-		//lint -e155 Ignoring { }'ed sequence within an expression, 0 assumed 
-		// MACRO implementation uses braces to prevent multiple increments
-		// when called.
-		//
-		//lint -e506 Constant value Boolean
-		// not the case because of tolower() evaluating to 0 under lint
-		// (see above)
-		//
-		while (*p && *q
-		       && (faux_ctype_tolower(*p) == faux_ctype_tolower(*q))) {
-			p++, q++;
-		}
-		if (0 == *p) {
-			break;
-		}
-		cs++;
-	}
-	if (p && !*p) {
-		// we've found the first match of ct within cs
-		result = cs;
-	}
-	return result;
-}
-*/
 
 // TODO: Is it needed?
 /*--------------------------------------------------------- */