Added helpers for error handling
This commit is contained in:
parent
390982c317
commit
d8b9e7d407
2
Makefile
2
Makefile
|
@ -27,6 +27,7 @@ dist:
|
|||
$(PACKAGE)-$(VERSION)/src/array.c \
|
||||
$(PACKAGE)-$(VERSION)/src/buffer.c \
|
||||
$(PACKAGE)-$(VERSION)/src/config.c \
|
||||
$(PACKAGE)-$(VERSION)/src/error.c \
|
||||
$(PACKAGE)-$(VERSION)/src/event.c \
|
||||
$(PACKAGE)-$(VERSION)/src/hash.c \
|
||||
$(PACKAGE)-$(VERSION)/src/string.c \
|
||||
|
@ -39,6 +40,7 @@ dist:
|
|||
$(PACKAGE)-$(VERSION)/include/System/array.h \
|
||||
$(PACKAGE)-$(VERSION)/include/System/buffer.h \
|
||||
$(PACKAGE)-$(VERSION)/include/System/config.h \
|
||||
$(PACKAGE)-$(VERSION)/include/System/error.h \
|
||||
$(PACKAGE)-$(VERSION)/include/System/event.h \
|
||||
$(PACKAGE)-$(VERSION)/include/System/file.h \
|
||||
$(PACKAGE)-$(VERSION)/include/System/hash.h \
|
||||
|
|
|
@ -27,6 +27,7 @@ install: all
|
|||
$(INSTALL) -m 0644 System/array.h $(DESTDIR)$(INCLUDEDIR)/System/array.h
|
||||
$(INSTALL) -m 0644 System/buffer.h $(DESTDIR)$(INCLUDEDIR)/System/buffer.h
|
||||
$(INSTALL) -m 0644 System/config.h $(DESTDIR)$(INCLUDEDIR)/System/config.h
|
||||
$(INSTALL) -m 0644 System/error.h $(DESTDIR)$(INCLUDEDIR)/System/error.h
|
||||
$(INSTALL) -m 0644 System/event.h $(DESTDIR)$(INCLUDEDIR)/System/event.h
|
||||
$(INSTALL) -m 0644 System/file.h $(DESTDIR)$(INCLUDEDIR)/System/file.h
|
||||
$(INSTALL) -m 0644 System/hash.h $(DESTDIR)$(INCLUDEDIR)/System/hash.h
|
||||
|
@ -40,6 +41,7 @@ uninstall:
|
|||
$(RM) $(DESTDIR)$(INCLUDEDIR)/System/array.h
|
||||
$(RM) $(DESTDIR)$(INCLUDEDIR)/System/buffer.h
|
||||
$(RM) $(DESTDIR)$(INCLUDEDIR)/System/config.h
|
||||
$(RM) $(DESTDIR)$(INCLUDEDIR)/System/error.h
|
||||
$(RM) $(DESTDIR)$(INCLUDEDIR)/System/event.h
|
||||
$(RM) $(DESTDIR)$(INCLUDEDIR)/System/file.h
|
||||
$(RM) $(DESTDIR)$(INCLUDEDIR)/System/hash.h
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
# include "System/appserver.h"
|
||||
# include "System/buffer.h"
|
||||
# include "System/config.h"
|
||||
# include "System/error.h"
|
||||
# include "System/event.h"
|
||||
# include "System/file.h"
|
||||
# include "System/string.h"
|
||||
|
|
35
include/System/error.h
Normal file
35
include/System/error.h
Normal file
|
@ -0,0 +1,35 @@
|
|||
/* $Id$ */
|
||||
/* Copyright (c) 2007 Pierre Pronchery <khorben@defora.org> */
|
||||
/* This file is part of DeforaOS System libSystem */
|
||||
/* libSystem is not free software; you can redistribute it and/or modify it
|
||||
* under the terms of the Creative Commons Attribution-NonCommercial-ShareAlike
|
||||
* 3.0 Unported as published by the Creative Commons organization.
|
||||
*
|
||||
* libSystem is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
||||
* A PARTICULAR PURPOSE. See the Creative Commons Attribution-NonCommercial-
|
||||
* ShareAlike 3.0 Unported license for more details.
|
||||
*
|
||||
* You should have received a copy of the Creative Commons Attribution-
|
||||
* NonCommercial-ShareAlike 3.0 along with libSystem; if not, browse to
|
||||
* http://creativecommons.org/licenses/by-nc-sa/3.0/ */
|
||||
|
||||
|
||||
|
||||
#ifndef LIBSYSTEM_ERROR_H
|
||||
# define LIBSYSTEM_ERROR_H
|
||||
|
||||
|
||||
/* functions */
|
||||
/* accessors */
|
||||
char const * error_get(void);
|
||||
char const * error_get_code(int * code);
|
||||
|
||||
void error_set(char const * message);
|
||||
int error_set_code(char const * message, int code);
|
||||
int error_set_print(char const * prefix, char const * message, int code);
|
||||
|
||||
/* useful */
|
||||
int error_print(char const * prefix);
|
||||
|
||||
#endif /* !LIBSYSTEM_ERROR_H */
|
|
@ -1,3 +1,3 @@
|
|||
subdirs=System
|
||||
includes=System.h,System/appclient.h,System/appserver.h,System/array.h,System/buffer.h,System/config.h,System/event.h,System/file.h,System/hash.h,System/string.h
|
||||
includes=System.h,System/appclient.h,System/appserver.h,System/array.h,System/buffer.h,System/config.h,System/error.h,System/event.h,System/file.h,System/hash.h,System/string.h
|
||||
dist=Makefile
|
||||
|
|
|
@ -16,7 +16,7 @@ INSTALL = install
|
|||
|
||||
all: $(TARGETS)
|
||||
|
||||
libSystem_OBJS = appclient.o appinterface.o appserver.o array.o buffer.o config.o event.o hash.o string.o
|
||||
libSystem_OBJS = appclient.o appinterface.o appserver.o array.o buffer.o config.o error.o event.o hash.o string.o
|
||||
libSystem_CFLAGS = $(CPPFLAGS) $(CFLAGSF) $(CFLAGS)
|
||||
|
||||
libSystem.a: $(libSystem_OBJS)
|
||||
|
@ -44,6 +44,9 @@ buffer.o: buffer.c
|
|||
config.o: config.c
|
||||
$(CC) $(libSystem_CFLAGS) -c config.c
|
||||
|
||||
error.o: error.c
|
||||
$(CC) $(libSystem_CFLAGS) -c error.c
|
||||
|
||||
event.o: event.c
|
||||
$(CC) $(libSystem_CFLAGS) -c event.c
|
||||
|
||||
|
|
92
src/error.c
Normal file
92
src/error.c
Normal file
|
@ -0,0 +1,92 @@
|
|||
/* $Id$ */
|
||||
/* Copyright (c) 2007 Pierre Pronchery <khorben@defora.org> */
|
||||
/* This file is part of DeforaOS System libSystem */
|
||||
/* libSystem is not free software; you can redistribute it and/or modify it
|
||||
* under the terms of the Creative Commons Attribution-NonCommercial-ShareAlike
|
||||
* 3.0 Unported as published by the Creative Commons organization.
|
||||
*
|
||||
* libSystem is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
||||
* A PARTICULAR PURPOSE. See the Creative Commons Attribution-NonCommercial-
|
||||
* ShareAlike 3.0 Unported license for more details.
|
||||
*
|
||||
* You should have received a copy of the Creative Commons Attribution-
|
||||
* NonCommercial-ShareAlike 3.0 along with libSystem; if not, browse to
|
||||
* http://creativecommons.org/licenses/by-nc-sa/3.0/ */
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "System.h"
|
||||
|
||||
|
||||
/* private */
|
||||
static char const * _error_do(char const * message, int * codeptr)
|
||||
{
|
||||
static char buf[256] = "";
|
||||
static int code = 0;
|
||||
|
||||
if(message != NULL) /* setting the error */
|
||||
{
|
||||
strncpy(buf, message, sizeof(buf) - 1);
|
||||
buf[sizeof(buf) - 1] = '\0';
|
||||
if(codeptr != NULL)
|
||||
code = *codeptr;
|
||||
return buf;
|
||||
}
|
||||
if(codeptr != NULL)
|
||||
*codeptr = code;
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
||||
/* public */
|
||||
/* accessors */
|
||||
/* error_get */
|
||||
char const * error_get(void)
|
||||
{
|
||||
return _error_do(NULL, NULL);
|
||||
}
|
||||
|
||||
|
||||
/* error_get_code */
|
||||
char const * error_get_code(int * code)
|
||||
{
|
||||
return _error_do(NULL, code);
|
||||
}
|
||||
|
||||
|
||||
/* error_set */
|
||||
void error_set(char const * message)
|
||||
{
|
||||
_error_do(message, NULL);
|
||||
}
|
||||
|
||||
|
||||
/* error_set_code */
|
||||
int error_set_code(char const * message, int code)
|
||||
{
|
||||
_error_do(message, &code);
|
||||
return code;
|
||||
}
|
||||
|
||||
|
||||
/* error_set_print */
|
||||
int error_set_print(char const * prefix, char const * message, int code)
|
||||
{
|
||||
_error_do(message, &code);
|
||||
return error_print(prefix);
|
||||
}
|
||||
|
||||
|
||||
/* useful */
|
||||
int error_print(char const * prefix)
|
||||
{
|
||||
int code = 0;
|
||||
|
||||
if(prefix != NULL)
|
||||
fputs(prefix, stderr);
|
||||
fputs(_error_do(NULL, &code), stderr);
|
||||
return code;
|
||||
}
|
|
@ -6,7 +6,7 @@ dist=Makefile,appinterface.h
|
|||
|
||||
[libSystem]
|
||||
type=library
|
||||
sources=appclient.c,appinterface.c,appserver.c,array.c,buffer.c,config.c,event.c,hash.c,string.c
|
||||
sources=appclient.c,appinterface.c,appserver.c,array.c,buffer.c,config.c,error.c,event.c,hash.c,string.c
|
||||
|
||||
[appclient.c]
|
||||
depends=appinterface.h
|
||||
|
|
Loading…
Reference in New Issue
Block a user