aboutsummaryrefslogtreecommitdiffstats
path: root/src/common
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2018-12-22 02:47:07 +0100
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2018-12-22 02:47:07 +0100
commitdb9f741875ad1fd3c7c3e5b932420081862159bc (patch)
tree03962b496e79b041ee1a0d503090a4fd9457739b /src/common
parent5fa28dc2b871b11f93d4206bea2780262f85af5e (diff)
downloadblogc-db9f741875ad1fd3c7c3e5b932420081862159bc.tar.gz
blogc-db9f741875ad1fd3c7c3e5b932420081862159bc.tar.bz2
blogc-db9f741875ad1fd3c7c3e5b932420081862159bc.zip
wip
Diffstat (limited to 'src/common')
-rw-r--r--src/common/error.c68
-rw-r--r--src/common/error.h8
-rw-r--r--src/common/file.c24
-rw-r--r--src/common/file.h1
4 files changed, 99 insertions, 2 deletions
diff --git a/src/common/error.c b/src/common/error.c
index a3a08e7..ef6edff 100644
--- a/src/common/error.c
+++ b/src/common/error.c
@@ -6,9 +6,19 @@
* See the file LICENSE.
*/
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif /* HAVE_CONFIG_H */
+
+#ifdef HAVE_WINDOWS_H
+#include <windows.h>
+#endif /* HAVE_WINDOWS_H */
+
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
+#include <string.h>
#include "error.h"
#include "utils.h"
@@ -24,19 +34,73 @@ bc_error_new(bc_error_type_t type, const char *msg)
bc_error_t*
+bc_error_new_vprintf(bc_error_type_t type, const char *format, va_list ap)
+{
+ char *tmp = bc_strdup_vprintf(format, ap);
+ bc_error_t *rv = bc_error_new(type, tmp);
+ free(tmp);
+ return rv;
+}
+
+
+bc_error_t*
bc_error_new_printf(bc_error_type_t type, const char *format, ...)
{
va_list ap;
va_start(ap, format);
- char *tmp = bc_strdup_vprintf(format, ap);
+ bc_error_t *rv = bc_error_new_vprintf(type, format, ap);
va_end(ap);
- bc_error_t *rv = bc_error_new(type, tmp);
+ return rv;
+}
+
+
+bc_error_t*
+bc_error_new_errno_vprintf(bc_error_type_t type, int errno_, const char *format,
+ va_list ap)
+{
+ char *tmp = bc_strdup_vprintf(format, ap);
+#if defined(WIN32) || defined(_WIN32)
+ LPTSTR buf = "bola";
+ FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
+ NULL, errno_, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&buf,
+ 0, NULL);
+ bc_error_t *rv = bc_error_new_printf(type, "%s: %s", tmp, buf);
+ LocalFree(buf);
+#else
+ bc_error_t *rv = bc_error_new_printf(type, "%s: %s", tmp, strerror(errno_));
+#endif
free(tmp);
return rv;
}
bc_error_t*
+bc_error_new_errno_printf(bc_error_type_t type, int errno_, const char *format, ...)
+{
+ va_list ap;
+ va_start(ap, format);
+ bc_error_t *rv = bc_error_new_errno_vprintf(type, errno_, format, ap);
+ va_end(ap);
+ return rv;
+}
+
+
+bc_error_t*
+bc_error_new_default_errno_printf(bc_error_type_t type, const char *format, ...)
+{
+ va_list ap;
+ va_start(ap, format);
+#if defined(WIN32) || defined(_WIN32)
+ bc_error_t *rv = bc_error_new_errno_vprintf(type, GetLastError(), format, ap);
+#else
+ bc_error_t *rv = bc_error_new_errno_vprintf(type, errno, format, ap);
+#endif
+ va_end(ap);
+ return rv;
+}
+
+
+bc_error_t*
bc_error_parser(bc_error_type_t type, const char *src, size_t src_len,
size_t current, const char *format, ...)
{
diff --git a/src/common/error.h b/src/common/error.h
index 2aac439..ccb4228 100644
--- a/src/common/error.h
+++ b/src/common/error.h
@@ -9,6 +9,7 @@
#ifndef _ERROR_H
#define _ERROR_H
+#include <stdarg.h>
#include <stddef.h>
// error handling is centralized here for the sake of simplicity :/
@@ -38,7 +39,14 @@ typedef struct {
} bc_error_t;
bc_error_t* bc_error_new(bc_error_type_t type, const char *msg);
+bc_error_t* bc_error_new_vprintf(bc_error_type_t type, const char *format, va_list ap);
bc_error_t* bc_error_new_printf(bc_error_type_t type, const char *format, ...);
+bc_error_t* bc_error_new_errno_vprintf(bc_error_type_t type, int errno_,
+ const char *format, va_list ap);
+bc_error_t* bc_error_new_errno_printf(bc_error_type_t type, int errno_,
+ const char *format, ...);
+bc_error_t* bc_error_new_default_errno_printf(bc_error_type_t type,
+ const char *format, ...);
bc_error_t* bc_error_parser(bc_error_type_t type, const char *src,
size_t src_len, size_t current, const char *format, ...);
void bc_error_print(bc_error_t *err, const char *prefix);
diff --git a/src/common/file.c b/src/common/file.c
index a2f7340..b44b202 100644
--- a/src/common/file.c
+++ b/src/common/file.c
@@ -7,9 +7,11 @@
*/
#include <errno.h>
+#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include "file.h"
#include "error.h"
@@ -64,3 +66,25 @@ bc_file_get_contents(const char *path, bool utf8, size_t *len, bc_error_t **err)
return bc_string_free(str, false);
}
+
+
+char*
+bc_file_get_realpath(const char *path)
+{
+ if (path == NULL)
+ return NULL;
+
+#if defined(WIN32) || defined(_WIN32)
+ char *buf = bc_malloc(_MAX_PATH);
+ if (NULL == _fullpath(buf, path, _MAX_PATH)) {
+#else
+ char *buf = bc_malloc(PATH_MAX);
+ if (NULL == realpath(path, buf)) {
+#endif
+
+ free(buf);
+ return NULL;
+ }
+
+ return buf;
+}
diff --git a/src/common/file.h b/src/common/file.h
index 73793ea..73590c6 100644
--- a/src/common/file.h
+++ b/src/common/file.h
@@ -16,5 +16,6 @@
#define BC_FILE_CHUNK_SIZE 1024
char* bc_file_get_contents(const char *path, bool utf8, size_t *len, bc_error_t **err);
+char* bc_file_get_realpath(const char *path);
#endif /* _FILE_H */